[llvm] [DebugInfo] Fix segfault in constructSubprogramScopeDIE with null subprogram type (PR #184299)

Shivam Kunwar via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 01:05:18 PST 2026


https://github.com/phyBrackets updated https://github.com/llvm/llvm-project/pull/184299

>From d05c2ce4726a129b7d998e03d17c28dc67de344b Mon Sep 17 00:00:00 2001
From: Shivam Kunwar <shivam.kunwar at kdab.com>
Date: Tue, 3 Mar 2026 14:19:18 +0530
Subject: [PATCH] [DebugInfo] Fix segfault in constructSubprogramScopeDIE with
 null subprogram type

---
 .../CodeGen/AsmPrinter/DwarfCompileUnit.cpp   | 22 +++++++++------
 .../DebugInfo/X86/empty-struct-no-crash.ll    | 28 +++++++++++++++++++
 2 files changed, 41 insertions(+), 9 deletions(-)
 create mode 100644 llvm/test/DebugInfo/X86/empty-struct-no-crash.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index eb55b47dfde2b..7567987ada1c4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -1141,15 +1141,19 @@ DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub,
   }
 
   // If this is a variadic function, add an unspecified parameter.
-  DITypeArray FnArgs = Sub->getType()->getTypeArray();
-
-  // If we have a single element of null, it is a function that returns void.
-  // If we have more than one elements and the last one is null, it is a
-  // variadic function.
-  if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
-      !includeMinimalInlineScopes())
-    ScopeDIE.addChild(
-        DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
+  // Sub->getType() may be null when using LineTablesOnly emission, since
+  // DISubprograms are not required to have a type in that mode.
+  if (auto *SPTy = Sub->getType()) {
+    DITypeArray FnArgs = SPTy->getTypeArray();
+
+    // If we have a single element of null, it is a function that returns void.
+    // If we have more than one elements and the last one is null, it is a
+    // variadic function.
+    if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
+        !includeMinimalInlineScopes())
+      ScopeDIE.addChild(
+          DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
+  }
 
   return ScopeDIE;
 }
diff --git a/llvm/test/DebugInfo/X86/empty-struct-no-crash.ll b/llvm/test/DebugInfo/X86/empty-struct-no-crash.ll
new file mode 100644
index 0000000000000..5723cd7094a57
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/empty-struct-no-crash.ll
@@ -0,0 +1,28 @@
+; RUN: llc -mtriple=x86_64-apple-darwin -o /dev/null %s
+; Verify we don't crash on DISubprogram without a type in LineTablesOnly mode.
+
+target triple = "x86_64-apple-darwin"
+
+define void @test_empty_struct_debug() !dbg !4 {
+entry:
+  %tmp = alloca { { i1, {} }, ptr, { { {} }, { {} } }, i64 }, align 8
+    #dbg_value({ { {} }, { {} } } zeroinitializer, !5, !DIExpression(), !6)
+    #dbg_value(i64 2, !7, !DIExpression(), !6)
+  %0 = insertvalue { { i1, {} }, ptr, { { {} }, { {} } }, i64 } { { i1, {} } zeroinitializer, ptr null, { { {} }, { {} } } zeroinitializer, i64 2 }, ptr null, 1, !dbg !6
+  %1 = insertvalue { { i1, {} }, ptr, { { {} }, { {} } }, i64 } %0, { i1, {} } zeroinitializer, 0, !dbg !8
+  store { { i1, {} }, ptr, { { {} }, { {} } }, i64 } %1, ptr %tmp, align 8
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly)
+!1 = !DIFile(filename: "test.cpp", directory: "/tmp")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !{i32 2, !"Dwarf Version", i32 4}
+!4 = distinct !DISubprogram(name: "test_empty_struct_debug", scope: !1, file: !1, line: 1, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0)
+!5 = !DILocalVariable(name: "v1", scope: !4, file: !1, line: 2)
+!6 = !DILocation(line: 2, column: 1, scope: !4)
+!7 = !DILocalVariable(name: "v2", scope: !4, file: !1, line: 3)
+!8 = !DILocation(line: 3, column: 1, scope: !4)



More information about the llvm-commits mailing list