Skip to content

[SPIR-V] Fix usage of indices in subfunctions#7242

Merged
Keenuts merged 3 commits intomicrosoft:mainfrom
Keenuts:fix-indices-builtin
Mar 27, 2025
Merged

[SPIR-V] Fix usage of indices in subfunctions#7242
Keenuts merged 3 commits intomicrosoft:mainfrom
Keenuts:fix-indices-builtin

Conversation

@Keenuts
Copy link
Copy Markdown
Collaborator

@Keenuts Keenuts commented Mar 21, 2025

The parameters tagged with indices are linked to a builtin. Because their layout is different between HLSL and SPIR-V, there is a common mechanism to handle those 'stage I/O variables'.

Usually, a local variable with the correct HLSL layout is created, and when required, the value is copied in and copied out in the entrypoint wrapper. Then, a function-scoped pointer is passed to sub-functions.

The issue is that indices marks an array which is also shared across invocations. Meaning we cannot simple copy-in/copy-out. We are only allowed to write to the indices touched by the shader.

This required pushing the handling to the assignment expression handling: when a value is assigned to such builtin, the layout transformation is done, and the builtin written to.

Issue was how to find back the Builtin from an assignment: the code assumed the ParmDecl of the entrypoint was the only way to access this variable, but nothing prevents the user to pass this indice array to another function.
The simple solution is to move this out of the generic map, and have a new field which stored the SpirvVariable we created, and allow any HLSL function to access this as soon as the HLSLIndices attribute is found.

Fixes #7009

The parameters tagged with indices are linked to a builtin.
Because their layout is different between HLSL and SPIR-V, there is a
common mechanism to handle those 'stage I/O variables'.

Usually, a local variable with the correct HLSL layout is created, and
when required, the value is copied in and copied out in the entrypoint
wrapper. Then, a function-scoped pointer is passed to sub-functions.

The issue is that `indices` marks an array which is also shared across
invocations. Meaning we cannot simple copy-in/copy-out. We are only
allowed to write to the indices touched by the shader.

This required pushing the handling to the assignment expression
handling: when a value is assigned to such builtin, the layout
transformation is done, and the builtin written to.

Issue was how to find back the Builtin from an assignment: the code
assumed the ParmDecl of the entrypoint was the only way to access this
variable, but nothing prevents the user to pass this indice array to
another function.
The simple solution is to move this out of the generic map, and have
a new field which stored the SpirvVariable we created, and allow any
HLSL function to access this as soon as the HLSLIndices attribute is
found.

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

@s-perron s-perron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just needs a little clean up.

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Outdated
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Outdated
@Keenuts Keenuts merged commit 31a2f58 into microsoft:main Mar 27, 2025
12 checks passed
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Mar 27, 2025
@Keenuts Keenuts deleted the fix-indices-builtin branch September 9, 2025 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[SPIR-V] Compiler throws an exception on mesh shader compilation when one entry point calls another.

3 participants