Skip to content

Commit 026165f

Browse files
authored
[Instrumentation] Support MachineFunction in ChangeReporter (#80946)
1 parent 75edf0c commit 026165f

File tree

4 files changed

+128
-12
lines changed

4 files changed

+128
-12
lines changed

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "llvm/ADT/STLExtras.h"
1919
#include "llvm/ADT/SmallVector.h"
2020
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/ADT/StringSet.h"
22+
#include "llvm/CodeGen/MachineBasicBlock.h"
2123
#include "llvm/IR/BasicBlock.h"
2224
#include "llvm/IR/OptBisect.h"
2325
#include "llvm/IR/PassTimingInfo.h"
@@ -33,6 +35,7 @@ namespace llvm {
3335

3436
class Module;
3537
class Function;
38+
class MachineFunction;
3639
class PassInstrumentationCallbacks;
3740

3841
/// Instrumentation to print IR before/after passes.
@@ -313,6 +316,11 @@ template <typename T> class BlockDataT {
313316
B.print(SS, nullptr, true, true);
314317
}
315318

319+
BlockDataT(const MachineBasicBlock &B) : Label(B.getName().str()), Data(B) {
320+
raw_string_ostream SS(Body);
321+
B.print(SS);
322+
}
323+
316324
bool operator==(const BlockDataT &That) const { return Body == That.Body; }
317325
bool operator!=(const BlockDataT &That) const { return Body != That.Body; }
318326

@@ -364,6 +372,7 @@ template <typename T> class OrderedChangedData {
364372
class EmptyData {
365373
public:
366374
EmptyData(const BasicBlock &) {}
375+
EmptyData(const MachineBasicBlock &) {}
367376
};
368377

369378
// The data saved for comparing functions.
@@ -405,7 +414,8 @@ template <typename T> class IRComparer {
405414

406415
protected:
407416
// Generate the data for \p F into \p Data.
408-
static bool generateFunctionData(IRDataT<T> &Data, const Function &F);
417+
template <typename FunctionT>
418+
static bool generateFunctionData(IRDataT<T> &Data, const FunctionT &F);
409419

410420
const IRDataT<T> &Before;
411421
const IRDataT<T> &After;
@@ -475,6 +485,7 @@ class DCData {
475485
public:
476486
// Fill the map with the transitions from basic block \p B.
477487
DCData(const BasicBlock &B);
488+
DCData(const MachineBasicBlock &B);
478489

479490
// Return an iterator to the names of the successor blocks.
480491
StringMap<std::string>::const_iterator begin() const {

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#include "llvm/Analysis/CallGraphSCCPass.h"
2020
#include "llvm/Analysis/LazyCallGraph.h"
2121
#include "llvm/Analysis/LoopInfo.h"
22+
#include "llvm/CodeGen/MIRPrinter.h"
2223
#include "llvm/CodeGen/MachineFunction.h"
24+
#include "llvm/CodeGen/MachineModuleInfo.h"
2325
#include "llvm/IR/Constants.h"
2426
#include "llvm/IR/Function.h"
2527
#include "llvm/IR/Module.h"
@@ -180,6 +182,12 @@ const Module *unwrapModule(Any IR, bool Force = false) {
180182
return F->getParent();
181183
}
182184

185+
if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
186+
if (!Force && !isFunctionInPrintList(MF->getName()))
187+
return nullptr;
188+
return MF->getFunction().getParent();
189+
}
190+
183191
llvm_unreachable("Unknown IR unit");
184192
}
185193

@@ -215,6 +223,12 @@ void printIR(raw_ostream &OS, const Loop *L) {
215223
printLoop(const_cast<Loop &>(*L), OS);
216224
}
217225

226+
void printIR(raw_ostream &OS, const MachineFunction *MF) {
227+
if (!isFunctionInPrintList(MF->getName()))
228+
return;
229+
MF->print(OS);
230+
}
231+
218232
std::string getIRName(Any IR) {
219233
if (unwrapIR<Module>(IR))
220234
return "[module]";
@@ -262,6 +276,9 @@ bool shouldPrintIR(Any IR) {
262276

263277
if (const auto *L = unwrapIR<Loop>(IR))
264278
return isFunctionInPrintList(L->getHeader()->getParent()->getName());
279+
280+
if (const auto *MF = unwrapIR<MachineFunction>(IR))
281+
return isFunctionInPrintList(MF->getName());
265282
llvm_unreachable("Unknown wrapped IR type");
266283
}
267284

@@ -275,6 +292,14 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
275292
auto *M = unwrapModule(IR);
276293
assert(M && "should have unwrapped module");
277294
printIR(OS, M);
295+
296+
if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
297+
auto &MMI = MF->getMMI();
298+
for (const auto &F : *M) {
299+
if (auto *MF = MMI.getMachineFunction(F))
300+
MF->print(OS);
301+
}
302+
}
278303
return;
279304
}
280305

@@ -297,6 +322,11 @@ void unwrapAndPrint(raw_ostream &OS, Any IR) {
297322
printIR(OS, L);
298323
return;
299324
}
325+
326+
if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
327+
printIR(OS, MF);
328+
return;
329+
}
300330
llvm_unreachable("Unknown wrapped IR type");
301331
}
302332

@@ -305,7 +335,8 @@ bool isIgnored(StringRef PassID) {
305335
return isSpecialPass(PassID,
306336
{"PassManager", "PassAdaptor", "AnalysisManagerProxy",
307337
"DevirtSCCRepeatedPass", "ModuleInlinerWrapperPass",
308-
"VerifierPass", "PrintModulePass"});
338+
"VerifierPass", "PrintModulePass", "PrintMIRPass",
339+
"PrintMIRPreparePass"});
309340
}
310341

311342
std::string makeHTMLReady(StringRef SR) {
@@ -664,20 +695,38 @@ template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
664695
return;
665696
}
666697

667-
const auto *F = unwrapIR<Function>(IR);
668-
if (!F) {
669-
const auto *L = unwrapIR<Loop>(IR);
670-
assert(L && "Unknown IR unit.");
671-
F = L->getHeader()->getParent();
698+
if (const auto *F = unwrapIR<Function>(IR)) {
699+
generateFunctionData(Data, *F);
700+
return;
701+
}
702+
703+
if (const auto *L = unwrapIR<Loop>(IR)) {
704+
auto *F = L->getHeader()->getParent();
705+
generateFunctionData(Data, *F);
706+
return;
672707
}
673-
assert(F && "Unknown IR unit.");
674-
generateFunctionData(Data, *F);
708+
709+
if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
710+
generateFunctionData(Data, *MF);
711+
return;
712+
}
713+
714+
llvm_unreachable("Unknown IR unit");
715+
}
716+
717+
static bool shouldGenerateData(const Function &F) {
718+
return !F.isDeclaration() && isFunctionInPrintList(F.getName());
719+
}
720+
721+
static bool shouldGenerateData(const MachineFunction &MF) {
722+
return isFunctionInPrintList(MF.getName());
675723
}
676724

677725
template <typename T>
678-
bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const Function &F) {
679-
if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
680-
FuncDataT<T> FD(F.getEntryBlock().getName().str());
726+
template <typename FunctionT>
727+
bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const FunctionT &F) {
728+
if (shouldGenerateData(F)) {
729+
FuncDataT<T> FD(F.front().getName().str());
681730
int I = 0;
682731
for (const auto &B : F) {
683732
std::string BBName = B.getName().str();
@@ -722,6 +771,12 @@ static SmallString<32> getIRFileDisplayName(Any IR) {
722771
ResultStream << "-loop-";
723772
stable_hash LoopNameHash = stable_hash_combine_string(L->getName());
724773
write_hex(ResultStream, LoopNameHash, HexPrintStyle::Lower, MaxHashWidth);
774+
} else if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
775+
ResultStream << "-machine-function-";
776+
stable_hash MachineFunctionNameHash =
777+
stable_hash_combine_string(MF->getName());
778+
write_hex(ResultStream, MachineFunctionNameHash, HexPrintStyle::Lower,
779+
MaxHashWidth);
725780
} else {
726781
llvm_unreachable("Unknown wrapped IR type");
727782
}
@@ -2122,6 +2177,11 @@ DCData::DCData(const BasicBlock &B) {
21222177
addSuccessorLabel(Succ->getName().str(), "");
21232178
}
21242179

2180+
DCData::DCData(const MachineBasicBlock &B) {
2181+
for (const MachineBasicBlock *Succ : successors(&B))
2182+
addSuccessorLabel(Succ->getName().str(), "");
2183+
}
2184+
21252185
DotCfgChangeReporter::DotCfgChangeReporter(bool Verbose)
21262186
: ChangeReporter<IRDataT<DCData>>(Verbose) {}
21272187

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# REQUIRES: x86-registered-target
2+
# Simple functionality check.
3+
# RUN: rm -rf %t && mkdir -p %t
4+
# RUN: llc -filetype=null -print-changed=dot-cfg -passes=no-op-machine-function -dot-cfg-dir=%t %s
5+
# RUN: ls %t/*.pdf %t/passes.html | count 3
6+
7+
---
8+
name: g
9+
body: |
10+
bb.0.entry:
11+
%0:gr32 = MOV32ri 5
12+
$eax = COPY %0
13+
RET 0, $eax
14+
15+
...
16+
---
17+
name: f
18+
body: |
19+
bb.0.entry:
20+
%0:gr32 = MOV32ri 7
21+
$eax = COPY %0
22+
RET 0, $eax
23+
24+
...

llvm/test/Other/change-printer.mir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# REQUIRES: x86-registered-target
2+
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=null %s \
3+
# RUN: -p no-op-machine-function -print-changed 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OP
4+
5+
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=null %s \
6+
# RUN: -p dead-mi-elimination -print-changed 2>&1 | FileCheck %s --check-prefix=CHECK-SIMPLE
7+
8+
---
9+
name: test
10+
body: |
11+
bb.0:
12+
%1:gr64 = MOV64ri 0
13+
%2:gr64 = MOV64ri 0
14+
$eax = COPY %1
15+
RET64 implicit $eax
16+
...
17+
18+
# CHECK-NO-OP: *** IR Dump After NoOpMachineFunctionPass on test omitted because no change ***
19+
20+
# CHECK-SIMPLE: *** IR Dump After DeadMachineInstructionElimPass on test ***
21+
# CHECK-SIMPLE-NOT: %2:gr64 = MOV64ri 0

0 commit comments

Comments
 (0)