Skip to content

Commit e60c1cc

Browse files
committed
Update CodeGenOpt
There is a flag-day change around CodeGen enums in llvm/llvm-project#66295. Update the code to use both old and new schemes depending on the LLVM version used. Change enum to uint32_t in some places to reduce the number of include guards.
1 parent 7aa4658 commit e60c1cc

File tree

12 files changed

+161
-16
lines changed

12 files changed

+161
-16
lines changed

lgc/include/lgc/patch/Patch.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class Patch {
5353
virtual ~Patch() {}
5454

5555
static void addPasses(PipelineState *pipelineState, lgc::PassManager &passMgr, llvm::Timer *patchTimer,
56-
llvm::Timer *optTimer, Pipeline::CheckShaderCacheFunc checkShaderCacheFunc,
57-
llvm::CodeGenOpt::Level optLevel);
56+
llvm::Timer *optTimer, Pipeline::CheckShaderCacheFunc checkShaderCacheFunc, uint32_t optLevel);
5857

5958
// Register all the patching passes into the given pass manager
6059
static void registerPasses(lgc::PassManager &passMgr);
@@ -65,7 +64,7 @@ class Patch {
6564
static llvm::GlobalVariable *getLdsVariable(PipelineState *pipelineState, llvm::Module *module);
6665

6766
protected:
68-
static void addOptimizationPasses(lgc::PassManager &passMgr, llvm::CodeGenOpt::Level optLevel);
67+
static void addOptimizationPasses(lgc::PassManager &passMgr, uint32_t optLevel);
6968

7069
void init(llvm::Module *module);
7170

lgc/interface/lgc/LgcContext.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ class LgcContext {
8484
// @param gpuName : LLVM GPU name (e.g. "gfx900"); empty to use -mcpu option setting
8585
// @param optLevel : LLVM optimization level used to initialize target machine
8686
static std::unique_ptr<llvm::TargetMachine> createTargetMachine(llvm::StringRef gpuName,
87-
llvm::CodeGenOpt::Level optLevel);
87+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
88+
// Old version of the code
89+
llvm::CodeGenOpt::Level optLevel
90+
#else
91+
// New version of the code (also handles unknown
92+
// version, which we treat as latest)
93+
llvm::CodeGenOptLevel optLevel
94+
#endif
95+
);
8896

8997
// Create the LgcContext.
9098
//
@@ -129,11 +137,21 @@ class LgcContext {
129137
// Adds target passes to pass manager, depending on "-filetype" and "-emit-llvm" options
130138
void addTargetPasses(lgc::LegacyPassManager &passMgr, llvm::Timer *codeGenTimer, llvm::raw_pwrite_stream &outStream);
131139

140+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
141+
// Old version of the code
132142
// Returns the optimization level for the context.
133143
llvm::CodeGenOpt::Level getOptimizationLevel() const;
134144

135145
// Returns the optimization level used for context initialization.
136146
llvm::CodeGenOpt::Level getInitialOptimizationLevel() const { return m_initialOptLevel; }
147+
#else
148+
// New version of the code (also handles unknown version, which we treat as latest)
149+
// Returns the optimization level for the context.
150+
llvm::CodeGenOptLevel getOptimizationLevel() const;
151+
152+
// Returns the optimization level used for context initialization.
153+
llvm::CodeGenOptLevel getInitialOptimizationLevel() const { return m_initialOptLevel; }
154+
#endif
137155

138156
// Utility method to create a start/stop timer pass
139157
static llvm::ModulePass *createStartStopTimer(llvm::Timer *timer, bool starting);
@@ -167,7 +185,13 @@ class LgcContext {
167185
TargetInfo *m_targetInfo = nullptr; // Target info
168186
unsigned m_palAbiVersion = 0xFFFFFFFF; // PAL pipeline ABI version to compile for
169187
PassManagerCache *m_passManagerCache = nullptr; // Pass manager cache and creator
170-
llvm::CodeGenOpt::Level m_initialOptLevel; // Optimization level at initialization
188+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
189+
// Old version of the code
190+
llvm::CodeGenOpt::Level m_initialOptLevel; // Optimization level at initialization
191+
#else
192+
// New version of the code (also handles unknown version, which we treat as latest)
193+
llvm::CodeGenOptLevel m_initialOptLevel; // Optimization level at initialization
194+
#endif
171195
};
172196

173197
} // namespace lgc

lgc/patch/Patch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace lgc {
117117
// @param optLevel : The optimization level uses to adjust the aggressiveness of
118118
// passes and which passes to add.
119119
void Patch::addPasses(PipelineState *pipelineState, lgc::PassManager &passMgr, Timer *patchTimer, Timer *optTimer,
120-
Pipeline::CheckShaderCacheFunc checkShaderCacheFunc, CodeGenOpt::Level optLevel) {
120+
Pipeline::CheckShaderCacheFunc checkShaderCacheFunc, uint32_t optLevel) {
121121
// Start timer for patching passes.
122122
if (patchTimer)
123123
LgcContext::createAndAddStartStopTimer(passMgr, patchTimer, true);
@@ -340,7 +340,7 @@ void Patch::registerPasses(PassBuilder &passBuilder) {
340340
// @param [in/out] passMgr : Pass manager to add passes to
341341
// @param optLevel : The optimization level uses to adjust the aggressiveness of
342342
// passes and which passes to add.
343-
void Patch::addOptimizationPasses(lgc::PassManager &passMgr, CodeGenOpt::Level optLevel) {
343+
void Patch::addOptimizationPasses(lgc::PassManager &passMgr, uint32_t optLevel) {
344344
LLPC_OUTS("PassManager optimization level = " << optLevel << "\n");
345345

346346
passMgr.addPass(ForceFunctionAttrsPass());

lgc/state/Compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ bool PipelineState::generate(Module *pipelineModule, raw_pwrite_stream &outStrea
219219
} else {
220220
// Patching.
221221
Patch::addPasses(this, *passMgr, patchTimer, optTimer, std::move(checkShaderCacheFunc),
222-
getLgcContext()->getOptimizationLevel());
222+
static_cast<uint32_t>(getLgcContext()->getOptimizationLevel()));
223223

224224
// Add pass to clear pipeline state from IR
225225
passMgr->addPass(PipelineStateClearer());

lgc/state/LgcContext.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,23 @@ static cl::opt<bool> EmitLgc("emit-lgc", cl::desc("Emit LLVM assembly suitable f
7777
static cl::opt<bool> ShowEncoding("show-encoding", cl::desc("Show instruction encodings"), cl::init(false));
7878

7979
// -opt: Override the optimization level passed in to LGC with the given one.
80+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
81+
// Old version of the code
8082
static cl::opt<CodeGenOpt::Level> OptLevel("opt", cl::desc("Set the optimization level for LGC:"),
8183
cl::init(CodeGenOpt::Default),
8284
values(clEnumValN(CodeGenOpt::None, "none", "no optimizations"),
8385
clEnumValN(CodeGenOpt::Less, "quick", "quick compilation time"),
8486
clEnumValN(CodeGenOpt::Default, "default", "default optimizations"),
8587
clEnumValN(CodeGenOpt::Aggressive, "fast", "fast execution time")));
88+
#else
89+
// New version of the code (also handles unknown version, which we treat as latest)
90+
static cl::opt<CodeGenOptLevel>
91+
OptLevel("opt", cl::desc("Set the optimization level for LGC:"), cl::init(CodeGenOptLevel::Default),
92+
values(clEnumValN(CodeGenOptLevel::None, "none", "no optimizations"),
93+
clEnumValN(CodeGenOptLevel::Less, "quick", "quick compilation time"),
94+
clEnumValN(CodeGenOptLevel::Default, "default", "default optimizations"),
95+
clEnumValN(CodeGenOptLevel::Aggressive, "fast", "fast execution time")));
96+
#endif
8697

8798
// =====================================================================================================================
8899
// Set default for a command-line option, but only if command-line processing has not happened yet, or did not see
@@ -216,7 +227,14 @@ bool LgcContext::isGpuNameValid(llvm::StringRef gpuName) {
216227
//
217228
// @param gpuName : LLVM GPU name (e.g. "gfx900"); empty to use -mcpu option setting
218229
// @param optLevel : LLVM optimization level used to initialize target machine
219-
std::unique_ptr<TargetMachine> LgcContext::createTargetMachine(StringRef gpuName, CodeGenOpt::Level optLevel) {
230+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
231+
// Old version of the code
232+
std::unique_ptr<TargetMachine> LgcContext::createTargetMachine(StringRef gpuName, CodeGenOpt::Level optLevel)
233+
#else
234+
// New version of the code (also handles unknown version, which we treat as latest)
235+
std::unique_ptr<TargetMachine> LgcContext::createTargetMachine(StringRef gpuName, CodeGenOptLevel optLevel)
236+
#endif
237+
{
220238
assert(Initialized && "Must call LgcContext::initialize before LgcContext::createTargetMachine");
221239

222240
std::string mcpuName = codegen::getMCPU(); // -mcpu setting from llvm/CodeGen/CommandFlags.h
@@ -245,7 +263,7 @@ std::unique_ptr<TargetMachine> LgcContext::createTargetMachine(StringRef gpuName
245263
if (OptLevel.getPosition() != 0)
246264
optLevel = OptLevel;
247265

248-
LLPC_OUTS("TargetMachine optimization level = " << optLevel << "\n");
266+
LLPC_OUTS("TargetMachine optimization level = " << static_cast<uint32_t>(optLevel) << "\n");
249267

250268
return std::unique_ptr<TargetMachine>(target->createTargetMachine(triple, gpuName, "", targetOpts, {}, {}, optLevel));
251269
}
@@ -358,7 +376,14 @@ void LgcContext::addTargetPasses(lgc::LegacyPassManager &passMgr, Timer *codeGen
358376
passMgr.add(createStartStopTimer(codeGenTimer, false));
359377
}
360378

379+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
380+
// Old version of the code
361381
llvm::CodeGenOpt::Level LgcContext::getOptimizationLevel() const {
382+
#else
383+
// New version of the code (also handles unknown version, which we treat as latest)
384+
llvm::CodeGenOptLevel LgcContext::getOptimizationLevel() const {
385+
#endif
386+
362387
return m_targetMachine->getOptLevel();
363388
}
364389

lgc/tool/lgc/lgc.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ static bool runPassPipeline(Pipeline &pipeline, Module &module, raw_pwrite_strea
159159
passMgr->addPass(VerifierPass());
160160
passMgr->addPass(PipelineStateRecorder());
161161

162+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
163+
// Old version of the code
162164
switch (codegen::getFileType()) {
163165
case CGFT_AssemblyFile:
164166
passMgr->addPass(PrintModulePass(outStream));
@@ -169,6 +171,19 @@ static bool runPassPipeline(Pipeline &pipeline, Module &module, raw_pwrite_strea
169171
case CGFT_Null:
170172
break;
171173
}
174+
#else
175+
// New version of the code (also handles unknown version, which we treat as latest)
176+
switch (codegen::getFileType()) {
177+
case CodeGenFileType::AssemblyFile:
178+
passMgr->addPass(PrintModulePass(outStream));
179+
break;
180+
case CodeGenFileType::ObjectFile:
181+
passMgr->addPass(BitcodeWriterPass(outStream));
182+
break;
183+
case CodeGenFileType::Null:
184+
break;
185+
}
186+
#endif
172187

173188
passMgr->run(module);
174189
return true;
@@ -239,11 +254,23 @@ int main(int argc, char **argv) {
239254
assert(optIterator != cl::getRegisteredOptions().end());
240255
cl::Option *opt = optIterator->second;
241256
if (opt->getNumOccurrences() == 0)
257+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
258+
// Old version of the code
242259
*static_cast<cl::opt<CodeGenFileType> *>(opt) = CGFT_AssemblyFile;
260+
#else
261+
// New version of the code (also handles unknown version, which we treat as latest)
262+
*static_cast<cl::opt<CodeGenFileType> *>(opt) = CodeGenFileType::AssemblyFile;
263+
#endif
243264
}
244265

245266
// Create the LgcContext.
267+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
268+
// Old version of the code
246269
std::unique_ptr<TargetMachine> targetMachine(LgcContext::createTargetMachine(gpuName, CodeGenOpt::Level::Default));
270+
#else
271+
// New version of the code (also handles unknown version, which we treat as latest)
272+
std::unique_ptr<TargetMachine> targetMachine(LgcContext::createTargetMachine(gpuName, CodeGenOptLevel::Default));
273+
#endif
247274
if (!targetMachine) {
248275
errs() << progName << ": GPU type '" << gpuName << "' not recognized\n";
249276
return 1;

llpc/context/llpcContext.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ LgcContext *Context::getLgcContext() {
105105
return &*m_builderContext;
106106
}
107107

108+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
109+
// Old version of the code
108110
// =====================================================================================================================
109111
// Get optimization level. Also resets what getLastOptimizationLevel() returns.
110112
//
111113
// @returns: the optimization level for the context.
112114
CodeGenOpt::Level Context::getOptimizationLevel() {
113-
uint32_t optLevel = CodeGenOpt::Level::Default;
115+
uint32_t optLevel = static_cast<uint32_t>(CodeGenOpt::Level::Default);
116+
114117
optLevel = getPipelineContext()->getPipelineOptions()->optimizationLevel;
115118
if (optLevel > 3)
116119
optLevel = 3;
@@ -126,6 +129,33 @@ CodeGenOpt::Level Context::getLastOptimizationLevel() const {
126129
return *m_lastOptLevel;
127130
}
128131

132+
#else
133+
// New version of the code (also handles unknown version, which we treat as latest)
134+
135+
// =====================================================================================================================
136+
// Get optimization level. Also resets what getLastOptimizationLevel() returns.
137+
//
138+
// @returns: the optimization level for the context.
139+
CodeGenOptLevel Context::getOptimizationLevel() {
140+
uint32_t optLevel = static_cast<uint32_t>(CodeGenOptLevel::Default);
141+
142+
optLevel = getPipelineContext()->getPipelineOptions()->optimizationLevel;
143+
if (optLevel > 3)
144+
optLevel = 3;
145+
else if (optLevel == 0) // Workaround for noopt bugs in the AMDGPU backend in LLVM.
146+
optLevel = 1;
147+
m_lastOptLevel = CodeGenOptLevel(optLevel);
148+
return *m_lastOptLevel;
149+
}
150+
151+
// =====================================================================================================================
152+
// Get the optimization level returned by the last getOptimizationLevel().
153+
CodeGenOptLevel Context::getLastOptimizationLevel() const {
154+
return *m_lastOptLevel;
155+
}
156+
157+
#endif
158+
129159
// =====================================================================================================================
130160
// Loads library from external LLVM library.
131161
//

llpc/context/llpcContext.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ class Context : public llvm::LLVMContext {
8484
// Get (create if necessary) LgcContext
8585
lgc::LgcContext *getLgcContext();
8686

87+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
88+
// Old version of the code
8789
llvm::CodeGenOpt::Level getOptimizationLevel();
8890
llvm::CodeGenOpt::Level getLastOptimizationLevel() const;
91+
#else
92+
// New version of the code (also handles unknown version, which we treat as latest)
93+
llvm::CodeGenOptLevel getOptimizationLevel();
94+
llvm::CodeGenOptLevel getLastOptimizationLevel() const;
95+
#endif
8996

9097
std::unique_ptr<llvm::Module> loadLibrary(const BinaryData *lib);
9198

@@ -129,7 +136,14 @@ class Context : public llvm::LLVMContext {
129136
std::unique_ptr<llvm::TargetMachine> m_targetMachine; // Target machine for LGC context
130137
std::unique_ptr<lgc::LgcContext> m_builderContext; // LGC context
131138

139+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
140+
// Old version of the code
132141
std::optional<llvm::CodeGenOpt::Level> m_lastOptLevel{}; // What getOptimizationLevel() last returned
142+
#else
143+
// New version of the code (also handles unknown version, which we treat as latest)
144+
std::optional<llvm::CodeGenOptLevel> m_lastOptLevel{}; // What getOptimizationLevel() last returned
145+
#endif
146+
133147
std::unique_ptr<llvm_dialects::DialectContext> m_dialectContext;
134148

135149
unsigned m_useCount = 0; // Number of times this context is used.

llpc/tool/amdllpc.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,24 @@ cl::opt<bool> DumpDuplicatePipelines(
303303
// -llpc_opt: Override the optimization level passed in to LGC with the given one. This options is the same as the
304304
// `-opt` option in lgc. The reason for the second option is to be able to test the LLPC API. If both options are set
305305
// then `-opt` wins.
306+
307+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
308+
// Old version of the code
306309
cl::opt<CodeGenOpt::Level> LlpcOptLevel("llpc-opt", cl::desc("The optimization level for amdllpc to pass to LLPC:"),
307310
cl::init(CodeGenOpt::Default),
308311
values(clEnumValN(CodeGenOpt::None, "none", "no optimizations"),
309312
clEnumValN(CodeGenOpt::Less, "quick", "quick compilation time"),
310313
clEnumValN(CodeGenOpt::Default, "default", "default optimizations"),
311314
clEnumValN(CodeGenOpt::Aggressive, "fast", "fast execution time")));
315+
#else
316+
// New version of the code (also handles unknown version, which we treat as latest)
317+
cl::opt<CodeGenOptLevel> LlpcOptLevel("llpc-opt", cl::desc("The optimization level for amdllpc to pass to LLPC:"),
318+
cl::init(CodeGenOptLevel::Default),
319+
values(clEnumValN(CodeGenOptLevel::None, "none", "no optimizations"),
320+
clEnumValN(CodeGenOptLevel::Less, "quick", "quick compilation time"),
321+
clEnumValN(CodeGenOptLevel::Default, "default", "default optimizations"),
322+
clEnumValN(CodeGenOptLevel::Aggressive, "fast", "fast execution time")));
323+
#endif
312324

313325
// -resource-layout-scheme: specifies the layout scheme of the resource
314326
cl::opt<ResourceLayoutScheme> LayoutScheme("resource-layout-scheme", cl::desc("The resource layout scheme:"),
@@ -534,8 +546,16 @@ static void initCompileInfo(CompileInfo *compileInfo) {
534546
}
535547

536548
// We want the default optimization level to be "Default" which is not 0.
537-
compileInfo->gfxPipelineInfo.options.optimizationLevel = CodeGenOpt::Level::Default;
538-
compileInfo->compPipelineInfo.options.optimizationLevel = CodeGenOpt::Level::Default;
549+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
550+
// Old version of the code
551+
compileInfo->gfxPipelineInfo.options.optimizationLevel = static_cast<uint32_t>(CodeGenOpt::Level::Default);
552+
compileInfo->compPipelineInfo.options.optimizationLevel = static_cast<uint32_t>(CodeGenOpt::Level::Default);
553+
#else
554+
// New version of the code (also handles unknown version, which we treat as latest)
555+
compileInfo->gfxPipelineInfo.options.optimizationLevel = static_cast<uint32_t>(CodeGenOptLevel::Default);
556+
compileInfo->compPipelineInfo.options.optimizationLevel = static_cast<uint32_t>(CodeGenOptLevel::Default);
557+
#endif
558+
539559
compileInfo->gfxPipelineInfo.options.resourceLayoutScheme = LayoutScheme;
540560
compileInfo->compPipelineInfo.options.forceCsThreadIdSwizzling = ForceCsThreadIdSwizzling;
541561
compileInfo->compPipelineInfo.options.overrideThreadGroupSizeX = OverrideThreadGroupSizeX;

llpc/tool/llpcCompilationUtils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ struct CompileInfo {
103103
bool scratchAccessBoundsChecks; // Whether to enable scratch access bounds checks
104104
bool enableImplicitInvariantExports; // Whether to enable implicit marking of position exports as invariant
105105
VfxPipelineType pipelineType; // Pipeline type
106+
#if LLVM_MAIN_REVISION && LLVM_MAIN_REVISION < 474768
107+
// Old version of the code
106108
std::optional<llvm::CodeGenOpt::Level> optimizationLevel; // The optimization level to pass the compiler
107-
bool internalRtShaders; // Whether to enable intrinsics for internal RT shaders
109+
#else
110+
// New version of the code (also handles unknown version, which we treat as latest)
111+
std::optional<llvm::CodeGenOptLevel> optimizationLevel; // The optimization level to pass the compiler
112+
#endif
113+
bool internalRtShaders; // Whether to enable intrinsics for internal RT shaders
108114
bool enableColorExportShader; // Enable color export shader, only compile each stage of the pipeline without linking
109115
};
110116

0 commit comments

Comments
 (0)