[llvm] Emit debug type vector (PR #200056)

Diego Novillo via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 07:44:09 PDT 2026


https://github.com/dnovillo updated https://github.com/llvm/llvm-project/pull/200056

>From 6926d816aaa70c10c687e51603afff775e3a2516 Mon Sep 17 00:00:00 2001
From: Diego Novillo <dnovillo at nvidia.com>
Date: Wed, 27 May 2026 17:05:28 -0400
Subject: [PATCH 1/2] [SPIRV] Emit DebugTypeVector for NSDI

This emits `DebugTypeVector` for HLSL `float4`-style vectors. It works by collecting those nodes in `collectType()` and emitting `DebugTypeVector` after the `DebugTypeBasic` loop. The collection recurses into `getBaseType()` so the element `DIBasicType` is reachable from a vector-only module.

Added a new test in `test/CodeGen/SPIRV/debug-info/debug-type-vector.ll`.
---
 .../SPIRV/SPIRVNonSemanticDebugHandler.cpp    | 51 ++++++++++++--
 .../SPIRV/SPIRVNonSemanticDebugHandler.h      |  5 +-
 .../SPIRV/debug-info/debug-type-vector.ll     | 66 +++++++++++++++++++
 3 files changed, 114 insertions(+), 8 deletions(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/debug-info/debug-type-vector.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
index de2dc8d17a4ad..0178441ac222f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
@@ -27,15 +27,17 @@ using namespace llvm;
 
 namespace {
 
-/// Partition \p Ty into \p BasicTypes, \p PointerTypes, and \p SubroutineTypes
-/// for NSDI emission. Used when iterating DebugInfoFinder.types(); each DI
-/// node is seen once, so no recursion into pointer bases. Other composites and
-/// non-pointer derived kinds are ignored because they are not yet supported.
-/// Only types that are supported (later used) are partitioned.
+/// Partition \p Ty into \p BasicTypes, \p PointerTypes, \p SubroutineTypes,
+/// and \p VectorTypes for NSDI emission. Used when iterating
+/// DebugInfoFinder.types(); each DI node is seen once, so no recursion into
+/// pointer bases. Other composites and non-pointer derived kinds are ignored
+/// because they are not yet supported. Only types that are supported (later
+/// used) are partitioned.
 static void
 partitionTypes(const DIType *Ty, SmallVector<const DIBasicType *> &BasicTypes,
                SmallVector<const DIDerivedType *> &PointerTypes,
-               SmallVector<const DISubroutineType *> &SubroutineTypes) {
+               SmallVector<const DISubroutineType *> &SubroutineTypes,
+               SmallVector<const DICompositeType *> &VectorTypes) {
   if (const auto *BT = dyn_cast<DIBasicType>(Ty)) {
     BasicTypes.push_back(BT);
     return;
@@ -44,6 +46,11 @@ partitionTypes(const DIType *Ty, SmallVector<const DIBasicType *> &BasicTypes,
     SubroutineTypes.push_back(ST);
     return;
   }
+  if (const auto *CT = dyn_cast<DICompositeType>(Ty)) {
+    if (CT->getTag() == dwarf::DW_TAG_array_type && CT->isVector())
+      VectorTypes.push_back(CT);
+    return;
+  }
   const auto *DT = dyn_cast<DIDerivedType>(Ty);
   if (DT && DT->getTag() == dwarf::DW_TAG_pointer_type)
     PointerTypes.push_back(DT);
@@ -173,6 +180,7 @@ void SPIRVNonSemanticDebugHandler::beginModule(Module *M) {
   BasicTypes.clear();
   PointerTypes.clear();
   SubroutineTypes.clear();
+  VectorTypes.clear();
   DebugTypeRegs.clear();
   OpStringContentCache.clear();
   I32ConstantCache.clear();
@@ -218,7 +226,7 @@ void SPIRVNonSemanticDebugHandler::beginModule(Module *M) {
   DebugInfoFinder Finder;
   Finder.processModule(*M);
   llvm::for_each(Finder.types(), [&](DIType *Ty) {
-    partitionTypes(Ty, BasicTypes, PointerTypes, SubroutineTypes);
+    partitionTypes(Ty, BasicTypes, PointerTypes, SubroutineTypes, VectorTypes);
   });
 }
 
@@ -587,6 +595,35 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticGlobalDebugInfo(
     DebugTypeRegs[BT] = BTReg;
   }
 
+  // Emit DebugTypeVector for each collected vector type. Skip entries that
+  // would produce a module rejected by spirv-val: Base Type must resolve to
+  // a DebugTypeBasic, the subrange count must be a constant, and the Vulkan
+  // flavor of NSDI requires Component Count in [1, 4].
+  for (const DICompositeType *VT : VectorTypes) {
+    const auto *BaseTy = dyn_cast_or_null<DIBasicType>(VT->getBaseType());
+    if (!BaseTy)
+      continue;
+    auto BTIt = DebugTypeRegs.find(BaseTy);
+    if (BTIt == DebugTypeRegs.end())
+      continue;
+
+    DINodeArray Elements = VT->getElements();
+    if (Elements.size() != 1)
+      continue;
+    const auto *SR = cast<DISubrange>(Elements[0]);
+    const auto *CI = dyn_cast_if_present<ConstantInt *>(SR->getCount());
+    if (!CI)
+      continue;
+    uint64_t Count = CI->getZExtValue();
+    if (Count == 0 || Count > 4)
+      continue;
+
+    MCRegister CountReg =
+        emitOpConstantI32(static_cast<uint32_t>(Count), I32TypeReg, MAI);
+    emitExtInst(SPIRV::NonSemanticExtInst::DebugTypeVector, VoidTypeReg,
+                ExtInstSetReg, {BTIt->second, CountReg}, MAI);
+  }
+
   // Emit DebugTypePointer for each referenced pointer type.
   for (const DIDerivedType *PT : PointerTypes) {
     if (auto PtrReg = emitDebugTypePointer(PT, ExtInstSetReg, MAI))
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
index b07e3f23a71af..9ca9b28ee8dab 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
@@ -61,10 +61,13 @@ class SPIRVNonSemanticDebugHandler : public DebugHandlerBase {
   int64_t DwarfVersion = 0;
 
   // DI types partitioned from DebugInfoFinder.types() in beginModule()
-  // (basics, pointers, subroutine types NSDI v1 may emit).
+  // (basics, pointers, vectors, subroutine types NSDI v1 may emit).
   SmallVector<const DIBasicType *> BasicTypes;
   SmallVector<const DIDerivedType *> PointerTypes;
   SmallVector<const DISubroutineType *> SubroutineTypes;
+  // DICompositeType nodes with DW_TAG_array_type and DINode::FlagVector,
+  // partitioned from DebugInfoFinder.types() in beginModule().
+  SmallVector<const DICompositeType *> VectorTypes;
 
   // Filled in emitNonSemanticGlobalDebugInfo(): DI types to their result
   // registers.
diff --git a/llvm/test/CodeGen/SPIRV/debug-info/debug-type-vector.ll b/llvm/test/CodeGen/SPIRV/debug-info/debug-type-vector.ll
new file mode 100644
index 0000000000000..4e9dee89e3fe0
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/debug-info/debug-type-vector.ll
@@ -0,0 +1,66 @@
+; RUN: llc --verify-machineinstrs --spirv-ext=+SPV_KHR_non_semantic_info -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
+; RUN: %if spirv-tools %{ llc --verify-machineinstrs --spirv-ext=+SPV_KHR_non_semantic_info -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; Verify that DICompositeType nodes with tag DW_TAG_array_type and the
+; DIFlagVector flag lower to DebugTypeVector. Three element widths exercise
+; the OpConstant cache: a 4-component float vector and a 3-component float
+; vector share the float DebugTypeBasic but have distinct component-count
+; constants, while a 4-component int vector reuses the OpConstant 4 already
+; emitted for the float4.
+;
+; The OpConstant cache merges values across uses. OpConstant 3 is emitted
+; once and shared between the float DebugTypeBasic's Encoding operand (3 =
+; Float, NSDI 4.5) and the 3-component vector's ComponentCount operand.
+; Likewise OpConstant 4 is shared between the int DebugTypeBasic's Encoding
+; (4 = Signed) and the 4-component vector's ComponentCount.
+
+; CHECK-SPIRV: [[ext_inst_non_semantic:%[0-9]+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
+; CHECK-SPIRV-DAG: [[type_void:%[0-9]+]] = OpTypeVoid
+; CHECK-SPIRV-DAG: [[type_int32:%[0-9]+]] = OpTypeInt 32 0
+; CHECK-SPIRV-DAG: [[str_float:%[0-9]+]] = OpString "float"
+; CHECK-SPIRV-DAG: [[str_int:%[0-9]+]] = OpString "int"
+; CHECK-SPIRV-DAG: [[size_32bit:%[0-9]+]] = OpConstant [[type_int32]] 32{{$}}
+; CHECK-SPIRV-DAG: [[flag_zero:%[0-9]+]] = OpConstant [[type_int32]] 0{{$}}
+; CHECK-SPIRV-DAG: [[const_3:%[0-9]+]] = OpConstant [[type_int32]] 3{{$}}
+; CHECK-SPIRV-DAG: [[const_4:%[0-9]+]] = OpConstant [[type_int32]] 4{{$}}
+; CHECK-SPIRV-DAG: [[basic_float:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugTypeBasic [[str_float]] [[size_32bit]] [[const_3]] [[flag_zero]]
+; CHECK-SPIRV-DAG: [[basic_int:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugTypeBasic [[str_int]] [[size_32bit]] [[const_4]] [[flag_zero]]
+; CHECK-SPIRV-DAG: OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugTypeVector [[basic_float]] [[const_4]]
+; CHECK-SPIRV-DAG: OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugTypeVector [[basic_float]] [[const_3]]
+; CHECK-SPIRV-DAG: OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugTypeVector [[basic_int]] [[const_4]]
+
+define spir_func void @test() !dbg !6 {
+entry:
+  %f4 = alloca <4 x float>, align 16
+  %f3 = alloca <3 x float>, align 16
+  %i4 = alloca <4 x i32>, align 16
+    #dbg_declare(ptr %f4, !10, !DIExpression(), !14)
+    #dbg_declare(ptr %f3, !15, !DIExpression(), !17)
+    #dbg_declare(ptr %i4, !18, !DIExpression(), !22)
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_HLSL, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "vector.hlsl", directory: "/src")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 1, type: !7, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
+!7 = !DISubroutineType(types: !8)
+!8 = !{null}
+!9 = !{}
+!10 = !DILocalVariable(name: "f4", scope: !6, file: !1, line: 2, type: !11)
+!11 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, size: 128, flags: DIFlagVector, elements: !13)
+!12 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!13 = !{!DISubrange(count: 4)}
+!14 = !DILocation(line: 2, column: 10, scope: !6)
+!15 = !DILocalVariable(name: "f3", scope: !6, file: !1, line: 3, type: !16)
+!16 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, size: 96, flags: DIFlagVector, elements: !20)
+!17 = !DILocation(line: 3, column: 10, scope: !6)
+!18 = !DILocalVariable(name: "i4", scope: !6, file: !1, line: 4, type: !19)
+!19 = !DICompositeType(tag: DW_TAG_array_type, baseType: !21, size: 128, flags: DIFlagVector, elements: !13)
+!20 = !{!DISubrange(count: 3)}
+!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!22 = !DILocation(line: 4, column: 8, scope: !6)

>From 27c1493f465fdd159cab0d6805ee29c3efd1958c Mon Sep 17 00:00:00 2001
From: Diego Novillo <dnovillo at nvidia.com>
Date: Thu, 4 Jun 2026 16:31:56 -0400
Subject: [PATCH 2/2] Address review feedback.

---
 llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
index 0178441ac222f..9f3a883904015 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
@@ -595,10 +595,9 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticGlobalDebugInfo(
     DebugTypeRegs[BT] = BTReg;
   }
 
-  // Emit DebugTypeVector for each collected vector type. Skip entries that
-  // would produce a module rejected by spirv-val: Base Type must resolve to
-  // a DebugTypeBasic, the subrange count must be a constant, and the Vulkan
-  // flavor of NSDI requires Component Count in [1, 4].
+  // Emit DebugTypeVector for each collected vector type. Skip vectors with
+  // non-scalar element types (not yet supported) and vectors with a
+  // non-constant subrange count (not yet supported).
   for (const DICompositeType *VT : VectorTypes) {
     const auto *BaseTy = dyn_cast_or_null<DIBasicType>(VT->getBaseType());
     if (!BaseTy)
@@ -615,9 +614,6 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticGlobalDebugInfo(
     if (!CI)
       continue;
     uint64_t Count = CI->getZExtValue();
-    if (Count == 0 || Count > 4)
-      continue;
-
     MCRegister CountReg =
         emitOpConstantI32(static_cast<uint32_t>(Count), I32TypeReg, MAI);
     emitExtInst(SPIRV::NonSemanticExtInst::DebugTypeVector, VoidTypeReg,



More information about the llvm-commits mailing list