Skip to content

[Instrumentation] Support verifying machine function #90931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
RequireAllMachineFunctionPropertiesPass())
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
#undef MACHINE_FUNCTION_PASS

// After a pass is converted to new pass manager, its entry should be moved from
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
#include "llvm/CodeGen/MIRPrinter.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
Expand Down Expand Up @@ -363,6 +364,14 @@ class TriggerVerifierErrorPass
return PreservedAnalyses::none();
}

PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
// Intentionally create a virtual register and set NoVRegs property.
auto &MRI = MF.getRegInfo();
MRI.createGenericVirtualRegister(LLT::scalar(8));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe, it would be best to introduce an actual instruction with the register. IIRC we permit virtual registers that have no uses in the MIR

MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
return PreservedAnalyses::all();
}

static StringRef name() { return "TriggerVerifierErrorPass"; }
};

Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Passes/StandardInstrumentations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,17 @@ void VerifyInstrumentation::registerCallbacks(
"\"{0}\", compilation aborted!",
P));
}

// TODO: Use complete MachineVerifierPass.
if (auto *MF = unwrapIR<MachineFunction>(IR)) {
if (DebugLogging)
dbgs() << "Verifying machine function " << MF->getName() << '\n';
verifyMachineFunction(
formatv("Broken machine function found after pass "
"\"{0}\", compilation aborted!",
P),
*MF);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/MIR/X86/machine-verifier.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RUN: not --crash llc -march=x86-64 -run-pass none -o /dev/null %s 2>&1 | FileCheck %s
# This test ensures that the MIR parser runs the machine verifier after parsing.
# This test ensures that the VerifyInstrumentation works for machine function.

--- |

Expand Down
10 changes: 10 additions & 0 deletions llvm/test/tools/llc/new-pm/verify.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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

# CHECK: Verifying machine function f
# CHECK: Broken machine function found after pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: CHECK: ... after pass "TriggerVerifierErrorPass"

---
name: f
body: |
bb.0:
RET 0
...
2 changes: 1 addition & 1 deletion llvm/tools/llc/NewPMDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int llvm::compileModuleWithNewPM(
MachineModuleInfo MMI(&LLVMTM);

PassInstrumentationCallbacks PIC;
StandardInstrumentations SI(Context, Opt.DebugPM);
StandardInstrumentations SI(Context, Opt.DebugPM, !NoVerify);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command line option verify-machineinstrs is overlapped with --disable-verify when we use new pass manager.

SI.registerCallbacks(PIC);
registerCodeGenCallback(PIC, LLVMTM);

Expand Down
Loading