Fix DebugGlobalVariable source file assignment for included/imported files #9114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DebugGlobalVariablefor synthesized entry point parameters (e.g.,input_texCoord,entryPointParam_main) incorrectly references included/imported files instead of the main shader file.Changes
IRDebugLocationDecorationsource and use it as the preferredm_defaultDebugSourceDebugSourcefrom imported modules as included during cloninggetOrEmitDebugSourceas includedRoot Cause
For
importsyntax, imported modules are compiled separately with theirDebugSourcehavingisIncludedFile=false. When linked, this flag is preserved, causing the imported module's source to potentially becomem_defaultDebugSource.Example
Before fix:
After fix:
Tests Added
tests/spirv/debug-global-variable-source.slang(#includecase)tests/spirv/debug-global-variable-source-import.slang(importcase)Original prompt
This section details on the original issue you should resolve
<issue_title>Source of DebugGlobalVariable is set to the wrong slang file</issue_title>
<issue_description># Issue Description
When the slang shader includes a header file, for global variable in the main slang file, Source of DebugGlobalVariable is set to the header file, which is wrong.
Reproducer Code
modular_slang_shader.slang
shader_utils.slang
With "slangc.exe modular_slang_shader.slang -target spirv-asm -gdwarf -g3 -O0 -line-directive-mode glsl -stage fragment -o modular_slang_shader_g2_o0_non_sem.spvasm -entry main -fvk-b-shift 0 0", in the generated modular_slang_shader_g2_o0_non_sem.spvasm file, we have:
; Debug Information
%1 = OpString "#ifndef SHADER_UTILS_SLANG
#define SHADER_UTILS_SLANG
// Helper function to calculate distance from center
float calculateDistanceFromCenter(float2 uv)
...
#endif // SHADER_UTILS_SLANG
"
%5 = OpString "D:\p4\bugfix_main\out_nvn2\wddm2_amd64_debug\shader_utils.slang"
%6 = OpString "#include "shader_utils.slang"
struct VertexOutput
{
float4 position : SV_Position;
float2 texCoord : TEXCOORD0;
}
...
"
%8 = OpString "D:\p4\bugfix_main\out_nvn2\wddm2_amd64_debug\modular_slang_shader.slang"
...
%4 = OpExtInst %void %2 DebugSource %5 %1
%7 = OpExtInst %void %2 DebugSource %8 %6
...
%input_texCoord = OpVariable %_ptr_Input_v2float Input ; Location 0
%70 = OpExtInst %void %2 DebugGlobalVariable %71 %51 %4 %uint_0 %uint_0 %13 %71 %%uint_0
%entryPointParam_main = OpVariable %_ptr_Output_v4float Output ; Location 0
%182 = OpExtInst %void %2 DebugGlobalVariable %183 %46 %4 %uint_0 %uint_0 %13 %183 %entryPointParam_main %uint_0
Expected Behavior
Source of DebugGlobalVariable (input_texCoord, entryPointParam_main) should be %7, the main slang shader file.
Actual Behavior
Source of DebugGlobalVariable is %4, the included shader file.
Environment
Additional context
The source file for global variable is still wrong after #7923 is fixed.
</issue_description>
Comments on the Issue (you are @copilot in this section)
@lujinwangnv @claude Could you help investigate and fix the issue? @lujinwangnv @claude help: would your current fix take care of global variables in the included file? would the source of DebugGlobalVariable for those global variables be the included file? @lujinwangnv @claude help: can you investigate into the AST-to-IR lowering phase where IRDebugLocationDecoration is initially assigned to root-cause the issue? @lujinwangnv I checked the change AI proposed, it might not work for all cases. Need some local testing and debugging to verify the change. @lujinwangnv This issue and https://github.com//issues/7923 (fixed with PR https://github.com//pull/7957, closed) both still repro with import ed files. @zangold-nv Assigning copilot to see if it can extend the fixes for this issue and issue #7923 to cover the `import "shader_utils.slang";` syntax case.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.