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

>From 4c66a7be0674fa690a98ad20554b4a2371ed3b77 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <[email protected]>
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 <[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 7e7ac97b8b0e5..75689c030fa38 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -251,7 +251,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 09ce5cd73685a3286541150968f5874287cbe76e Mon Sep 17 00:00:00 2001
From: Abid Qadeer <[email protected]>
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 <[email protected]>
---
 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 75689c030fa38..ccd99b5e0892c 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -251,7 +251,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 a76f1dcdf5d0f514c098a0fd27538135350f77f4 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <[email protected]>
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

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

Reply via email to