Skip to content

Commit 99ec5b9

Browse files
authored
[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 #112910.
1 parent 0cca9e4 commit 99ec5b9

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,8 +4682,10 @@ def gdbx : Flag<["-"], "gdbx">, Group<gTune_Group>;
46824682
// Equivalent to our default dwarf version. Forces usual dwarf emission when
46834683
// CodeView is enabled.
46844684
def gdwarf : Flag<["-"], "gdwarf">, Group<g_Group>,
4685-
Visibility<[ClangOption, CLOption, DXCOption]>,
4685+
Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
46864686
HelpText<"Generate source-level debug information with the default dwarf version">;
4687+
4688+
let Visibility = [ClangOption, FlangOption] in {
46874689
def gdwarf_2 : Flag<["-"], "gdwarf-2">, Group<g_Group>,
46884690
HelpText<"Generate source-level debug information with dwarf version 2">;
46894691
def gdwarf_3 : Flag<["-"], "gdwarf-3">, Group<g_Group>,
@@ -4692,6 +4694,7 @@ def gdwarf_4 : Flag<["-"], "gdwarf-4">, Group<g_Group>,
46924694
HelpText<"Generate source-level debug information with dwarf version 4">;
46934695
def gdwarf_5 : Flag<["-"], "gdwarf-5">, Group<g_Group>,
46944696
HelpText<"Generate source-level debug information with dwarf version 5">;
4697+
}
46954698
def gdwarf64 : Flag<["-"], "gdwarf64">, Group<g_Group>,
46964699
Visibility<[ClangOption, CC1Option, CC1AsOption]>,
46974700
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=">;
76337636
def record_command_line : Separate<["-"], "record-command-line">,
76347637
HelpText<"The string to embed in the .LLVM.command.line section.">,
76357638
MarshallingInfoString<CodeGenOpts<"RecordCommandLine">>;
7639+
def dwarf_version_EQ : Joined<["-"], "dwarf-version=">,
7640+
MarshallingInfoInt<CodeGenOpts<"DwarfVersion">>;
76367641

76377642
} // let Visibility = [CC1Option, CC1AsOption, FC1Option]
76387643

@@ -7644,8 +7649,6 @@ def debug_info_macro : Flag<["-"], "debug-info-macro">,
76447649
def default_function_attr : Separate<["-"], "default-function-attr">,
76457650
HelpText<"Apply given attribute to all functions">,
76467651
MarshallingInfoStringVector<CodeGenOpts<"DefaultFunctionAttrs">>;
7647-
def dwarf_version_EQ : Joined<["-"], "dwarf-version=">,
7648-
MarshallingInfoInt<CodeGenOpts<"DwarfVersion">>;
76497652
def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">,
76507653
Values<"gdb,lldb,sce,dbx">,
76517654
NormalizedValuesScope<"llvm::DebuggerKind">, NormalizedValues<["GDB", "LLDB", "SCE", "DBX"]>,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,17 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
134134
if (Args.hasArg(options::OPT_gN_Group)) {
135135
Arg *gNArg = Args.getLastArg(options::OPT_gN_Group);
136136
DebugInfoKind = debugLevelToInfoKind(*gNArg);
137-
} else if (Args.hasArg(options::OPT_g_Flag)) {
137+
} else if (Args.hasArg(options::OPT_g_Group)) {
138138
DebugInfoKind = llvm::codegenoptions::FullDebugInfo;
139139
} else {
140140
DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
141141
}
142142
addDebugInfoKind(CmdArgs, DebugInfoKind);
143+
if (getDwarfNArg(Args)) {
144+
const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
145+
CmdArgs.push_back(
146+
Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
147+
}
143148
}
144149

145150
void Flang::addCodegenOptions(const ArgList &Args,

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ CODEGENOPT(FuseLoops, 1, 0) ///< Enable loop fusion.
4747
CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning.
4848
CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
4949
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass
50+
CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version
5051

5152
CODEGENOPT(Underscoring, 1, 1)
5253
ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use.

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
157157
clang::DiagnosticsEngine::Warning, "Unsupported debug option: %0");
158158
diags.Report(debugWarning) << arg->getValue();
159159
}
160+
// The default value of 2 here is to match clang.
161+
opts.DwarfVersion =
162+
getLastArgIntValue(args, clang::driver::options::OPT_dwarf_version_EQ,
163+
/*Default=*/2, diags);
160164
}
161165
return true;
162166
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %flang -### -S %s -g -gdwarf-5 2>&1 \
2+
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
3+
// RUN: %flang -### -S %s -gdwarf-5 2>&1 \
4+
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
5+
// RUN: %flang -### -S %s -g1 -gdwarf-5 2>&1 \
6+
// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s
7+
// RUN: %flang -### -S %s -gdwarf-4 2>&1 \
8+
// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
9+
// RUN: %flang -### -S %s -gdwarf-3 2>&1 \
10+
// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
11+
// RUN: %flang -### -S %s -gdwarf-2 2>&1 \
12+
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
13+
14+
// CHECK-DWARF5: -debug-info-kind=standalone
15+
// CHECK-DWARF5-SAME: -dwarf-version=5
16+
17+
// CHECK-WITH-G1-DWARF5: -debug-info-kind=line-tables-only
18+
// CHECK-WITH-G1-DWARF5-SAME: -dwarf-version=5
19+
20+
// CHECK-DWARF4: -dwarf-version=4
21+
22+
// CHECK-DWARF3: -dwarf-version=3
23+
24+
// CHECK-DWARF2: -dwarf-version=2

0 commit comments

Comments
 (0)