[llvm-branch-commits] [llvm] [SPIRV] Enable NSDI features by default for AMD (PR #244633)
Manuel Carrasco via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 20 07:58:38 PST 2026
https://github.com/mgcarrasco updated https://github.com/llvm/llvm-project/pull/244633
>From 0574d56a2f68ed9076942f643364f99195e9ecdd Mon Sep 17 00:00:00 2001
From: Manuel Carrasco <Manuel.Carrasco at amd.com>
Date: Fri, 20 Nov 2026 09:54:28 -0600
Subject: [PATCH] [SPIRV] Enable NSDI features by default for AMD
Enable -spirv-debug-lexical-block-discriminator-scope,
-spirv-debug-scope-forward-refs and -spirv-debug-declare-untyped-variable
when the target vendor is AMD.
---
.../SPIRV/SPIRVNonSemanticDebugHandler.cpp | 23 ++++--
.../SPIRV/SPIRVNonSemanticDebugHandler.h | 16 ++--
.../debug-info/debug-amd-vendor-defaults.ll | 73 +++++++++++++++++++
3 files changed, 101 insertions(+), 11 deletions(-)
create mode 100644 llvm/test/CodeGen/SPIRV/debug-info/debug-amd-vendor-defaults.ll
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
index 0c40411a470277..4fa1dffc46cc9d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
@@ -183,8 +183,19 @@ findLastFunctionOpVariableDeclaration(const MachineFunction &MF,
} // namespace
SPIRVNonSemanticDebugHandler::SPIRVNonSemanticDebugHandler(AsmPrinter &AP)
- : DebugHandlerBase(&AP),
- CLOpts(static_cast<const SPIRVTargetMachine &>(AP.TM).getCLOpts()) {}
+ : DebugHandlerBase(&AP) {
+ initOptions(static_cast<const SPIRVTargetMachine &>(AP.TM));
+}
+
+void SPIRVNonSemanticDebugHandler::initOptions(const SPIRVTargetMachine &TM) {
+ const SPIRVOptions &CLOpts = TM.getCLOpts();
+ bool VendorDefault = TM.getTargetTriple().getVendor() == Triple::AMD;
+ UseLexicalBlockDiscriminatorScope =
+ CLOpts.debug_lexical_block_discriminator_scope || VendorDefault;
+ UseDebugScopeForwardRefs = CLOpts.debug_scope_forward_refs || VendorDefault;
+ AllowDebugDeclareUntypedVariable =
+ CLOpts.debug_declare_untyped_variable || VendorDefault;
+}
// Map DWARF source language codes to NonSemantic.Shader.DebugInfo.100 source
// language codes. Values are from the SourceLanguage enum in the
@@ -457,7 +468,7 @@ void SPIRVNonSemanticDebugHandler::prepareModuleOutput(
MAI.Reqs.addExtension(SPIRV::Extension::SPV_KHR_non_semantic_info);
HasRelaxedExtInst =
- CLOpts.debug_scope_forward_refs &&
+ UseDebugScopeForwardRefs &&
ST.canUseExtension(
SPIRV::Extension::SPV_KHR_relaxed_extended_instruction);
if (HasRelaxedExtInst)
@@ -1074,7 +1085,7 @@ auto SPIRVNonSemanticDebugHandler::emitDebugTypeFunctionForSubroutineType(
auto SPIRVNonSemanticDebugHandler::resolveScope(const DIScope *Scope,
const DICompileUnit *FallbackCU)
-> EmitResult<MCRegister> {
- if (!CLOpts.debug_lexical_block_discriminator_scope)
+ if (!UseLexicalBlockDiscriminatorScope)
while (const auto *LBF = dyn_cast_or_null<DILexicalBlockFile>(Scope))
Scope = LBF->getScope();
@@ -2153,8 +2164,8 @@ void SPIRVNonSemanticDebugHandler::resolveDebugRecord(
// A #dbg_declare reaches Machine IR as an indirect DBG_VALUE. Its storage is
// an OpVariable in the entry block, so it is in the output by the time any
// record naming it is reached. It optionally allows an OpUntypedVariableKHR.
- if (std::optional<Register> LocReg = getDebugDeclareStorageReg(
- MI, CLOpts.debug_declare_untyped_variable)) {
+ if (std::optional<Register> LocReg =
+ getDebugDeclareStorageReg(MI, AllowDebugDeclareUntypedVariable)) {
MCRegister StorageReg = MAI.getRegisterAlias(&MF, *LocReg);
if (StorageReg.isValid())
placeRecord(SPIRV::NonSemanticExtInst::DebugDeclare, MI, StorageReg,
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
index 0826bc09411421..7bbd1b3b4d1171 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
@@ -38,9 +38,9 @@
namespace llvm {
class GlobalVariable;
-struct SPIRVOptions;
class MachineDominatorTree;
class SPIRVSubtarget;
+class SPIRVTargetMachine;
/// AsmPrinter handler that emits NonSemantic.Shader.DebugInfo.100 (NSDI)
/// instructions for the SPIR-V backend. Registered with SPIRVAsmPrinter when
@@ -61,7 +61,9 @@ class SPIRVSubtarget;
/// being printed, and the DebugDeclare and DebugValue anchored on it.
/// - endFunctionImpl() resets per-function state.
class SPIRVNonSemanticDebugHandler : public DebugHandlerBase {
- const SPIRVOptions &CLOpts;
+ bool UseLexicalBlockDiscriminatorScope = false;
+ bool UseDebugScopeForwardRefs = false;
+ bool AllowDebugDeclareUntypedVariable = false;
static constexpr unsigned NSSet = static_cast<unsigned>(
SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100);
@@ -151,8 +153,8 @@ class SPIRVNonSemanticDebugHandler : public DebugHandlerBase {
// Types and scopes whose instruction is currently being built.
SmallDenseMap<const DIScope *, InProgressScope, 8> ScopesInProgress;
- // True when -spirv-debug-scope-forward-refs is set and prepareModuleOutput
- // saw SPV_KHR_relaxed_extended_instruction.
+ // True when UseDebugScopeForwardRefs is set and prepareModuleOutput saw
+ // SPV_KHR_relaxed_extended_instruction.
bool HasRelaxedExtInst = false;
// Types and scopes that are not supported.
@@ -288,6 +290,10 @@ class SPIRVNonSemanticDebugHandler : public DebugHandlerBase {
// analyzeDebugRecords() and only read afterwards.
DenseMap<const MachineInstr *, SmallVector<DebugRecord>> Records;
+ /// Enable each NSDI option when its command-line flag is set or the target
+ /// vendor enables it by default.
+ void initOptions(const SPIRVTargetMachine &TM);
+
public:
explicit SPIRVNonSemanticDebugHandler(AsmPrinter &AP);
@@ -747,7 +753,7 @@ class SPIRVNonSemanticDebugHandler : public DebugHandlerBase {
///
/// A \c DIType, \c DILexicalBlock, \c DINamespace, or \c DISubprogram scope
/// is created through \c getOrCreateDebugScope. When
- /// \c -spirv-debug-lexical-block-discriminator-scope is enabled, a
+ /// \c UseLexicalBlockDiscriminatorScope is set, a
/// \c DILexicalBlockFile is also created this way, becoming a
/// \c DebugLexicalBlockDiscriminator; otherwise it is unwrapped to its
/// enclosing scope first. When \p Scope is null, a \c DIFile, or another
diff --git a/llvm/test/CodeGen/SPIRV/debug-info/debug-amd-vendor-defaults.ll b/llvm/test/CodeGen/SPIRV/debug-info/debug-amd-vendor-defaults.ll
new file mode 100644
index 00000000000000..a3b0d60a65deaa
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/debug-info/debug-amd-vendor-defaults.ll
@@ -0,0 +1,73 @@
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_KHR_non_semantic_info,+SPV_KHR_untyped_pointers,+SPV_KHR_relaxed_extended_instruction %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-OFF --implicit-check-not=OpExtInstWithForwardRefsKHR --implicit-check-not=DebugDeclare
+; RUN: llc -O0 -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-ON
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_KHR_non_semantic_info,+SPV_KHR_untyped_pointers,+SPV_KHR_relaxed_extended_instruction --spirv-debug-lexical-block-discriminator-scope --spirv-debug-scope-forward-refs --spirv-debug-declare-untyped-variable %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-ON
+
+; The AMD vendor always enables -spirv-debug-lexical-block-discriminator-scope,
+; -spirv-debug-scope-forward-refs and -spirv-debug-declare-untyped-variable by
+; default.
+;
+
+; CHECK-ON: OpExtension "SPV_KHR_relaxed_extended_instruction"
+; CHECK-DAG: [[EXT:%[0-9]+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
+; CHECK-DAG: [[VOID:%[0-9]+]] = OpTypeVoid
+; CHECK-DAG: [[STR_S:%[0-9]+]] = OpString "S"
+; CHECK-DAG: [[STR_X:%[0-9]+]] = OpString "x"
+; CHECK-ON-DAG: [[STR_NEXT:%[0-9]+]] = OpString "next"
+
+; CHECK: [[LB:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugLexicalBlock
+; CHECK: [[DISC:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugLexicalBlockDiscriminator {{%[0-9]+}} {{%[0-9]+}} [[LB]]
+
+; CHECK: [[MEM_X:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugTypeMember [[STR_X]]
+; CHECK-OFF: [[COMP:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugTypeComposite [[STR_S]] {{.*}} [[MEM_X]]{{$}}
+; CHECK-OFF: OpExtInst [[VOID]] [[EXT]] DebugTypePointer [[COMP]]
+; CHECK-ON: [[PTR:%[0-9]+]] = OpExtInstWithForwardRefsKHR [[VOID]] [[EXT]] DebugTypePointer [[COMP:%[0-9]+]]
+; CHECK-ON: [[MEM_NEXT:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugTypeMember [[STR_NEXT]] [[PTR]]
+; CHECK-ON: [[COMP]] = OpExtInst [[VOID]] [[EXT]] DebugTypeComposite [[STR_S]] {{.*}} [[MEM_X]] [[MEM_NEXT]]{{$}}
+
+; CHECK: [[LV:%[0-9]+]] = OpExtInst [[VOID]] [[EXT]] DebugLocalVariable [[STR_X]]
+; CHECK: [[VAR:%[0-9]+]] = OpUntypedVariableKHR {{%[0-9]+}} Function
+; CHECK-ON: OpExtInst [[VOID]] [[EXT]] DebugDeclare [[LV]] [[VAR]]
+
+; CHECK-OFF-NOT: DebugScope [[DISC]]
+; CHECK-OFF: OpExtInst [[VOID]] [[EXT]] DebugScope [[LB]]
+; CHECK-OFF-NOT: DebugScope [[DISC]]
+; CHECK-ON: OpExtInst [[VOID]] [[EXT]] DebugScope [[DISC]]
+; CHECK-ON-NOT: DebugScope [[LB]]
+
+define spir_func i32 @foo(i32 %N) !dbg !6 {
+entry:
+ %x = alloca i32, align 4
+ #dbg_declare(ptr %x, !20, !DIExpression(), !14)
+ store i32 %N, ptr %x, align 4, !dbg !14
+ %cmp = icmp sgt i32 %N, 0, !dbg !11
+ br i1 %cmp, label %t, label %f, !dbg !11
+
+t:
+ ret i32 1, !dbg !14
+
+f:
+ ret i32 0, !dbg !14
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !15)
+!1 = !DIFile(filename: "foo.c", directory: "/tmp")
+!2 = !{null}
+!3 = !{i32 7, !"Dwarf Version", i32 5}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 2, type: !7, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
+!7 = !DISubroutineType(types: !2)
+!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !DILocation(line: 4, scope: !12)
+!12 = !DILexicalBlockFile(scope: !13, file: !1, discriminator: 1)
+!13 = distinct !DILexicalBlock(scope: !6, file: !1, line: 4, column: 3)
+!14 = !DILocation(line: 5, scope: !6)
+!15 = !{!16}
+!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !1, line: 1, size: 128, elements: !17)
+!17 = !{!18, !19}
+!18 = !DIDerivedType(tag: DW_TAG_member, name: "x", file: !1, line: 1, baseType: !9, size: 32)
+!19 = !DIDerivedType(tag: DW_TAG_member, name: "next", file: !1, line: 1, baseType: !21, size: 64, offset: 64)
+!20 = !DILocalVariable(name: "x", scope: !6, file: !1, line: 3, type: !9)
+!21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64, dwarfAddressSpace: 4)
More information about the llvm-branch-commits
mailing list