diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index f6fde9f4aaf5..af6deb273f96 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -90,6 +90,25 @@ static bool parseShowColorsArgs(const llvm::opt::ArgList &args, llvm::sys::Process::StandardErrHasColors()); } +/// Extracts the optimisation level from \a args. +static unsigned getOptimizationLevel(llvm::opt::ArgList &args, + clang::DiagnosticsEngine &diags) { + unsigned defaultOpt = llvm::CodeGenOpt::None; + + if (llvm::opt::Arg *a = + args.getLastArg(clang::driver::options::OPT_O_Group)) { + if (a->getOption().matches(clang::driver::options::OPT_O0)) + return llvm::CodeGenOpt::None; + + assert(a->getOption().matches(clang::driver::options::OPT_O)); + + return getLastArgIntValue(args, clang::driver::options::OPT_O, defaultOpt, + diags); + } + + return defaultOpt; +} + bool Fortran::frontend::parseDiagnosticArgs(clang::DiagnosticOptions &opts, llvm::opt::ArgList &args) { opts.ShowColors = parseShowColorsArgs(args); @@ -100,9 +119,7 @@ bool Fortran::frontend::parseDiagnosticArgs(clang::DiagnosticOptions &opts, static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts, llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { - unsigned defaultOpt = llvm::CodeGenOpt::None; - opts.OptimizationLevel = clang::getLastArgIntValue( - args, clang::driver::options::OPT_O, defaultOpt, diags); + opts.OptimizationLevel = getOptimizationLevel(args, diags); if (args.hasFlag(clang::driver::options::OPT_fdebug_pass_manager, clang::driver::options::OPT_fno_debug_pass_manager, false)) diff --git a/flang/test/Driver/default-optimization-pipelines.f90 b/flang/test/Driver/default-optimization-pipelines.f90 index 3f2090411fa7..8a6ea57bdab3 100644 --- a/flang/test/Driver/default-optimization-pipelines.f90 +++ b/flang/test/Driver/default-optimization-pipelines.f90 @@ -6,6 +6,10 @@ ! RUN: %flang -S -O2 %s -Xflang -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O2 ! RUN: %flang_fc1 -S -O2 %s -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O2 +! Verify that only the left-most `-O{n}` is used +! RUN: %flang -S -O2 -O0 %s -Xflang -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O0 +! RUN: %flang_fc1 -S -O2 -O0 %s -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O0 + ! CHECK-O0-NOT: Running pass: SimplifyCFGPass on simple_loop_ ! CHECK-O0: Running analysis: TargetLibraryAnalysis on simple_loop_