[flang-commits] [flang] bbdf5c3 - [flang][driver] Fix opt-level option parsing

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Wed Jul 20 04:15:40 PDT 2022


Author: Andrzej Warzynski
Date: 2022-07-20T11:14:29Z
New Revision: bbdf5c37e38e172228963fed8ebc3c37d940ede1

URL: https://github.com/llvm/llvm-project/commit/bbdf5c37e38e172228963fed8ebc3c37d940ede1
DIFF: https://github.com/llvm/llvm-project/commit/bbdf5c37e38e172228963fed8ebc3c37d940ede1.diff

LOG: [flang][driver] Fix opt-level option parsing

This update makes sure that `flang-new -O2 -O0` will run at `-O0` rather
than `-O2`.

This bug was identified and originally fixed by Vyacheslav Zakharin in
https://reviews.llvm.org/D130035. I've extracted the fix into a separate
patch.

co-authored by: Vyacheslav Zakharin <vyacheslav.p.zakharin at intel.com>

Differential Revision: https://reviews.llvm.org/D130104

Added: 
    

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/test/Driver/default-optimization-pipelines.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f6fde9f4aaf56..af6deb273f966 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 3f2090411fa7c..8a6ea57bdab33 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_
 


        


More information about the flang-commits mailing list