spv/lift: elide empty targets of an if-else/switch, as allowed by SPIR-V.#28
Open
eddyb wants to merge 1 commit intoRust-GPU:mainfrom
Open
spv/lift: elide empty targets of an if-else/switch, as allowed by SPIR-V.#28eddyb wants to merge 1 commit intoRust-GPU:mainfrom
if-else/switch, as allowed by SPIR-V.#28eddyb wants to merge 1 commit intoRust-GPU:mainfrom
Conversation
LegNeato
requested changes
Sep 11, 2025
Collaborator
LegNeato
left a comment
There was a problem hiding this comment.
Do we have tests? This seems super testable.
| .then(|| targets[0]) | ||
| }; | ||
| if let Some(target_of_target) = target_is_trivial_branch { | ||
| // FIXME(eddyb) what does it mean for this to not be true? |
Collaborator
There was a problem hiding this comment.
Let's put an error or panic in here so if we hit it we are aware.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SPIR-V allows what it calls "merge edges" (edges from a structured header block, to its merge block), e.g.:
can directly use the merge block as the "else" edge of the conditional branch:
but before this PR, SPIR-T -> SPIR-V lifting was not taking advantage of that, instead emitting:
This pattern became most obvious on Rust-GPU shaders with a lot of panics from e.g. bounds-checking (which get turned into conditional "
OpReturnfrom entry point"), but not from inspecting the SPIR-V output.Instead, I ended up noticing that
spirv-opthas amerge-blockspass, which can remove the slight inefficiency from situations like the above example, but it's very slow for what it does (and it was the slowest pass inspirv-opt --time-report, for the SPIR-V module I was looking at).(if linear, that would mean around 200µs per trivial basic block, which feels absurd given how simple the check in this PR is, so I suspect it's accidentally quadratic or worse, but I haven't looked at the C++ code to confirm either)
EDIT: with this PR used in Rust-GPU, the specific testcase which prompted it (schell/renderling@d9f4d6f) has the time spent in
spirv-optshrink by ~20% (4s->3.21s), which is even better than the0.5sI was expecting.