shader: factor live execution guards before resource planning

This commit is contained in:
nmzik
2026-10-13 01:03:46 +02:00
parent 2f88a0a3b2
commit c9ec82e32e
4 changed files with 65 additions and 17 deletions
@@ -789,19 +789,20 @@ TranslateResult TranslateProgram(const Decoder::Program& decoded, const CompileO
GetDumpLabel(options), StageName(options.stage), options.shader_hash,
static_cast<uint64_t>(ir.blocks.size()), phase_ms());
IR::RewriteToSsa(ir.blocks);
IR::ConstantPropagationPass(ir.blocks, ir.wave_size);
IR::ResolveControlFlowIdentities(ir);
IR::RemoveIdentities(ir.blocks);
IR::EliminateDeadCode(ir.blocks);
const auto read_lane_stats = IR::EliminateReadLane(ir, ir.wave_size);
if (read_lane_stats.rewritten_reads != 0) {
LOGF("%s read-lane elimination: reads=%" PRIu32 "\n", GetDumpLabel(options),
read_lane_stats.rewritten_reads);
const auto simplify = [&ir] {
IR::ConstantPropagationPass(ir.blocks, ir.wave_size);
IR::ResolveControlFlowIdentities(ir);
IR::RemoveIdentities(ir.blocks);
IR::EliminateDeadCode(ir.blocks);
};
simplify();
const auto read_lane_stats = IR::EliminateReadLane(ir, ir.wave_size);
if (read_lane_stats.rewritten_reads != 0) {
LOGF("%s read-lane elimination: reads=%" PRIu32 "\n", GetDumpLabel(options),
read_lane_stats.rewritten_reads);
}
// Cleanup also exposes profitable folds by removing dead predicate consumers.
simplify();
if (decoded.has_swap_pc) {
// Leaf returns are resolved CFG edges; native PC values must not escape into GPU data.
for (const auto* block: ir.blocks)
@@ -22,6 +22,13 @@ void Replace(Inst& inst, Value value) {
inst.ReplaceUsesWith(value.Resolve());
}
bool HasOneUse(const Inst& inst) {
// Replacement aliases preserve external Values but emit no additional operation.
return std::ranges::count_if(inst.Uses(), [](const Use& use) {
return use.user->GetOpcode() != ValueOpcode::Identity;
}) == 1;
}
class FoldWorklist {
public:
struct Location {
@@ -744,7 +751,7 @@ void FoldInstruction(Block& block, Block::iterator instruction,
if (inverse) {
test = Arg(*test, 0).TryInstruction();
if (test == nullptr) return false;
if (test->GetOpcode() == ValueOpcode::LogicalAnd)
if (test->GetOpcode() == ValueOpcode::LogicalAnd && HasOneUse(*test))
for (uint32_t arg = 0; arg < 2u; ++arg)
if (Arg(*test, arg) == assumption)
return replace(negate(Arg(*test, arg ^ 1u)));
@@ -786,6 +793,25 @@ void FoldInstruction(Block& block, Block::iterator instruction,
Replace(inst, lhs.U1() ? lhs : rhs);
} else if (IsImmediate(rhs, Type::U1)) {
Replace(inst, rhs.U1() ? rhs : lhs);
} else {
const auto* left = lhs.TryInstruction();
const auto* right = rhs.TryInstruction();
if (left == nullptr || right == nullptr ||
left->GetOpcode() != ValueOpcode::LogicalAnd ||
right->GetOpcode() != ValueOpcode::LogicalAnd ||
(!HasOneUse(*left) && !HasOneUse(*right))) return;
// Factor a shared execution predicate without retaining more operations.
for (uint32_t a = 0; a < 2u; ++a) {
for (uint32_t b = 0; b < 2u; ++b) {
const auto common = Arg(*left, a);
if (common != Arg(*right, b)) continue;
const auto either = Value(&*worklist.Prepend(block, instruction,
ValueOpcode::LogicalOr, {Arg(*left, a ^ 1u), Arg(*right, b ^ 1u)}));
Replace(inst, Value(&*worklist.Prepend(block, instruction,
ValueOpcode::LogicalAnd, {common, either})));
return;
}
}
}
}
return;