[clang] [flang] [flang][driver] Support -gdwarf-N option. (PR #158314)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 12 08:31:05 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Abid Qadeer (abidh)
<details>
<summary>Changes</summary>
This PR adds the support for -gdwarf-N option where allows user to choose the version of the dwarf. Currently N can be 2, 3, 4, or 5. This is only the driver part of the change. Later PRs will propogate it to the IR.
Fixes https://github.com/llvm/llvm-project/issues/112910.
---
Full diff: https://github.com/llvm/llvm-project/pull/158314.diff
5 Files Affected:
- (modified) clang/include/clang/Driver/Options.td (+5-3)
- (modified) clang/lib/Driver/ToolChains/Flang.cpp (+6-1)
- (modified) flang/include/flang/Frontend/CodeGenOptions.def (+1)
- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+4)
- (added) flang/test/Driver/flang-dwarf-version.f90 (+27)
``````````diff
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 49e917a6a0786..7768f56fbd77e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4674,10 +4674,11 @@ def ggdb3 : Flag<["-"], "ggdb3">, Group<ggdbN_Group>;
def glldb : Flag<["-"], "glldb">, Group<gTune_Group>;
def gsce : Flag<["-"], "gsce">, Group<gTune_Group>;
def gdbx : Flag<["-"], "gdbx">, Group<gTune_Group>;
+
+let Visibility = [ClangOption, CLOption, DXCOption, FlangOption] in {
// Equivalent to our default dwarf version. Forces usual dwarf emission when
// CodeView is enabled.
def gdwarf : Flag<["-"], "gdwarf">, Group<g_Group>,
- Visibility<[ClangOption, CLOption, DXCOption]>,
HelpText<"Generate source-level debug information with the default dwarf version">;
def gdwarf_2 : Flag<["-"], "gdwarf-2">, Group<g_Group>,
HelpText<"Generate source-level debug information with dwarf version 2">;
@@ -4687,6 +4688,7 @@ def gdwarf_4 : Flag<["-"], "gdwarf-4">, Group<g_Group>,
HelpText<"Generate source-level debug information with dwarf version 4">;
def gdwarf_5 : Flag<["-"], "gdwarf-5">, Group<g_Group>,
HelpText<"Generate source-level debug information with dwarf version 5">;
+}
def gdwarf64 : Flag<["-"], "gdwarf64">, Group<g_Group>,
Visibility<[ClangOption, CC1Option, CC1AsOption]>,
HelpText<"Enables DWARF64 format for ELF binaries, if debug information emission is enabled.">,
@@ -7626,6 +7628,8 @@ 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_version_EQ : Joined<["-"], "dwarf-version=">,
+ MarshallingInfoInt<CodeGenOpts<"DwarfVersion">>;
} // let Visibility = [CC1Option, CC1AsOption, FC1Option]
@@ -7637,8 +7641,6 @@ def debug_info_macro : Flag<["-"], "debug-info-macro">,
def default_function_attr : Separate<["-"], "default-function-attr">,
HelpText<"Apply given attribute to all functions">,
MarshallingInfoStringVector<CodeGenOpts<"DefaultFunctionAttrs">>;
-def dwarf_version_EQ : Joined<["-"], "dwarf-version=">,
- MarshallingInfoInt<CodeGenOpts<"DwarfVersion">>;
def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">,
Values<"gdb,lldb,sce,dbx">,
NormalizedValuesScope<"llvm::DebuggerKind">, NormalizedValues<["GDB", "LLDB", "SCE", "DBX"]>,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 1535f4cebf436..2118c67dedc54 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -134,12 +134,17 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
if (Args.hasArg(options::OPT_gN_Group)) {
Arg *gNArg = Args.getLastArg(options::OPT_gN_Group);
DebugInfoKind = debugLevelToInfoKind(*gNArg);
- } else if (Args.hasArg(options::OPT_g_Flag)) {
+ } else if (Args.hasArg(options::OPT_g_Group)) {
DebugInfoKind = llvm::codegenoptions::FullDebugInfo;
} else {
DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
}
addDebugInfoKind(CmdArgs, DebugInfoKind);
+ if (getDwarfNArg(Args)) {
+ const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
+ CmdArgs.push_back(
+ Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
+ }
}
void Flang::addCodegenOptions(const ArgList &Args,
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index cdeea93c9aecb..22f778c5d3d38 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -46,6 +46,7 @@ CODEGENOPT(InterchangeLoops, 1, 0) ///< Enable loop interchange.
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass
+CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version
CODEGENOPT(Underscoring, 1, 1)
ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use.
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 6295a58b1bdad..c7dbfc46f432c 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -157,6 +157,10 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
clang::DiagnosticsEngine::Warning, "Unsupported debug option: %0");
diags.Report(debugWarning) << arg->getValue();
}
+ // The default value of 2 here is to match clang.
+ opts.DwarfVersion =
+ getLastArgIntValue(args, clang::driver::options::OPT_dwarf_version_EQ,
+ /*Default=*/2, diags);
}
return true;
}
diff --git a/flang/test/Driver/flang-dwarf-version.f90 b/flang/test/Driver/flang-dwarf-version.f90
new file mode 100644
index 0000000000000..7319c47de04b7
--- /dev/null
+++ b/flang/test/Driver/flang-dwarf-version.f90
@@ -0,0 +1,27 @@
+// RUN: %flang -### -S %s -g -gdwarf-5 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-WITH-G-DWARF5 %s
+// RUN: %flang -### -S %s -g1 -gdwarf-5 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s
+// RUN: %flang -### -S %s -gdwarf-5 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-WITHOUT-G-DWARF5 %s
+// RUN: %flang -### -S %s -gdwarf-4 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
+// RUN: %flang -### -S %s -gdwarf-3 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
+// RUN: %flang -### -S %s -gdwarf-2 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+
+// CHECK-WITH-G-DWARF5: -debug-info-kind=standalone
+// CHECK-WITH-G-DWARF5-SAME: -dwarf-version=5
+
+// CHECK-WITH-G1-DWARF5: -debug-info-kind=line-tables-only
+// CHECK-WITH-G1-DWARF5-SAME: -dwarf-version=5
+
+// CHECK-WITHOUT-G-DWARF5: -debug-info-kind=standalone
+// CHECK-WITHOUT-G-DWARF5: -dwarf-version=5
+
+// CHECK-DWARF4: -dwarf-version=4
+
+// CHECK-DWARF3: -dwarf-version=3
+
+// CHECK-DWARF2: -dwarf-version=2
``````````
</details>
https://github.com/llvm/llvm-project/pull/158314
More information about the cfe-commits
mailing list