Skip to content

[SPIR-V] Invalid spir-v generated when using nointerpolation keyword #5270

Closed

Description

Hello, when compiling the following drastically reduced HLSL example to spir-v with dxc.exe -spirv -fspv-target-env=vulkan1.1 -fvk-use-dx-layout -O3 -T ps_6_0 -E psMain nointerpolationbug.hlsl:

struct PSInput
{
	nointerpolation int level : LEVEL;
};

int psMain(PSInput input) : SV_Target0
{
	return input.level;
}

I get the following error:

fatal error: generated SPIR-V is invalid: Expected Result Type to be a composite type
  %18 = OpCompositeConstruct %int %17 %12 %12

This occurs on the current latest nightly build against commit e0a2965, it does not occur using an earlier build against commit 740dd09.

Activity

  1. jmccarthy634 commented on Jun 7, 2023

    @jmccarthy634
    Author

    Chow (@ShchchowAMD) this appears to have started happening with commit cd3b40b

    Going through more of our shader builds and it definitely seems to affect everywhere we use nointerpolation, with results varying from something like the above to straight up crashing inside the optimizer in spvtools.

  2. ShchchowAMD commented on Jun 7, 2023

    @ShchchowAMD
    Contributor

    Thanks for reporting this issue.

    Ref to comment in the commit: #4638 (comment)

    For latest KHR extension, following SV_BaryCentrics description in wiki (https://github.com/microsoft/DirectXShaderCompiler/wiki/SV_Barycentrics):

    For pixel shaders to perform attribute interpolation using the system-generated barycentric weights, the attributes values at the vertices must be provided. These can be accessed by declaring the attribute as nointerpolation, and using new intrinsics to read the values at the 3 vertices.

    So I think an inputs decorated with 'nointerpolation' should be treated as it has an extra dimension, indicating which vertices it mapped to. For the error reported here, its type is treated as an int[3] now.

    Steven Perron (@s-perron) Nathan Gauër (@Keenuts)
    I think this is the case we discussed before, like, whether 'nointerpolation' inputs could be used outside GetAttributeAtVertex() in a function body?

    I'm not sure whether above case is legal in KHR extension, like the case in discussion before:

    float4 main(nointerpolation float4 col : COLOR) : SV_Target {
         return GetAttributeAtVertex(col, 2) + col;
    }
    

    If we could make sure it should only be used in such intrinsics, I'll add error message in compile result info.

  3. s-perron commented on Jun 12, 2023

    @s-perron
    Collaborator

    Yes, we did discuss this before. This case cannot be handled in Vulkan because the spir-v cannot correctly represent it. The spir-v validator message is not useful. If we can, we should try to add an error message before we generate the spir-v.

    Chow (@ShchchowAMD) Are you able to do that?

  4. ShchchowAMD commented on Jun 12, 2023

    @ShchchowAMD
    Contributor

    Yea. I'll make a new commit for this.

  5. handsomematt commented on Jun 12, 2023

    @handsomematt

    Also been getting issues due to inappropriate usage of nointerpolation but from an LLVM assert Error: assert(new_array_type != nullptr && "Can't copy an array to a non-array.") without it spitting out any errors about invalid spir-v, making it initially difficult to narrow down the troublesome code.

    // dxc.exe -T ps_6_0 -E MainPs -spirv
    // Internal compiler error: LLVM Assert
    
    struct PS_INPUT
    {
    	nointerpolation uint nView : TEXCOORD12;
    };
    
    Texture2D g_tBlueNoise : register( t0 );
    
    uint2 TextureDimensions2D( Texture2D tex, uint nMipLevel )
    {
    	uint2 nDim;
    	uint nLevels;
    	tex.GetDimensions( nMipLevel, nDim.x, nDim.y, nLevels );
    	return nDim;
    }
    
    float4 MainPs( PS_INPUT i ) : SV_Target0
    {
    	uint nView = i.nView;
    	TextureDimensions2D( g_tBlueNoise, 0 );
    	return float4( 1.0, 1.0, 1.0, 1.0 );
    }
  6. danginsburg commented on Jun 13, 2023

    @danginsburg
    Contributor

    Yea. I'll make a new commit for this.

    Matt Stevens (@handsomematt) Steven Perron (@s-perron) - I might not be fully understanding the discussion here, but can you confirm you will be fixing the regression with the case Matt Stevens (@handsomematt) mentions above. We have this in our shaders too, and this has worked for a very long time, so I presume this is just a regression due to your change?

    I presume nointerpolation on a PS input should just map to the SPIR-V Flat decoration as it always has - should be fully represenable in SPIR-V?

  7. s-perron commented on Jun 13, 2023

    @s-perron
    Collaborator

    I cannot find what HLSL is supposed to do if you access a nointerpolation input without using GetAttributeAtVertex. Is there anything that specifies what it does?

    This is all that I could find.

    https://github.com/microsoft/DirectXShaderCompiler/wiki/SV_Barycentrics#per-vertex-attributes

  8. danginsburg commented on Jun 13, 2023

    @danginsburg
    Contributor

    I cannot find what HLSL is supposed to do if you access a nointerpolation input without using GetAttributeAtVertex. Is there anything that specifies what it does?

    This is all that I could find.

    https://github.com/microsoft/DirectXShaderCompiler/wiki/SV_Barycentrics#per-vertex-attributes

    The case that regressed is a PS input, not VS input. It should be a Flat interpolation modifier. I don’t think there is any ambiguity about that? It used to work.

  9. ShchchowAMD commented on Jun 13, 2023

    @ShchchowAMD
    Contributor

    Dan Ginsburg (@danginsburg) From the spec, GetAttributeAtVertex is used in PS. We now support this intrinsic so nointerpolation input will be treated as an array. Its index will represent VertexID, indicating where this value comes from / which vertex it is representing now. We are assuming it won't be used outside this intrinsics.

    Matt Stevens (@handsomematt) I'll check this. Another question for this example is just as first comment to Dan Ginsburg (@danginsburg) here.

  10. danginsburg commented on Jun 13, 2023

    @danginsburg
    Contributor

    Ok. I am confused. The SV_Barycentric proposal changes the meaning of nointerpolation PS inputs for all shader models? Shouldn’t it be opt in somehow? This has been supported since SM5 and has been in the spir-v backend for a long time https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#id66

  11. handsomematt commented on Jun 13, 2023

    @handsomematt

    Just for further clarity here's the expected output of this code before cd3b40b and the error we're getting after, the assert I posted earlier is an additional error with the same root problem.

    struct PS_INPUT
    {
    	nointerpolation uint nView : TEXCOORD12;
    };
    
    uint MainPs( PS_INPUT i ) : SV_Target0
    {
    	return i.nView;
    }
    
    // Before cd3b40b
    // dxc.exe -T ps_6_0 -E MainPs -spirv
    // ...
    //               OpName %MainPs "MainPs"
    //               OpDecorate %in_var_TEXCOORD12 Flat
    //               OpDecorate %in_var_TEXCOORD12 Location 0
    // ...
    
    // After cd3b40b
    // dxc.exe -T ps_6_0 -E MainPs -spirv
    // fatal error: generated SPIR-V is invalid: Expected Result Type to be a composite type
    //  %17 = OpCompositeConstruct %uint %16 %11 %11 
  12. s-perron commented on Jun 13, 2023

    @s-perron
    Collaborator

    I'll review the specs, and get back to you.

  13. added
    bug
    spirv
    on Jun 13, 2023
  14. danginsburg commented on Jun 13, 2023

    @danginsburg
    Contributor

    Here are some observations I have from poking at it:

    • ps_6_0 in DXIL does not allow GetAttributeAtVertex, it gives this error:
    error: Opcode AttributeAtVertex not valid in shader model ps_6_0.
    note: at '%4 = call float @dx.op.attributeAtVertex.f32(i32 137, i32 0, i32 0, i8 3, i8 1)' in block '#0' of function 'PSMain'.
    Validation failed.
    

    I would propose, therefore, that for the purposes of SPIR-V:

    • ps_6_0 should not allow barycentrics and should continue to generate the same code for naked interpolation qualifiers
    • Vulkan barycentrics should only been enabled at ps_6_1 AND the inclusion of barycentrics in the shader code

    That seems to be what DX12 is doing from what I can tell.

  15. 11 remaining items

  16. s-perron commented on Jun 30, 2023

    @s-perron
    Collaborator

    Chow (@ShchchowAMD) Sorry, but I will be on you a lot about this. People keep reporting new issues related to this. This is a serious regression. If we cannot get the fix soon, should we revert your change? Then you can put it back in when you have to fix ready.

  17. danginsburg commented on Jun 30, 2023

    @danginsburg
    Contributor

    Yeah, we are stuck not being able to take newer version of DXC until this gets fixed. I don't want to make extra work, but if it's not going to be addressed soon I agree it should be reverted. I wouldn't want this to go into an official release. It's broken for most of our pixel shaders, and I imagine many other people's as well.

  18. ShchchowAMD commented on Jul 1, 2023

    @ShchchowAMD
    Contributor

    Hi perron,
    I'll update a fix today or tomorrow ASAP. I'm refining current new codes.
    Apologize again for the regression.

  19. Goshido commented on Jul 1, 2023

    @Goshido

    Are there any autotests to prevent such breaking changes in the future?

  20. jmccarthy634 commented on Jul 2, 2023

    @jmccarthy634
    Author

    Thanks everyone for your responses, I would also agree that this change should probably be reverted - even if a fix is forthcoming, it seems like it might be a better idea to instead have some test coverage (as suggested by Goshido Matazuki (@Goshido) above) alongside a fixed version of the original change instead?

  21. ShchchowAMD commented on Jul 3, 2023

    @ShchchowAMD
    Contributor

    Steven Perron (@s-perron) Please revert my previous commit. Current fix spent more time than expected.

    I'll add more test coverages before making a new PR.
    For some corner cases, I'll raise a new issue ticket for discussion.

    Sorry for this regression again.

  22. s-perron commented on Jul 3, 2023

    @s-perron
    Collaborator

    Nathan Gauër (@Keenuts) Cassandra Beckley (@cassiebeckley) I'm off today can one of you take care of the revert? If not I'll do it tomorrow.

  23. Keenuts commented on Jul 3, 2023

    @Keenuts
    Collaborator

    Sure, but everybody is OOO for the next 2 days (except me in Europe 🙃) so might not get the approvals for revert before Wednesday.

  24. Goshido commented on Jul 4, 2023

    @Goshido

    DirectX Shader Compiler v1.7.2212.10216 2096d1b8054d1de864924ef0c5009f533597b9d4 does not have the issue anymore.

    Thanks 👍

  25. jmccarthy634 commented on Jul 4, 2023

    @jmccarthy634
    Author

    Thanks all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugspirv

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions