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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/slang/slang-ir-simplify-cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
26 changes: 26 additions & 0 deletions tests/bugs/simplify-if-else.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//TEST:SIMPLE(filecheck=CHECK): -stage compute -entry computeMain -target hlsl
//CHECK: computeMain

//TEST_INPUT:ubuffer(data=[9 9 9 9], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer;

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
vector<float32_t, 4> vvv = vector<float32_t, 4>(0);
float32_t ret = 0.0f;
if (vvv.y < 1.0f)
{
ret = 1.0f;
}
else
{
if (vvv.y > 1.0f && outputBuffer[3] == 3)
{
ret = 0.0f;
} else {
if (true) {}
}
}
outputBuffer[int(dispatchThreadID.x)] = int(ret);
}
Loading