diff options
| author | cheneym2 <acheney@nvidia.com> | 2025-01-14 11:22:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-14 08:22:04 -0800 |
| commit | 4da52b65ac04251ab52d713fa28146f76a84446c (patch) | |
| tree | fff3710a934dc704dc185bd78f45a998cba393d0 /source | |
| parent | 9a926058f45c7adfa569946b4f6b15a8772ac035 (diff) | |
Fix simplify if-else (#6077)
* Fix simplify if-else
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 #5972
* format code
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-ir-simplify-cfg.cpp | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/source/slang/slang-ir-simplify-cfg.cpp b/source/slang/slang-ir-simplify-cfg.cpp index 90d30dcc7..68d79617a 100644 --- a/source/slang/slang-ir-simplify-cfg.cpp +++ b/source/slang/slang-ir-simplify-cfg.cpp @@ -490,11 +490,19 @@ static bool trySimplifyIfElse(IRBuilder& builder, IRIfElse* ifElseInst) bool isFalseBranchTrivial = false; if (isTrivialIfElse(ifElseInst, isTrueBranchTrivial, isFalseBranchTrivial)) { - // If both branches of `if-else` are trivial jumps into after block, + // If either branch of `if-else` is a trivial jump into after block, // 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 = + as<IRUnconditionalBranch>(ifElseInst->getTrueBlock()->getTerminator()); + if (!termInst || (termInst->getTargetBlock() != ifElseInst->getAfterBlock())) { + termInst = as<IRUnconditionalBranch>(ifElseInst->getFalseBlock()->getTerminator()); + } + + if (termInst) + { + SLANG_ASSERT(termInst->getTargetBlock() == ifElseInst->getAfterBlock()); List<IRInst*> args; for (UInt i = 0; i < termInst->getArgCount(); i++) args.add(termInst->getArg(i)); |
