From 4da52b65ac04251ab52d713fa28146f76a84446c Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Tue, 14 Jan 2025 11:22:04 -0500 Subject: 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 --- source/slang/slang-ir-simplify-cfg.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'source/slang') 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(ifElseInst->getTrueBlock()->getTerminator())) + IRUnconditionalBranch* termInst = + as(ifElseInst->getTrueBlock()->getTerminator()); + if (!termInst || (termInst->getTargetBlock() != ifElseInst->getAfterBlock())) { + termInst = as(ifElseInst->getFalseBlock()->getTerminator()); + } + + if (termInst) + { + SLANG_ASSERT(termInst->getTargetBlock() == ifElseInst->getAfterBlock()); List args; for (UInt i = 0; i < termInst->getArgCount(); i++) args.add(termInst->getArg(i)); -- cgit v1.2.3