Skip to content

Commit

Permalink
[flang][driver] Add support for -mllvm
Browse files Browse the repository at this point in the history
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
  • Loading branch information
banach-space committed Mar 16, 2022
1 parent 10766b7 commit a7c08bc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Expand Up @@ -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=">,
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Frontend/FrontendOptions.h
Expand Up @@ -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.
///
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions flang/test/Driver/driver-help-hidden.f90
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Driver/driver-help.f90
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions 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

0 comments on commit a7c08bc

Please sign in to comment.