[llvm] [CodeLayout] Size-aware machine block placement (PR #109711)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 23 07:43:46 PDT 2024
spupyrev wrote:
You could try something along these lines. I haven't fine-tuned or polished the code, so be careful.
```diff
// optimization; prioritize slightly jumps with a single successor, since
// the corresponding jump instruction will be removed from the binary.
const uint64_t Freq = Succs.size() == 1 ? 110 : 100;
- for (const MachineBasicBlock *Succ : Succs)
- JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succ], Freq});
+ if (Succs.size() == 2) {
+ // NEW
+ BranchProbability BP0 = MBPI->getEdgeProbability(&MBB, Succs[0]);
+ BlockFrequency BF0 = BlockFrequency(100) * BP0;
+ size_t Pct0 = BF0.getFrequency();
+ assert(Pct0 <= 100);
+ BranchProbability BP1 = MBPI->getEdgeProbability(&MBB, Succs[1]);
+ BlockFrequency BF1 = BlockFrequency(100) * BP1;
+ size_t Pct1 = BF1.getFrequency();
+ assert(Pct1 <= 100);
+
+ if (Pct0 <= 5) {
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[0]], 10});
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[1]], Freq});
+ } else if (Pct1 <= 5) {
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[1]], 10});
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[0]], Freq});
+ } else {
+ // In case of ties, the last element of JumpCounts is prioritized.
+ // Thus, make sure that Succs[1] is the original fall-through.
+ if (BlockIndex[&MBB] + 1 == BlockIndex[Succs[0]])
+ std::swap(Succs[0], Succs[1]);
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[0]], Freq});
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succs[1]], Freq});
+ }
+ } else {
+ // OLD
+ for (const MachineBasicBlock *Succ : Succs)
+ JumpCounts.push_back({BlockIndex[&MBB], BlockIndex[Succ], Freq});
+ }
} else {
for (MachineBasicBlock *Succ : MBB.successors()) {
auto EP = MBPI->getEdgeProbability(&MBB, Succ);
```
https://github.com/llvm/llvm-project/pull/109711
More information about the llvm-commits
mailing list