Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop inside shader causes duplication of nointerpolation keyword in generated HLSL code #4922

Closed
zopsicle opened this issue Aug 27, 2024 · 2 comments · Fixed by #4964
Closed
Assignees
Labels
goal:client support Feature or fix needed for a current slang user. kind:bug something doesn't work like it should

Comments

@zopsicle
Copy link
Contributor

zopsicle commented Aug 27, 2024

Consider the following shader:

[shader("pixel")]
void main(out float4 SV_Target : SV_Target, in nointerpolation float4 color : color)
{
    for (int i = 0; i < 10; ++i)
        ;
    SV_Target = color;
}

Let's compile it to HLSL:

$ slangc -entry main -profile sm_5_0 -o shader.hlsl shader.slang

And look at the generated code:

#pragma pack_matrix(column_major)
#ifdef SLANG_HLSL_ENABLE_NVAPI
#include "nvHLSLExtns.h"
#endif

#ifndef __DXC_VERSION_MAJOR
    // warning X3557: loop doesn't seem to do anything, forcing loop to unroll
    #pragma warning(disable: 3557)
#endif


#line 2 "shader.slang"
void main(out float4 SV_Target_0 : SV_Target, nointerpolation nointerpolation nointerpolation nointerpolation nointerpolation float4 color_0 : color)
{


    SV_Target_0 = color_0;
    return;
}


#line 20419 "hlsl.meta.slang"
struct NullDifferential_0
{
    uint dummy_0;
};

The nointerpolation keyword is repeated five times in the generated code. FXC will reject the shader with the error message "duplicate usages specified". Removing the loop from the shader fixes the problem:

[shader("pixel")]
void main(out float4 SV_Target : SV_Target, in nointerpolation float4 color : color)
{
    SV_Target = color;
}
#pragma pack_matrix(column_major)
#ifdef SLANG_HLSL_ENABLE_NVAPI
#include "nvHLSLExtns.h"
#endif

#ifndef __DXC_VERSION_MAJOR
    // warning X3557: loop doesn't seem to do anything, forcing loop to unroll
    #pragma warning(disable: 3557)
#endif


#line 2 "shader.slang"
void main(out float4 SV_Target_0 : SV_Target, nointerpolation float4 color_0 : color)
{
    SV_Target_0 = color_0;
    return;
}


#line 20419 "hlsl.meta.slang"
struct NullDifferential_0
{
    uint dummy_0;
};

Tested with Slang 2024.10. It does not appear to matter what is inside the body of the loop; there just being a loop anywhere causes nointerpolation keywords to be repeated.

@ArielG-NV ArielG-NV added kind:bug something doesn't work like it should goal:client support Feature or fix needed for a current slang user. labels Aug 27, 2024
@csyonghe csyonghe added this to the Q3 2024 (Summer) milestone Aug 27, 2024
@jkwak-work
Copy link
Collaborator

The issue is observed at the "POST IR VALIDATION" level when IR is dumped with "-dump-ir".
Still investigating.

@jkwak-work
Copy link
Collaborator

It appears that a same set of decoration is added to a IRParam for some reason.
And this appears to happen not only for the nointerpolation modifier but also for other modifiers too.

### POST IR VALIDATION:
[layout(%3)]
func %main      : Func(Void, Out(Vec(Float, 4 : Int)), Vec(Float, 4 : Int))
{
block %23(
                [layout(%18)]
                [nameHint("SV_Target")]
                [semantic("SV_Target", 0 : Int)]
                [output]
                [nameHint("SV_Target")]
                [semantic("SV_Target", 0 : Int)]
                [output]
                [nameHint("SV_Target")]
                [semantic("SV_Target", 0 : Int)]
                [output]
                [nameHint("SV_Target")]
                [semantic("SV_Target", 0 : Int)]
                [output]
                [nameHint("SV_Target")]
                [semantic("SV_Target", 0 : Int)]
                [output]
                param %SVx5FTarget      : Out(Vec(Float, 4 : Int)),
                [layout(%11)]
                [nameHint("color")]
                [semantic("color", 0 : Int)]
                [interpolationMode(2 : Int)]
                [input]
                [nameHint("color")]
                [semantic("color", 0 : Int)]
                [interpolationMode(2 : Int)]
                [input]
                [nameHint("color")]
                [semantic("color", 0 : Int)]
                [interpolationMode(2 : Int)]
                [input]
                [nameHint("color")]
                [semantic("color", 0 : Int)]
                [interpolationMode(2 : Int)]
                [input]
                [nameHint("color")]
                [semantic("color", 0 : Int)]
                [interpolationMode(2 : Int)]
                [input]
                param %color    : Vec(Float, 4 : Int)):
        [loopMaxIters(10 : Int)]
        loop(%24, %25, %26, 0 : Int)

I haven't been able to locate where the bug is yet.

jkwak-work added a commit to jkwak-work/slang that referenced this issue Aug 29, 2024
Closes shader-slang#4922

The problem was that same decorations were added to an IRParam multiple
times while running `specializeIRForEntryPoint()`.

`cloneGlobalValueWithCodeCommon()` kept cloning decorations for the
params that were already processed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:client support Feature or fix needed for a current slang user. kind:bug something doesn't work like it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants