[Openmp-commits] [openmp] 25c0ea2 - [NFC] Consolidate llvm::CodeGenOpt::Level handling
Scott Linder via Openmp-commits
openmp-commits at lists.llvm.org
Mon Jan 23 14:51:42 PST 2023
Author: Scott Linder
Date: 2023-01-23T22:50:49Z
New Revision: 25c0ea2a5370813f46686918a84e0de27e107d08
URL: https://github.com/llvm/llvm-project/commit/25c0ea2a5370813f46686918a84e0de27e107d08
DIFF: https://github.com/llvm/llvm-project/commit/25c0ea2a5370813f46686918a84e0de27e107d08.diff
LOG: [NFC] Consolidate llvm::CodeGenOpt::Level handling
Add free functions llvm::CodeGenOpt::{getLevel,getID,parseLevel} to
provide common implementations for functionality that has been
duplicated in many places across the codebase.
Differential Revision: https://reviews.llvm.org/D141968
Added:
Modified:
clang/lib/CodeGen/BackendUtil.cpp
clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
flang/lib/Frontend/FrontendActions.cpp
llvm/include/llvm/Support/CodeGen.h
llvm/lib/LTO/LTOCodeGenerator.cpp
llvm/tools/gold/gold-plugin.cpp
llvm/tools/llc/llc.cpp
llvm/tools/lli/lli.cpp
llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
llvm/tools/llvm-lto2/llvm-lto2.cpp
llvm/tools/lto/lto.cpp
openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index ecc727d6dd281..10d6bff25e6d6 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -300,21 +300,6 @@ static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
return TLII;
}
-static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
- switch (CodeGenOpts.OptimizationLevel) {
- default:
- llvm_unreachable("Invalid optimization level!");
- case 0:
- return CodeGenOpt::None;
- case 1:
- return CodeGenOpt::Less;
- case 2:
- return CodeGenOpt::Default; // O2/Os/Oz
- case 3:
- return CodeGenOpt::Aggressive;
- }
-}
-
static std::optional<llvm::CodeModel::Model>
getCodeModel(const CodeGenOptions &CodeGenOpts) {
unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
@@ -579,7 +564,10 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
std::string FeaturesStr =
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
- CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
+ std::optional<CodeGenOpt::Level> OptLevelOrNone =
+ CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
+ assert(OptLevelOrNone && "Invalid optimization level!");
+ CodeGenOpt::Level OptLevel = *OptLevelOrNone;
llvm::TargetOptions Options;
if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
@@ -1161,7 +1149,10 @@ static void runThinLTOBackend(
Conf.CodeModel = getCodeModel(CGOpts);
Conf.MAttrs = TOpts.Features;
Conf.RelocModel = CGOpts.RelocationModel;
- Conf.CGOptLevel = getCGOptLevel(CGOpts);
+ std::optional<CodeGenOpt::Level> OptLevelOrNone =
+ CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
+ assert(OptLevelOrNone && "Invalid optimization level!");
+ Conf.CGOptLevel = *OptLevelOrNone;
Conf.OptLevel = CGOpts.OptimizationLevel;
initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
Conf.SampleProfile = std::move(SampleProfile);
diff --git a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
index 5c9066c148d61..06df39dcdc479 100644
--- a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -54,14 +54,11 @@ getOptLevel(const std::vector<const char *> &ExtraArgs) {
CodeGenOpt::Level OLvl = CodeGenOpt::Default;
for (auto &A : ExtraArgs) {
if (A[0] == '-' && A[1] == 'O') {
- switch(A[2]) {
- case '0': OLvl = CodeGenOpt::None; break;
- case '1': OLvl = CodeGenOpt::Less; break;
- case '2': OLvl = CodeGenOpt::Default; break;
- case '3': OLvl = CodeGenOpt::Aggressive; break;
- default:
- errs() << "error: opt level must be between 0 and 3.\n";
- std::exit(1);
+ if (auto Level = CodeGenOpt::parseLevel(A[2])) {
+ OLvl = *Level;
+ } else {
+ errs() << "error: opt level must be between 0 and 3.\n";
+ std::exit(1);
}
}
}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 5cae73ef543eb..57603ccef01f9 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -491,20 +491,6 @@ std::vector<std::string> getTargetFeatures(ArrayRef<OffloadFile> InputFiles) {
return UnifiedFeatures;
}
-CodeGenOpt::Level getCGOptLevel(unsigned OptLevel) {
- switch (OptLevel) {
- case 0:
- return CodeGenOpt::None;
- case 1:
- return CodeGenOpt::Less;
- case 2:
- return CodeGenOpt::Default;
- case 3:
- return CodeGenOpt::Aggressive;
- }
- llvm_unreachable("Invalid optimization level");
-}
-
template <typename ModuleHook = function_ref<bool(size_t, const Module &)>>
std::unique_ptr<lto::LTO> createLTO(
const ArgList &Args, const std::vector<std::string> &Features,
@@ -522,7 +508,10 @@ std::unique_ptr<lto::LTO> createLTO(
StringRef OptLevel = Args.getLastArgValue(OPT_opt_level, "O2");
Conf.MAttrs = Features;
- Conf.CGOptLevel = getCGOptLevel(OptLevel[1] - '0');
+ std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
+ CodeGenOpt::parseLevel(OptLevel[1]);
+ assert(CGOptLevelOrNone && "Invalid optimization level");
+ Conf.CGOptLevel = *CGOptLevelOrNone;
Conf.OptLevel = OptLevel[1] - '0';
if (Conf.OptLevel > 0)
Conf.UseDefaultPipeline = true;
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index d46d281f94da6..ea8131e4e0b57 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -572,22 +572,6 @@ void CodeGenAction::generateLLVMIR() {
}
}
-static llvm::CodeGenOpt::Level
-getCGOptLevel(const Fortran::frontend::CodeGenOptions &opts) {
- switch (opts.OptimizationLevel) {
- default:
- llvm_unreachable("Invalid optimization level!");
- case 0:
- return llvm::CodeGenOpt::None;
- case 1:
- return llvm::CodeGenOpt::Less;
- case 2:
- return llvm::CodeGenOpt::Default;
- case 3:
- return llvm::CodeGenOpt::Aggressive;
- }
-}
-
void CodeGenAction::setUpTargetMachine() {
CompilerInstance &ci = this->getInstance();
@@ -602,7 +586,10 @@ void CodeGenAction::setUpTargetMachine() {
// Create `TargetMachine`
const auto &CGOpts = ci.getInvocation().getCodeGenOpts();
- llvm::CodeGenOpt::Level OptLevel = getCGOptLevel(CGOpts);
+ std::optional<llvm::CodeGenOpt::Level> OptLevelOrNone =
+ CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
+ assert(OptLevelOrNone && "Invalid optimization level!");
+ llvm::CodeGenOpt::Level OptLevel = *OptLevelOrNone;
std::string featuresStr = llvm::join(targetOpts.featuresAsWritten.begin(),
targetOpts.featuresAsWritten.end(), ",");
tm.reset(theTarget->createTargetMachine(
diff --git a/llvm/include/llvm/Support/CodeGen.h b/llvm/include/llvm/Support/CodeGen.h
index 425d3a3d95d4d..77f999f741299 100644
--- a/llvm/include/llvm/Support/CodeGen.h
+++ b/llvm/include/llvm/Support/CodeGen.h
@@ -14,6 +14,9 @@
#ifndef LLVM_SUPPORT_CODEGEN_H
#define LLVM_SUPPORT_CODEGEN_H
+#include <cstdint>
+#include <optional>
+
namespace llvm {
// Relocation model types.
@@ -47,15 +50,37 @@ namespace llvm {
};
}
- // Code generation optimization level.
namespace CodeGenOpt {
- enum Level {
- None = 0, // -O0
- Less = 1, // -O1
- Default = 2, // -O2, -Os
- Aggressive = 3 // -O3
- };
+ /// Type for the unique integer IDs of code generation optimization levels.
+ using IDType = uint8_t;
+ /// Code generation optimization level.
+ enum Level : IDType {
+ None = 0, ///< -O0
+ Less = 1, ///< -O1
+ Default = 2, ///< -O2, -Os
+ Aggressive = 3 ///< -O3
+ };
+ /// Get the \c Level identified by the integer \p ID.
+ ///
+ /// Returns std::nullopt if \p ID is invalid.
+ inline std::optional<Level> getLevel(IDType ID) {
+ if (ID < 0 || ID > 3)
+ return std::nullopt;
+ return static_cast<Level>(ID);
+ }
+ /// Get the integer \c ID of \p Level.
+ inline IDType getID(CodeGenOpt::Level Level) {
+ return static_cast<IDType>(Level);
+ }
+ /// Parse \p C as a single digit integer ID and get matching \c Level.
+ ///
+ /// Returns std::nullopt if the input is not a valid digit or not a valid ID.
+ inline std::optional<Level> parseLevel(char C) {
+ if (C < '0')
+ return std::nullopt;
+ return getLevel(static_cast<IDType>(C - '0'));
}
+ } // namespace CodeGenOpt
/// These enums are meant to be passed into addPassesToEmitFile to indicate
/// what type of file to emit, and returned by it to indicate what type of
diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 9d20fd63a84ac..ae7b7e4b54812 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -200,21 +200,10 @@ void LTOCodeGenerator::setOptLevel(unsigned Level) {
Config.OptLevel = Level;
Config.PTO.LoopVectorization = Config.OptLevel > 1;
Config.PTO.SLPVectorization = Config.OptLevel > 1;
- switch (Config.OptLevel) {
- case 0:
- Config.CGOptLevel = CodeGenOpt::None;
- return;
- case 1:
- Config.CGOptLevel = CodeGenOpt::Less;
- return;
- case 2:
- Config.CGOptLevel = CodeGenOpt::Default;
- return;
- case 3:
- Config.CGOptLevel = CodeGenOpt::Aggressive;
- return;
- }
- llvm_unreachable("Unknown optimization level!");
+ std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
+ CodeGenOpt::getLevel(Config.OptLevel);
+ assert(CGOptLevelOrNone && "Unknown optimization level!");
+ Config.CGOptLevel = *CGOptLevelOrNone;
}
bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 660f51c40d6b9..939dbaf06b04c 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -846,20 +846,6 @@ static int getOutputFileName(StringRef InFilename, bool TempOutFile,
return FD;
}
-static CodeGenOpt::Level getCGOptLevel() {
- switch (options::OptLevel) {
- case 0:
- return CodeGenOpt::None;
- case 1:
- return CodeGenOpt::Less;
- case 2:
- return CodeGenOpt::Default;
- case 3:
- return CodeGenOpt::Aggressive;
- }
- llvm_unreachable("Invalid optimization level");
-}
-
/// Parse the thinlto_prefix_replace option into the \p OldPrefix and
/// \p NewPrefix strings, if it was specified.
static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
@@ -896,7 +882,10 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
Conf.MAttrs = codegen::getMAttrs();
Conf.RelocModel = RelocationModel;
Conf.CodeModel = codegen::getExplicitCodeModel();
- Conf.CGOptLevel = getCGOptLevel();
+ std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
+ CodeGenOpt::getLevel(options::OptLevel);
+ assert(CGOptLevelOrNone && "Invalid optimization level");
+ Conf.CGOptLevel = *CGOptLevelOrNone;
Conf.DisableVerify = options::DisableVerify;
Conf.OptLevel = options::OptLevel;
Conf.PTO.LoopVectorization = options::OptLevel > 1;
diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
index e6b65ac446618..f2dae67040ffd 100644
--- a/llvm/tools/llc/llc.cpp
+++ b/llvm/tools/llc/llc.cpp
@@ -120,7 +120,7 @@ static cl::opt<char>
OptLevel("O",
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
"(default = '-O2')"),
- cl::Prefix, cl::init(' '));
+ cl::Prefix, cl::init('2'));
static cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
@@ -472,16 +472,12 @@ static int compileModule(char **argv, LLVMContext &Context) {
bool SkipModule =
CPUStr == "help" || (!MAttrs.empty() && MAttrs.front() == "help");
- CodeGenOpt::Level OLvl = CodeGenOpt::Default;
- switch (OptLevel) {
- default:
+ CodeGenOpt::Level OLvl;
+ if (auto Level = CodeGenOpt::parseLevel(OptLevel)) {
+ OLvl = *Level;
+ } else {
WithColor::error(errs(), argv[0]) << "invalid optimization level.\n";
return 1;
- case ' ': break;
- case '0': OLvl = CodeGenOpt::None; break;
- case '1': OLvl = CodeGenOpt::Less; break;
- case '2': OLvl = CodeGenOpt::Default; break;
- case '3': OLvl = CodeGenOpt::Aggressive; break;
}
// Parse 'none' or '$major.$minor'. Disallow -binutils-version=0 because we
diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index 313abeaeb5a97..c9b77e23ba07a 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -174,7 +174,7 @@ namespace {
cl::opt<char> OptLevel("O",
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
"(default = '-O2')"),
- cl::Prefix, cl::init(' '));
+ cl::Prefix, cl::init('2'));
cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
@@ -408,17 +408,10 @@ static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
}
CodeGenOpt::Level getOptLevel() {
- switch (OptLevel) {
- default:
- WithColor::error(errs(), "lli") << "invalid optimization level.\n";
- exit(1);
- case '0': return CodeGenOpt::None;
- case '1': return CodeGenOpt::Less;
- case ' ':
- case '2': return CodeGenOpt::Default;
- case '3': return CodeGenOpt::Aggressive;
- }
- llvm_unreachable("Unrecognized opt level.");
+ if (auto Level = CodeGenOpt::parseLevel(OptLevel))
+ return *Level;
+ WithColor::error(errs(), "lli") << "invalid optimization level.\n";
+ exit(1);
}
[[noreturn]] static void reportError(SMDiagnostic Err, const char *ProgName) {
diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
index 599c414b17b1e..dbcdba730d2d3 100644
--- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
+++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
@@ -42,7 +42,7 @@ static cl::opt<char>
OptLevel("O",
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
"(default = '-O2')"),
- cl::Prefix, cl::init(' '));
+ cl::Prefix, cl::init('2'));
static cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
@@ -144,16 +144,12 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
std::string CPUStr = codegen::getCPUStr(),
FeaturesStr = codegen::getFeaturesStr();
- CodeGenOpt::Level OLvl = CodeGenOpt::Default;
- switch (OptLevel) {
- default:
+ CodeGenOpt::Level OLvl;
+ if (auto Level = CodeGenOpt::parseLevel(OptLevel)) {
+ OLvl = *Level;
+ } else {
errs() << argv[0] << ": invalid optimization level.\n";
return 1;
- case ' ': break;
- case '0': OLvl = CodeGenOpt::None; break;
- case '1': OLvl = CodeGenOpt::Less; break;
- case '2': OLvl = CodeGenOpt::Default; break;
- case '3': OLvl = CodeGenOpt::Aggressive; break;
}
TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp
index 14e9ecb8c94d4..09c74fc586f54 100644
--- a/llvm/tools/llvm-lto2/llvm-lto2.cpp
+++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp
@@ -306,20 +306,9 @@ static int run(int argc, char **argv) {
Conf.Freestanding = EnableFreestanding;
for (auto &PluginFN : PassPlugins)
Conf.PassPlugins.push_back(PluginFN);
- switch (CGOptLevel) {
- case '0':
- Conf.CGOptLevel = CodeGenOpt::None;
- break;
- case '1':
- Conf.CGOptLevel = CodeGenOpt::Less;
- break;
- case '2':
- Conf.CGOptLevel = CodeGenOpt::Default;
- break;
- case '3':
- Conf.CGOptLevel = CodeGenOpt::Aggressive;
- break;
- default:
+ if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) {
+ Conf.CGOptLevel = *Level;
+ } else {
llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
return 1;
}
diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
index f15441835f395..22ebf54e7cbfe 100644
--- a/llvm/tools/lto/lto.cpp
+++ b/llvm/tools/lto/lto.cpp
@@ -528,20 +528,10 @@ thinlto_code_gen_t thinlto_create_codegen(void) {
if (OptLevel < '0' || OptLevel > '3')
report_fatal_error("Optimization level must be between 0 and 3");
CodeGen->setOptLevel(OptLevel - '0');
- switch (OptLevel) {
- case '0':
- CodeGen->setCodeGenOptLevel(CodeGenOpt::None);
- break;
- case '1':
- CodeGen->setCodeGenOptLevel(CodeGenOpt::Less);
- break;
- case '2':
- CodeGen->setCodeGenOptLevel(CodeGenOpt::Default);
- break;
- case '3':
- CodeGen->setCodeGenOptLevel(CodeGenOpt::Aggressive);
- break;
- }
+ std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
+ CodeGenOpt::getLevel(OptLevel - '0');
+ assert(CGOptLevelOrNone);
+ CodeGen->setCodeGenOptLevel(*CGOptLevelOrNone);
}
return wrap(CodeGen);
}
diff --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
index ca52059606cd1..d7f42a117e2ba 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
@@ -138,20 +138,6 @@ createModuleFromImage(const __tgt_device_image &Image, LLVMContext &Context) {
return createModuleFromMemoryBuffer(MB, Context);
}
-CodeGenOpt::Level getCGOptLevel(unsigned OptLevel) {
- switch (OptLevel) {
- case 0:
- return CodeGenOpt::None;
- case 1:
- return CodeGenOpt::Less;
- case 2:
- return CodeGenOpt::Default;
- case 3:
- return CodeGenOpt::Aggressive;
- }
- llvm_unreachable("Invalid optimization level");
-}
-
OptimizationLevel getOptLevel(unsigned OptLevel) {
switch (OptLevel) {
case 0:
@@ -169,7 +155,10 @@ OptimizationLevel getOptLevel(unsigned OptLevel) {
Expected<std::unique_ptr<TargetMachine>>
createTargetMachine(Module &M, std::string CPU, unsigned OptLevel) {
Triple TT(M.getTargetTriple());
- CodeGenOpt::Level CGOptLevel = getCGOptLevel(OptLevel);
+ std::optional<CodeGenOpt::Level> CGOptLevelOrNone =
+ CodeGenOpt::getLevel(OptLevel);
+ assert(CGOptLevelOrNone && "Invalid optimization level");
+ CodeGenOpt::Level CGOptLevel = *CGOptLevelOrNone;
std::string Msg;
const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
More information about the Openmp-commits
mailing list