[llvm] [WebAssembly] Optimize `WebAssemblyCFGSort`'s post sort verifications. (PR #182458)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 01:05:23 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-webassembly
Author: Demetrius Kanios (QuantumSegfault)
<details>
<summary>Changes</summary>
Currently, the post-sort verifications `WebAssemblyCFGSort` does in assertion builds is quite expensive. Given the example of [usagi-coffee/tree-sitter-abl](https://github.com/usagi-coffee/tree-sitter-abl), we have one MASSIVE function with nearly 120K blocks. Sorting this is fast, but the verifications after take some 40+ seconds (on my machine).
The culprit was the manipulation of the region `OnStack`. I've tried to replace that with interval based verifications to make sure the regions are well formed, well nested, and that the numbers of the blocks within a region are within the interval of the numbers of the header and bottom of the region.
This brings the aforementioned example down to well under a second spent in the pass in an assertions build.
---
Full diff: https://github.com/llvm/llvm-project/pull/182458.diff
1 Files Affected:
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp (+48-18)
``````````diff
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
index 6525c6d93bee8..724257843c867 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
@@ -332,13 +332,6 @@ static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
MDT.updateBlockNumbers();
#ifndef NDEBUG
- SmallSetVector<const SortRegion *, 8> OnStack;
-
- // Insert a sentinel representing the degenerate loop that starts at the
- // function entry block and includes the entire function as a "loop" that
- // executes once.
- OnStack.insert(nullptr);
-
for (auto &MBB : MF) {
assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
const SortRegion *Region = SRI.getRegionFor(&MBB);
@@ -359,24 +352,61 @@ static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
assert(Pred->getNumber() < MBB.getNumber() &&
"Non-loop-header predecessors should be topologically sorted");
}
- assert(OnStack.insert(Region) &&
- "Regions should be declared at most once.");
-
} else {
// Not a region header. All predecessors should be sorted above.
for (auto *Pred : MBB.predecessors())
assert(Pred->getNumber() < MBB.getNumber() &&
"Non-loop-header predecessors should be topologically sorted");
- assert(OnStack.count(SRI.getRegionFor(&MBB)) &&
- "Blocks must be nested in their regions");
}
- while (OnStack.size() > 1 && &MBB == SRI.getBottom(OnStack.back()))
- OnStack.pop_back();
}
- assert(OnStack.pop_back_val() == nullptr &&
- "The function entry block shouldn't actually be a region header");
- assert(OnStack.empty() &&
- "Control flow stack pushes and pops should be balanced.");
+
+ SmallSet<const SortRegion *, 8> Regions;
+ for (auto &MBB : MF) {
+ const SortRegion *Region = SRI.getRegionFor(&MBB);
+ if (Region)
+ Regions.insert(Region);
+ }
+
+ SmallVector<std::pair<int, int>, 8> RegionIntervals(Regions.size(), {-1, -1});
+
+ unsigned RegionIdx = 0;
+ for (auto *Region : Regions) {
+ assert(Region->getHeader() != &MF.front() &&
+ "The function entry block shouldn't actually be a region header");
+
+ auto *Header = Region->getHeader();
+ auto *Bottom = SRI.getBottom(Region);
+
+ assert(Header && "Regions must have a header");
+ assert(Bottom && "Regions must have a bottom");
+
+ std::pair<int, int> Interval = {Header->getNumber(), Bottom->getNumber()};
+ assert(Interval.first <= Interval.second &&
+ "Region bottoms must be sorted after region headers");
+
+ RegionIntervals[RegionIdx++] = Interval;
+
+ for (auto *MBB : Region->blocks()) {
+ assert(MBB->getNumber() >= Interval.first &&
+ MBB->getNumber() <= Interval.second &&
+ "All blocks within a region must have numbers within the region's "
+ "interval");
+ }
+ }
+
+ for (const auto &IntervalA : RegionIntervals) {
+ for (const auto &IntervalB : RegionIntervals) {
+ auto AContainsB = IntervalA.first <= IntervalB.first &&
+ IntervalA.second >= IntervalB.second;
+ auto BContainsA = IntervalB.first <= IntervalA.first &&
+ IntervalB.second >= IntervalA.second;
+ auto Disjoint = IntervalA.second < IntervalB.first ||
+ IntervalA.first > IntervalB.second;
+ assert((AContainsB || BContainsA || Disjoint) &&
+ "Regions must be fully contained within their parents and not "
+ "overlap their siblings");
+ }
+ }
#endif
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/182458
More information about the llvm-commits
mailing list