Skip to content

spirv-opt: CreateRemoveUnusedInterfaceVariablesPass should preserve interface if asked to#6674

Open
uwx wants to merge 1 commit into
KhronosGroup:mainfrom
uwx:patch/preserve-interface
Open

spirv-opt: CreateRemoveUnusedInterfaceVariablesPass should preserve interface if asked to#6674
uwx wants to merge 1 commit into
KhronosGroup:mainfrom
uwx:patch/preserve-interface

Conversation

@uwx

@uwx uwx commented May 6, 2026

Copy link
Copy Markdown

This fixes #6673: CreateRemoveUnusedInterfaceVariablesPass was not respecting the preserve_interface flag passed in and collapsing unused attributes. The simple test case in microsoft/DirectXShaderCompiler#7982 now preserves both TEXCOORD0 and TEXCOORD1 with this patch applied:

               OpName %in_var_TEXCOORD0 "in.var.TEXCOORD0"
               OpName %in_var_TEXCOORD1 "in.var.TEXCOORD1"

I chose to pass the argument in instead of not registering the pass because that's what's done for the other passes, even though for this one it makes it a total no-op. Let me know if that is inadequate.

@uwx uwx changed the title Fix: CreateRemoveUnusedInterfaceVariablesPass should preserve interface if asked to spirv-opt: CreateRemoveUnusedInterfaceVariablesPass should preserve interface if asked to May 6, 2026
@CLAassistant

CLAassistant commented May 6, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@uwx

uwx commented May 7, 2026

Copy link
Copy Markdown
Author

Oh, I just noticed clang-format touched a bunch of code that I didn't edit. Let me know if it needs to be undone.


RemoveUnusedInterfaceVariablesPass::Status
RemoveUnusedInterfaceVariablesPass::Process() {
if (preserve_interface_) return Status::SuccessWithoutChange;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is the correct solution. We need to consider all of the uses cases.

This pass was intended to cover the cases where there were multiple entry points. If a variables is used one entry point, and not in another, we do not want it on the OpEntryPoint of the one in which it is not used. Based on the comment from the PR that added the pass, the issue was that some variables are added to the OpEntryPoint for simplicity by front-ends that do not know which ones to add (See microsoft/DirectXShaderCompiler#4621).

The other issue is that the Preserve interface flag does stops the removal of the input and output variables. However, we should still remove the resource variables, which is covered by another flag.

Can you provide more details on exactly what you want out of this?

@dnovillo is also interested in this flag, getting the use cases right might help us get something that works in all cases.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Basically our issue in SDL_gpu is that we need the byte for byte structure of the vertex attributes to be the same as what's declared in the shader otherwise we're uploading incorrect vertex data to the GPU. So we need to keep unused variables. cc @thatcosmonaut

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's right, we're experiencing gotchas because unused variables are optimized out and then pipeline vertex definitions don't match the compiled shader.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it works if the option applied to inputs and outputs that are not builtin? That is, we still remove builtin inputs. The other problem we need to fix is that some builtins get added to shader stages where they are not allowed. We have to remove them.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That would probably work.

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.

CreateRemoveUnusedInterfaceVariablesPass removes variables even if preserve_interface=true

4 participants