[flang][driver] Add support for `-mmlir`

The semantics of `-mmlir` are identical to `-mllvm`. The only notable
difference is that `-mmlir` options should be forwarded to MLIR rather
than LLVM.

Note that MLIR llvm::cl options are lazily constructed on demand (see
the definition of options in PassManagerOptions.cpp). This means that:
  * MLIR global options are only visible when explicitly initialised and
    displayed only when using `-mmlir --help`,
  * Flang and LLVM global options are always visible and displayed when
    using either `-mllvm -help` or `-mmlir --help`.

In other words, `-mmlir --help` is a superset of `-mllvm --help`. This is not
ideal, but we'd need to refactor all option definitions in Flang and
LLVM to improve this. I suggesting leaving this for later.

Differential Revision: https://reviews.llvm.org/D123297
This commit is contained in:
Andrzej Warzynski 2022-04-07 09:47:23 +00:00
parent 04a3f3f167
commit 6c93e1d329
10 changed files with 55 additions and 0 deletions

View File

@ -3264,6 +3264,8 @@ def mlinker_version_EQ : Joined<["-"], "mlinker-version=">,
def mllvm : Separate<["-"], "mllvm">,Flags<[CC1Option,CC1AsOption,CoreOption,FC1Option,FlangOption]>, def mllvm : Separate<["-"], "mllvm">,Flags<[CC1Option,CC1AsOption,CoreOption,FC1Option,FlangOption]>,
HelpText<"Additional arguments to forward to LLVM's option processing">, HelpText<"Additional arguments to forward to LLVM's option processing">,
MarshallingInfoStringVector<FrontendOpts<"LLVMArgs">>; MarshallingInfoStringVector<FrontendOpts<"LLVMArgs">>;
def mmlir : Separate<["-"], "mmlir">, Flags<[CoreOption,FC1Option,FlangOption]>,
HelpText<"Additional arguments to forward to MLIR's option processing">;
def ffuchsia_api_level_EQ : Joined<["-"], "ffuchsia-api-level=">, def ffuchsia_api_level_EQ : Joined<["-"], "ffuchsia-api-level=">,
Group<m_Group>, Flags<[CC1Option]>, HelpText<"Set Fuchsia API level">, Group<m_Group>, Flags<[CC1Option]>, HelpText<"Set Fuchsia API level">,
MarshallingInfoInt<LangOpts<"FuchsiaAPILevel">>; MarshallingInfoInt<LangOpts<"FuchsiaAPILevel">>;

View File

@ -113,6 +113,11 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
A->render(Args, CmdArgs); A->render(Args, CmdArgs);
} }
for (const Arg *A : Args.filtered(options::OPT_mmlir)) {
A->claim();
A->render(Args, CmdArgs);
}
if (Output.isFilename()) { if (Output.isFilename()) {
CmdArgs.push_back("-o"); CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename()); CmdArgs.push_back(Output.getFilename());

View File

@ -273,6 +273,10 @@ struct FrontendOptions {
/// should only be used for debugging and experimental features. /// should only be used for debugging and experimental features.
std::vector<std::string> llvmArgs; std::vector<std::string> llvmArgs;
/// A list of arguments to forward to MLIR's option processing; this
/// should only be used for debugging and experimental features.
std::vector<std::string> mlirArgs;
// Return the appropriate input kind for a file extension. For example, // Return the appropriate input kind for a file extension. For example,
/// "*.f" would return Language::Fortran. /// "*.f" would return Language::Fortran.
/// ///

View File

@ -597,6 +597,9 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
res.frontendOpts_.llvmArgs = res.frontendOpts_.llvmArgs =
args.getAllArgValues(clang::driver::options::OPT_mllvm); args.getAllArgValues(clang::driver::options::OPT_mllvm);
res.frontendOpts_.mlirArgs =
args.getAllArgValues(clang::driver::options::OPT_mmlir);
return success; return success;
} }

View File

@ -427,6 +427,7 @@ void CodeGenAction::GenerateLLVMIR() {
// Create the pass pipeline // Create the pass pipeline
fir::createMLIRToLLVMPassPipeline(pm); fir::createMLIRToLLVMPassPipeline(pm);
mlir::applyPassManagerCLOptions(pm);
// Run the pass manager // Run the pass manager
if (!mlir::succeeded(pm.run(*mlirModule))) { if (!mlir::succeeded(pm.run(*mlirModule))) {

View File

@ -11,6 +11,7 @@ add_flang_library(flangFrontendTool
flangFrontend flangFrontend
clangBasic clangBasic
clangDriver clangDriver
MLIRPass
LINK_COMPONENTS LINK_COMPONENTS
Option Option

View File

@ -19,6 +19,8 @@
#include "llvm/Option/Option.h" #include "llvm/Option/Option.h"
#include "llvm/Support/BuryPointer.h" #include "llvm/Support/BuryPointer.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Pass/PassManager.h"
namespace Fortran::frontend { namespace Fortran::frontend {
@ -150,6 +152,21 @@ bool ExecuteCompilerInvocation(CompilerInstance *flang) {
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get()); llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
} }
// Honor -mmlir. This should happen AFTER plugins have been loaded!
if (!flang->frontendOpts().mlirArgs.empty()) {
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
unsigned numArgs = flang->frontendOpts().mlirArgs.size();
auto args = std::make_unique<const char *[]>(numArgs + 2);
args[0] = "flang (MLIR option parsing)";
for (unsigned i = 0; i != numArgs; ++i)
args[i + 1] = flang->frontendOpts().mlirArgs[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 there were errors in processing arguments, don't do anything else.
if (flang->diagnostics().hasErrorOccurred()) { if (flang->diagnostics().hasErrorOccurred()) {
return false; return false;

View File

@ -48,6 +48,7 @@
! CHECK-NEXT: -help Display available options ! CHECK-NEXT: -help Display available options
! CHECK-NEXT: -I <dir> Add directory to the end of the list of include search paths ! 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: -mllvm <value> Additional arguments to forward to LLVM's option processing
! CHECK-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir> ! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir>
! CHECK-NEXT: -nocpp Disable predefined and command line preprocessor macros ! CHECK-NEXT: -nocpp Disable predefined and command line preprocessor macros
! CHECK-NEXT: -o <file> Write output to <file> ! CHECK-NEXT: -o <file> Write output to <file>

View File

@ -48,6 +48,7 @@
! HELP-NEXT: -help Display available options ! HELP-NEXT: -help Display available options
! HELP-NEXT: -I <dir> Add directory to the end of the list of include search paths ! 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: -mllvm <value> Additional arguments to forward to LLVM's option processing
! HELP-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir> ! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir>
! HELP-NEXT: -nocpp Disable predefined and command line preprocessor macros ! HELP-NEXT: -nocpp Disable predefined and command line preprocessor macros
! HELP-NEXT: -o <file> Write output to <file> ! HELP-NEXT: -o <file> Write output to <file>
@ -124,6 +125,7 @@
! HELP-FC1-NEXT: -I <dir> Add directory to the end of the list of include search paths ! 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: -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: -mllvm <value> Additional arguments to forward to LLVM's option processing
! HELP-FC1-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
! HELP-FC1-NEXT: -module-dir <dir> Put MODULE files in <dir> ! 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: -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 ! HELP-FC1-NEXT: -nocpp Disable predefined and command line preprocessor macros

View File

@ -0,0 +1,19 @@
! Verify that `-mllvm` options are forwarded to LLVM and `-mmlir` to MLIR.
! In practice, '-mmlir --help' is a super-set of '-mllvm --help' and that limits what we can test here. With a better seperation of
! LLVM, MLIR and Flang global options, we should be able to write a stricter test.
!------------
! RUN COMMAND
!------------
! RUN: %flang_fc1 -mmlir --help | FileCheck %s --check-prefix=MLIR
! RUN: %flang_fc1 -mllvm --help | FileCheck %s --check-prefix=MLLVM
!----------------
! EXPECTED OUTPUT
!----------------
! MLIR: flang (MLIR option parsing) [options]
! MLIR: --mlir-{{.*}}
! MLLVM: flang (LLVM option parsing) [options]
! MLLVM-NOT: --mlir-{{.*}}