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

[SPIR-V] Reapeated SV_DomainLocation semantics don't produce an error and can crash the compiler (works with the DXIL backend) #3737

Closed
Dredhog opened this issue May 3, 2021 · 7 comments · Fixed by #6396
Assignees
Labels
spirv Work related to SPIR-V

Comments

@Dredhog
Copy link

Dredhog commented May 3, 2021

Title

[SPIR-V] Reapeated SV_DomainLocation semantics don't produce an error and can crash the compiler (works with the DXIL backend)

Functional impact

Declaring repeated SV_DomainLocation input semantics in the same Domain shader entry point does not produce a compiler error and crashes in some cases.

Minimal repro steps

  1. Compile the following shader with dxc.exe -T ds_6_0 -E Domain -spirv repro_shader.txt
    repro_shader.txt

Expected result

The SPIR-V backend should produce error similar to that of the DXIL backend:

float4 Domain (const OutputPatch<HullOut,3> vi,
^
repro_shader.txt:4:1: error: Parameter with semantic SV_DomainLocation has overlapping semantic index at 0

Actual result

The SPIR-V backend crashes with an access violation exception:
Internal compiler error: access violation. Attempted to read from address 0x0000000000000010

Further technical details

The repro_shader.txt in the Minimal repro steps section crashes the compiler with 3 repeats, if repeating only twice it still compiles, which is still an issue as it should instead produce a compiler error (like the DXIL backend does)
Used DXC release v1.6.2104 (e09a454)

@jaebaek jaebaek added the spirv Work related to SPIR-V label May 3, 2021
@Keenuts Keenuts self-assigned this Feb 20, 2024
@Keenuts
Copy link
Collaborator

Keenuts commented Feb 26, 2024

Seems like this reproduction code hits 2 issues:

  • the semantic duplication
  • the float broadcast to a vector which fails.

For the first issue, we don't crash today, but we allow it while DXIL doesn't.
The reason is: When we find 2 input variables referencing the same semantic, we consider the second as an alias, and will merge them.

Seems like this aliasing is the cause of the second issue: some codegen relies on the different variable type, but when we alias, we might end up changing the type of bary2, and cause the type issue on broadcast.

Seems like we should not allow aliasing (to match DXIL behavior). I'll see the cleanest way to handle that.

@Keenuts
Copy link
Collaborator

Keenuts commented Feb 28, 2024

Looking a bit at this, I want do add the error.
But I'm reluctant as we did allowed aliasing input variables in the past to be compatible with FXC.

@tex3d: is it allowed or not in HLSL to use the same semantic for an input parameter twice to alias it?
If yes, is it allowed if the type of the input changes?
The HLSL spec doesn't seem to restrict it, but I do see errors listed https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/hlsl-errors-and-warnings which seems to go in the DXIL direction.

@Keenuts
Copy link
Collaborator

Keenuts commented Mar 5, 2024

Or @llvm-beanz maybe?

  • Shall input parameter aliasing be allowed?
  • If yes, can its type change, as long as it's a flat-conversion like cast? (float3 : A and float2 : A)?

My opinion is no, we should not allow input alias.

@llvm-beanz
Copy link
Collaborator

I think the rules here as implemented in DXC are complicated and inconsistent. For example both the case in this bug and your example with user-defined semantics are invalid (see: Hull Shader & Vertex Shader).

We currently only enforce this in the DXIL validator rather than in Sema, which is why the error handling between DXIL and SPIR-V are hard to keep consistent. @bob80905 worked up a feature proposal for moving this into Sema (see: microsoft/hlsl-specs#156), but we decided that wasn't justified for DXC, but we would pursue this approach in Clang.

We do also seem to allow duplicate compute shader system values (see: SV_GroupIndex & SV_GroupID). I can't think of a legitimate use case for this, but changing it would likely need to be a language change because we'd almost certainly break users in some cases.

@Keenuts
Copy link
Collaborator

Keenuts commented Mar 5, 2024

Right, btw, the validator doesn't seem to catch SV_Position & POSITION alias:
https://godbolt.org/z/aYn73joGz

Assuming this line of the documentation is right:

Note to Direct3D 9 developers: For Direct3D 9 targets, shader semantics must map to valid Direct3D 9 semantics. For backwards compatibility POSITION0 (and its variant names) is treated as SV_Position, COLOR is treated as SV_TARGET.

We do also seem to allow duplicate compute shader system values (see: SV_GroupIndex & SV_GroupID).

Seems like this is just an overlook: the shader has no input, those are grabbed from DXIL builtins, hence probably why this wasn't caught.

At least for the SPIR-V side, I'm in favor of disallow any aliasing. As long as we can say that's desirable from the HLSL language point of view.

@llvm-beanz
Copy link
Collaborator

Right, btw, the validator doesn't seem to catch SV_Position & POSITION alias: https://godbolt.org/z/aYn73joGz

Oh! This is actually kinda fascinating. It looks like if you use SV_Position, then POSITION gets treated as a user-defined semantic. That's probably also a bug.

Seems like this is just an overlook: the shader has no input, those are grabbed from DXIL builtins, hence probably why this wasn't caught.

Yep, and it is a product of relying on validation for these errors.

At least for the SPIR-V side, I'm in favor of disallow any aliasing. As long as we can say that's desirable from the HLSL language point of view.

I think that's fair, and seems consistent with the intent.

Keenuts added a commit to Keenuts/DirectXShaderCompiler that referenced this issue Mar 7, 2024
The MSDN spec is not very clear regarding input parameter
aliasing, BUT DXIL & MS agrees (See microsoft#3737) using the same
semantic annotation twice should be disallowed.

DXIL currently disallows it for most, and due to a bug, allows
some compute related semantics to be aliased.
SPIR-V did allowed aliasing, but it was a implemented as a hack causing
typing issues if the type changed between the 2 parameters using the
same semantic annotation.

To align more closely with DXIL, and the "spec", we are not disallowing
all semantic attribute reuse for input parameters.

Signed-off-by: Nathan Gauër <[email protected]>
Keenuts added a commit to Keenuts/DirectXShaderCompiler that referenced this issue Mar 7, 2024
The MSDN spec is not very clear regarding input parameter
aliasing, BUT DXIL & MS agrees (See microsoft#3737) using the same
semantic annotation twice should be disallowed.

DXIL currently disallows it for most, and due to a bug, allows
some compute related semantics to be aliased.
SPIR-V did allowed aliasing, but it was a implemented as a hack causing
typing issues if the type changed between the 2 parameters using the
same semantic annotation.

To align more closely with DXIL, and the "spec", we are not disallowing
all semantic attribute reuse for input parameters.

Fixes microsoft#3737

Signed-off-by: Nathan Gauër <[email protected]>
Keenuts added a commit to Keenuts/DirectXShaderCompiler that referenced this issue Mar 7, 2024
The MSDN spec is not very clear regarding input parameter
aliasing, BUT DXIL & MS agrees (See microsoft#3737) using the same
semantic annotation twice should be disallowed.

DXIL currently disallows it for most, and due to a bug, allows
some compute related semantics to be aliased.
SPIR-V did allowed aliasing, but it was a implemented as a hack causing
typing issues if the type changed between the 2 parameters using the
same semantic annotation.

To align more closely with DXIL, and the "spec", we are not disallowing
all semantic attribute reuse for input parameters.

Fixes microsoft#3737

Signed-off-by: Nathan Gauër <[email protected]>
Keenuts added a commit to Keenuts/DirectXShaderCompiler that referenced this issue Mar 7, 2024
The MSDN spec is not very clear regarding input parameter
aliasing, BUT DXIL & MS agrees (See microsoft#3737) using the same
semantic annotation twice should be disallowed.

DXIL currently disallows it for most, and due to a bug, allows
some compute related semantics to be aliased.
SPIR-V did allowed aliasing, but it was a implemented as a hack causing
typing issues if the type changed between the 2 parameters using the
same semantic annotation.

To align more closely with DXIL, and the "spec", we are not disallowing
all semantic attribute reuse for input parameters.

Fixes microsoft#3737

Signed-off-by: Nathan Gauër <[email protected]>
Keenuts added a commit that referenced this issue Mar 8, 2024
The MSDN spec is not very clear regarding input parameter aliasing, BUT
DXIL & MS agrees (See #3737) using the same semantic annotation twice
should be disallowed.

DXIL currently disallows it for most, and due to a bug, allows some
compute related semantics to be aliased.
SPIR-V did allowed aliasing, but it was a implemented as a hack causing
typing issues if the type changed between the 2 parameters using the
same semantic annotation.

To align more closely with DXIL, and the "spec", we are now disallowing
all semantic attribute reuse for input parameters.

Fixes #3737

Signed-off-by: Nathan Gauër <[email protected]>
@Keenuts
Copy link
Collaborator

Keenuts commented Mar 11, 2024

Right, btw, the validator doesn't seem to catch SV_Position & POSITION alias: https://godbolt.org/z/aYn73joGz

Oh! This is actually kinda fascinating. It looks like if you use SV_Position, then POSITION gets treated as a user-defined semantic. That's probably also a bug.

Seems like this is just an overlook: the shader has no input, those are grabbed from DXIL builtins, hence probably why this wasn't caught.

Yep, and it is a product of relying on validation for these errors.

At least for the SPIR-V side, I'm in favor of disallow any aliasing. As long as we can say that's desirable from the HLSL language point of view.

I think that's fair, and seems consistent with the intent.

@tex3d @pow2clk the other case for SV_Position

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spirv Work related to SPIR-V
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants