https://github.com/Prabhuk updated 
https://github.com/llvm/llvm-project/pull/188383

>From e49d5cdc716b36fcde23299f4a05700eb87c6ddb Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Tue, 24 Mar 2026 16:47:37 -0700
Subject: [PATCH 1/4] [CodeGen] Fix PDB file generation for UEFI triples

The PR #142970 started emitting debug info for all COFF targets even
when no debug info was available for whatever reason. This is only
necessary for windows triples that generate COFF binaries but not other
targets in LLVM such as the UEFI triples. This PR #142970 started
crashing UEFI triples. An earlier attempted fix #174460 prevented the
crash but did not address the fact that PDB files were not generated for
UEFI triple. This patch effectively addresses both the crash and
generation of PDB files for UEFI triples.
---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp        |  4 ++--
 llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp     |  7 +++++++
 llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll | 15 ---------------
 3 files changed, 9 insertions(+), 17 deletions(-)
 delete mode 100644 llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 569a477690958b..59de955d57088d 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -636,8 +636,8 @@ bool AsmPrinter::doInitialization(Module &M) {
     bool EmitCodeView = M.getCodeViewFlag();
     // On Windows targets, emit minimal CodeView compiler info even when debug
     // info is disabled.
-    if ((Target.isOSWindows() || (Target.isUEFI() && EmitCodeView)) &&
-        M.getNamedMetadata("llvm.dbg.cu"))
+    if ((Target.isOSWindows() && M.getNamedMetadata("llvm.dbg.cu")) ||
+        (Target.isUEFI() && EmitCodeView))
       Handlers.push_back(std::make_unique<CodeViewDebug>(this));
     if (!EmitCodeView || M.getDwarfVersion()) {
       if (hasDebugInfo()) {
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index 4b6b76c48dcbff..105d37c65aa2c7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -611,6 +611,13 @@ static SourceLanguage 
MapDWARFLanguageToCVLang(dwarf::SourceLanguage DWLang) {
 }
 
 void CodeViewDebug::beginModule(Module *M) {
+  if (!Asm->hasDebugInfo()) {
+    // Windows triple requires emitting debug info even if there is no debug
+    // info which no other triples (e.g *-UEFI) require.
+    if (!M->getTargetTriple().isOSWindows())
+      return;
+  }
+
   // If COFF debug section is not available, skip any debug info related stuff.
   if (!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
     Asm = nullptr;
diff --git a/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll 
b/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll
deleted file mode 100644
index 37ef201313ed64..00000000000000
--- a/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll
+++ /dev/null
@@ -1,15 +0,0 @@
-;; This test ensures that the backend does not crash when CodeView is enabled
-;; through module flags but llvm.dbg.cu is missing.
-
-; RUN: llc < %s -mtriple=x86_64-unknown-uefi | FileCheck %s
-
-define void @foo() {
-entry:
-  ret void
-}
-
-!llvm.module.flags = !{!0}
-!0 = !{i32 2, !"CodeView", i32 1}
-
-; CHECK-LABEL: foo:
-; CHECK: retq

>From 51a032d8f89fe1de68f9a05fad902096e37313b9 Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Tue, 24 Mar 2026 16:56:42 -0700
Subject: [PATCH 2/4] fix comment

---
 llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index 105d37c65aa2c7..ffd8719360ef1c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -612,8 +612,8 @@ static SourceLanguage 
MapDWARFLanguageToCVLang(dwarf::SourceLanguage DWLang) {
 
 void CodeViewDebug::beginModule(Module *M) {
   if (!Asm->hasDebugInfo()) {
-    // Windows triple requires emitting debug info even if there is no debug
-    // info which no other triples (e.g *-UEFI) require.
+    // Windows triple requires emitting minimal CodeView even if there is
+    // no debug info which no other triples (e.g *-UEFI) require.
     if (!M->getTargetTriple().isOSWindows())
       return;
   }

>From 0faf90dc486f5542086705c552e23c671738727a Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Wed, 25 Mar 2026 11:02:14 -0700
Subject: [PATCH 3/4] Bring back the no crash test.

---
 llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll

diff --git a/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll 
b/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll
new file mode 100644
index 00000000000000..37ef201313ed64
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/uefi-no-codeview-crash.ll
@@ -0,0 +1,15 @@
+;; This test ensures that the backend does not crash when CodeView is enabled
+;; through module flags but llvm.dbg.cu is missing.
+
+; RUN: llc < %s -mtriple=x86_64-unknown-uefi | FileCheck %s
+
+define void @foo() {
+entry:
+  ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 2, !"CodeView", i32 1}
+
+; CHECK-LABEL: foo:
+; CHECK: retq

>From 610863f10e0067e72ab87a1781c07dec23215c70 Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Thu, 15 Oct 2026 17:54:27 -0700
Subject: [PATCH 4/4] [CodeGen] Always emit CodeView compiler info for UEFI
 targets

UEFI is based on the MSVC toolchain conventions. PR #142970 made Windows
targets always emit minimal CodeView compiler information (S_OBJNAME and
S_COMPILE3) even when debug info is otherwise disabled, matching MSVC.
Make UEFI targets behave the same way instead of special casing them:

* clang creates the NoDebug compile unit and emits the "Debug Info
  Version" module flag for UEFI targets just like it does for Windows.
* AsmPrinter registers the CodeView handler for UEFI whenever llvm.dbg.cu
  is present, exactly as for Windows, instead of keying off the CodeView
  module flag.

This removes the UEFI special case in CodeViewDebug::beginModule and
updates the tests to expect compiler info for UEFI objects.
---
 clang/lib/CodeGen/CodeGenModule.cpp           | 10 +++---
 clang/test/DebugInfo/CXX/coff.cpp             | 31 +++++++++++++++++++
 clang/test/DebugInfo/Generic/version-coff.c   |  2 ++
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    |  9 +++---
 llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp |  7 -----
 llvm/test/DebugInfo/COFF/uefi-nodebug.ll      | 27 ++++++++++++++--
 6 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 00a82819681c14..4e0c8db06d05f6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -628,10 +628,10 @@ CodeGenModule::CodeGenModule(ASTContext &C,
       CodeGenOpts.CoverageNotesFile.size() ||
       CodeGenOpts.CoverageDataFile.size())
     DebugInfo.reset(new CGDebugInfo(*this));
-  else if (getTriple().isOSWindows())
-    // On Windows targets, we want to emit compiler info even if debug info is
-    // otherwise disabled. Use a temporary CGDebugInfo instance to emit only
-    // basic compiler metadata.
+  else if (getTriple().isOSWindows() || getTriple().isUEFI())
+    // On Windows and UEFI targets, we want to emit compiler info even if debug
+    // info is otherwise disabled. Use a temporary CGDebugInfo instance to emit
+    // only basic compiler metadata.
     CGDebugInfo(*this);
 
   Block.GlobalUniqueCount = 0;
@@ -1433,7 +1433,7 @@ void CodeGenModule::Release() {
                               "StrictVTablePointersRequirement",
                               llvm::MDNode::get(VMContext, Ops));
   }
-  if (getModuleDebugInfo() || getTriple().isOSWindows())
+  if (getModuleDebugInfo() || getTriple().isOSWindows() || 
getTriple().isUEFI())
     // We support a single version in the linked module. The LLVM
     // parser will drop debug info with a different version number
     // (and warn about it, too).
diff --git a/clang/test/DebugInfo/CXX/coff.cpp 
b/clang/test/DebugInfo/CXX/coff.cpp
index 2535c5cc7511f6..591c317d0e10d1 100644
--- a/clang/test/DebugInfo/CXX/coff.cpp
+++ b/clang/test/DebugInfo/CXX/coff.cpp
@@ -32,6 +32,37 @@
 // CHECK-NEXT:   ]
 // CHECK-NEXT: ]
 
+// UEFI targets follow the MSVC toolchain conventions and emit the same
+// minimal compiler info.
+
+// RUN: %clang --target=x86_64-unknown-uefi -S -emit-llvm %s -o - | FileCheck 
--check-prefix=IR %s
+
+// RUN: %clang --target=x86_64-unknown-uefi -c %s -o %t.uefi.o
+// RUN: llvm-readobj --codeview %t.uefi.o | FileCheck --check-prefix=UEFI %s
+// UEFI:      CodeViewDebugInfo [
+// UEFI-NEXT:   Section: .debug$S ({{[0-9]+}})
+// UEFI-NEXT:   Magic: 0x4
+// UEFI-NEXT:   Subsection [
+// UEFI-NEXT:     SubSectionType: Symbols (0xF1)
+// UEFI-NEXT:     SubSectionSize:
+// UEFI-NEXT:     ObjNameSym {
+// UEFI-NEXT:       Kind: S_OBJNAME (0x1101)
+// UEFI-NEXT:       Signature: 0x0
+// UEFI-NEXT:       ObjectName:
+// UEFI-NEXT:     }
+// UEFI-NEXT:     Compile3Sym {
+// UEFI-NEXT:       Kind: S_COMPILE3 (0x113C)
+// UEFI-NEXT:       Language: Cpp (0x1)
+// UEFI-NEXT:       Flags [ (0x0)
+// UEFI-NEXT:       ]
+// UEFI-NEXT:       Machine: X64 (0xD0)
+// UEFI-NEXT:       FrontendVersion:
+// UEFI-NEXT:       BackendVersion:
+// UEFI-NEXT:       VersionName: {{.*}}clang version
+// UEFI-NEXT:     }
+// UEFI-NEXT:   ]
+// UEFI-NEXT: ]
+
 int main() {
   return 0;
 }
diff --git a/clang/test/DebugInfo/Generic/version-coff.c 
b/clang/test/DebugInfo/Generic/version-coff.c
index 6497a58292362d..beddab75091d9b 100644
--- a/clang/test/DebugInfo/Generic/version-coff.c
+++ b/clang/test/DebugInfo/Generic/version-coff.c
@@ -1,6 +1,8 @@
 // REQUIRES: x86-registered-target
 // RUN: %clang --target=x86_64-windows -g -S -emit-llvm -o - %s | FileCheck %s
 // RUN: %clang --target=x86_64-windows -S -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang --target=x86_64-unknown-uefi -g -S -emit-llvm -o - %s | 
FileCheck %s
+// RUN: %clang --target=x86_64-unknown-uefi -S -emit-llvm -o - %s | FileCheck 
%s
 int main (void) {
   return 0;
 }
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 2940478249effd..cbb339b2f7c629 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -627,10 +627,11 @@ bool AsmPrinter::doInitialization(Module &M) {
 
   if (MAI.doesSupportDebugInformation()) {
     bool EmitCodeView = M.getCodeViewFlag();
-    // On Windows targets, emit minimal CodeView compiler info even when debug
-    // info is disabled.
-    if ((Target.isOSWindows() && M.getNamedMetadata("llvm.dbg.cu")) ||
-        (Target.isUEFI() && EmitCodeView))
+    // On Windows and UEFI targets, emit minimal CodeView compiler info even
+    // when debug info is disabled. UEFI follows the MSVC toolchain
+    // conventions, so it is treated the same way as Windows here.
+    if ((Target.isOSWindows() || Target.isUEFI()) &&
+        M.getNamedMetadata("llvm.dbg.cu"))
       Handlers.push_back(std::make_unique<CodeViewDebug>(this));
     if (!EmitCodeView || M.getDwarfVersion()) {
       if (hasDebugInfo()) {
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index 4d186d2c4f7673..9cf268ce8ce4f7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -617,13 +617,6 @@ static SourceLanguage 
MapDWARFLanguageToCVLang(dwarf::SourceLanguage DWLang) {
 }
 
 void CodeViewDebug::beginModule(Module *M) {
-  if (!Asm->hasDebugInfo()) {
-    // Windows triple requires emitting minimal CodeView even if there is
-    // no debug info which no other triples (e.g *-UEFI) require.
-    if (!M->getTargetTriple().isOSWindows())
-      return;
-  }
-
   // If COFF debug section is not available, skip any debug info related stuff.
   if (!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
     Asm = nullptr;
diff --git a/llvm/test/DebugInfo/COFF/uefi-nodebug.ll 
b/llvm/test/DebugInfo/COFF/uefi-nodebug.ll
index 92e5fd6b5796bb..faf2781c508196 100644
--- a/llvm/test/DebugInfo/COFF/uefi-nodebug.ll
+++ b/llvm/test/DebugInfo/COFF/uefi-nodebug.ll
@@ -1,8 +1,31 @@
 ; RUN: llc -filetype=obj -o - < %s | llvm-readobj --codeview - | FileCheck %s
-; Check that compiler info is not emitted when CodeView flag is not specified
+; Check that only basic compiler info is emitted for UEFI targets with
+; emissionKind NoDebug, matching the behavior of Windows targets.
 
 ; CHECK-NOT:  CodeViewTypes
-; CHECK-NOT:  CodeViewDebugInfo
+; CHECK:      CodeViewDebugInfo [
+; CHECK-NEXT:   Section: .debug$S (4)
+; CHECK-NEXT:   Magic: 0x4
+; CHECK-NEXT:   Subsection [
+; CHECK-NEXT:     SubSectionType: Symbols (0xF1)
+; CHECK-NEXT:     SubSectionSize: 0x2C
+; CHECK-NEXT:     ObjNameSym {
+; CHECK-NEXT:       Kind: S_OBJNAME (0x1101)
+; CHECK-NEXT:       Signature: 0x0
+; CHECK-NEXT:       ObjectName:
+; CHECK-NEXT:     }
+; CHECK-NEXT:     Compile3Sym {
+; CHECK-NEXT:       Kind: S_COMPILE3 (0x113C)
+; CHECK-NEXT:       Language: C (0x0)
+; CHECK-NEXT:       Flags [ (0x0)
+; CHECK-NEXT:       ]
+; CHECK-NEXT:       Machine: X64 (0xD0)
+; CHECK-NEXT:       FrontendVersion:
+; CHECK-NEXT:       BackendVersion:
+; CHECK-NEXT:       VersionName: clang
+; CHECK-NEXT:     }
+; CHECK-NEXT:   ]
+; CHECK-NEXT: ]
 
 source_filename = "empty"
 target datalayout = 
"e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"

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

Reply via email to