Skip to content

Commit a7c08bc

Browse files
committed
[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
1 parent 10766b7 commit a7c08bc

File tree

8 files changed

+62
-1
lines changed

8 files changed

+62
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3254,7 +3254,7 @@ def miphonesimulator_version_min_EQ : Joined<["-"], "miphonesimulator-version-mi
32543254
def mkernel : Flag<["-"], "mkernel">, Group<m_Group>;
32553255
def mlinker_version_EQ : Joined<["-"], "mlinker-version=">,
32563256
Flags<[NoXarchOption]>;
3257-
def mllvm : Separate<["-"], "mllvm">, Flags<[CC1Option,CC1AsOption,CoreOption]>,
3257+
def mllvm : Separate<["-"], "mllvm">,Flags<[CC1Option,CC1AsOption,CoreOption,FC1Option,FlangOption]>,
32583258
HelpText<"Additional arguments to forward to LLVM's option processing">,
32593259
MarshallingInfoStringVector<FrontendOpts<"LLVMArgs">>;
32603260
def ffuchsia_api_level_EQ : Joined<["-"], "ffuchsia-api-level=">,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
106106
// Forward -Xflang arguments to -fc1
107107
Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
108108

109+
// Forward -mllvm options to the LLVM option parser. In practice, this means
110+
// forwarding to `-fc1` as that's where the LLVM parser is run.
111+
for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
112+
A->claim();
113+
A->render(Args, CmdArgs);
114+
}
115+
109116
if (Output.isFilename()) {
110117
CmdArgs.push_back("-o");
111118
CmdArgs.push_back(Output.getFilename());

flang/include/flang/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ struct FrontendOptions {
266266
/// The name of the action to run when using a plugin action.
267267
std::string ActionName;
268268

269+
/// A list of arguments to forward to LLVM's option processing; this
270+
/// should only be used for debugging and experimental features.
271+
std::vector<std::string> llvmArgs;
272+
269273
// Return the appropriate input kind for a file extension. For example,
270274
/// "*.f" would return Language::Fortran.
271275
///

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
584584
success &= parseSemaArgs(res, args, diags);
585585
success &= parseDialectArgs(res, args, diags);
586586
success &= parseDiagArgs(res, args, diags);
587+
res.frontendOpts_.llvmArgs =
588+
args.getAllArgValues(clang::driver::options::OPT_mllvm);
587589

588590
return success;
589591
}

flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ bool ExecuteCompilerInvocation(CompilerInstance *flang) {
135135
}
136136
}
137137

138+
// Honor -mllvm. This should happen AFTER plugins have been loaded!
139+
if (!flang->frontendOpts().llvmArgs.empty()) {
140+
unsigned numArgs = flang->frontendOpts().llvmArgs.size();
141+
auto args = std::make_unique<const char *[]>(numArgs + 2);
142+
args[0] = "flang (LLVM option parsing)";
143+
144+
for (unsigned i = 0; i != numArgs; ++i)
145+
args[i + 1] = flang->frontendOpts().llvmArgs[i].c_str();
146+
147+
args[numArgs + 1] = nullptr;
148+
llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
149+
}
150+
138151
// If there were errors in processing arguments, don't do anything else.
139152
if (flang->diagnostics().hasErrorOccurred()) {
140153
return false;

flang/test/Driver/driver-help-hidden.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
4848
! CHECK-NEXT: -help Display available options
4949
! CHECK-NEXT: -I <dir> Add directory to the end of the list of include search paths
50+
! CHECK-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
5051
! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir>
5152
! CHECK-NEXT: -nocpp Disable predefined and command line preprocessor macros
5253
! CHECK-NEXT: -o <file> Write output to <file>

flang/test/Driver/driver-help.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
4848
! HELP-NEXT: -help Display available options
4949
! HELP-NEXT: -I <dir> Add directory to the end of the list of include search paths
50+
! HELP-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
5051
! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir>
5152
! HELP-NEXT: -nocpp Disable predefined and command line preprocessor macros
5253
! HELP-NEXT: -o <file> Write output to <file>
@@ -121,6 +122,7 @@
121122
! HELP-FC1-NEXT: -init-only Only execute frontend initialization
122123
! HELP-FC1-NEXT: -I <dir> Add directory to the end of the list of include search paths
123124
! HELP-FC1-NEXT: -load <dsopath> Load the named plugin (dynamic shared object)
125+
! HELP-FC1-NEXT: -mllvm <value> Additional arguments to forward to LLVM's option processing
124126
! HELP-FC1-NEXT: -module-dir <dir> Put MODULE files in <dir>
125127
! HELP-FC1-NEXT: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
126128
! HELP-FC1-NEXT: -nocpp Disable predefined and command line preprocessor macros

flang/test/Driver/mllvm.f90

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
! Test the `-mllvm` option
2+
3+
!------------
4+
! RUN COMMAND
5+
!------------
6+
! 1. Test typical usage.
7+
! RUN: %flang -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT
8+
! RUN: %flang_fc1 -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OUTPUT
9+
10+
! 2. Does the option forwarding from `flang-new` to `flang-new -fc1` work?
11+
! RUN: %flang -### -S -mllvm -print-before-all %s -o - 2>&1 | FileCheck %s --check-prefix=OPTION_FORWARDING
12+
13+
! 3. Test invalid usage (`-print-before` requires an argument)
14+
! RUN: not %flang -S -mllvm -print-before %s -o - 2>&1 | FileCheck %s --check-prefix=INVALID_USAGE
15+
16+
!----------------
17+
! EXPECTED OUTPUT
18+
!----------------
19+
! OUTPUT: *** IR Dump Before Pre-ISel Intrinsic Lowering (pre-isel-intrinsic-lowering) ***
20+
! OUTPUT-NEXT: ; ModuleID = 'FIRModule'
21+
! OUTPUT-NEXT: source_filename = "FIRModule"
22+
23+
! Verify that `-mllvm <option>` is forwarded to flang -fc1
24+
! OPTION_FORWARDING: flang-new" "-fc1"
25+
! OPTION_FORWARDING-SAME: "-mllvm" "-print-before-all"
26+
27+
! INVALID_USAGE: flang (LLVM option parsing): for the --print-before option: requires a value!
28+
29+
!------
30+
! INPUT
31+
!------
32+
end program

0 commit comments

Comments
 (0)