[llvm] [Clang][WebAssembly] Replace Reachability with SCCs in Irreducible CFG Fixer (PR #179722)
Kamil Jakubus via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 4 10:18:29 PST 2026
https://github.com/jkbz64 created https://github.com/llvm/llvm-project/pull/179722
Replace reachability-based loop analysis with SCC-based detection in the WebAssembly irreducible control-flow fixer.
Details and reproductions in: #47793 and #165041.
>From 403f46904d2065fcb6497639182dd4692164c8b1 Mon Sep 17 00:00:00 2001
From: Kamil Jakubus <kamil.jakubus at usagi.coffee>
Date: Wed, 4 Feb 2026 19:04:16 +0100
Subject: [PATCH] [Clang][WebAssembly] Replace Reachability with SCCs in
Irreducible CFG Fixer
Replace reachability-based loop analysis with SCC-based
detection in the WebAssembly irreducible control-flow fixer.
---
.../WebAssemblyFixIrreducibleControlFlow.cpp | 161 ++++++++++++------
1 file changed, 113 insertions(+), 48 deletions(-)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
index 07171d472dc2d..afc735922fac7 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
@@ -58,6 +58,7 @@
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Support/Debug.h"
+#include <limits>
using namespace llvm;
#define DEBUG_TYPE "wasm-fix-irreducible-control-flow"
@@ -98,16 +99,8 @@ class ReachabilityGraph {
calculate();
}
- bool canReach(MachineBasicBlock *From, MachineBasicBlock *To) const {
- assert(inRegion(From) && inRegion(To));
- auto I = Reachable.find(From);
- if (I == Reachable.end())
- return false;
- return I->second.count(To);
- }
-
// "Loopers" are blocks that are in a loop. We detect these by finding blocks
- // that can reach themselves.
+ // that are in a non-trivial SCC or have a self-loop.
const BlockSet &getLoopers() const { return Loopers; }
// Get all blocks that are loop entries.
@@ -120,65 +113,134 @@ class ReachabilityGraph {
assert(I != LoopEnterers.end());
return I->second;
}
+ unsigned getSCCId(MachineBasicBlock *MBB) const { return SccId[getIndex(MBB)]; }
private:
MachineBasicBlock *Entry;
const BlockSet &Blocks;
+ SmallVector<MachineBasicBlock *, 16> BlockList;
+ DenseMap<MachineBasicBlock *, unsigned> BlockIndex;
+ unsigned NumBlocks = 0;
BlockSet Loopers, LoopEntries;
DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
- // Maps a block to all the other blocks it can reach.
- DenseMap<MachineBasicBlock *, BlockSet> Reachable;
+ // Per-node adjacency in the region (excluding edges to Entry).
+ SmallVector<SmallVector<unsigned, 4>, 0> Succs;
+ SmallVector<bool, 0> SelfLoop;
+ SmallVector<unsigned, 0> SccId;
+ SmallVector<unsigned, 0> SccSize;
+
+ unsigned getIndex(MachineBasicBlock *MBB) const {
+ auto It = BlockIndex.find(MBB);
+ assert(It != BlockIndex.end());
+ return It->second;
+ }
void calculate() {
- // Reachability computation work list. Contains pairs of recent additions
- // (A, B) where we just added a link A => B.
- using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
- SmallVector<BlockPair, 4> WorkList;
+ BlockList.assign(Blocks.begin(), Blocks.end());
+ NumBlocks = BlockList.size();
+ BlockIndex.clear();
+ BlockIndex.reserve(NumBlocks);
+ for (unsigned I = 0; I < NumBlocks; ++I)
+ BlockIndex[BlockList[I]] = I;
+ Succs.clear();
+ Succs.resize(NumBlocks);
+ SelfLoop.clear();
+ SelfLoop.assign(NumBlocks, false);
- // Add all relevant direct branches.
for (auto *MBB : Blocks) {
+ unsigned MBBIdx = getIndex(MBB);
for (auto *Succ : MBB->successors()) {
- if (Succ != Entry && inRegion(Succ)) {
- Reachable[MBB].insert(Succ);
- WorkList.emplace_back(MBB, Succ);
- }
+ if (Succ == Entry || !inRegion(Succ))
+ continue;
+ unsigned SuccIdx = getIndex(Succ);
+ if (SuccIdx == MBBIdx)
+ SelfLoop[MBBIdx] = true;
+ Succs[MBBIdx].push_back(SuccIdx);
}
}
- while (!WorkList.empty()) {
- MachineBasicBlock *MBB, *Succ;
- std::tie(MBB, Succ) = WorkList.pop_back_val();
- assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
- if (MBB != Entry) {
- // We recently added MBB => Succ, and that means we may have enabled
- // Pred => MBB => Succ.
- for (auto *Pred : MBB->predecessors()) {
- if (Reachable[Pred].insert(Succ).second) {
- WorkList.emplace_back(Pred, Succ);
+ // Tarjan SCC (iterative) on the region graph.
+ SccId.assign(NumBlocks, std::numeric_limits<unsigned>::max());
+ SccSize.clear();
+ SmallVector<int, 0> Index(NumBlocks, -1);
+ SmallVector<int, 0> Lowlink(NumBlocks, 0);
+ SmallVector<unsigned, 0> Stack;
+ SmallVector<bool, 0> OnStack(NumBlocks, false);
+ int NextIndex = 0;
+
+ struct Frame {
+ unsigned V;
+ unsigned NextSucc;
+ };
+ SmallVector<Frame, 0> DFS;
+
+ auto pushNode = [&](unsigned V) {
+ Index[V] = Lowlink[V] = NextIndex++;
+ Stack.push_back(V);
+ OnStack[V] = true;
+ DFS.push_back({V, 0});
+ };
+
+ for (unsigned V = 0; V < NumBlocks; ++V) {
+ if (Index[V] != -1)
+ continue;
+ pushNode(V);
+ while (!DFS.empty()) {
+ Frame &F = DFS.back();
+ unsigned Cur = F.V;
+ if (F.NextSucc < Succs[Cur].size()) {
+ unsigned W = Succs[Cur][F.NextSucc++];
+ if (Index[W] == -1) {
+ pushNode(W);
+ } else if (OnStack[W]) {
+ Lowlink[Cur] = std::min(Lowlink[Cur], Index[W]);
+ }
+ continue;
+ }
+
+ // Finished exploring Cur.
+ if (Lowlink[Cur] == Index[Cur]) {
+ unsigned Size = 0;
+ while (true) {
+ unsigned W = Stack.pop_back_val();
+ OnStack[W] = false;
+ SccId[W] = SccSize.size();
+ ++Size;
+ if (W == Cur)
+ break;
}
+ SccSize.push_back(Size);
+ }
+
+ DFS.pop_back();
+ if (!DFS.empty()) {
+ unsigned Parent = DFS.back().V;
+ Lowlink[Parent] = std::min(Lowlink[Parent], Lowlink[Cur]);
}
}
}
- // Blocks that can return to themselves are in a loop.
- for (auto *MBB : Blocks) {
- if (canReach(MBB, MBB)) {
+ // Blocks that are in a loop are those in non-trivial SCCs or self-loops.
+ for (unsigned I = 0; I < NumBlocks; ++I) {
+ auto *MBB = BlockList[I];
+ if (MBB == Entry)
+ continue;
+ if (SccSize[SccId[I]] > 1 || SelfLoop[I]) {
Loopers.insert(MBB);
}
}
assert(!Loopers.count(Entry));
- // Find the loop entries - loopers reachable from blocks not in that loop -
+ // Find the loop entries - loopers with predecessors outside their SCC -
// and those outside blocks that reach them, the "loop enterers".
for (auto *Looper : Loopers) {
+ unsigned LoopScc = SccId[getIndex(Looper)];
for (auto *Pred : Looper->predecessors()) {
- // Pred can reach Looper. If Looper can reach Pred, it is in the loop;
- // otherwise, it is a block that enters into the loop.
- if (!canReach(Looper, Pred)) {
+ if (SccId[getIndex(Pred)] != LoopScc) {
LoopEntries.insert(Looper);
LoopEnterers[Looper].insert(Pred);
}
@@ -258,10 +320,16 @@ bool WebAssemblyFixIrreducibleControlFlow::processRegion(
bool FoundIrreducibility = false;
- for (auto *LoopEntry : getSortedEntries(Graph.getLoopEntries())) {
+ BlockVector SortedLoopEntries = getSortedEntries(Graph.getLoopEntries());
+ DenseMap<unsigned, BlockVector> EntriesByScc;
+ EntriesByScc.reserve(SortedLoopEntries.size());
+ for (auto *LoopEntry : SortedLoopEntries)
+ EntriesByScc[Graph.getSCCId(LoopEntry)].push_back(LoopEntry);
+
+ for (auto *LoopEntry : SortedLoopEntries) {
// Find mutual entries - all entries which can reach this one, and
// are reached by it (that always includes LoopEntry itself). All mutual
- // entries must be in the same loop, so if we have more than one, then we
+ // entries must be in the same SCC, so if we have more than one, then we
// have irreducible control flow.
//
// (Note that we need to sort the entries here, as otherwise the order can
@@ -284,15 +352,12 @@ bool WebAssemblyFixIrreducibleControlFlow::processRegion(
// a group of blocks all of whom can reach each other. (We'll see the
// irreducibility after removing branches to the top of that enclosing
// loop.)
+ const auto &Mutuals = EntriesByScc[Graph.getSCCId(LoopEntry)];
+ if (Mutuals.size() <= 1)
+ continue;
BlockSet MutualLoopEntries;
- MutualLoopEntries.insert(LoopEntry);
- for (auto *OtherLoopEntry : Graph.getLoopEntries()) {
- if (OtherLoopEntry != LoopEntry &&
- Graph.canReach(LoopEntry, OtherLoopEntry) &&
- Graph.canReach(OtherLoopEntry, LoopEntry)) {
- MutualLoopEntries.insert(OtherLoopEntry);
- }
- }
+ for (auto *MBB : Mutuals)
+ MutualLoopEntries.insert(MBB);
if (MutualLoopEntries.size() > 1) {
makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
@@ -404,7 +469,7 @@ void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
for (auto *Entry : Pred->successors()) {
if (!Entries.count(Entry))
continue;
- if (Graph.canReach(Entry, Pred)) {
+ if (Graph.getSCCId(Entry) == Graph.getSCCId(Pred)) {
InLoop.insert(Pred);
break;
}
More information about the llvm-commits
mailing list