Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

DebugGlobalVariable for synthesized entry point parameters (e.g., input_texCoord, entryPointParam_main) incorrectly references included/imported files instead of the main shader file.

Changes

  • SPIRV emit: Pre-scan entry points to extract their IRDebugLocationDecoration source and use it as the preferred m_defaultDebugSource
  • SPIRV emit: Only fall back to non-included files when no entry point source is available
  • IR linking: Mark DebugSource from imported modules as included during cloning
  • Lower-to-IR: Mark dynamically loaded sources via getOrEmitDebugSource as included

Root Cause

For import syntax, imported modules are compiled separately with their DebugSource having isIncludedFile=false. When linked, this flag is preserved, causing the imported module's source to potentially become m_defaultDebugSource.

Example

Before fix:

%4 = OpExtInst %void %2 DebugSource %5 %1   ; shader-utils-module.slang
%14 = OpExtInst %void %2 DebugSource %15 %13 ; main.slang
%76 = OpExtInst %void %2 DebugGlobalVariable ... %4 ...  ; Wrong: references imported file

After fix:

%76 = OpExtInst %void %2 DebugGlobalVariable ... %14 ...  ; Correct: references main file

Tests Added

  • tests/spirv/debug-global-variable-source.slang (#include case)
  • tests/spirv/debug-global-variable-source-import.slang (import case)
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

#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;
}

shader_utils.slang

#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

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

  • Slang Version (v2025.17.1)
  • OS (Windows)

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.

Copilot AI and others added 3 commits November 24, 2025 20:58
… import

Co-authored-by: zangold-nv <242329104+zangold-nv@users.noreply.github.com>
… debug location

Co-authored-by: zangold-nv <242329104+zangold-nv@users.noreply.github.com>
Co-authored-by: zangold-nv <242329104+zangold-nv@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DebugGlobalVariable source in slang shader Fix DebugGlobalVariable source file assignment for included/imported files Nov 24, 2025
Copilot AI requested a review from zangold-nv November 24, 2025 21:11
Copilot finished work on behalf of zangold-nv November 24, 2025 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Source of DebugGlobalVariable is set to the wrong slang file

2 participants