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.
Member
|
@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
|
aeubanks
approved these changes
Nov 20, 2025
|
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 |
aadeshps-mcw
pushed a commit
to aadeshps-mcw/llvm-project
that referenced
this pull request
Nov 26, 2025
… non-static. NFC. (llvm#168839) 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.
Priyanshu3820
pushed a commit
to Priyanshu3820/llvm-project
that referenced
this pull request
Nov 26, 2025
… non-static. NFC. (llvm#168839) 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.
augusto2112
pushed a commit
to augusto2112/llvm-project
that referenced
this pull request
Dec 3, 2025
… non-static. NFC. (llvm#168839) 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.