https://github.com/abidh created 
https://github.com/llvm/llvm-project/pull/217610

The driver only rendered -dwarf-version= when the user named a version 
explicitly with -gdwarf-N. With plain -g the option was omitted, and the 
backend fell back to dwarf::DWARF_VERSION. As a result `flang -g` always 
produced DWARF 4 while `clang -g` produced the toolchain default for the same 
target..

The fix is to generate the "-dwarf-version" flag when either the debug 
information is enabled or an explicit -gdwarf-N is given.



>From d33127ba2510fe86bc43789762eb8cf82f8a4b72 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <[email protected]>
Date: Thu, 20 Aug 2026 11:29:35 +0100
Subject: [PATCH] [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 <[email protected]>
---
 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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to