[clang] 99ec5b9 - [flang][driver] Support -gdwarf-N option. (#158314)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 15 07:57:37 PDT 2025
Author: Abid Qadeer
Date: 2025-09-15T15:57:33+01:00
New Revision: 99ec5b95da4fa4e32cf9854513413510d48781f6
URL: https://github.com/llvm/llvm-project/commit/99ec5b95da4fa4e32cf9854513413510d48781f6
DIFF: https://github.com/llvm/llvm-project/commit/99ec5b95da4fa4e32cf9854513413510d48781f6.diff
LOG: [flang][driver] Support -gdwarf-N option. (#158314)
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.
Added:
flang/test/Driver/flang-dwarf-version.f90
Modified:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CodeGenOptions.def
flang/lib/Frontend/CompilerInvocation.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 47d328f862e07..def7c09d58cfb 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4682,8 +4682,10 @@ def gdbx : Flag<["-"], "gdbx">, Group<gTune_Group>;
// 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]>,
+ Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
HelpText<"Generate source-level debug information with the default dwarf version">;
+
+let Visibility = [ClangOption, FlangOption] in {
def gdwarf_2 : Flag<["-"], "gdwarf-2">, Group<g_Group>,
HelpText<"Generate source-level debug information with dwarf version 2">;
def gdwarf_3 : Flag<["-"], "gdwarf-3">, Group<g_Group>,
@@ -4692,6 +4694,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.">,
@@ -7633,6 +7636,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]
@@ -7644,8 +7649,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 d3f4af164f672..12e510ab1562d 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 edab48a70d29d..f09159962883f 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -47,6 +47,7 @@ CODEGENOPT(FuseLoops, 1, 0) ///< Enable loop fusion.
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 4f42fbd66eac0..a00e568bb4a54 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..dc69140a7eda1
--- /dev/null
+++ b/flang/test/Driver/flang-dwarf-version.f90
@@ -0,0 +1,24 @@
+// RUN: %flang -### -S %s -g -gdwarf-5 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
+// RUN: %flang -### -S %s -gdwarf-5 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-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-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-DWARF5: -debug-info-kind=standalone
+// CHECK-DWARF5-SAME: -dwarf-version=5
+
+// CHECK-WITH-G1-DWARF5: -debug-info-kind=line-tables-only
+// CHECK-WITH-G1-DWARF5-SAME: -dwarf-version=5
+
+// CHECK-DWARF4: -dwarf-version=4
+
+// CHECK-DWARF3: -dwarf-version=3
+
+// CHECK-DWARF2: -dwarf-version=2
More information about the cfe-commits
mailing list