Skip to content

Commit 2144274

Browse files
Clean up conditional branch codegen.
Should only need to look at whether or not we're branching to our own merge target. Any other branch needs to emit code in some way.
1 parent 54cc0b0 commit 2144274

1 file changed

Lines changed: 9 additions & 59 deletions

File tree

spirv_glsl.cpp

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12732,87 +12732,37 @@ void CompilerGLSL::branch(BlockID from, uint32_t cond, BlockID true_block, Block
1273212732
auto &from_block = get<SPIRBlock>(from);
1273312733
BlockID merge_block = from_block.merge == SPIRBlock::MergeSelection ? from_block.next_block : BlockID(0);
1273412734

12735-
// If we branch directly to a selection merge target, we don't need a code path.
12736-
// This covers both merge out of if () / else () as well as a break for switch blocks.
12737-
bool true_sub = !is_conditional(true_block);
12738-
bool false_sub = !is_conditional(false_block);
12735+
// If we branch directly to our selection merge target, we don't need a code path.
12736+
bool true_block_needs_code = true_block != merge_block || flush_phi_required(from, true_block);
12737+
bool false_block_needs_code = false_block != merge_block || flush_phi_required(from, false_block);
1273912738

12740-
bool true_block_is_selection_merge = true_block == merge_block;
12741-
bool false_block_is_selection_merge = false_block == merge_block;
12739+
if (!true_block_needs_code && !false_block_needs_code)
12740+
return;
1274212741

12743-
// Can happen if one branch merges to selection branch merge target,
12744-
// and then the other branches to outer switch merge target.
12745-
if (!true_sub && !false_sub)
12746-
{
12747-
if (true_block_is_selection_merge && false_block_is_selection_merge)
12748-
{
12749-
// Useless case (should just be OpBranch), just flush PHI once if needed and execution will continue
12750-
// at the merge target.
12751-
if (flush_phi_required(from, merge_block))
12752-
flush_phi(from, merge_block);
12753-
return;
12754-
}
12755-
else if (true_block_is_selection_merge)
12756-
false_sub = true;
12757-
else if (false_block_is_selection_merge)
12758-
true_sub = true;
12759-
else
12760-
{
12761-
false_sub = true;
12762-
true_sub = true;
12763-
}
12764-
}
12742+
emit_block_hints(get<SPIRBlock>(from));
1276512743

12766-
if (true_sub)
12744+
if (true_block_needs_code)
1276712745
{
12768-
emit_block_hints(get<SPIRBlock>(from));
1276912746
statement("if (", to_expression(cond), ")");
1277012747
begin_scope();
1277112748
branch(from, true_block);
1277212749
end_scope();
1277312750

12774-
// If we merge to continue, we handle that explicitly in emit_block_chain(),
12775-
// so there is no need to branch to it directly here.
12776-
// break; is required to handle ladder fallthrough cases, so keep that in for now, even
12777-
// if we could potentially handle it in emit_block_chain().
12778-
if (false_sub || (!false_block_is_selection_merge && is_continue(false_block)) || is_break(false_block))
12751+
if (false_block_needs_code)
1277912752
{
1278012753
statement("else");
1278112754
begin_scope();
1278212755
branch(from, false_block);
1278312756
end_scope();
1278412757
}
12785-
else if (flush_phi_required(from, false_block))
12786-
{
12787-
statement("else");
12788-
begin_scope();
12789-
flush_phi(from, false_block);
12790-
end_scope();
12791-
}
1279212758
}
12793-
else if (false_sub)
12759+
else if (false_block_needs_code)
1279412760
{
1279512761
// Only need false path, use negative conditional.
12796-
emit_block_hints(get<SPIRBlock>(from));
1279712762
statement("if (!", to_enclosed_expression(cond), ")");
1279812763
begin_scope();
1279912764
branch(from, false_block);
1280012765
end_scope();
12801-
12802-
if ((!true_block_is_selection_merge && is_continue(true_block)) || is_break(true_block))
12803-
{
12804-
statement("else");
12805-
begin_scope();
12806-
branch(from, true_block);
12807-
end_scope();
12808-
}
12809-
else if (flush_phi_required(from, true_block))
12810-
{
12811-
statement("else");
12812-
begin_scope();
12813-
flush_phi(from, true_block);
12814-
end_scope();
12815-
}
1281612766
}
1281712767
}
1281812768

0 commit comments

Comments
 (0)