Skip to content

Commit 8443cdd

Browse files
committed
Add hook for target verifier in llc,opt
1 parent 7b8cde7 commit 8443cdd

16 files changed

+68
-21
lines changed

llvm/include/llvm/Passes/StandardInstrumentations.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ class VerifyInstrumentation {
464464
public:
465465
VerifyInstrumentation(bool DebugLogging) : DebugLogging(DebugLogging) {}
466466
void registerCallbacks(PassInstrumentationCallbacks &PIC,
467-
ModuleAnalysisManager *MAM);
467+
ModuleAnalysisManager *MAM,
468+
FunctionAnalysisManager *FAM);
468469
};
469470

470471
/// This class implements --time-trace functionality for new pass manager.
@@ -609,7 +610,8 @@ class StandardInstrumentations {
609610
// Register all the standard instrumentation callbacks. If \p FAM is nullptr
610611
// then PreservedCFGChecker is not enabled.
611612
void registerCallbacks(PassInstrumentationCallbacks &PIC,
612-
ModuleAnalysisManager *MAM = nullptr);
613+
ModuleAnalysisManager *MAM,
614+
FunctionAnalysisManager *FAM);
613615

614616
TimePassesHandler &getTimePasses() { return TimePasses; }
615617
};

llvm/include/llvm/Target/TargetVerifier.h

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TargetVerify {
7575
MessagesStr(Messages) {}
7676

7777
void run(Function &F) {};
78+
void run(Function &F, FunctionAnalysisManager &AM);
7879
};
7980

8081
} // namespace llvm

llvm/include/llvm/Target/TargetVerify/AMDGPUTargetVerifier.h

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
#include "llvm/Target/TargetVerifier.h"
2424

25+
#include "llvm/Analysis/UniformityAnalysis.h"
26+
#include "llvm/Analysis/PostDominators.h"
27+
#include "llvm/IR/Dominators.h"
28+
2529
namespace llvm {
2630

2731
class Function;
@@ -31,6 +35,20 @@ class AMDGPUTargetVerifierPass : public TargetVerifierPass {
3135
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
3236
};
3337

38+
class AMDGPUTargetVerify : public TargetVerify {
39+
public:
40+
Module *Mod;
41+
42+
DominatorTree *DT;
43+
PostDominatorTree *PDT;
44+
UniformityInfo *UA;
45+
46+
AMDGPUTargetVerify(Module *Mod, DominatorTree *DT, PostDominatorTree *PDT, UniformityInfo *UA)
47+
: TargetVerify(Mod), Mod(Mod), DT(DT), PDT(PDT), UA(UA) {}
48+
49+
void run(Function &F);
50+
};
51+
3452
} // namespace llvm
3553

3654
#endif // LLVM_AMDGPU_TARGET_VERIFIER_H

llvm/lib/LTO/LTOBackend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
270270
PassInstrumentationCallbacks PIC;
271271
StandardInstrumentations SI(Mod.getContext(), Conf.DebugPassManager,
272272
Conf.VerifyEach);
273-
SI.registerCallbacks(PIC, &MAM);
273+
SI.registerCallbacks(PIC, &MAM, &FAM);
274274
PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
275275

276276
RegisterPassPlugins(Conf.PassPlugins, PB);

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
245245

246246
PassInstrumentationCallbacks PIC;
247247
StandardInstrumentations SI(TheModule.getContext(), DebugPassManager);
248-
SI.registerCallbacks(PIC, &MAM);
248+
SI.registerCallbacks(PIC, &MAM, &FAM);
249249
PipelineTuningOptions PTO;
250250
PTO.LoopVectorization = true;
251251
PTO.SLPVectorization = true;

llvm/lib/Passes/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_llvm_component_library(LLVMPasses
3030
Scalar
3131
Support
3232
Target
33+
TargetParser
3334
TransformUtils
3435
Vectorize
3536
Instrumentation

llvm/lib/Passes/PassBuilderBindings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static LLVMErrorRef runPasses(Module *Mod, Function *Fun, const char *Passes,
7676
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
7777

7878
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach);
79-
SI.registerCallbacks(PIC, &MAM);
79+
SI.registerCallbacks(PIC, &MAM, &FAM);
8080

8181
// Run the pipeline.
8282
if (Fun) {

llvm/lib/Passes/StandardInstrumentations.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "llvm/Support/Signals.h"
4646
#include "llvm/Support/raw_ostream.h"
4747
#include "llvm/Support/xxhash.h"
48+
#include "llvm/Target/TargetVerifier.h"
4849
#include <unordered_map>
4950
#include <unordered_set>
5051
#include <utility>
@@ -1461,9 +1462,10 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
14611462
}
14621463

14631464
void VerifyInstrumentation::registerCallbacks(PassInstrumentationCallbacks &PIC,
1464-
ModuleAnalysisManager *MAM) {
1465+
ModuleAnalysisManager *MAM,
1466+
FunctionAnalysisManager *FAM) {
14651467
PIC.registerAfterPassCallback(
1466-
[this, MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1468+
[this, MAM, FAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
14671469
if (isIgnored(P) || P == "VerifierPass")
14681470
return;
14691471
const auto *F = unwrapIR<Function>(IR);
@@ -1480,6 +1482,15 @@ void VerifyInstrumentation::registerCallbacks(PassInstrumentationCallbacks &PIC,
14801482
report_fatal_error(formatv("Broken function found after pass "
14811483
"\"{0}\", compilation aborted!",
14821484
P));
1485+
1486+
if (FAM) {
1487+
TargetVerify TV(const_cast<Module*>(F->getParent()));
1488+
TV.run(*const_cast<Function*>(F), *FAM);
1489+
if (!F->getParent()->IsValid)
1490+
report_fatal_error(formatv("Broken function found after pass "
1491+
"\"{0}\", compilation aborted!",
1492+
P));
1493+
}
14831494
} else {
14841495
const auto *M = unwrapIR<Module>(IR);
14851496
if (!M) {
@@ -2524,7 +2535,7 @@ void PrintCrashIRInstrumentation::registerCallbacks(
25242535
}
25252536

25262537
void StandardInstrumentations::registerCallbacks(
2527-
PassInstrumentationCallbacks &PIC, ModuleAnalysisManager *MAM) {
2538+
PassInstrumentationCallbacks &PIC, ModuleAnalysisManager *MAM, FunctionAnalysisManager *FAM) {
25282539
PrintIR.registerCallbacks(PIC);
25292540
PrintPass.registerCallbacks(PIC);
25302541
TimePasses.registerCallbacks(PIC);
@@ -2533,7 +2544,7 @@ void StandardInstrumentations::registerCallbacks(
25332544
PrintChangedIR.registerCallbacks(PIC);
25342545
PseudoProbeVerification.registerCallbacks(PIC);
25352546
if (VerifyEach)
2536-
Verify.registerCallbacks(PIC, MAM);
2547+
Verify.registerCallbacks(PIC, MAM, FAM);
25372548
PrintChangedDiff.registerCallbacks(PIC);
25382549
WebsiteChangeReporter.registerCallbacks(PIC);
25392550
ChangeTester.registerCallbacks(PIC);

llvm/lib/Target/AMDGPU/AMDGPUTargetVerifier.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
using namespace llvm;
1616

17-
static cl::opt<bool>
18-
MarkUniform("mark-uniform", cl::desc("Mark instructions as uniform"), cl::init(false));
17+
//static cl::opt<bool>
18+
//MarkUniform("mark-uniform", cl::desc("Mark instructions as uniform"), cl::init(false));
1919

2020
// Check - We know that cond should be true, if not print an error message.
2121
#define Check(C, ...) \
@@ -81,7 +81,7 @@ static bool isMFMA(unsigned IID) {
8181
}
8282

8383
namespace llvm {
84-
class AMDGPUTargetVerify : public TargetVerify {
84+
/*class AMDGPUTargetVerify : public TargetVerify {
8585
public:
8686
Module *Mod;
8787
@@ -93,7 +93,7 @@ class AMDGPUTargetVerify : public TargetVerify {
9393
: TargetVerify(Mod), Mod(Mod), DT(DT), PDT(PDT), UA(UA) {}
9494
9595
void run(Function &F);
96-
};
96+
};*/
9797

9898
static bool IsValidInt(const Type *Ty) {
9999
return Ty->isIntegerTy(1) ||
@@ -129,8 +129,8 @@ void AMDGPUTargetVerify::run(Function &F) {
129129
for (auto &BB : F) {
130130

131131
for (auto &I : BB) {
132-
if (MarkUniform)
133-
outs() << UA->isUniform(&I) << ' ' << I << '\n';
132+
//if (MarkUniform)
133+
//outs() << UA->isUniform(&I) << ' ' << I << '\n';
134134

135135
// Ensure integral types are valid: i8, i16, i32, i64, i128
136136
if (I.getType()->isIntegerTy())

llvm/lib/Target/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ add_llvm_component_library(LLVMTarget
88
TargetLoweringObjectFile.cpp
99
TargetMachine.cpp
1010
TargetMachineC.cpp
11+
TargetVerifier.cpp
12+
AMDGPU/AMDGPUTargetVerifier.cpp
1113

1214
ADDITIONAL_HEADER_DIRS
1315
${LLVM_MAIN_INCLUDE_DIR}/llvm/Target
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: not not llc -mtriple=amdgcn -mcpu=gfx900 -verify-machineinstrs -enable-new-pm -verify-each -o - < %s 2>&1 | FileCheck %s
2+
3+
define amdgpu_cs i32 @nonvoid_shader() {
4+
; CHECK: LLVM ERROR
5+
ret i32 0
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: llc -mtriple=amdgcn -mcpu=gfx900 -verify-machineinstrs -enable-new-pm -verify-each %s -o - 2>&1 | FileCheck %s
2+
3+
define amdgpu_cs void @void_shader() {
4+
; CHECK: ModuleToFunctionPassAdaptor
5+
ret void
6+
}

llvm/tools/llc/NewPMDriver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int llvm::compileModuleWithNewPM(
126126
PB.registerLoopAnalyses(LAM);
127127
PB.registerMachineFunctionAnalyses(MFAM);
128128
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
129-
SI.registerCallbacks(PIC, &MAM);
129+
SI.registerCallbacks(PIC, &MAM, &FAM);
130130

131131
FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
132132
MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });

llvm/tools/llvm-tgt-verify/llvm-tgt-verify.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int main(int argc, char **argv) {
147147
PB.registerMachineFunctionAnalyses(MFAM);
148148
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
149149

150-
SI.registerCallbacks(PIC, &MAM);
150+
SI.registerCallbacks(PIC, &MAM, &FAM);
151151

152152
//FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
153153

llvm/tools/opt/NewPMDriver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ bool llvm::runPassPipeline(
409409
PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
410410
StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
411411
VK == VerifierKind::EachPass, PrintPassOpts);
412-
SI.registerCallbacks(PIC, &MAM);
412+
SI.registerCallbacks(PIC, &MAM, &FAM);
413413
DebugifyEachInstrumentation Debugify;
414414
DebugifyStatsMap DIStatsMap;
415415
DebugInfoPerPass DebugInfoBeforePass;

llvm/unittests/IR/PassManagerTest.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ TEST_F(PassManagerTest, FunctionPassCFGChecker) {
828828
FunctionPassManager FPM;
829829
PassInstrumentationCallbacks PIC;
830830
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
831-
SI.registerCallbacks(PIC, &MAM);
831+
SI.registerCallbacks(PIC, &MAM, &FAM);
832832
MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
833833
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
834834
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
@@ -877,7 +877,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerInvalidateAnalysis) {
877877
FunctionPassManager FPM;
878878
PassInstrumentationCallbacks PIC;
879879
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
880-
SI.registerCallbacks(PIC, &MAM);
880+
SI.registerCallbacks(PIC, &MAM, &FAM);
881881
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
882882
MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
883883
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
@@ -945,7 +945,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerWrapped) {
945945
FunctionPassManager FPM;
946946
PassInstrumentationCallbacks PIC;
947947
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
948-
SI.registerCallbacks(PIC, &MAM);
948+
SI.registerCallbacks(PIC, &MAM, &FAM);
949949
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
950950
MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
951951
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });

0 commit comments

Comments
 (0)