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

WGSL: Fix issue where global calls are generated #5768

Merged
merged 13 commits into from
Dec 12, 2024

Conversation

aleino-nv
Copy link
Collaborator

  • Refactor the SPIR-V inlining legalization code so that it's reusable by other targets
  • Apply said legalization code for WGSL
  • Extend the legalization code such that globals left unused after inlining are removed

The last step (also the last commit) gets rid of the global call and fixes the issue.

#5607

@aleino-nv aleino-nv requested a review from a team as a code owner December 5, 2024 08:57
@aleino-nv aleino-nv added the pr: non-breaking PRs without breaking changes label Dec 5, 2024
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from 254d4da to 9a44009 Compare December 5, 2024 09:00
@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

slangbot commented Dec 5, 2024

🌈 Formatted, please merge the changes from this PR

@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

slangbot commented Dec 5, 2024

🌈 Formatted, please merge the changes from this PR

aleino-nv added a commit to aleino-nv/slang that referenced this pull request Dec 5, 2024
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from f2f9e56 to fc18647 Compare December 5, 2024 10:48
@aleino-nv
Copy link
Collaborator Author

aleino-nv commented Dec 5, 2024

Some tests were failing:

  • tests/spirv/global-compute.slang fails for SPIR-V, seemingly because OpEntryPoint is removed
    • I have a hacky workaround and a TODO comment for this, at the moment.
    • It would be good to understand why things fail for SPIR-V without that workaround...
  • Some WGSL tests failed because the inlining code produced IRGlobalValueRef which were not handled during emit.
    • I added some code for this

@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

slangbot commented Dec 5, 2024

🌈 Formatted, please merge the changes from this PR

aleino-nv added a commit to aleino-nv/slang that referenced this pull request Dec 5, 2024
@aleino-nv aleino-nv requested a review from csyonghe December 5, 2024 11:44
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from d8abcde to e57ab0e Compare December 10, 2024 06:53
@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

aleino-nv added a commit to aleino-nv/slang that referenced this pull request Dec 10, 2024
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from fce390b to 7949b34 Compare December 11, 2024 12:57
@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

@aleino-nv aleino-nv requested a review from csyonghe December 11, 2024 14:06
@aleino-nv
Copy link
Collaborator Author

I got a test timeout...

  internal/renderpasses/test_InternalPathTracerVolumeTransmittance_vulkan : FAILED (600.0 s)
    Process killed due to timeout
    View test at: http://SlangWin5:8080//unknown/internal/renderpasses/test_InternalPathTracerVolumeTransmittance_vulkan

@aleino-nv aleino-nv enabled auto-merge (squash) December 11, 2024 14:42
// after inlining.
for (auto& globalInst : globalInstsToConsiderDeleting)
{
if (!hasDescendantSatisfyingPredicate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a band aid instead of an actual fix.

As we discussed in the meeting, the true problem is when we clone the global SpirvASM inst to a function, we didn't update the operand references to point to the cloned version of the local spirv operand insts, as a result, the inlined asm block is still referencing the child insts in the original global asm block.

The fix here just prevents removal of the global asm block, but the right fix is to make sure we dont have such inter block references back to the original global asm block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I must have misunderstood something.

Yesterday I stepped through the inlining code in the debugger and noticed that the SPIRVAsmOperandInsts didn't get inlined (unlike all of the other operands) and so there was no clone to be found.

I figured this is something that can happen in general, and so we must check for uses recursively before deleting the global inst.

So you are saying that the existing inlining logic is broken?

I will take another look.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@csyonghe Should the fix be that, when we decide to inline SPIRV ASM insts then we force-inline things it contains, like the SPIRVAsmOperandInsts etc.. ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

When we looked through the code together, it appears to me that the SPIRVAsm parent inst is being cloned, and its children are also being cloned. It is just the operands of the clone seems to be still referencing the original ones instead of the cloned ones.

e.g.

%b = SPIRV_Asm {
    %x = SPIRVAsmOperandInst ...
    %y = SPIRVAsmOperandInst ...
     %z = SPIRVAsmInst %x %y
}

got cloned into main as:

func %main  {
%block = block {
    %b' = SPIRV_Asm {
       %x' = SPIRVAsmOperandInst ...
       %y' = SPIRVAsmOperandInst ...
        %z' = SPIRVAsmInst %x %y // !!! should be %x' here, instead of %x
    }
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I think I see now!

The SPIRVAsmOperands are special in that they are parented to the SPIRVAsm which we would like to delete.

The inlineInst function does indeed inline its children, but then later when we get to the last child which is a SPIRVAsmInst taking SPIRVAsmOperands as operands, we decide not to inline them.

I see that there is a dictionary called m_mapGlobalInstToShouldInline that is first consulted.
Perhaps whenever we inline some inst we should set it as true in m_mapGlobalInstToShouldInline...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I think I see now!

The SPIRVAsmOperands are special in that they are parented to the SPIRVAsm which we would like to delete.

The inlineInst function does indeed inline its children, but then later when we get to the last child which is a SPIRVAsmInst taking SPIRVAsmOperands as operands, we decide not to inline them.

I see that there is a dictionary called m_mapGlobalInstToShouldInline that is first consulted. Perhaps whenever we inline some inst we should set it as true in m_mapGlobalInstToShouldInline...

That works, but only because the SPIRVAsmInst taking these SPIRVAsmOperandInst's as operands happen to appear after the SPIRVAsmOperandInst's children of the SPIRVAsm...

If I instead mark m_mapGlobalInstToShouldInline to true for any inlined insts first thing in inlineInst, and then I can change shouldInlineInstImpl to return true in case any ancestors of the inst in question should be inlined according to m_mapGlobalInstToShouldInline.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did something similar to the above, and it seems to work.

@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from 9374e2f to 92b0199 Compare December 12, 2024 11:59
@aleino-nv
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

aleino-nv added a commit to aleino-nv/slang that referenced this pull request Dec 12, 2024
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch 2 times, most recently from 4d95af8 to 215ddc4 Compare December 12, 2024 13:03
@aleino-nv aleino-nv requested a review from csyonghe December 12, 2024 13:22
@aleino-nv aleino-nv force-pushed the aleino/global-call-fix branch from 215ddc4 to 82e0928 Compare December 12, 2024 17:36
Copy link
Collaborator

@csyonghe csyonghe left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@aleino-nv aleino-nv merged commit 9c82ed3 into shader-slang:master Dec 12, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants