[flang] Implement -grecord-command-line for Flang (#181686)

Enable Flang to match Clang behavior for command-line recording in DWARF
producer strings when using -grecord-command-line.

Signed-off-by: Yangyu Chen <cyy@cyyself.name>
This commit is contained in:
Yangyu Chen 2026-02-28 01:45:52 +08:00 committed by GitHub
parent bad56dbb23
commit 7f0a343a8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 70 additions and 14 deletions

View File

@ -5005,9 +5005,11 @@ def gxcoff : Joined<["-"], "gxcoff">, Group<g_Group>, Flags<[Unsupported]>;
def gvms : Joined<["-"], "gvms">, Group<g_Group>, Flags<[Unsupported]>;
def gtoggle : Flag<["-"], "gtoggle">, Group<g_flags_Group>, Flags<[Unsupported]>;
def grecord_command_line : Flag<["-"], "grecord-command-line">,
Group<g_flags_Group>;
Group<g_flags_Group>,
Visibility<[ClangOption, FlangOption]>;
def gno_record_command_line : Flag<["-"], "gno-record-command-line">,
Group<g_flags_Group>;
Group<g_flags_Group>,
Visibility<[ClangOption, FlangOption]>;
def : Flag<["-"], "grecord-gcc-switches">, Alias<grecord_command_line>;
def : Flag<["-"], "gno-record-gcc-switches">, Alias<gno_record_command_line>;
defm strict_dwarf : BoolOption<"g", "strict-dwarf",
@ -7951,6 +7953,9 @@ def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">;
def record_command_line : Separate<["-"], "record-command-line">,
HelpText<"The string to embed in the .LLVM.command.line section.">,
MarshallingInfoString<CodeGenOpts<"RecordCommandLine">>;
def dwarf_debug_flags : Separate<["-"], "dwarf-debug-flags">,
HelpText<"The string to embed in the Dwarf debug flags record.">,
MarshallingInfoString<CodeGenOpts<"DwarfDebugFlags">>;
def dwarf_version_EQ : Joined<["-"], "dwarf-version=">,
MarshallingInfoInt<CodeGenOpts<"DwarfVersion">>;
@ -7972,9 +7977,6 @@ def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">,
Values<"gdb,lldb,sce,dbx">,
NormalizedValuesScope<"llvm::DebuggerKind">, NormalizedValues<["GDB", "LLDB", "SCE", "DBX"]>,
MarshallingInfoEnum<CodeGenOpts<"DebuggerTuning">, "Default">;
def dwarf_debug_flags : Separate<["-"], "dwarf-debug-flags">,
HelpText<"The string to embed in the Dwarf debug flags record.">,
MarshallingInfoString<CodeGenOpts<"DwarfDebugFlags">>;
def compress_debug_sections_EQ : Joined<["-", "--"], "compress-debug-sections=">,
HelpText<"DWARF debug sections compression type">, Values<"none,zlib,zstd">,
NormalizedValuesScope<"llvm::DebugCompressionType">, NormalizedValues<["None", "Zlib", "Zstd"]>,

View File

@ -73,6 +73,11 @@ public:
/// The string containing the commandline for the llvm.commandline metadata.
std::optional<std::string> RecordCommandLine;
/// The value from -dwarf-debug-flags to append to DW_AT_producer.
/// This is typically a reconstructed user command line (e.g. from
/// -grecord-command-line) and may contain multiple space-separated flags.
std::string DwarfDebugFlags;
/// The name of the file to which the backend should save YAML optimization
/// records.
std::string OptRecordFile;

View File

@ -103,7 +103,9 @@ void addCompilerGeneratedNamesConversionPass(mlir::PassManager &pm);
void addDebugInfoPass(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel optLevel,
llvm::StringRef inputFilename, int32_t dwarfVersion);
llvm::StringRef inputFilename, int32_t dwarfVersion,
llvm::StringRef splitDwarfFile,
llvm::StringRef dwarfDebugFlags);
/// Create FIRToLLVMPassOptions from pipeline configuration.
FIRToLLVMPassOptions
@ -164,7 +166,8 @@ void createDebugPasses(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel OptLevel,
llvm::StringRef inputFilename, int32_t dwarfVersion,
llvm::StringRef splitDwarfFile);
llvm::StringRef splitDwarfFile,
llvm::StringRef dwarfDebugFlags);
void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
MLIRToLLVMPassPipelineConfig config,

View File

@ -260,7 +260,10 @@ def AddDebugInfo : Pass<"add-debug-info", "mlir::ModuleOp"> {
"dwarf version">,
Option<"splitDwarfFile", "split-dwarf-file",
"std::string", /*default=*/"std::string{}",
"Name of the split dwarf file">
"Name of the split dwarf file">,
Option<"dwarfDebugFlags", "dwarf-debug-flags",
"std::string", /*default=*/"std::string{}",
"Command-line flags to append to DWARF producer">
];
}

View File

@ -110,6 +110,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
}
DwarfVersion = opts.DwarfVersion;
SplitDwarfFile = opts.SplitDwarfFile;
DwarfDebugFlags = opts.DwarfDebugFlags;
}
llvm::OptimizationLevel OptLevel; ///< optimisation level
@ -148,6 +149,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
CX_Full; ///< Method for calculating complex number division
int32_t DwarfVersion = 0; ///< Version of DWARF debug info to generate
std::string SplitDwarfFile = ""; ///< File name for the split debug info
std::string DwarfDebugFlags = ""; ///< Debug flags to append to DWARF producer
};
struct OffloadModuleOpts {

View File

@ -165,6 +165,11 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
args.getLastArg(clang::options::OPT_split_dwarf_output))
opts.SplitDwarfOutput = a->getValue();
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::options::OPT_dwarf_debug_flags))
opts.DwarfDebugFlags = arg->getValue();
return true;
}

View File

@ -97,13 +97,15 @@ void addDebugInfoPass(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel optLevel,
llvm::StringRef inputFilename, int32_t dwarfVersion,
llvm::StringRef splitDwarfFile) {
llvm::StringRef splitDwarfFile,
llvm::StringRef dwarfDebugFlags) {
fir::AddDebugInfoOptions options;
options.debugLevel = getEmissionKind(debugLevel);
options.isOptimized = optLevel != llvm::OptimizationLevel::O0;
options.inputFilename = inputFilename;
options.dwarfVersion = dwarfVersion;
options.splitDwarfFile = splitDwarfFile;
options.dwarfDebugFlags = dwarfDebugFlags;
addPassConditionally(pm, disableDebugInfo,
[&]() { return fir::createAddDebugInfoPass(options); });
}
@ -361,10 +363,11 @@ void createDebugPasses(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel OptLevel,
llvm::StringRef inputFilename, int32_t dwarfVersion,
llvm::StringRef splitDwarfFile) {
llvm::StringRef splitDwarfFile,
llvm::StringRef dwarfDebugFlags) {
if (debugLevel != llvm::codegenoptions::NoDebugInfo)
addDebugInfoPass(pm, debugLevel, OptLevel, inputFilename, dwarfVersion,
splitDwarfFile);
splitDwarfFile, dwarfDebugFlags);
}
void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
@ -383,7 +386,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
pm, (config.DebugInfo != llvm::codegenoptions::NoDebugInfo));
fir::addExternalNameConversionPass(pm, config.Underscoring);
fir::createDebugPasses(pm, config.DebugInfo, config.OptLevel, inputFilename,
config.DwarfVersion, config.SplitDwarfFile);
config.DwarfVersion, config.SplitDwarfFile,
config.DwarfDebugFlags);
fir::addTargetRewritePass(pm);
fir::addCompilerGeneratedNamesConversionPass(pm);

View File

@ -927,8 +927,12 @@ void AddDebugInfoPass::runOnOperation() {
mlir::LLVM::DIFileAttr fileAttr =
mlir::LLVM::DIFileAttr::get(context, fileName, filePath);
mlir::StringAttr producer =
mlir::StringAttr::get(context, Fortran::common::getFlangFullVersion());
// Match Clang style by starting with the full compiler version and
// appending -dwarf-debug-flags content when provided.
std::string producerString = Fortran::common::getFlangFullVersion();
if (!dwarfDebugFlags.empty())
producerString += " " + dwarfDebugFlags;
mlir::StringAttr producer = mlir::StringAttr::get(context, producerString);
mlir::LLVM::DICompileUnitAttr cuAttr = mlir::LLVM::DICompileUnitAttr::get(
mlir::DistinctAttr::create(mlir::UnitAttr::get(context)),
llvm::dwarf::getLanguage("DW_LANG_Fortran95"), fileAttr, producer,

View File

@ -0,0 +1,14 @@
! This checks that -grecord-command-line is forwarded by the flang driver to
! an FC1 -dwarf-debug-flags argument and that -gno-record-command-line
! disables it, matching clang behavior.
!
! RUN: %flang -### -grecord-command-line %s 2>&1 | FileCheck --check-prefix=GRECORD %s
! RUN: %flang -### -gno-record-command-line %s 2>&1 | FileCheck --check-prefix=GNO_RECORD %s
! RUN: %flang -### -grecord-command-line -gno-record-command-line %s 2>&1 | FileCheck --check-prefix=GNO_RECORD %s
!
! GRECORD: "-dwarf-debug-flags"
!
! GNO_RECORD-NOT: "-dwarf-debug-flags"
program p
end program p

View File

@ -0,0 +1,14 @@
// Test the dwarf-debug-flags option will show up in the generated debug info.
// RUN: fir-opt --add-debug-info="debug-level=Full dwarf-debug-flags=\"-grecord-command-line -O2\"" \
// RUN: --mlir-print-debuginfo %s | FileCheck --check-prefix=CHECK-FLAGS %s
module {
func.func @_QPs() {
return loc(#loc_s)
} loc(#loc_s)
} loc(#loc_module)
#loc_module = loc("simple.f90":1:1)
#loc_s = loc("simple.f90":2:1)
// CHECK-FLAGS: #llvm.di_compile_unit<
// CHECK-FLAGS: producer = "{{.*}} -grecord-command-line -O2"