Skip to content

Commit e7939d0

Browse files
authored
[Instrumentation] Support verifying machine function (#90931)
We need it to test isel related passes. Currently `verifyMachineFunction` is incomplete (no LiveIntervals support), but is enough for testing isel pass, will migrate to complete `MachineVerifierPass` in future.
1 parent b958ef1 commit e7939d0

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
128128
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
129129
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
130130
RequireAllMachineFunctionPropertiesPass())
131+
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
131132
#undef MACHINE_FUNCTION_PASS
132133

133134
// After a pass is converted to new pass manager, its entry should be moved from

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#include "llvm/CodeGen/MIRPrinter.h"
9494
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
9595
#include "llvm/CodeGen/MachinePassManager.h"
96+
#include "llvm/CodeGen/MachineRegisterInfo.h"
9697
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
9798
#include "llvm/CodeGen/SafeStack.h"
9899
#include "llvm/CodeGen/SelectOptimize.h"
@@ -363,6 +364,14 @@ class TriggerVerifierErrorPass
363364
return PreservedAnalyses::none();
364365
}
365366

367+
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
368+
// Intentionally create a virtual register and set NoVRegs property.
369+
auto &MRI = MF.getRegInfo();
370+
MRI.createGenericVirtualRegister(LLT::scalar(8));
371+
MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
372+
return PreservedAnalyses::all();
373+
}
374+
366375
static StringRef name() { return "TriggerVerifierErrorPass"; }
367376
};
368377

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,17 @@ void VerifyInstrumentation::registerCallbacks(
14871487
"\"{0}\", compilation aborted!",
14881488
P));
14891489
}
1490+
1491+
// TODO: Use complete MachineVerifierPass.
1492+
if (auto *MF = unwrapIR<MachineFunction>(IR)) {
1493+
if (DebugLogging)
1494+
dbgs() << "Verifying machine function " << MF->getName() << '\n';
1495+
verifyMachineFunction(
1496+
formatv("Broken machine function found after pass "
1497+
"\"{0}\", compilation aborted!",
1498+
P),
1499+
*MF);
1500+
}
14901501
}
14911502
});
14921503
}

llvm/test/CodeGen/MIR/X86/machine-verifier.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RUN: not --crash llc -march=x86-64 -run-pass none -o /dev/null %s 2>&1 | FileCheck %s
2-
# This test ensures that the MIR parser runs the machine verifier after parsing.
2+
# This test ensures that the VerifyInstrumentation works for machine function.
33

44
--- |
55

llvm/test/tools/llc/new-pm/verify.mir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# RUN: not --crash llc -mtriple=x86_64-pc-linux-gnu -debug-pass-manager -passes='module(function(machine-function(trigger-verifier-error)))' -filetype=null %s 2>&1 | FileCheck %s
2+
3+
# CHECK: Verifying machine function f
4+
# CHECK: Broken machine function found after pass "TriggerVerifierErrorPass"
5+
---
6+
name: f
7+
body: |
8+
bb.0:
9+
RET 0
10+
...

llvm/tools/llc/NewPMDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int llvm::compileModuleWithNewPM(
115115
MachineModuleInfo MMI(&LLVMTM);
116116

117117
PassInstrumentationCallbacks PIC;
118-
StandardInstrumentations SI(Context, Opt.DebugPM);
118+
StandardInstrumentations SI(Context, Opt.DebugPM, !NoVerify);
119119
SI.registerCallbacks(PIC);
120120
registerCodeGenCallback(PIC, LLVMTM);
121121

0 commit comments

Comments
 (0)