shader: batch dependent folds to avoid repeated SSA graph walks

This commit is contained in:
nmzik committed 2026-10-17 06:20:02 +02:00
1 parent b82512be06
commit f6f1ac8b40
1 file changed
+9 -3
@@ -51,11 +51,13 @@ public:
} }
void QueueUsers(const std::vector<Use>& users) { void QueueUsers(const std::vector<Use>& users) {
++generation;
affected.clear();
for (const auto& use: users) affected.push_back(use.user); for (const auto& use: users) affected.push_back(use.user);
}
void FlushUsers() {
++generation;
// Pattern folds inspect nested operands even when intermediate operations // Pattern folds inspect nested operands even when intermediate operations
// stay dynamic. Visit consumers once per change, including cyclic Phi edges. // stay dynamic. Visit their union once per drain, including cyclic Phi edges.
for (size_t i = 0; i < affected.size(); ++i) { for (size_t i = 0; i < affected.size(); ++i) {
auto* inst = affected[i]; auto* inst = affected[i];
const auto found = locations.find(inst); const auto found = locations.find(inst);
@@ -64,6 +66,7 @@ public:
Queue(inst); Queue(inst);
for (const auto& use: inst->Uses()) affected.push_back(use.user); for (const auto& use: inst->Uses()) affected.push_back(use.user);
} }
affected.clear();
} }
Block::iterator Prepend(Block& block, Block::iterator before, ValueOpcode opcode, Block::iterator Prepend(Block& block, Block::iterator before, ValueOpcode opcode,
@@ -977,6 +980,7 @@ void ConstantPropagationPass(const BlockList& blocks, uint32_t wave_size) {
std::vector<Use> users; std::vector<Use> users;
std::vector<Value> operands; std::vector<Value> operands;
const auto drain = [&] { const auto drain = [&] {
do {
for (size_t i = 0; i < worklist.pending.size(); ++i) { for (size_t i = 0; i < worklist.pending.size(); ++i) {
auto* inst = worklist.pending[i]; auto* inst = worklist.pending[i];
auto& location = worklist.locations.at(inst); auto& location = worklist.locations.at(inst);
@@ -998,6 +1002,8 @@ void ConstantPropagationPass(const BlockList& blocks, uint32_t wave_size) {
} }
} }
worklist.pending.clear(); worklist.pending.clear();
worklist.FlushUsers();
} while (!worklist.pending.empty());
}; };
drain(); drain();
// Normalize retained PHI/select values only after every supported field read has // Normalize retained PHI/select values only after every supported field read has