Skip to content

Commit 8439aeb

Browse files
authored
[Clang] Refactor getOptimizationLevel and getOptimizationLevelSize to non-static. NFC. (#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.
1 parent 3954df9 commit 8439aeb

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
6767
DiagnosticsEngine *Diags = nullptr,
6868
bool DefaultDiagColor = true);
6969

70+
unsigned getOptimizationLevel(llvm::opt::ArgList &Args, InputKind IK,
71+
DiagnosticsEngine &Diags);
72+
73+
unsigned getOptimizationLevelSize(llvm::opt::ArgList &Args);
74+
7075
/// The base class of CompilerInvocation. It keeps individual option objects
7176
/// behind reference-counted pointers, which is useful for clients that want to
7277
/// keep select option objects alive (even after CompilerInvocation gets

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -703,52 +703,6 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
703703
// Deserialization (from args)
704704
//===----------------------------------------------------------------------===//
705705

706-
static unsigned getOptimizationLevel(ArgList &Args, InputKind IK,
707-
DiagnosticsEngine &Diags) {
708-
unsigned DefaultOpt = 0;
709-
if ((IK.getLanguage() == Language::OpenCL ||
710-
IK.getLanguage() == Language::OpenCLCXX) &&
711-
!Args.hasArg(OPT_cl_opt_disable))
712-
DefaultOpt = 2;
713-
714-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
715-
if (A->getOption().matches(options::OPT_O0))
716-
return 0;
717-
718-
if (A->getOption().matches(options::OPT_Ofast))
719-
return 3;
720-
721-
assert(A->getOption().matches(options::OPT_O));
722-
723-
StringRef S(A->getValue());
724-
if (S == "s" || S == "z")
725-
return 2;
726-
727-
if (S == "g")
728-
return 1;
729-
730-
return getLastArgIntValue(Args, OPT_O, DefaultOpt, Diags);
731-
}
732-
733-
return DefaultOpt;
734-
}
735-
736-
static unsigned getOptimizationLevelSize(ArgList &Args) {
737-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
738-
if (A->getOption().matches(options::OPT_O)) {
739-
switch (A->getValue()[0]) {
740-
default:
741-
return 0;
742-
case 's':
743-
return 1;
744-
case 'z':
745-
return 2;
746-
}
747-
}
748-
}
749-
return 0;
750-
}
751-
752706
static void GenerateArg(ArgumentConsumer Consumer,
753707
llvm::opt::OptSpecifier OptSpecifier) {
754708
Option Opt = getDriverOptTable().getOption(OptSpecifier);
@@ -1859,17 +1813,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
18591813
const LangOptions &LangOptsRef) {
18601814
unsigned NumErrorsBefore = Diags.getNumErrors();
18611815

1862-
unsigned OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
1863-
// TODO: This could be done in Driver
1864-
unsigned MaxOptLevel = 3;
1865-
if (OptimizationLevel > MaxOptLevel) {
1866-
// If the optimization level is not supported, fall back on the default
1867-
// optimization
1868-
Diags.Report(diag::warn_drv_optimization_value)
1869-
<< Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel;
1870-
OptimizationLevel = MaxOptLevel;
1871-
}
1872-
Opts.OptimizationLevel = OptimizationLevel;
1816+
Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
18731817

18741818
// The key paths of codegen options defined in Options.td start with
18751819
// "CodeGenOpts.". Let's provide the expected variable name and type.
@@ -2728,6 +2672,61 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
27282672
return Diags->getNumErrors() == NumErrorsBefore;
27292673
}
27302674

2675+
unsigned clang::getOptimizationLevel(ArgList &Args, InputKind IK,
2676+
DiagnosticsEngine &Diags) {
2677+
unsigned DefaultOpt = 0;
2678+
if ((IK.getLanguage() == Language::OpenCL ||
2679+
IK.getLanguage() == Language::OpenCLCXX) &&
2680+
!Args.hasArg(OPT_cl_opt_disable))
2681+
DefaultOpt = 2;
2682+
2683+
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
2684+
if (A->getOption().matches(options::OPT_O0))
2685+
return 0;
2686+
2687+
if (A->getOption().matches(options::OPT_Ofast))
2688+
return 3;
2689+
2690+
assert(A->getOption().matches(options::OPT_O));
2691+
2692+
StringRef S(A->getValue());
2693+
if (S == "s" || S == "z")
2694+
return 2;
2695+
2696+
if (S == "g")
2697+
return 1;
2698+
2699+
DefaultOpt = getLastArgIntValue(Args, OPT_O, DefaultOpt, Diags);
2700+
}
2701+
2702+
unsigned MaxOptLevel = 3;
2703+
if (DefaultOpt > MaxOptLevel) {
2704+
// If the optimization level is not supported, fall back on the default
2705+
// optimization
2706+
Diags.Report(diag::warn_drv_optimization_value)
2707+
<< Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel;
2708+
DefaultOpt = MaxOptLevel;
2709+
}
2710+
2711+
return DefaultOpt;
2712+
}
2713+
2714+
unsigned clang::getOptimizationLevelSize(ArgList &Args) {
2715+
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
2716+
if (A->getOption().matches(options::OPT_O)) {
2717+
switch (A->getValue()[0]) {
2718+
default:
2719+
return 0;
2720+
case 's':
2721+
return 1;
2722+
case 'z':
2723+
return 2;
2724+
}
2725+
}
2726+
}
2727+
return 0;
2728+
}
2729+
27312730
/// Parse the argument to the -ftest-module-file-extension
27322731
/// command-line argument.
27332732
///

0 commit comments

Comments
 (0)