[flang-commits] [flang] a7c08bc - [flang][driver] Add support for `-mllvm`

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Wed Mar 16 03:41:13 PDT 2022


Author: Andrzej Warzynski
Date: 2022-03-16T10:41:04Z
New Revision: a7c08bcf777ee0712c140052d0814f55a02dd27b

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

LOG: [flang][driver] Add support for `-mllvm`

This option is added in both `flang-new` (the compiler driver) and
`flang-new -fc1` (the frontend driver). The semantics are consistent
with `clang` and `clang -cc1`.

As Flang does not run any LLVM passes when invoked with `-emit-llvm`
(i.e. `flang-new -S -emit-llvm <file>`), the tests use
`-S`/`-c`/`-emit-obj` instead. These options require an LLVM backend to
be run by the driver to generate the output (this makese `-mllvm`
relevant here).

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

Added: 
    flang/test/Driver/mllvm.f90

Modified: 
    clang/include/clang/Driver/Options.td
    clang/lib/Driver/ToolChains/Flang.cpp
    flang/include/flang/Frontend/FrontendOptions.h
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
    flang/test/Driver/driver-help-hidden.f90
    flang/test/Driver/driver-help.f90

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index fa54d4a7689cd..2a23695c149fa 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3254,7 +3254,7 @@ def miphonesimulator_version_min_EQ : Joined<["-"], "miphonesimulator-version-mi
 def mkernel : Flag<["-"], "mkernel">, Group<m_Group>;
 def mlinker_version_EQ : Joined<["-"], "mlinker-version=">,
   Flags<[NoXarchOption]>;
-def mllvm : Separate<["-"], "mllvm">, Flags<[CC1Option,CC1AsOption,CoreOption]>,
+def mllvm : Separate<["-"], "mllvm">,Flags<[CC1Option,CC1AsOption,CoreOption,FC1Option,FlangOption]>,
   HelpText<"Additional arguments to forward to LLVM's option processing">,
   MarshallingInfoStringVector<FrontendOpts<"LLVMArgs">>;
 def ffuchsia_api_level_EQ : Joined<["-"], "ffuchsia-api-level=">,

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index c77aa68257a67..283b5ca80b28c 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -106,6 +106,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
   // Forward -Xflang arguments to -fc1
   Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
 
+  // Forward -mllvm options to the LLVM option parser. In practice, this means
+  // forwarding to `-fc1` as that's where the LLVM parser is run.
+  for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
+    A->claim();
+    A->render(Args, CmdArgs);
+  }
+
   if (Output.isFilename()) {
     CmdArgs.push_back("-o");
     CmdArgs.push_back(Output.getFilename());

diff  --git a/flang/include/flang/Frontend/FrontendOptions.h b/flang/include/flang/Frontend/FrontendOptions.h
index b299c87ef15f1..5d97580af5a56 100644
--- a/flang/include/flang/Frontend/FrontendOptions.h
+++ b/flang/include/flang/Frontend/FrontendOptions.h
@@ -266,6 +266,10 @@ struct FrontendOptions {
   /// The name of the action to run when using a plugin action.
   std::string ActionName;
 
+  /// A list of arguments to forward to LLVM's option processing; this
+  /// should only be used for debugging and experimental features.
+  std::vector<std::string> llvmArgs;
+
   // Return the appropriate input kind for a file extension. For example,
   /// "*.f" would return Language::Fortran.
   ///

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d1b0d0e532cd9..cf92e71c755d2 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -584,6 +584,8 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
   success &= parseSemaArgs(res, args, diags);
   success &= parseDialectArgs(res, args, diags);
   success &= parseDiagArgs(res, args, diags);
+  res.frontendOpts_.llvmArgs =
+      args.getAllArgValues(clang::driver::options::OPT_mllvm);
 
   return success;
 }

diff  --git a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 4e8c22550c406..0f91d6e1a44b0 100644
--- a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -135,6 +135,19 @@ bool ExecuteCompilerInvocation(CompilerInstance *flang) {
     }
   }
 
+  // Honor -mllvm. This should happen AFTER plugins have been loaded!
+  if (!flang->frontendOpts().llvmArgs.empty()) {
+    unsigned numArgs = flang->frontendOpts().llvmArgs.size();
+    auto args = std::make_unique<const char *[]>(numArgs + 2);
+    args[0] = "flang (LLVM option parsing)";
+
+    for (unsigned i = 0; i != numArgs; ++i)
+      args[i + 1] = flang->frontendOpts().llvmArgs[i].c_str();
+
+    args[numArgs + 1] = nullptr;
+    llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
+  }
+
   // If there were errors in processing arguments, don't do anything else.
   if (flang->diagnostics().hasErrorOccurred()) {
     return false;

diff  --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 150900a281e85..48b5a93700d70 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -47,6 +47,7 @@
 ! CHECK-NEXT: -fxor-operator         Enable .XOR. as a synonym of .NEQV.
 ! CHECK-NEXT: -help     Display available options
 ! CHECK-NEXT: -I <dir>               Add directory to the end of the list of include search paths
+! CHECK-NEXT: -mllvm <value>         Additional arguments to forward to LLVM's option processing
 ! CHECK-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! CHECK-NEXT: -nocpp                 Disable predefined and command line preprocessor macros
 ! CHECK-NEXT: -o <file> Write output to <file>

diff  --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index fd192203e4f35..16cc6c428082b 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -47,6 +47,7 @@
 ! HELP-NEXT: -fxor-operator         Enable .XOR. as a synonym of .NEQV.
 ! HELP-NEXT: -help                  Display available options
 ! HELP-NEXT: -I <dir>               Add directory to the end of the list of include search paths
+! HELP-NEXT: -mllvm <value>         Additional arguments to forward to LLVM's option processing
 ! HELP-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! HELP-NEXT: -nocpp                 Disable predefined and command line preprocessor macros
 ! HELP-NEXT: -o <file>              Write output to <file>
@@ -121,6 +122,7 @@
 ! HELP-FC1-NEXT: -init-only             Only execute frontend initialization
 ! HELP-FC1-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! HELP-FC1-NEXT: -load <dsopath>        Load the named plugin (dynamic shared object)
+! HELP-FC1-NEXT: -mllvm <value>         Additional arguments to forward to LLVM's option processing
 ! HELP-FC1-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! HELP-FC1-NEXT: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
 ! HELP-FC1-NEXT: -nocpp                 Disable predefined and command line preprocessor macros

diff  --git a/flang/test/Driver/mllvm.f90 b/flang/test/Driver/mllvm.f90
new file mode 100644
index 0000000000000..affed5e1b884a
--- /dev/null
+++ b/flang/test/Driver/mllvm.f90
@@ -0,0 +1,32 @@
+! Test the `-mllvm` option
+
+!------------
+! RUN COMMAND
+!------------
+! 1. Test typical usage.
+! RUN: %flang -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT
+! RUN: %flang_fc1 -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT
+
+! 2. Does the option forwarding from `flang-new` to `flang-new -fc1` work?
+! RUN: %flang -### -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OPTION_FORWARDING
+
+! 3. Test invalid usage (`-print-before` requires an argument)
+! RUN: not %flang -S -mllvm -print-before %s -o - 2>&1 | FileCheck %s --check-prefix=INVALID_USAGE
+
+!----------------
+! EXPECTED OUTPUT
+!----------------
+! OUTPUT: *** IR Dump Before Pre-ISel Intrinsic Lowering (pre-isel-intrinsic-lowering) ***
+! OUTPUT-NEXT: ; ModuleID = 'FIRModule'
+! OUTPUT-NEXT: source_filename = "FIRModule"
+
+! Verify that `-mllvm <option>` is forwarded to flang -fc1
+! OPTION_FORWARDING: flang-new" "-fc1"
+! OPTION_FORWARDING-SAME: "-mllvm" "-print-before-all"
+
+! INVALID_USAGE: flang (LLVM option parsing): for the --print-before option: requires a value!
+
+!------
+! INPUT
+!------
+end program


        


More information about the flang-commits mailing list