summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLujin Wang <143145775+lujinwangnv@users.noreply.github.com>2025-09-18 10:37:14 -0700
committerGitHub <noreply@github.com>2025-09-18 17:37:14 +0000
commited9f42361e2f07a79fde39ded9e57473d5fac39f (patch)
tree1c3523d09781d4493c553fa7664d8ece0edf02a3
parent54faa55c0bd4c4beede7337a76ed3a56d1eb4f15 (diff)
Fix DebugCompilationUnit to reference main shader file instead of header files (#7957)
This PR implements the requested fix for issue #7923 where DebugCompilationUnit incorrectly referenced header files instead of the main shader file. ## Summary - Modified IRDebugSource to include isIncludedFile flag as third operand - Updated emitDebugSource function to accept and pass the included file flag - Updated call sites to use source->isIncludedFile() from SourceFile class - Modified SPIR-V emission to only create DebugCompilationUnit for non-included files ## Test Results The fix has been verified with the provided reproducer code. The SPIR-V output now correctly shows DebugCompilationUnit referencing the main shader file instead of header files. Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Lujin Wang <lujinwangnv@users.noreply.github.com> Co-authored-by: Claude Code <claude@anthropic.com> Co-authored-by: slangbot <ellieh+slangbot@nvidia.com> Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
-rw-r--r--source/slang/slang-emit-spirv.cpp4
-rw-r--r--source/slang/slang-ir-insts.h6
-rw-r--r--source/slang/slang-ir-insts.lua2
-rw-r--r--source/slang/slang-ir.cpp12
-rw-r--r--source/slang/slang-lower-to-ir.cpp5
-rw-r--r--tests/spirv/debug-compilation-unit-main-file.slang37
-rw-r--r--tests/spirv/shader-utils.slang18
7 files changed, 76 insertions, 8 deletions
diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp
index f82bf24c1..18c54587a 100644
--- a/source/slang/slang-emit-spirv.cpp
+++ b/source/slang/slang-emit-spirv.cpp
@@ -2297,7 +2297,9 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
auto moduleInst = inst->getModule()->getModuleInst();
if (!m_defaultDebugSource)
m_defaultDebugSource = debugSource;
- if (!m_mapIRInstToSpvDebugInst.containsKey(moduleInst))
+ // Only create DebugCompilationUnit for non-included files
+ auto isIncludedFile = as<IRBoolLit>(debugSource->getIsIncludedFile())->getValue();
+ if (!m_mapIRInstToSpvDebugInst.containsKey(moduleInst) && !isIncludedFile)
{
IRBuilder builder(inst);
builder.setInsertBefore(inst);
diff --git a/source/slang/slang-ir-insts.h b/source/slang/slang-ir-insts.h
index 9208e546c..e3014119c 100644
--- a/source/slang/slang-ir-insts.h
+++ b/source/slang/slang-ir-insts.h
@@ -3261,6 +3261,7 @@ struct IRDebugSource : IRInst
FIDDLE(leafInst())
IRInst* getFileName() { return getOperand(0); }
IRInst* getSource() { return getOperand(1); }
+ IRInst* getIsIncludedFile() { return getOperand(2); }
};
FIDDLE()
@@ -3894,7 +3895,10 @@ public:
return (IRMetalMeshType*)getType(kIROp_MetalMeshType, 5, ops);
}
- IRInst* emitDebugSource(UnownedStringSlice fileName, UnownedStringSlice source);
+ IRInst* emitDebugSource(
+ UnownedStringSlice fileName,
+ UnownedStringSlice source,
+ bool isIncludedFile);
IRInst* emitDebugBuildIdentifier(UnownedStringSlice buildIdentifier, IRIntegerValue flags);
IRInst* emitDebugBuildIdentifier(IRInst* debugBuildIdentifier);
IRInst* emitDebugLine(
diff --git a/source/slang/slang-ir-insts.lua b/source/slang/slang-ir-insts.lua
index c1777c2cf..6487f36d0 100644
--- a/source/slang/slang-ir-insts.lua
+++ b/source/slang/slang-ir-insts.lua
@@ -2027,7 +2027,7 @@ local insts = {
EndFragmentShaderInterlock = { struct_name = "EndFragmentShaderInterlock" },
},
-- DebugInfo
- { DebugSource = { min_operands = 2, hoistable = true } },
+ { DebugSource = { min_operands = 3, hoistable = true } },
{
DebugLine = {
min_operands = 5,
diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp
index c63ca2bec..a61859a5e 100644
--- a/source/slang/slang-ir.cpp
+++ b/source/slang/slang-ir.cpp
@@ -3379,10 +3379,16 @@ IRInst* IRBuilder::emitOutImplicitCast(IRInst* type, IRInst* value)
{
return emitIntrinsicInst((IRType*)type, kIROp_OutImplicitCast, 1, &value);
}
-IRInst* IRBuilder::emitDebugSource(UnownedStringSlice fileName, UnownedStringSlice source)
+IRInst* IRBuilder::emitDebugSource(
+ UnownedStringSlice fileName,
+ UnownedStringSlice source,
+ bool isIncludedFile)
{
- IRInst* args[] = {getStringValue(fileName), getStringValue(source)};
- return emitIntrinsicInst(getVoidType(), kIROp_DebugSource, 2, args);
+ IRInst* args[] = {
+ getStringValue(fileName),
+ getStringValue(source),
+ getBoolValue(isIncludedFile)};
+ return emitIntrinsicInst(getVoidType(), kIROp_DebugSource, 3, args);
}
IRInst* IRBuilder::emitDebugBuildIdentifier(
UnownedStringSlice buildIdentifier,
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 3fbf0bda9..792848ce6 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -7486,7 +7486,7 @@ IRInst* getOrEmitDebugSource(IRGenContext* context, PathInfo path)
content = UnownedStringSlice((char*)outBlob->getBufferPointer(), outBlob->getBufferSize());
IRBuilder builder(*context->irBuilder);
builder.setInsertInto(context->irBuilder->getModule());
- auto debugSrcInst = builder.emitDebugSource(path.foundPath.getUnownedSlice(), content);
+ auto debugSrcInst = builder.emitDebugSource(path.foundPath.getUnownedSlice(), content, false);
context->shared->mapSourcePathToDebugSourceInst[path.foundPath] = debugSrcInst;
return debugSrcInst;
}
@@ -12130,7 +12130,8 @@ RefPtr<IRModule> generateIRForTranslationUnit(
{
auto debugSource = builder->emitDebugSource(
source->getPathInfo().getMostUniqueIdentity().getUnownedSlice(),
- source->getContent());
+ source->getContent(),
+ source->isIncludedFile());
context->shared->mapSourceFileToDebugSourceInst.add(source, debugSource);
}
}
diff --git a/tests/spirv/debug-compilation-unit-main-file.slang b/tests/spirv/debug-compilation-unit-main-file.slang
new file mode 100644
index 000000000..c8e89ae93
--- /dev/null
+++ b/tests/spirv/debug-compilation-unit-main-file.slang
@@ -0,0 +1,37 @@
+//TEST:SIMPLE(filecheck=CHECK):-target spirv -entry main -stage fragment -g2 -emit-spirv-directly
+
+#include "shader-utils.slang"
+
+struct VertexOutput
+{
+ float4 position : SV_Position;
+ float2 texCoord : TEXCOORD0;
+}
+
+[shader("fragment")]
+float4 main(VertexOutput input) : SV_Target
+{
+ // Create a simple gradient pattern based on texture coordinates
+ float2 uv = input.texCoord;
+
+ // Call function from the included header file
+ float distanceFromCenter = calculateDistanceFromCenter(uv);
+
+ // Use another function from the header for a radial fade effect
+ float radialFade = createRadialFade(uv, 0.7);
+
+ // Create color pattern with both distance effects
+ float4 color = float4(
+ uv.x * (1.0 - distanceFromCenter * 0.5), // Red with subtle distance fade
+ uv.y * radialFade, // Green with smooth radial fade
+ uv.x * uv.y, // Blue as product of coordinates
+ 1.0 // Full opacity
+ );
+
+ return color;
+}
+
+// Verify that DebugCompilationUnit references this main shader file
+// CHECK: [[MAIN_FILE:%[0-9]+]] = OpString "{{.*}}debug-compilation-unit-main-file.slang"
+// CHECK: [[SOURCE_ID:%[0-9]+]] = OpExtInst %void %{{[0-9]+}} DebugSource [[MAIN_FILE]] %{{[0-9]+}}
+// CHECK: OpExtInst %void %{{[0-9]+}} DebugCompilationUnit %uint_{{[0-9]+}} %uint_{{[0-9]+}} [[SOURCE_ID]] %uint_{{[0-9]+}}
diff --git a/tests/spirv/shader-utils.slang b/tests/spirv/shader-utils.slang
new file mode 100644
index 000000000..967991ad3
--- /dev/null
+++ b/tests/spirv/shader-utils.slang
@@ -0,0 +1,18 @@
+#ifndef SHADER_UTILS_SLANG
+#define SHADER_UTILS_SLANG
+
+// Helper function to calculate distance from center
+float calculateDistanceFromCenter(float2 uv)
+{
+ float2 center = float2(0.5, 0.5);
+ return length(uv - center);
+}
+
+// Additional utility function - simple smoothstep fade
+float createRadialFade(float2 uv, float radius)
+{
+ float dist = calculateDistanceFromCenter(uv);
+ return smoothstep(radius, 0.0, dist);
+}
+
+#endif // SHADER_UTILS_SLANG \ No newline at end of file