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

Fix simplify if-else #6077

Merged
merged 4 commits into from
Jan 14, 2025
Merged

Fix simplify if-else #6077

merged 4 commits into from
Jan 14, 2025

Conversation

cheneym2
Copy link
Collaborator

@cheneym2 cheneym2 commented Jan 13, 2025

The if-else optimization observes that at if at least one true/false block is merely an unconditional jump to the after block, and both are "trivial", that the whole if-else can be replaced with a jump to the after block. But it's important to copy the phi arguments from the aforementioned unconditional jump, rather than what is present in the 'true' block, since the 'true' block might actually just be the after block itself.

Below, the ifElse() would be replaced with an unconditional jump to block %39, but with the phi arguments copied from the branch to %29, which is an unrelated block.

ifElse(%38, %39, %40, %39)

block %40:
unconditionalBranch(%39)

block %39:
unconditionalBranch(%29, 0 : Float)

block %29(
[nameHint("ret")]
param %ret : Float):

Fixes issue #5972

@cheneym2 cheneym2 requested a review from a team as a code owner January 13, 2025 22:41
// we can get rid of the entire conditional branch and replace it
// with a jump into the after block.
if (auto termInst = as<IRUnconditionalBranch>(ifElseInst->getTrueBlock()->getTerminator()))
IRUnconditionalBranch* termInst;
if (termInst = as<IRUnconditionalBranch>(ifElseInst->getTrueBlock()->getTerminator()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

style: fold the initialization to the declaration site of termInst, and just do if (termInst & termInst->getTargetBlock() != ifElseInst->getAfterBlock())

@cheneym2 cheneym2 added the pr: non-breaking PRs without breaking changes label Jan 14, 2025
The if-else optimization observes that at if at least one
true/false block is merely an unconditional jump to the
after block, that the whole if-else can be replaced with
a jump to the after block. But it's important to copy
the phi arguments from the aforementioned unconditional
jump, rather than what is present in the 'true' block,
since the 'true' block might actually just be the after
block itself.

Below, the ifElse() would be replaced with an unconditional
jump to block %39, but with the `phi` arguments copied from
the branch to %29, which is an unrelated block.

ifElse(%38, %39, %40, %39)

block %40:
    unconditionalBranch(%39)

block %39:
    unconditionalBranch(%29, 0 : Float)

block %29(
        [nameHint("ret")]
        param %ret  : Float):

Fixes issue shader-slang#5972
@cheneym2
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

@csyonghe csyonghe merged commit 4da52b6 into shader-slang:master Jan 14, 2025
15 of 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