https://github.com/tclin914 created https://github.com/llvm/llvm-project/pull/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. >From d2c2ad0c5a3fd2975d74d7c6d2e44dc57ba276bc Mon Sep 17 00:00:00 2001 From: Jim Lin <[email protected]> Date: Wed, 19 Nov 2025 16:34:23 +0800 Subject: [PATCH] [Clang] Refactor getOptimizationLevel and getOptimizationLevelSize to 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. --- .../clang/Frontend/CompilerInvocation.h | 5 + clang/lib/Frontend/CompilerInvocation.cpp | 113 +++++++++--------- 2 files changed, 61 insertions(+), 57 deletions(-) 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. /// _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
