-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Refactor getOptimizationLevel and getOptimizationLevelSize to non-static. NFC. #168839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… non-static. NFC. So that we can reuse these functions in few place, such as in clang/lib/Driver/ToolChains/CommonArgs.cpp. Part of the code there is currently copied from getOptimizationLevel.
|
@llvm/pr-subscribers-clang Author: Jim Lin (tclin914) ChangesSo that we can reuse these functions in few place, such as in clang/lib/Driver/ToolChains/CommonArgs.cpp. Part of the code there is currently copied from getOptimizationLevel. Full diff: https://github.com/llvm/llvm-project/pull/168839.diff 2 Files Affected:
diff --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h
index 51787d914e1ec..b19a6e1a8acc3 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -67,6 +67,11 @@ bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
DiagnosticsEngine *Diags = nullptr,
bool DefaultDiagColor = true);
+unsigned getOptimizationLevel(llvm::opt::ArgList &Args, InputKind IK,
+ DiagnosticsEngine &Diags);
+
+unsigned getOptimizationLevelSize(llvm::opt::ArgList &Args);
+
/// The base class of CompilerInvocation. It keeps individual option objects
/// behind reference-counted pointers, which is useful for clients that want to
/// keep select option objects alive (even after CompilerInvocation gets
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 54b302e829e1f..4ce06e8261d42 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -703,52 +703,6 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
// Deserialization (from args)
//===----------------------------------------------------------------------===//
-static unsigned getOptimizationLevel(ArgList &Args, InputKind IK,
- DiagnosticsEngine &Diags) {
- unsigned DefaultOpt = 0;
- if ((IK.getLanguage() == Language::OpenCL ||
- IK.getLanguage() == Language::OpenCLCXX) &&
- !Args.hasArg(OPT_cl_opt_disable))
- DefaultOpt = 2;
-
- if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
- if (A->getOption().matches(options::OPT_O0))
- return 0;
-
- if (A->getOption().matches(options::OPT_Ofast))
- return 3;
-
- assert(A->getOption().matches(options::OPT_O));
-
- StringRef S(A->getValue());
- if (S == "s" || S == "z")
- return 2;
-
- if (S == "g")
- return 1;
-
- return getLastArgIntValue(Args, OPT_O, DefaultOpt, Diags);
- }
-
- return DefaultOpt;
-}
-
-static unsigned getOptimizationLevelSize(ArgList &Args) {
- if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
- if (A->getOption().matches(options::OPT_O)) {
- switch (A->getValue()[0]) {
- default:
- return 0;
- case 's':
- return 1;
- case 'z':
- return 2;
- }
- }
- }
- return 0;
-}
-
static void GenerateArg(ArgumentConsumer Consumer,
llvm::opt::OptSpecifier OptSpecifier) {
Option Opt = getDriverOptTable().getOption(OptSpecifier);
@@ -1859,17 +1813,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
const LangOptions &LangOptsRef) {
unsigned NumErrorsBefore = Diags.getNumErrors();
- unsigned OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
- // TODO: This could be done in Driver
- unsigned MaxOptLevel = 3;
- if (OptimizationLevel > MaxOptLevel) {
- // If the optimization level is not supported, fall back on the default
- // optimization
- Diags.Report(diag::warn_drv_optimization_value)
- << Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel;
- OptimizationLevel = MaxOptLevel;
- }
- Opts.OptimizationLevel = OptimizationLevel;
+ Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
// The key paths of codegen options defined in Options.td start with
// "CodeGenOpts.". Let's provide the expected variable name and type.
@@ -2728,6 +2672,61 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
return Diags->getNumErrors() == NumErrorsBefore;
}
+unsigned clang::getOptimizationLevel(ArgList &Args, InputKind IK,
+ DiagnosticsEngine &Diags) {
+ unsigned DefaultOpt = 0;
+ if ((IK.getLanguage() == Language::OpenCL ||
+ IK.getLanguage() == Language::OpenCLCXX) &&
+ !Args.hasArg(OPT_cl_opt_disable))
+ DefaultOpt = 2;
+
+ if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+ if (A->getOption().matches(options::OPT_O0))
+ return 0;
+
+ if (A->getOption().matches(options::OPT_Ofast))
+ return 3;
+
+ assert(A->getOption().matches(options::OPT_O));
+
+ StringRef S(A->getValue());
+ if (S == "s" || S == "z")
+ return 2;
+
+ if (S == "g")
+ return 1;
+
+ DefaultOpt = getLastArgIntValue(Args, OPT_O, DefaultOpt, Diags);
+ }
+
+ unsigned MaxOptLevel = 3;
+ if (DefaultOpt > MaxOptLevel) {
+ // If the optimization level is not supported, fall back on the default
+ // optimization
+ Diags.Report(diag::warn_drv_optimization_value)
+ << Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel;
+ DefaultOpt = MaxOptLevel;
+ }
+
+ return DefaultOpt;
+}
+
+unsigned clang::getOptimizationLevelSize(ArgList &Args) {
+ if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+ if (A->getOption().matches(options::OPT_O)) {
+ switch (A->getValue()[0]) {
+ default:
+ return 0;
+ case 's':
+ return 1;
+ case 'z':
+ return 2;
+ }
+ }
+ }
+ return 0;
+}
+
/// Parse the argument to the -ftest-module-file-extension
/// command-line argument.
///
|
🐧 Linux x64 Test Results
|
Thibault-Monnier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/19239 Here is the relevant piece of the build log for the reference |
So that we can reuse these functions in few place, such as in clang/lib/Driver/ToolChains/CommonArgs.cpp. Part of the code there is currently copied from getOptimizationLevel.