|
| 1 | +//==-------------------------- FusionPipeline.cpp --------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "FusionPipeline.h" |
| 10 | + |
| 11 | +#include "debug/PassDebug.h" |
| 12 | +#include "helper/ConfigHelper.h" |
| 13 | +#include "internalization/Internalization.h" |
| 14 | +#include "kernel-fusion/SYCLKernelFusion.h" |
| 15 | +#include "kernel-info/SYCLKernelInfo.h" |
| 16 | +#include "syclcp/SYCLCP.h" |
| 17 | + |
| 18 | +#include "llvm/IR/PassManager.h" |
| 19 | +#include "llvm/Transforms/Scalar/IndVarSimplify.h" |
| 20 | +#include "llvm/Transforms/Scalar/InferAddressSpaces.h" |
| 21 | +#include "llvm/Transforms/Scalar/LoopUnrollPass.h" |
| 22 | +#ifndef NDEBUG |
| 23 | +#include "llvm/IR/Verifier.h" |
| 24 | +#endif // NDEBUG |
| 25 | +#include "llvm/Passes/PassBuilder.h" |
| 26 | +#include "llvm/Transforms/InstCombine/InstCombine.h" |
| 27 | +#include "llvm/Transforms/Scalar/ADCE.h" |
| 28 | +#include "llvm/Transforms/Scalar/EarlyCSE.h" |
| 29 | +#include "llvm/Transforms/Scalar/SCCP.h" |
| 30 | +#include "llvm/Transforms/Scalar/SROA.h" |
| 31 | +#include "llvm/Transforms/Scalar/SimplifyCFG.h" |
| 32 | + |
| 33 | +using namespace llvm; |
| 34 | +using namespace jit_compiler; |
| 35 | +using namespace jit_compiler::fusion; |
| 36 | + |
| 37 | +std::unique_ptr<SYCLModuleInfo> |
| 38 | +FusionPipeline::runFusionPasses(Module &Mod, SYCLModuleInfo &InputInfo, |
| 39 | + int BarriersFlags) { |
| 40 | + // Perform the actual kernel fusion, i.e., generate a kernel function for the |
| 41 | + // fused kernel from the kernel functions of the input kernels. This is done |
| 42 | + // by the SYCLKernelFusion LLVM pass, which is run here through a custom LLVM |
| 43 | + // pass pipeline. In order to perform internalization, we run the |
| 44 | + // SYCLInternalizer pass. |
| 45 | + |
| 46 | + bool DebugEnabled = ConfigHelper::get<option::JITEnableVerbose>(); |
| 47 | + if (DebugEnabled) { |
| 48 | + // Enabled debug output from the fusion passes. |
| 49 | + jit_compiler::PassDebug = true; |
| 50 | + } |
| 51 | + |
| 52 | + // Initialize the analysis managers with all the registered analyses. |
| 53 | + PassBuilder PB; |
| 54 | + LoopAnalysisManager LAM; |
| 55 | + FunctionAnalysisManager FAM; |
| 56 | + CGSCCAnalysisManager CGAM; |
| 57 | + ModuleAnalysisManager MAM; |
| 58 | + PB.registerModuleAnalyses(MAM); |
| 59 | + PB.registerCGSCCAnalyses(CGAM); |
| 60 | + PB.registerFunctionAnalyses(FAM); |
| 61 | + PB.registerLoopAnalyses(LAM); |
| 62 | + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 63 | + |
| 64 | + // Make the existing SYCLModuleInfo available to the pass pipeline via the |
| 65 | + // corresponding analysis pass. |
| 66 | + MAM.registerPass([&]() { |
| 67 | + auto ModInfo = std::make_unique<SYCLModuleInfo>(InputInfo); |
| 68 | + return SYCLModuleInfoAnalysis{std::move(ModInfo)}; |
| 69 | + }); |
| 70 | + ModulePassManager MPM; |
| 71 | + // Run the fusion pass on the LLVM IR module. |
| 72 | + MPM.addPass(SYCLKernelFusion{BarriersFlags}); |
| 73 | + { |
| 74 | + FunctionPassManager FPM; |
| 75 | + // Run loop unrolling and SROA to split the kernel functor struct into its |
| 76 | + // scalar parts, to avoid problems with address-spaces and enable |
| 77 | + // internalization. |
| 78 | + FPM.addPass(createFunctionToLoopPassAdaptor(IndVarSimplifyPass{})); |
| 79 | + LoopUnrollOptions UnrollOptions; |
| 80 | + FPM.addPass(LoopUnrollPass{UnrollOptions}); |
| 81 | + FPM.addPass(SROAPass{}); |
| 82 | + // Run the InferAddressSpace pass to remove as many address-space casts |
| 83 | + // to/from generic address-space as possible, because these hinder |
| 84 | + // internalization. |
| 85 | + // FIXME: TTI should tell the pass which address space to use. |
| 86 | + // Ideally, the static compiler should have performed that job. |
| 87 | + constexpr unsigned FlatAddressSpace = 4; |
| 88 | + FPM.addPass(InferAddressSpacesPass(FlatAddressSpace)); |
| 89 | + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 90 | + } |
| 91 | + // Run dataflow internalization and runtime constant propagation. |
| 92 | + MPM.addPass(SYCLInternalizer{}); |
| 93 | + MPM.addPass(SYCLCP{}); |
| 94 | + // Run additional optimization passes after completing fusion. |
| 95 | + { |
| 96 | + FunctionPassManager FPM; |
| 97 | + FPM.addPass(SROAPass{}); |
| 98 | + FPM.addPass(SCCPPass{}); |
| 99 | + FPM.addPass(InstCombinePass{}); |
| 100 | + FPM.addPass(SimplifyCFGPass{}); |
| 101 | + FPM.addPass(SROAPass{}); |
| 102 | + FPM.addPass(InstCombinePass{}); |
| 103 | + FPM.addPass(SimplifyCFGPass{}); |
| 104 | + FPM.addPass(ADCEPass{}); |
| 105 | + FPM.addPass(EarlyCSEPass{/*UseMemorySSA*/ true}); |
| 106 | + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 107 | + } |
| 108 | + MPM.run(Mod, MAM); |
| 109 | + |
| 110 | + if (DebugEnabled) { |
| 111 | + // Restore debug option |
| 112 | + jit_compiler::PassDebug = false; |
| 113 | + } |
| 114 | + |
| 115 | + assert(!verifyModule(Mod, &errs()) && "Invalid LLVM IR generated"); |
| 116 | + |
| 117 | + auto NewModInfo = MAM.getResult<SYCLModuleInfoAnalysis>(Mod); |
| 118 | + assert(NewModInfo.ModuleInfo && "Failed to retrieve SYCL module info"); |
| 119 | + |
| 120 | + return std::make_unique<SYCLModuleInfo>(std::move(*NewModInfo.ModuleInfo)); |
| 121 | +} |
0 commit comments