Skip to content

Commit c09ed6a

Browse files
authored
[CodeGen][NewPM] Port MachineVerifier to new pass manager (llvm#98628)
- Add `MachineVerifierPass`. - Use complete `MachineVerifierPass` in `VerifyInstrumentation` if possible. `LiveStacksAnalysis` will be added in future, all other analyses are done.
1 parent e025e5e commit c09ed6a

24 files changed

+106
-20
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===- llvm/CodeGen/MachineVerifier.h - Machine Code Verifier ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_MACHINEVERIFIER_H
10+
#define LLVM_CODEGEN_MACHINEVERIFIER_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
#include <string>
14+
15+
namespace llvm {
16+
class MachineVerifierPass : public PassInfoMixin<MachineVerifierPass> {
17+
std::string Banner;
18+
19+
public:
20+
MachineVerifierPass(const std::string &Banner = std::string())
21+
: Banner(Banner) {}
22+
PreservedAnalyses run(MachineFunction &MF,
23+
MachineFunctionAnalysisManager &MFAM);
24+
};
25+
26+
} // namespace llvm
27+
28+
#endif // LLVM_CODEGEN_MACHINEVERIFIER_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void initializeMachineSinkingPass(PassRegistry&);
208208
void initializeMachineTraceMetricsPass(PassRegistry&);
209209
void initializeMachineUniformityInfoPrinterPassPass(PassRegistry &);
210210
void initializeMachineUniformityAnalysisPassPass(PassRegistry &);
211-
void initializeMachineVerifierPassPass(PassRegistry&);
211+
void initializeMachineVerifierLegacyPassPass(PassRegistry &);
212212
void initializeMemoryDependenceWrapperPassPass(PassRegistry&);
213213
void initializeMemorySSAWrapperPassPass(PassRegistry&);
214214
void initializeMergeICmpsLegacyPassPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(dbgs()))
148148
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
149149
RequireAllMachineFunctionPropertiesPass())
150150
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
151+
MACHINE_FUNCTION_PASS("verify", MachineVerifierPass())
151152
#undef MACHINE_FUNCTION_PASS
152153

153154
#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ class VerifyInstrumentation {
461461

462462
public:
463463
VerifyInstrumentation(bool DebugLogging) : DebugLogging(DebugLogging) {}
464-
void registerCallbacks(PassInstrumentationCallbacks &PIC);
464+
void registerCallbacks(PassInstrumentationCallbacks &PIC,
465+
ModuleAnalysisManager *MAM);
465466
};
466467

467468
/// This class implements --time-trace functionality for new pass manager.

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
9797
initializeMachineSinkingPass(Registry);
9898
initializeMachineUniformityAnalysisPassPass(Registry);
9999
initializeMachineUniformityInfoPrinterPassPass(Registry);
100-
initializeMachineVerifierPassPass(Registry);
100+
initializeMachineVerifierLegacyPassPass(Registry);
101101
initializeObjCARCContractLegacyPassPass(Registry);
102102
initializeOptimizePHIsPass(Registry);
103103
initializePEIPass(Registry);

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// -verify-machineinstrs.
2121
//===----------------------------------------------------------------------===//
2222

23+
#include "llvm/CodeGen/MachineVerifier.h"
2324
#include "llvm/ADT/BitVector.h"
2425
#include "llvm/ADT/DenseMap.h"
2526
#include "llvm/ADT/DenseSet.h"
@@ -93,6 +94,9 @@ using namespace llvm;
9394
namespace {
9495

9596
struct MachineVerifier {
97+
MachineVerifier(MachineFunctionAnalysisManager &MFAM, const char *b)
98+
: MFAM(&MFAM), Banner(b) {}
99+
96100
MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {}
97101

98102
MachineVerifier(const char *b, LiveVariables *LiveVars,
@@ -103,6 +107,7 @@ namespace {
103107

104108
unsigned verify(const MachineFunction &MF);
105109

110+
MachineFunctionAnalysisManager *MFAM = nullptr;
106111
Pass *const PASS = nullptr;
107112
const char *Banner;
108113
const MachineFunction *MF = nullptr;
@@ -302,15 +307,15 @@ namespace {
302307
void verifyProperties(const MachineFunction &MF);
303308
};
304309

305-
struct MachineVerifierPass : public MachineFunctionPass {
310+
struct MachineVerifierLegacyPass : public MachineFunctionPass {
306311
static char ID; // Pass ID, replacement for typeid
307312

308313
const std::string Banner;
309314

310-
MachineVerifierPass(std::string banner = std::string())
311-
: MachineFunctionPass(ID), Banner(std::move(banner)) {
312-
initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
313-
}
315+
MachineVerifierLegacyPass(std::string banner = std::string())
316+
: MachineFunctionPass(ID), Banner(std::move(banner)) {
317+
initializeMachineVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
318+
}
314319

315320
void getAnalysisUsage(AnalysisUsage &AU) const override {
316321
AU.addUsedIfAvailable<LiveStacks>();
@@ -338,13 +343,28 @@ namespace {
338343

339344
} // end anonymous namespace
340345

341-
char MachineVerifierPass::ID = 0;
346+
PreservedAnalyses
347+
MachineVerifierPass::run(MachineFunction &MF,
348+
MachineFunctionAnalysisManager &MFAM) {
349+
// Skip functions that have known verification problems.
350+
// FIXME: Remove this mechanism when all problematic passes have been
351+
// fixed.
352+
if (MF.getProperties().hasProperty(
353+
MachineFunctionProperties::Property::FailsVerification))
354+
return PreservedAnalyses::all();
355+
unsigned FoundErrors = MachineVerifier(MFAM, Banner.c_str()).verify(MF);
356+
if (FoundErrors)
357+
report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors.");
358+
return PreservedAnalyses::all();
359+
}
360+
361+
char MachineVerifierLegacyPass::ID = 0;
342362

343-
INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
363+
INITIALIZE_PASS(MachineVerifierLegacyPass, "machineverifier",
344364
"Verify generated machine code", false, false)
345365

346366
FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
347-
return new MachineVerifierPass(Banner);
367+
return new MachineVerifierLegacyPass(Banner);
348368
}
349369

350370
void llvm::verifyMachineFunction(const std::string &Banner,
@@ -438,6 +458,14 @@ unsigned MachineVerifier::verify(const MachineFunction &MF) {
438458
auto *SIWrapper = PASS->getAnalysisIfAvailable<SlotIndexesWrapperPass>();
439459
Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
440460
}
461+
if (MFAM) {
462+
MachineFunction &Func = const_cast<MachineFunction &>(MF);
463+
LiveInts = MFAM->getCachedResult<LiveIntervalsAnalysis>(Func);
464+
if (!LiveInts)
465+
LiveVars = MFAM->getCachedResult<LiveVariablesAnalysis>(Func);
466+
// TODO: LiveStks = MFAM->getCachedResult<LiveStacksAnalysis>(Func);
467+
Indexes = MFAM->getCachedResult<SlotIndexesAnalysis>(Func);
468+
}
441469

442470
verifySlotIndexes();
443471

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#include "llvm/CodeGen/MachinePassManager.h"
105105
#include "llvm/CodeGen/MachinePostDominators.h"
106106
#include "llvm/CodeGen/MachineRegisterInfo.h"
107+
#include "llvm/CodeGen/MachineVerifier.h"
107108
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
108109
#include "llvm/CodeGen/RegAllocFast.h"
109110
#include "llvm/CodeGen/SafeStack.h"

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/CodeGen/MIRPrinter.h"
2323
#include "llvm/CodeGen/MachineFunction.h"
2424
#include "llvm/CodeGen/MachineModuleInfo.h"
25+
#include "llvm/CodeGen/MachineVerifier.h"
2526
#include "llvm/Demangle/Demangle.h"
2627
#include "llvm/IR/Constants.h"
2728
#include "llvm/IR/Function.h"
@@ -1451,10 +1452,10 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
14511452
});
14521453
}
14531454

1454-
void VerifyInstrumentation::registerCallbacks(
1455-
PassInstrumentationCallbacks &PIC) {
1455+
void VerifyInstrumentation::registerCallbacks(PassInstrumentationCallbacks &PIC,
1456+
ModuleAnalysisManager *MAM) {
14561457
PIC.registerAfterPassCallback(
1457-
[this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1458+
[this, MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
14581459
if (isIgnored(P) || P == "VerifierPass")
14591460
return;
14601461
const auto *F = unwrapIR<Function>(IR);
@@ -1488,15 +1489,23 @@ void VerifyInstrumentation::registerCallbacks(
14881489
P));
14891490
}
14901491

1491-
// TODO: Use complete MachineVerifierPass.
14921492
if (auto *MF = unwrapIR<MachineFunction>(IR)) {
14931493
if (DebugLogging)
14941494
dbgs() << "Verifying machine function " << MF->getName() << '\n';
1495-
verifyMachineFunction(
1495+
std::string Banner =
14961496
formatv("Broken machine function found after pass "
14971497
"\"{0}\", compilation aborted!",
1498-
P),
1499-
*MF);
1498+
P);
1499+
if (MAM) {
1500+
Module &M = const_cast<Module &>(*MF->getFunction().getParent());
1501+
auto &MFAM =
1502+
MAM->getResult<MachineFunctionAnalysisManagerModuleProxy>(M)
1503+
.getManager();
1504+
MachineVerifierPass Verifier(Banner);
1505+
Verifier.run(const_cast<MachineFunction &>(*MF), MFAM);
1506+
} else {
1507+
verifyMachineFunction(Banner, *MF);
1508+
}
15001509
}
15011510
}
15021511
});
@@ -2515,7 +2524,7 @@ void StandardInstrumentations::registerCallbacks(
25152524
PrintChangedIR.registerCallbacks(PIC);
25162525
PseudoProbeVerification.registerCallbacks(PIC);
25172526
if (VerifyEach)
2518-
Verify.registerCallbacks(PIC);
2527+
Verify.registerCallbacks(PIC, MAM);
25192528
PrintChangedDiff.registerCallbacks(PIC);
25202529
WebsiteChangeReporter.registerCallbacks(PIC);
25212530
ChangeTester.registerCallbacks(PIC);

llvm/test/CodeGen/AMDGPU/verifier-sdwa-cvt.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx940 -run-pass machineverifier -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" %s
2+
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx940 --passes='machine-function(verify)' -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" %s
23

34
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
45
# CHECK: $vgpr0 = V_CVT_F32_FP8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec

llvm/test/CodeGen/AMDGPU/verify-constant-bus-violations.mir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx900 -run-pass machineverifier -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX9-ERR %s
22
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx1010 -mattr=-wavefrontsize32,+wavefrontsize64 -run-pass machineverifier -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX10PLUS-ERR %s
33
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 -run-pass machineverifier -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX10PLUS-ERR %s
4+
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx900 --passes='machine-function(verify)' -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX9-ERR %s
5+
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx1010 -mattr=-wavefrontsize32,+wavefrontsize64 --passes='machine-function(verify)' -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX10PLUS-ERR %s
6+
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=-wavefrontsize32,+wavefrontsize64 --passes='machine-function(verify)' -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" -check-prefix=GFX10PLUS-ERR %s
47

58
# GFX9-ERR: *** Bad machine code: VOP* instruction violates constant bus restriction ***
69
# GFX9-ERR: $vgpr0 = V_CNDMASK_B32_e64 0, $sgpr0, 0, -1, killed $sgpr0_sgpr1, implicit $exec

0 commit comments

Comments
 (0)