[clang] [flang] [flang][driver] Honour the toolchain default DWARF version (PR #217610)
Abid Qadeer via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 21 10:18:57 PDT 2026
https://github.com/abidh updated https://github.com/llvm/llvm-project/pull/217610
>From d33127ba2510fe86bc43789762eb8cf82f8a4b72 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 20 Aug 2026 11:29:35 +0100
Subject: [PATCH 1/3] [flang][driver] Honour the toolchain default DWARF
version
The driver only rendered -dwarf-version= when the user named a version
explicitly with -gdwarf-N. With plain -g the option was omitted, flang's
DwarfVersion stayed 0, AddDebugInfo skipped the "Dwarf Version" module
flag, and the backend fell back to dwarf::DWARF_VERSION. As a result
`flang -g` produced DWARF 4 while `clang -g` produced DWARF 5 on the same
target.
Render the option whenever debug info is being emitted, as clang does in
RenderDebugEnablingArgs. getDwarfVersion() already resolves the toolchain
default, so targets that ask for something other than 5 (AIX, Android,
z/OS, ...) keep their own default, and an explicit -gdwarf-N still wins.
A side effect of reaching DWARF 5 is that the accelerator table changes
from .debug_pubnames/.debug_pubtypes to .debug_names, since flang leaves
DICompileUnit's nameTableKind at its default.
Co-authored-by: Cursor <cursoragent at cursor.com>
---
clang/lib/Driver/ToolChains/Flang.cpp | 9 ++++-
flang/test/Driver/flang-dwarf-version.f90 | 41 +++++++++++++++++++++++
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index a48e41159f367..64d07fd2f1955 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -248,7 +248,14 @@ void Flang::addDebugOptions(const llvm::opt::ArgList &Args, const JobAction &JA,
DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
}
addDebugInfoKind(CmdArgs, DebugInfoKind);
- if (hasDwarfNArg) {
+ // Pass the DWARF version on when debug information is being generated, or
+ // when -gdwarf-N names a version. Leaving it out means the version stays
+ // unset and the backend falls back to dwarf::DWARF_VERSION (4) instead of
+ // honouring toolchain default like clang does.
+ //
+ // Note that both conditions are needed to match clang for cases like
+ // "-gdwarf-5 -g0".
+ if (hasDwarfNArg || DebugInfoKind != llvm::codegenoptions::NoDebugInfo) {
const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
CmdArgs.push_back(
Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
diff --git a/flang/test/Driver/flang-dwarf-version.f90 b/flang/test/Driver/flang-dwarf-version.f90
index d860c970a91f8..e85010161abf9 100644
--- a/flang/test/Driver/flang-dwarf-version.f90
+++ b/flang/test/Driver/flang-dwarf-version.f90
@@ -20,6 +20,43 @@
// RUN: %flang -### -S %s -gdwarf-2 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+// Without an explicit -gdwarf-N, the toolchain default DWARF version is used.
+
+// Linux.
+// RUN: %flang -### -S %s -g --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
+// RUN: %flang -### -S %s -g1 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s
+
+// Android always uses DWARF 4.
+// RUN: %flang -### -S %s -g --target=aarch64-unknown-linux-android21 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
+
+// Darwin derives the version from the OS version rather than using a constant.
+// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx15 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
+// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx10.10 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+
+// AIX.
+// RUN: %flang -### -S %s -g --target=powerpc64-ibm-aix 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
+
+// OpenBSD.
+// RUN: %flang -### -S %s -g --target=x86_64-unknown-openbsd 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+
+// No debug info requested means no DWARF version is passed at all.
+// RUN: %flang -### -S %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
+// RUN: %flang -### -S %s -g0 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
+
+// A version named explicitly is still passed on when debug info is switched
+// off, as clang does.
+// RUN: %flang -### -S %s -gdwarf-5 -g0 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5-G0 %s
+
// CHECK-DWARF5: -debug-info-kind=standalone
// CHECK-DWARF5-SAME: -dwarf-version=5
@@ -31,3 +68,7 @@
// CHECK-DWARF3: -dwarf-version=3
// CHECK-DWARF2: -dwarf-version=2
+
+// CHECK-NO-DWARF-NOT: -dwarf-version=
+
+// CHECK-DWARF5-G0: -dwarf-version=5
>From a95b3870891028be67b0013808ca87c74b82bfcf Mon Sep 17 00:00:00 2001
From: Abid Qadeer <HafizAbid.Qadeer at amd.com>
Date: Fri, 21 Aug 2026 18:13:45 +0100
Subject: [PATCH 2/3] Update clang/lib/Driver/ToolChains/Flang.cpp
Co-authored-by: Tarun Prabhu <tarunprabhu at gmail.com>
---
clang/lib/Driver/ToolChains/Flang.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 64d07fd2f1955..3d4a7ef5529df 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -248,7 +248,7 @@ void Flang::addDebugOptions(const llvm::opt::ArgList &Args, const JobAction &JA,
DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
}
addDebugInfoKind(CmdArgs, DebugInfoKind);
- // Pass the DWARF version on when debug information is being generated, or
+ // Pass on the DWARF version when debug information is being generated, or
// when -gdwarf-N names a version. Leaving it out means the version stays
// unset and the backend falls back to dwarf::DWARF_VERSION (4) instead of
// honouring toolchain default like clang does.
>From 2e4621511a27b6c533217d73ac27990e0dea1478 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 21 Aug 2026 18:18:29 +0100
Subject: [PATCH 3/3] Handle review comments.
Remove a -S that was not needed.
---
flang/test/Driver/flang-dwarf-version.f90 | 32 +++++++++++------------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/flang/test/Driver/flang-dwarf-version.f90 b/flang/test/Driver/flang-dwarf-version.f90
index e85010161abf9..b24575e6aed97 100644
--- a/flang/test/Driver/flang-dwarf-version.f90
+++ b/flang/test/Driver/flang-dwarf-version.f90
@@ -1,60 +1,60 @@
// RUN: %if !target={{.*aix.*}} %{ \
-// RUN: %flang -### -S %s -g -gdwarf-5 2>&1 \
+// RUN: %flang -### %s -g -gdwarf-5 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s \
// RUN: %}
// RUN: %if !target={{.*aix.*}} %{ \
-// RUN: %flang -### -S %s -gdwarf-5 2>&1 \
+// RUN: %flang -### %s -gdwarf-5 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s \
// RUN: %}
// RUN: %if !target={{.*aix.*}} %{ \
-// RUN: %flang -### -S %s -g1 -gdwarf-5 2>&1 \
+// RUN: %flang -### %s -g1 -gdwarf-5 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s \
// RUN: %}
-// RUN: %flang -### -S %s -gdwarf-4 2>&1 \
+// RUN: %flang -### %s -gdwarf-4 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
-// RUN: %flang -### -S %s -gdwarf-3 2>&1 \
+// RUN: %flang -### %s -gdwarf-3 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
-// RUN: %flang -### -S %s -gdwarf-2 2>&1 \
+// RUN: %flang -### %s -gdwarf-2 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
// Without an explicit -gdwarf-N, the toolchain default DWARF version is used.
// Linux.
-// RUN: %flang -### -S %s -g --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: %flang -### %s -g --target=x86_64-unknown-linux-gnu 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
-// RUN: %flang -### -S %s -g1 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: %flang -### %s -g1 --target=x86_64-unknown-linux-gnu 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s
// Android always uses DWARF 4.
-// RUN: %flang -### -S %s -g --target=aarch64-unknown-linux-android21 2>&1 \
+// RUN: %flang -### %s -g --target=aarch64-unknown-linux-android21 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
// Darwin derives the version from the OS version rather than using a constant.
-// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx15 2>&1 \
+// RUN: %flang -### %s -g --target=x86_64-apple-macosx15 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
-// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx10.10 2>&1 \
+// RUN: %flang -### %s -g --target=x86_64-apple-macosx10.10 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
// AIX.
-// RUN: %flang -### -S %s -g --target=powerpc64-ibm-aix 2>&1 \
+// RUN: %flang -### %s -g --target=powerpc64-ibm-aix 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
// OpenBSD.
-// RUN: %flang -### -S %s -g --target=x86_64-unknown-openbsd 2>&1 \
+// RUN: %flang -### %s -g --target=x86_64-unknown-openbsd 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
// No debug info requested means no DWARF version is passed at all.
-// RUN: %flang -### -S %s 2>&1 \
+// RUN: %flang -### %s 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
-// RUN: %flang -### -S %s -g0 2>&1 \
+// RUN: %flang -### %s -g0 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
// A version named explicitly is still passed on when debug info is switched
// off, as clang does.
-// RUN: %flang -### -S %s -gdwarf-5 -g0 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: %flang -### %s -gdwarf-5 -g0 --target=x86_64-unknown-linux-gnu 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF5-G0 %s
// CHECK-DWARF5: -debug-info-kind=standalone
More information about the cfe-commits
mailing list