Skip to content

Commit f0bd705

Browse files
[CodeGen] Restore MachineBlockPlacement block ordering (#99351)
PR #91843 changed the algorithm used to find the next unplaced block so that it iterates through the blocks in BlockFilter instead of iterating through the blocks in the function and checking if they are in the block filter. Unfortunately this sometimes results in a different block ordering being chosen, as the order of blocks in BlockFilter comes from the order in MachineLoopInfo, and in some cases this differs from the order they are in the function. This can also give an end result that has worse performance. Fix this by making collectLoopBlockSet place blocks in its output in the order that they are in the function.
1 parent 71a8486 commit f0bd705

File tree

2 files changed

+543
-2
lines changed

2 files changed

+543
-2
lines changed

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,15 @@ void MachineBlockPlacement::rotateLoopWithProfile(
25962596
/// otherwise, collect all blocks in the loop.
25972597
MachineBlockPlacement::BlockFilterSet
25982598
MachineBlockPlacement::collectLoopBlockSet(const MachineLoop &L) {
2599-
BlockFilterSet LoopBlockSet;
2599+
// Collect the blocks in a set ordered by block number, as this gives the same
2600+
// order as they appear in the function.
2601+
struct MBBCompare {
2602+
bool operator()(const MachineBasicBlock *X,
2603+
const MachineBasicBlock *Y) const {
2604+
return X->getNumber() < Y->getNumber();
2605+
}
2606+
};
2607+
std::set<const MachineBasicBlock *, MBBCompare> LoopBlockSet;
26002608

26012609
// Filter cold blocks off from LoopBlockSet when profile data is available.
26022610
// Collect the sum of frequencies of incoming edges to the loop header from
@@ -2627,7 +2635,11 @@ MachineBlockPlacement::collectLoopBlockSet(const MachineLoop &L) {
26272635
} else
26282636
LoopBlockSet.insert(L.block_begin(), L.block_end());
26292637

2630-
return LoopBlockSet;
2638+
// Copy the blocks into a BlockFilterSet, as iterating it is faster than
2639+
// std::set. We will only remove blocks and never insert them, which will
2640+
// preserve the ordering.
2641+
BlockFilterSet Ret(LoopBlockSet.begin(), LoopBlockSet.end());
2642+
return Ret;
26312643
}
26322644

26332645
/// Forms basic block chains from the natural loop structures.

0 commit comments

Comments
 (0)