Skip to content

Commit 6f87b16

Browse files
committed
[MachineVerifier] Doing ::calcRegsPassed in RPO: ~35% faster MV, NFC
Depending on the target, test suite, pipeline config and perhaps other factors machine verifier when forced on with -verify-machineinstrs can increase compile time 2-2.5 times over (Release, Asserts On), taking up ~60% of the time. An invaluable tool, it significantly slows down machine verifier-enabled testing. Nearly 75% of its time MachineVerifier spends in the calcRegsPassed method. It's a classic forward dataflow analysis executed over sets, but visiting MBBs in arbitrary order. We switch that to RPO here. This speeds up MachineVerifier by about 35%, decreasing the overall compile time with -verify-machineinstrs by 20-25% or so. calcRegsPassed itself gets 2x faster here. All measured on a large suite of shaders targeting a number of GPUs. Reviewers: bogner, stoklund, rudkx, qcolombet Reviewed By: bogner Tags: #llvm Differential Revision: https://reviews.llvm.org/D75032
1 parent a8a4f99 commit 6f87b16

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/DenseMap.h"
2525
#include "llvm/ADT/DenseSet.h"
2626
#include "llvm/ADT/DepthFirstIterator.h"
27+
#include "llvm/ADT/PostOrderIterator.h"
2728
#include "llvm/ADT/STLExtras.h"
2829
#include "llvm/ADT/SetOperations.h"
2930
#include "llvm/ADT/SmallPtrSet.h"
@@ -2126,34 +2127,46 @@ MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
21262127
// can pass through an MBB live, but may not be live every time. It is assumed
21272128
// that all vregsPassed sets are empty before the call.
21282129
void MachineVerifier::calcRegsPassed() {
2130+
// This is a forward dataflow, doing it in RPO. A standard map serves as a
2131+
// priority (sorting by RPO number) queue, deduplicating worklist, and an RPO
2132+
// number to MBB mapping all at once.
2133+
std::map<unsigned, const MachineBasicBlock *> RPOWorklist;
2134+
DenseMap<const MachineBasicBlock *, unsigned> RPONumbers;
2135+
if (MF->empty()) {
2136+
// ReversePostOrderTraversal doesn't handle empty functions.
2137+
return;
2138+
}
2139+
for (const MachineBasicBlock *MBB :
2140+
ReversePostOrderTraversal<const MachineFunction *>(MF)) {
2141+
// Careful with the evaluation order, fetch next number before allocating.
2142+
unsigned Number = RPONumbers.size();
2143+
RPONumbers[MBB] = Number;
2144+
}
21292145
// First push live-out regs to successors' vregsPassed. Remember the MBBs that
21302146
// have any vregsPassed.
2131-
SmallPtrSet<const MachineBasicBlock*, 8> todo;
2132-
for (const auto &MBB : *MF) {
2147+
for (const MachineBasicBlock &MBB : *MF) {
21332148
BBInfo &MInfo = MBBInfoMap[&MBB];
21342149
if (!MInfo.reachable)
21352150
continue;
2136-
for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
2137-
SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
2138-
BBInfo &SInfo = MBBInfoMap[*SuI];
2151+
for (const MachineBasicBlock *Succ : MBB.successors()) {
2152+
BBInfo &SInfo = MBBInfoMap[Succ];
21392153
if (SInfo.addPassed(MInfo.regsLiveOut))
2140-
todo.insert(*SuI);
2154+
RPOWorklist.emplace(RPONumbers[Succ], Succ);
21412155
}
21422156
}
21432157

2144-
// Iteratively push vregsPassed to successors. This will converge to the same
2145-
// final state regardless of DenseSet iteration order.
2146-
while (!todo.empty()) {
2147-
const MachineBasicBlock *MBB = *todo.begin();
2148-
todo.erase(MBB);
2158+
// Iteratively push vregsPassed to successors.
2159+
while (!RPOWorklist.empty()) {
2160+
auto Next = RPOWorklist.begin();
2161+
const MachineBasicBlock *MBB = Next->second;
2162+
RPOWorklist.erase(Next);
21492163
BBInfo &MInfo = MBBInfoMap[MBB];
2150-
for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
2151-
SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
2152-
if (*SuI == MBB)
2164+
for (const MachineBasicBlock *Succ : MBB->successors()) {
2165+
if (Succ == MBB)
21532166
continue;
2154-
BBInfo &SInfo = MBBInfoMap[*SuI];
2167+
BBInfo &SInfo = MBBInfoMap[Succ];
21552168
if (SInfo.addPassed(MInfo.vregsPassed))
2156-
todo.insert(*SuI);
2169+
RPOWorklist.emplace(RPONumbers[Succ], Succ);
21572170
}
21582171
}
21592172
}

0 commit comments

Comments
 (0)