Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
CoreLLVM.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_CORE_LLVM_HPP
2#define PROTEUS_CORE_LLVM_HPP
3
4static_assert(__cplusplus >= 201703L,
5 "This header requires C++17 or later due to LLVM.");
6
7#include <llvm/CodeGen/CommandFlags.h>
8#include <llvm/IR/DebugInfo.h>
9#include <llvm/IR/Module.h>
10#include <llvm/IR/PassManager.h>
11#include <llvm/Linker/Linker.h>
12#include <llvm/MC/TargetRegistry.h>
13#include <llvm/Passes/PassBuilder.h>
14#include <llvm/Support/TargetSelect.h>
15#include <llvm/Target/TargetMachine.h>
16#include <llvm/Transforms/IPO/MergeFunctions.h>
17
18#if LLVM_VERSION_MAJOR >= 18
19#include <llvm/TargetParser/SubtargetFeature.h>
20// This convoluted logic below is because AMD ROCm 5.7.1 identifies as LLVM 17
21// but includes the header SubtargetFeature.h to a different directory than
22// upstream LLVM 17. We basically detect if it's the HIP version and include it
23// from the expected MC directory, otherwise from TargetParser.
24#elif LLVM_VERSION_MAJOR == 17
25#if defined(__HIP_PLATFORM_HCC__) || defined(HIP_VERSION_MAJOR)
26#include <llvm/MC/SubtargetFeature.h>
27#else
28#include <llvm/TargetParser/SubtargetFeature.h>
29#endif
30#else
31#define STRINGIFY_HELPER(x) #x
32#define STRINGIFY(x) STRINGIFY_HELPER(x)
33#error "Unsupported LLVM version " STRINGIFY(LLVM_VERSION_MAJOR)
34#endif
35#include <llvm/Transforms/IPO/GlobalDCE.h>
36#include <llvm/Transforms/IPO/Internalize.h>
37#include <llvm/Transforms/IPO/StripDeadPrototypes.h>
38#include <llvm/Transforms/IPO/StripSymbols.h>
39#include <llvm/Transforms/Utils/ModuleUtils.h>
40
41#include "proteus/Debug.h"
42#include "proteus/Error.h"
43#include "proteus/Logger.hpp"
45
46namespace proteus {
47using namespace llvm;
48
49namespace detail {
50
51inline Expected<std::unique_ptr<TargetMachine>>
52createTargetMachine(Module &M, StringRef Arch, unsigned OptLevel = 3) {
53 Triple TT(M.getTargetTriple());
54 auto CGOptLevel = CodeGenOpt::getLevel(OptLevel);
55 if (CGOptLevel == std::nullopt)
56 PROTEUS_FATAL_ERROR("Invalid opt level");
57
58 std::string Msg;
59 const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
60 if (!T)
61 return make_error<StringError>(Msg, inconvertibleErrorCode());
62
63 SubtargetFeatures Features;
64 Features.getDefaultSubtargetFeatures(TT);
65
66 std::optional<Reloc::Model> RelocModel;
67 if (M.getModuleFlag("PIC Level"))
68 RelocModel =
69 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
70
71 std::optional<CodeModel::Model> CodeModel = M.getCodeModel();
72
73 // Use default target options.
74 // TODO: Customize based on AOT compilation flags or by creating a
75 // constructor that sets target options based on the triple.
76 TargetOptions Options;
77 std::unique_ptr<TargetMachine> TM(T->createTargetMachine(
78 M.getTargetTriple(), Arch, Features.getString(), Options, RelocModel,
79 CodeModel, CGOptLevel.value()));
80 if (!TM)
81 return make_error<StringError>("Failed to create target machine",
82 inconvertibleErrorCode());
83 return TM;
84}
85
86inline void runOptimizationPassPipeline(Module &M, StringRef Arch,
87 char OptLevel = '3',
88 unsigned CodegenOptLevel = 3) {
89 PipelineTuningOptions PTO;
90
91 std::optional<PGOOptions> PGOOpt;
92 auto TM = createTargetMachine(M, Arch, CodegenOptLevel);
93 if (auto Err = TM.takeError())
94 report_fatal_error(std::move(Err));
95 TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
96
97 PassBuilder PB(TM->get(), PTO, PGOOpt, nullptr);
98 LoopAnalysisManager LAM;
99 FunctionAnalysisManager FAM;
100 CGSCCAnalysisManager CGAM;
101 ModuleAnalysisManager MAM;
102
103 FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
104
105 PB.registerModuleAnalyses(MAM);
106 PB.registerCGSCCAnalyses(CGAM);
107 PB.registerFunctionAnalyses(FAM);
108 PB.registerLoopAnalyses(LAM);
109 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
110
111 OptimizationLevel OptSetting;
112 switch (OptLevel) {
113 case '0':
114 OptSetting = OptimizationLevel::O0;
115 break;
116 case '1':
117 OptSetting = OptimizationLevel::O1;
118 break;
119 case '2':
120 OptSetting = OptimizationLevel::O2;
121 break;
122 case '3':
123 OptSetting = OptimizationLevel::O3;
124 break;
125 case 's':
126 OptSetting = OptimizationLevel::Os;
127 break;
128 case 'z':
129 OptSetting = OptimizationLevel::Oz;
130 break;
131 default:
132 PROTEUS_FATAL_ERROR("Unsupported optimization level " + OptLevel);
133 };
134
135 ModulePassManager Passes = PB.buildPerModuleDefaultPipeline(OptSetting);
136 Passes.run(M, MAM);
137}
138
139} // namespace detail
140
143 InitializeAllTargetInfos();
144 InitializeAllTargets();
145 InitializeAllTargetMCs();
146 InitializeAllAsmParsers();
147 InitializeAllAsmPrinters();
148 }
149};
150
151inline void optimizeIR(Module &M, StringRef Arch, char OptLevel,
152 unsigned CodegenOptLevel) {
153 Timer T;
154 detail::runOptimizationPassPipeline(M, Arch, OptLevel, CodegenOptLevel);
156 << "optimizeIR optlevel " << OptLevel << " codegenopt "
157 << CodegenOptLevel << " " << T.elapsed() << " ms\n");
158}
159
160inline std::unique_ptr<Module>
161linkModules(LLVMContext &Ctx,
162 SmallVector<std::unique_ptr<Module>> LinkedModules) {
163 if (LinkedModules.empty())
164 PROTEUS_FATAL_ERROR("Expected jit module");
165
166 auto LinkedModule = std::make_unique<llvm::Module>("JitModule", Ctx);
167 Linker IRLinker(*LinkedModule);
168 // Link in all the proteus-enabled extracted modules.
169 for (auto &LinkedM : LinkedModules) {
170 // Returns true if linking failed.
171 if (IRLinker.linkInModule(std::move(LinkedM)))
172 PROTEUS_FATAL_ERROR("Linking failed");
173 }
174
175 return LinkedModule;
176}
177
178inline void runCleanupPassPipeline(Module &M) {
179 PassBuilder PB;
180 LoopAnalysisManager LAM;
181 FunctionAnalysisManager FAM;
182 CGSCCAnalysisManager CGAM;
183 ModuleAnalysisManager MAM;
184
185 PB.registerModuleAnalyses(MAM);
186 PB.registerCGSCCAnalyses(CGAM);
187 PB.registerFunctionAnalyses(FAM);
188 PB.registerLoopAnalyses(LAM);
189 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
190
191 ModulePassManager Passes;
192 Passes.addPass(MergeFunctionsPass());
193 Passes.addPass(GlobalDCEPass());
194 // Passes.addPass(StripDeadDebugInfoPass());
195 Passes.addPass(StripDeadPrototypesPass());
196
197 Passes.run(M, MAM);
198
199 StripDebugInfo(M);
200}
201
202inline void pruneIR(Module &M, bool UnsetExternallyInitialized = true) {
203 // Remove llvm.global.annotations now that we have read them.
204 if (auto *GlobalAnnotations = M.getGlobalVariable("llvm.global.annotations"))
205 M.eraseGlobalVariable(GlobalAnnotations);
206
207 // Remove llvm.compiler.used
208 if (auto *CompilerUsed = M.getGlobalVariable("llvm.compiler.used"))
209 M.eraseGlobalVariable(CompilerUsed);
210
211 // Remove the __clang_gpu_used_external used in HIP RDC compilation and its
212 // uses in llvm.used, llvm.compiler.used.
213 SmallVector<GlobalVariable *> GlobalsToErase;
214 for (auto &GV : M.globals()) {
215 auto Name = GV.getName();
216 if (Name.starts_with("__clang_gpu_used_external") ||
217 Name.starts_with("_jit_bitcode") || Name.starts_with("__hip_cuid")) {
218 GlobalsToErase.push_back(&GV);
219 removeFromUsedLists(M, [&GV](Constant *C) {
220 if (auto *Global = dyn_cast<GlobalVariable>(C))
221 return Global == &GV;
222 return false;
223 });
224 }
225 }
226 for (auto *GV : GlobalsToErase) {
227 M.eraseGlobalVariable(GV);
228 }
229
230 // Remove externaly_initialized attributes.
231 if (UnsetExternallyInitialized)
232 for (auto &GV : M.globals())
233 if (GV.isExternallyInitialized())
234 GV.setExternallyInitialized(false);
235}
236
237inline void internalize(Module &M, StringRef PreserveFunctionName) {
238 auto *F = M.getFunction(PreserveFunctionName);
239 // Internalize others besides the kernel function.
240 internalizeModule(M, [&F](const GlobalValue &GV) {
241 // Do not internalize the kernel function.
242 if (&GV == F)
243 return true;
244
245 // Internalize everything else.
246 return false;
247 });
248}
249
250} // namespace proteus
251
253
254#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:4
#define PROTEUS_TIMER_OUTPUT(x)
Definition TimeTracing.hpp:57
static llvm::raw_ostream & outs(const std::string &Name)
Definition Logger.hpp:25
Definition TimeTracing.hpp:36
uint64_t elapsed()
Definition TimeTracing.hpp:45
Expected< std::unique_ptr< TargetMachine > > createTargetMachine(Module &M, StringRef Arch, unsigned OptLevel=3)
Definition CoreLLVM.hpp:52
void runOptimizationPassPipeline(Module &M, StringRef Arch, char OptLevel='3', unsigned CodegenOptLevel=3)
Definition CoreLLVM.hpp:86
Definition Dispatcher.cpp:14
void optimizeIR(Module &M, StringRef Arch, char OptLevel, unsigned CodegenOptLevel)
Definition CoreLLVM.hpp:151
void pruneIR(Module &M, bool UnsetExternallyInitialized=true)
Definition CoreLLVM.hpp:202
void internalize(Module &M, StringRef PreserveFunctionName)
Definition CoreLLVM.hpp:237
void runCleanupPassPipeline(Module &M)
Definition CoreLLVM.hpp:178
std::unique_ptr< Module > linkModules(LLVMContext &Ctx, SmallVector< std::unique_ptr< Module > > LinkedModules)
Definition CoreLLVM.hpp:161
Definition CoreLLVM.hpp:141
InitLLVMTargets()
Definition CoreLLVM.hpp:142