Skip to content

Commit b3533a1

Browse files
authored
[clang][flang][mlir] Support -frecord-command-line option (#102975)
Add support for the -frecord-command-line option that will produce the llvm.commandline metadata which will eventually be saved in the object file. This behavior is also supported in clang. Some refactoring of the code in flang to handle these command line options was carried out. The corresponding -grecord-command-line option which saves the command line in the debug information has not yet been enabled for flang.
1 parent 594efd2 commit b3533a1

File tree

23 files changed

+245
-59
lines changed

23 files changed

+245
-59
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,16 +1994,18 @@ def fparse_all_comments : Flag<["-"], "fparse-all-comments">, Group<f_clang_Grou
19941994
Visibility<[ClangOption, CC1Option]>,
19951995
MarshallingInfoFlag<LangOpts<"CommentOpts.ParseAllComments">>;
19961996
def frecord_command_line : Flag<["-"], "frecord-command-line">,
1997-
DocBrief<[{Generate a section named ".GCC.command.line" containing the clang
1997+
DocBrief<[{Generate a section named ".GCC.command.line" containing the
19981998
driver command-line. After linking, the section may contain multiple command
19991999
lines, which will be individually terminated by null bytes. Separate arguments
20002000
within a command line are combined with spaces; spaces and backslashes within an
20012001
argument are escaped with backslashes. This format differs from the format of
20022002
the equivalent section produced by GCC with the -frecord-gcc-switches flag.
20032003
This option is currently only supported on ELF targets.}]>,
2004-
Group<f_clang_Group>;
2004+
Group<f_Group>,
2005+
Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>;
20052006
def fno_record_command_line : Flag<["-"], "fno-record-command-line">,
2006-
Group<f_clang_Group>;
2007+
Group<f_Group>,
2008+
Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>;
20072009
def : Flag<["-"], "frecord-gcc-switches">, Alias<frecord_command_line>;
20082010
def : Flag<["-"], "fno-record-gcc-switches">, Alias<fno_record_command_line>;
20092011
def fcommon : Flag<["-"], "fcommon">, Group<f_Group>,
@@ -7141,6 +7143,9 @@ def mrelocation_model : Separate<["-"], "mrelocation-model">,
71417143
NormalizedValues<["Static", "PIC_", "ROPI", "RWPI", "ROPI_RWPI", "DynamicNoPIC"]>,
71427144
MarshallingInfoEnum<CodeGenOpts<"RelocationModel">, "PIC_">;
71437145
def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">;
7146+
def record_command_line : Separate<["-"], "record-command-line">,
7147+
HelpText<"The string to embed in the .LLVM.command.line section.">,
7148+
MarshallingInfoString<CodeGenOpts<"RecordCommandLine">>;
71447149

71457150
} // let Visibility = [CC1Option, CC1AsOption, FC1Option]
71467151

@@ -7161,9 +7166,6 @@ def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">,
71617166
def dwarf_debug_flags : Separate<["-"], "dwarf-debug-flags">,
71627167
HelpText<"The string to embed in the Dwarf debug flags record.">,
71637168
MarshallingInfoString<CodeGenOpts<"DwarfDebugFlags">>;
7164-
def record_command_line : Separate<["-"], "record-command-line">,
7165-
HelpText<"The string to embed in the .LLVM.command.line section.">,
7166-
MarshallingInfoString<CodeGenOpts<"RecordCommandLine">>;
71677169
def compress_debug_sections_EQ : Joined<["-", "--"], "compress-debug-sections=">,
71687170
HelpText<"DWARF debug sections compression type">, Values<"none,zlib,zstd">,
71697171
NormalizedValuesScope<"llvm::DebugCompressionType">, NormalizedValues<["None", "Zlib", "Zstd"]>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,6 @@ static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
9494
<< "-static";
9595
}
9696

97-
// Add backslashes to escape spaces and other backslashes.
98-
// This is used for the space-separated argument list specified with
99-
// the -dwarf-debug-flags option.
100-
static void EscapeSpacesAndBackslashes(const char *Arg,
101-
SmallVectorImpl<char> &Res) {
102-
for (; *Arg; ++Arg) {
103-
switch (*Arg) {
104-
default:
105-
break;
106-
case ' ':
107-
case '\\':
108-
Res.push_back('\\');
109-
break;
110-
}
111-
Res.push_back(*Arg);
112-
}
113-
}
114-
11597
/// Apply \a Work on the current tool chain \a RegularToolChain and any other
11698
/// offloading tool chain that is associated with the current action \a JA.
11799
static void
@@ -7702,31 +7684,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
77027684
// Also record command line arguments into the debug info if
77037685
// -grecord-gcc-switches options is set on.
77047686
// By default, -gno-record-gcc-switches is set on and no recording.
7705-
auto GRecordSwitches =
7706-
Args.hasFlag(options::OPT_grecord_command_line,
7707-
options::OPT_gno_record_command_line, false);
7708-
auto FRecordSwitches =
7709-
Args.hasFlag(options::OPT_frecord_command_line,
7710-
options::OPT_fno_record_command_line, false);
7711-
if (FRecordSwitches && !Triple.isOSBinFormatELF() &&
7712-
!Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO())
7713-
D.Diag(diag::err_drv_unsupported_opt_for_target)
7714-
<< Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
7715-
<< TripleStr;
7716-
if (TC.UseDwarfDebugFlags() || GRecordSwitches || FRecordSwitches) {
7717-
ArgStringList OriginalArgs;
7718-
for (const auto &Arg : Args)
7719-
Arg->render(Args, OriginalArgs);
7720-
7721-
SmallString<256> Flags;
7722-
EscapeSpacesAndBackslashes(Exec, Flags);
7723-
for (const char *OriginalArg : OriginalArgs) {
7724-
SmallString<128> EscapedArg;
7725-
EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
7726-
Flags += " ";
7727-
Flags += EscapedArg;
7728-
}
7729-
auto FlagsArgString = Args.MakeArgString(Flags);
7687+
auto GRecordSwitches = false;
7688+
auto FRecordSwitches = false;
7689+
if (shouldRecordCommandLine(TC, Args, FRecordSwitches, GRecordSwitches)) {
7690+
auto FlagsArgString = renderEscapedCommandLine(TC, Args);
77307691
if (TC.UseDwarfDebugFlags() || GRecordSwitches) {
77317692
CmdArgs.push_back("-dwarf-debug-flags");
77327693
CmdArgs.push_back(FlagsArgString);
@@ -8726,10 +8687,10 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
87268687

87278688
SmallString<256> Flags;
87288689
const char *Exec = getToolChain().getDriver().getClangProgramPath();
8729-
EscapeSpacesAndBackslashes(Exec, Flags);
8690+
escapeSpacesAndBackslashes(Exec, Flags);
87308691
for (const char *OriginalArg : OriginalArgs) {
87318692
SmallString<128> EscapedArg;
8732-
EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
8693+
escapeSpacesAndBackslashes(OriginalArg, EscapedArg);
87338694
Flags += " ";
87348695
Flags += EscapedArg;
87358696
}

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,3 +2960,62 @@ void tools::addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
29602960
}
29612961
}
29622962
}
2963+
2964+
void tools::escapeSpacesAndBackslashes(const char *Arg,
2965+
llvm::SmallVectorImpl<char> &Res) {
2966+
for (; *Arg; ++Arg) {
2967+
switch (*Arg) {
2968+
default:
2969+
break;
2970+
case ' ':
2971+
case '\\':
2972+
Res.push_back('\\');
2973+
break;
2974+
}
2975+
Res.push_back(*Arg);
2976+
}
2977+
}
2978+
2979+
const char *tools::renderEscapedCommandLine(const ToolChain &TC,
2980+
const llvm::opt::ArgList &Args) {
2981+
const Driver &D = TC.getDriver();
2982+
const char *Exec = D.getClangProgramPath();
2983+
2984+
llvm::opt::ArgStringList OriginalArgs;
2985+
for (const auto &Arg : Args)
2986+
Arg->render(Args, OriginalArgs);
2987+
2988+
llvm::SmallString<256> Flags;
2989+
escapeSpacesAndBackslashes(Exec, Flags);
2990+
for (const char *OriginalArg : OriginalArgs) {
2991+
llvm::SmallString<128> EscapedArg;
2992+
escapeSpacesAndBackslashes(OriginalArg, EscapedArg);
2993+
Flags += " ";
2994+
Flags += EscapedArg;
2995+
}
2996+
2997+
return Args.MakeArgString(Flags);
2998+
}
2999+
3000+
bool tools::shouldRecordCommandLine(const ToolChain &TC,
3001+
const llvm::opt::ArgList &Args,
3002+
bool &FRecordCommandLine,
3003+
bool &GRecordCommandLine) {
3004+
const Driver &D = TC.getDriver();
3005+
const llvm::Triple &Triple = TC.getEffectiveTriple();
3006+
const std::string &TripleStr = Triple.getTriple();
3007+
3008+
FRecordCommandLine =
3009+
Args.hasFlag(options::OPT_frecord_command_line,
3010+
options::OPT_fno_record_command_line, false);
3011+
GRecordCommandLine =
3012+
Args.hasFlag(options::OPT_grecord_command_line,
3013+
options::OPT_gno_record_command_line, false);
3014+
if (FRecordCommandLine && !Triple.isOSBinFormatELF() &&
3015+
!Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO())
3016+
D.Diag(diag::err_drv_unsupported_opt_for_target)
3017+
<< Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
3018+
<< TripleStr;
3019+
3020+
return FRecordCommandLine || TC.UseDwarfDebugFlags() || GRecordCommandLine;
3021+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,31 @@ void addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
233233
const llvm::Reloc::Model &RelocationModel,
234234
llvm::opt::ArgStringList &CmdArgs);
235235

236+
/// Add backslashes to escape spaces and other backslashes.
237+
/// This is used for the space-separated argument list specified with
238+
/// the -dwarf-debug-flags option.
239+
void escapeSpacesAndBackslashes(const char *Arg,
240+
llvm::SmallVectorImpl<char> &Res);
241+
242+
/// Join the args in the given ArgList, escape spaces and backslashes and
243+
/// return the joined string. This is used when saving the command line as a
244+
/// result of using either the -frecord-command-line or -grecord-command-line
245+
/// options. The lifetime of the returned c-string will match that of the Args
246+
/// argument.
247+
const char *renderEscapedCommandLine(const ToolChain &TC,
248+
const llvm::opt::ArgList &Args);
249+
250+
/// Check if the command line should be recorded in the object file. This is
251+
/// done if either -frecord-command-line or -grecord-command-line options have
252+
/// been passed. This also does some error checking since -frecord-command-line
253+
/// is currently only supported on ELF platforms. The last two boolean
254+
/// arguments are out parameters and will be set depending on the command
255+
/// line options that were passed.
256+
bool shouldRecordCommandLine(const ToolChain &TC,
257+
const llvm::opt::ArgList &Args,
258+
bool &FRecordCommandLine,
259+
bool &GRecordCommandLine);
260+
236261
} // end namespace tools
237262
} // end namespace driver
238263
} // end namespace clang

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,20 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
885885

886886
addDashXForInput(Args, Input, CmdArgs);
887887

888+
bool FRecordCmdLine = false;
889+
bool GRecordCmdLine = false;
890+
if (shouldRecordCommandLine(TC, Args, FRecordCmdLine, GRecordCmdLine)) {
891+
const char *CmdLine = renderEscapedCommandLine(TC, Args);
892+
if (FRecordCmdLine) {
893+
CmdArgs.push_back("-record-command-line");
894+
CmdArgs.push_back(CmdLine);
895+
}
896+
if (TC.UseDwarfDebugFlags() || GRecordCmdLine) {
897+
CmdArgs.push_back("-dwarf-debug-flags");
898+
CmdArgs.push_back(CmdLine);
899+
}
900+
}
901+
888902
CmdArgs.push_back(Input.getFilename());
889903

890904
// TODO: Replace flang-new with flang once the new driver replaces the

flang/include/flang/Frontend/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
6363
/// The directory where temp files are stored if specified by -save-temps
6464
std::optional<std::string> SaveTempsDir;
6565

66+
/// The string containing the commandline for the llvm.commandline metadata.
67+
std::optional<std::string> RecordCommandLine;
68+
6669
/// The name of the file to which the backend should save YAML optimization
6770
/// records.
6871
std::string OptRecordFile;

flang/include/flang/Lower/Bridge.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define FORTRAN_LOWER_BRIDGE_H
1515

1616
#include "flang/Common/Fortran.h"
17+
#include "flang/Frontend/CodeGenOptions.h"
18+
#include "flang/Frontend/TargetOptions.h"
1719
#include "flang/Lower/AbstractConverter.h"
1820
#include "flang/Lower/EnvironmentDefault.h"
1921
#include "flang/Lower/LoweringOptions.h"
@@ -65,11 +67,13 @@ class LoweringBridge {
6567
const Fortran::lower::LoweringOptions &loweringOptions,
6668
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
6769
const Fortran::common::LanguageFeatureControl &languageFeatures,
68-
const llvm::TargetMachine &targetMachine, llvm::StringRef tuneCPU) {
70+
const llvm::TargetMachine &targetMachine,
71+
const Fortran::frontend::TargetOptions &targetOptions,
72+
const Fortran::frontend::CodeGenOptions &codeGenOptions) {
6973
return LoweringBridge(ctx, semanticsContext, defaultKinds, intrinsics,
7074
targetCharacteristics, allCooked, triple, kindMap,
7175
loweringOptions, envDefaults, languageFeatures,
72-
targetMachine, tuneCPU);
76+
targetMachine, targetOptions, codeGenOptions);
7377
}
7478

7579
//===--------------------------------------------------------------------===//
@@ -148,7 +152,9 @@ class LoweringBridge {
148152
const Fortran::lower::LoweringOptions &loweringOptions,
149153
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
150154
const Fortran::common::LanguageFeatureControl &languageFeatures,
151-
const llvm::TargetMachine &targetMachine, const llvm::StringRef tuneCPU);
155+
const llvm::TargetMachine &targetMachine,
156+
const Fortran::frontend::TargetOptions &targetOptions,
157+
const Fortran::frontend::CodeGenOptions &codeGenOptions);
152158
LoweringBridge() = delete;
153159
LoweringBridge(const LoweringBridge &) = delete;
154160

flang/include/flang/Optimizer/Dialect/Support/FIRContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ void setIdent(mlir::ModuleOp mod, llvm::StringRef ident);
7777
/// Get the compiler identifier from the Module.
7878
llvm::StringRef getIdent(mlir::ModuleOp mod);
7979

80+
/// Set the command line used in this invocation.
81+
void setCommandline(mlir::ModuleOp mod, llvm::StringRef cmdLine);
82+
83+
/// Get the command line used in this invocation.
84+
llvm::StringRef getCommandline(mlir::ModuleOp mod);
85+
8086
/// Helper for determining the target from the host, etc. Tools may use this
8187
/// function to provide a consistent interpretation of the `--target=<string>`
8288
/// command-line option.

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
348348
if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
349349
opts.SaveTempsDir = a->getValue();
350350

351+
// -record-command-line option.
352+
if (const llvm::opt::Arg *a =
353+
args.getLastArg(clang::driver::options::OPT_record_command_line)) {
354+
opts.RecordCommandLine = a->getValue();
355+
}
356+
351357
// -mlink-builtin-bitcode
352358
for (auto *a :
353359
args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ bool CodeGenAction::beginSourceFileAction() {
302302
kindMap, ci.getInvocation().getLoweringOpts(),
303303
ci.getInvocation().getFrontendOpts().envDefaults,
304304
ci.getInvocation().getFrontendOpts().features, targetMachine,
305-
ci.getInvocation().getTargetOpts().cpuToTuneFor);
305+
ci.getInvocation().getTargetOpts(), ci.getInvocation().getCodeGenOpts());
306306

307307
// Fetch module from lb, so we can set
308308
mlirModule = std::make_unique<mlir::ModuleOp>(lb.getModule());

flang/lib/Lower/Bridge.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6064,7 +6064,9 @@ Fortran::lower::LoweringBridge::LoweringBridge(
60646064
const Fortran::lower::LoweringOptions &loweringOptions,
60656065
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
60666066
const Fortran::common::LanguageFeatureControl &languageFeatures,
6067-
const llvm::TargetMachine &targetMachine, const llvm::StringRef tuneCPU)
6067+
const llvm::TargetMachine &targetMachine,
6068+
const Fortran::frontend::TargetOptions &targetOpts,
6069+
const Fortran::frontend::CodeGenOptions &cgOpts)
60686070
: semanticsContext{semanticsContext}, defaultKinds{defaultKinds},
60696071
intrinsics{intrinsics}, targetCharacteristics{targetCharacteristics},
60706072
cooked{&cooked}, context{context}, kindMap{kindMap},
@@ -6121,11 +6123,13 @@ Fortran::lower::LoweringBridge::LoweringBridge(
61216123
fir::setTargetTriple(*module.get(), triple);
61226124
fir::setKindMapping(*module.get(), kindMap);
61236125
fir::setTargetCPU(*module.get(), targetMachine.getTargetCPU());
6124-
fir::setTuneCPU(*module.get(), tuneCPU);
6126+
fir::setTuneCPU(*module.get(), targetOpts.cpuToTuneFor);
61256127
fir::setTargetFeatures(*module.get(), targetMachine.getTargetFeatureString());
61266128
fir::support::setMLIRDataLayout(*module.get(),
61276129
targetMachine.createDataLayout());
61286130
fir::setIdent(*module.get(), Fortran::common::getFlangFullVersion());
6131+
if (cgOpts.RecordCommandLine)
6132+
fir::setCommandline(*module.get(), *cgOpts.RecordCommandLine);
61296133
}
61306134

61316135
void Fortran::lower::genCleanUpInRegionIfAny(

flang/lib/Optimizer/Dialect/Support/FIRContext.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ llvm::StringRef fir::getIdent(mlir::ModuleOp mod) {
130130
return {};
131131
}
132132

133+
void fir::setCommandline(mlir::ModuleOp mod, llvm::StringRef cmdLine) {
134+
if (cmdLine.empty())
135+
return;
136+
137+
mlir::MLIRContext *ctx = mod.getContext();
138+
mod->setAttr(mlir::LLVM::LLVMDialect::getCommandlineAttrName(),
139+
mlir::StringAttr::get(ctx, cmdLine));
140+
}
141+
142+
llvm::StringRef fir::getCommandline(mlir::ModuleOp mod) {
143+
if (auto attr = mod->getAttrOfType<mlir::StringAttr>(
144+
mlir::LLVM::LLVMDialect::getCommandlineAttrName()))
145+
return attr;
146+
return {};
147+
}
148+
133149
std::string fir::determineTargetTriple(llvm::StringRef triple) {
134150
// Treat "" or "default" as stand-ins for the default machine.
135151
if (triple.empty() || triple == "default")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! This only checks that the command line is correctly passed on to the
2+
! -record-command-line option FC1 option and that the latter does not complain
3+
! about anything. The correct lowering to a module attribute and beyond will
4+
! be checked in other tests.
5+
!
6+
! RUN: %flang -### -target x86_64-unknown-linux -frecord-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-RECORD %s
7+
! RUN: %flang -### -target x86_64-unknown-macosx -frecord-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-RECORD %s
8+
! RUN: not %flang -### -target x86_64-unknown-windows -frecord-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-RECORD-ERROR %s
9+
10+
! RUN: %flang -### -target x86_64-unknown-linux -fno-record-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-NO-RECORD %s
11+
! RUN: %flang -### -target x86_64-unknown-macosx -fno-record-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-NO-RECORD %s
12+
! RUN: %flang -### -target x86_64-unknown-windows -fno-record-command-line %s 2>&1 | FileCheck --check-prefix=CHECK-NO-RECORD %s
13+
14+
! CHECK-RECORD: "-record-command-line"
15+
! CHECK-NO-RECORD-NOT: "-record-command-line"
16+
! CHECK-RECORD-ERROR: error: unsupported option '-frecord-command-line' for target
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! The actual command line is recorded by the frontend and passed on to FC1 as
2+
! the argument to -record-command-line, so in this test, we just match against
3+
! some string with spaces that mimics what a hypothetical command line.
4+
5+
! RUN: %flang_fc1 -record-command-line "exec -o infile" %s -emit-fir -o - | FileCheck %s
6+
7+
! CHECK: module attributes {
8+
! CHECK-SAME: llvm.commandline = "exec -o infile"
9+

flang/tools/bbc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ FortranParser
3535
FortranEvaluate
3636
FortranSemantics
3737
FortranLower
38+
flangFrontend
3839
)

0 commit comments

Comments
 (0)