1#ifndef PROTEUS_CORE_LLVM_HPP
2#define PROTEUS_CORE_LLVM_HPP
4static_assert(__cplusplus >= 201703L,
5 "This header requires C++17 or later due to LLVM.");
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>
18#if LLVM_VERSION_MAJOR >= 18
19#include <llvm/TargetParser/SubtargetFeature.h>
24#elif LLVM_VERSION_MAJOR == 17
25#if defined(__HIP_PLATFORM_HCC__) || defined(HIP_VERSION_MAJOR)
26#include <llvm/MC/SubtargetFeature.h>
28#include <llvm/TargetParser/SubtargetFeature.h>
31#define STRINGIFY_HELPER(x) #x
32#define STRINGIFY(x) STRINGIFY_HELPER(x)
33#error "Unsupported LLVM version " STRINGIFY(LLVM_VERSION_MAJOR)
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>
54 auto CGOptLevel = CodeGenOpt::getLevel(OptLevel);
59 const Target *
T = TargetRegistry::lookupTarget(M.getTargetTriple(),
Msg);
67 if (M.getModuleFlag(
"PIC Level"))
69 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
71 std::optional<CodeModel::Model>
CodeModel = M.getCodeModel();
77 std::unique_ptr<TargetMachine>
TM(
T->createTargetMachine(
87 const std::string &PassPipeline,
88 unsigned CodegenOptLevel = 3) {
91 std::optional<PGOOptions>
PGOOpt;
93 if (
auto Err =
TM.takeError())
105 PB.registerModuleAnalyses(
MAM);
106 PB.registerCGSCCAnalyses(
CGAM);
107 PB.registerFunctionAnalyses(
FAM);
108 PB.registerLoopAnalyses(
LAM);
111 if (
auto E =
PB.parsePassPipeline(
Passes, PassPipeline))
119 unsigned CodegenOptLevel = 3) {
122 std::optional<PGOOptions>
PGOOpt;
124 if (
auto Err =
TM.takeError())
136 PB.registerModuleAnalyses(
MAM);
137 PB.registerCGSCCAnalyses(
CGAM);
138 PB.registerFunctionAnalyses(
FAM);
139 PB.registerLoopAnalyses(
LAM);
183 unsigned CodegenOptLevel) {
187 <<
"optimizeIR optlevel " << OptLevel <<
" codegenopt "
188 << CodegenOptLevel <<
" " <<
T.elapsed() <<
" ms\n");
192 const std::string &PassPipeline,
193 unsigned CodegenOptLevel) {
195 auto TraceOut = [](
const std::string &PassPipeline) {
198 OS <<
"[CustomPipeline] " << PassPipeline <<
"\n";
207 <<
"optimizeIR optlevel " << PassPipeline
208 <<
" codegenopt " << CodegenOptLevel <<
" "
209 <<
T.elapsed() <<
" ms\n");
212inline std::unique_ptr<Module>
218 auto LinkedModule = std::make_unique<llvm::Module>(
"JitModule", Ctx);
237 PB.registerModuleAnalyses(
MAM);
238 PB.registerCGSCCAnalyses(
CGAM);
239 PB.registerFunctionAnalyses(
FAM);
240 PB.registerLoopAnalyses(
LAM);
259 if (
auto *
CompilerUsed = M.getGlobalVariable(
"llvm.compiler.used"))
265 for (
auto &
GV : M.globals()) {
266 auto Name =
GV.getName();
267 if (Name.starts_with(
"__clang_gpu_used_external") ||
268 Name.starts_with(
"_jit_bitcode") || Name.starts_with(
"__hip_cuid")) {
278 M.eraseGlobalVariable(
GV);
283 for (
auto &
GV : M.globals())
284 if (
GV.isExternallyInitialized())
285 GV.setExternallyInitialized(
false);
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
#define PROTEUS_TIMER_OUTPUT(x)
Definition TimeTracing.hpp:57
static Config & get()
Definition Config.hpp:284
static llvm::raw_ostream & outs(const std::string &Name)
Definition Logger.hpp:25
static void trace(llvm::StringRef Msg)
Definition Logger.hpp:30
Definition TimeTracing.hpp:36
void runOptimizationPassPipeline(Module &M, StringRef Arch, const std::string &PassPipeline, unsigned CodegenOptLevel=3)
Definition CoreLLVM.hpp:86
Expected< std::unique_ptr< TargetMachine > > createTargetMachine(Module &M, StringRef Arch, unsigned OptLevel=3)
Definition CoreLLVM.hpp:52
Definition BuiltinsCUDA.cpp:4
void optimizeIR(Module &M, StringRef Arch, char OptLevel, unsigned CodegenOptLevel)
Definition CoreLLVM.hpp:182
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
void pruneIR(Module &M, bool UnsetExternallyInitialized=true)
Definition CoreLLVM.hpp:253
std::string toString(CodegenOption Option)
Definition Config.hpp:26
void internalize(Module &M, StringRef PreserveFunctionName)
Definition CoreLLVM.hpp:288
void runCleanupPassPipeline(Module &M)
Definition CoreLLVM.hpp:230
std::unique_ptr< Module > linkModules(LLVMContext &Ctx, SmallVector< std::unique_ptr< Module > > LinkedModules)
Definition CoreLLVM.hpp:213
Definition CoreLLVM.hpp:172
InitLLVMTargets()
Definition CoreLLVM.hpp:173