Skip to content

Commit fe72f81

Browse files
Standardize LLVM module verification (#46387)
* hide away the llvm verification steps behind JL_VERIFY_PASSES in our llvm passes * verify modules before and after optimization runs * Verify passes when compiling with assertions
1 parent e50b32f commit fe72f81

16 files changed

+89
-16
lines changed

Make.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ JULIACODEGEN := LLVM
452452
ifeq ($(FORCE_ASSERTIONS), 1)
453453
# C++ code needs to include LLVM header with the same assertion flag as LLVM
454454
# Use this flag to re-enable assertion in our code after all the LLVM headers are included
455-
CXX_DISABLE_ASSERTION :=
456-
DISABLE_ASSERTIONS :=
455+
CXX_DISABLE_ASSERTION := -DJL_VERIFY_PASSES
456+
DISABLE_ASSERTIONS := -DJL_VERIFY_PASSES
457457
else
458458
CXX_DISABLE_ASSERTION := -DJL_NDEBUG
459459
DISABLE_ASSERTIONS := -DNDEBUG -DJL_NDEBUG

src/aotcompile.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,11 @@ void jl_dump_native_impl(void *native_code,
590590
// do the actual work
591591
auto add_output = [&] (Module &M, StringRef unopt_bc_Name, StringRef bc_Name, StringRef obj_Name, StringRef asm_Name) {
592592
preopt.run(M, empty.MAM);
593-
if (bc_fname || obj_fname || asm_fname) optimizer.run(M);
593+
if (bc_fname || obj_fname || asm_fname) {
594+
assert(!verifyModule(M, &errs()));
595+
optimizer.run(M);
596+
assert(!verifyModule(M, &errs()));
597+
}
594598

595599
// We would like to emit an alias or an weakref alias to redirect these symbols
596600
// but LLVM doesn't let us emit a GlobalAlias to a declaration...
@@ -1031,6 +1035,7 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
10311035
// and will better match what's actually in sysimg.
10321036
for (auto &global : output.globals)
10331037
global.second->setLinkage(GlobalValue::ExternalLinkage);
1038+
assert(!verifyModule(*m.getModuleUnlocked(), &errs()));
10341039
if (optimize) {
10351040
#ifndef JL_USE_NEW_PM
10361041
legacy::PassManager PM;
@@ -1042,6 +1047,7 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
10421047
#endif
10431048
//Safe b/c context lock is held by output
10441049
PM.run(*m.getModuleUnlocked());
1050+
assert(!verifyModule(*m.getModuleUnlocked(), &errs()));
10451051
}
10461052
const std::string *fname;
10471053
if (decls.functionObject == "jl_fptr_args" || decls.functionObject == "jl_fptr_sparam")

src/jitlayers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#if JL_LLVM_VERSION >= 130000
1515
#include <llvm/ExecutionEngine/Orc/ExecutorProcessControl.h>
1616
#endif
17+
#include <llvm/IR/Verifier.h>
1718
#include <llvm/Support/DynamicLibrary.h>
1819
#include <llvm/Support/FormattedStream.h>
1920
#include <llvm/Support/SmallVectorMemoryBuffer.h>
@@ -1106,7 +1107,9 @@ namespace {
11061107
JL_TIMING(LLVM_OPT);
11071108

11081109
//Run the optimization
1110+
assert(!verifyModule(M, &errs()));
11091111
(***PMs).run(M);
1112+
assert(!verifyModule(M, &errs()));
11101113

11111114
uint64_t end_time = 0;
11121115
{

src/llvm-alloc-opt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,9 @@ bool AllocOpt::runOnFunction(Function &F, function_ref<DominatorTree&()> GetDT)
11841184
optimizer.initialize();
11851185
optimizer.optimizeAll();
11861186
bool modified = optimizer.finalize();
1187+
#ifdef JL_VERIFY_PASSES
11871188
assert(!verifyFunction(F, &errs()));
1189+
#endif
11881190
return modified;
11891191
}
11901192

src/llvm-cpufeatures.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ bool lowerCPUFeatures(Module &M)
110110
for (auto I: Materialized) {
111111
I->eraseFromParent();
112112
}
113+
#ifdef JL_VERIFY_PASSES
113114
assert(!verifyModule(M, &errs()));
115+
#endif
114116
return true;
115117
} else {
116118
return false;

src/llvm-demote-float16.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ static bool demoteFloat16(Function &F)
153153
if (erase.size() > 0) {
154154
for (auto V : erase)
155155
V->eraseFromParent();
156+
#ifdef JL_VERIFY_PASSES
156157
assert(!verifyFunction(F, &errs()));
158+
#endif
157159
return true;
158160
}
159161
else

src/llvm-final-gc-lowering.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <llvm/IR/IntrinsicInst.h>
1010
#include <llvm/IR/Module.h>
1111
#include <llvm/IR/IRBuilder.h>
12+
#include <llvm/IR/Verifier.h>
1213
#include <llvm/Pass.h>
1314
#include <llvm/Support/Debug.h>
1415
#include <llvm/Transforms/Utils/ModuleUtils.h>
@@ -390,7 +391,11 @@ bool FinalLowerGCLegacy::doInitialization(Module &M) {
390391
}
391392

392393
bool FinalLowerGCLegacy::doFinalization(Module &M) {
393-
return finalLowerGC.doFinalization(M);
394+
auto ret = finalLowerGC.doFinalization(M);
395+
#ifdef JL_VERIFY_PASSES
396+
assert(!verifyModule(M, &errs()));
397+
#endif
398+
return ret;
394399
}
395400

396401

@@ -405,6 +410,9 @@ PreservedAnalyses FinalLowerGCPass::run(Module &M, ModuleAnalysisManager &AM)
405410
modified |= finalLowerGC.runOnFunction(F);
406411
}
407412
modified |= finalLowerGC.doFinalization(M);
413+
#ifdef JL_VERIFY_PASSES
414+
assert(!verifyModule(M, &errs()));
415+
#endif
408416
if (modified) {
409417
return PreservedAnalyses::allInSet<CFGAnalyses>();
410418
}

src/llvm-julia-licm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ struct JuliaLICM : public JuliaPassContext {
284284
if (changed && SE) {
285285
SE->forgetLoopDispositions(L);
286286
}
287+
#ifdef JL_VERIFY_PASSES
287288
assert(!verifyFunction(*L->getHeader()->getParent(), &errs()));
289+
#endif
288290
return changed;
289291
}
290292
};

src/llvm-late-gc-lowering.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,11 @@ bool LateLowerGCFrameLegacy::runOnFunction(Function &F) {
27412741
return getAnalysis<DominatorTreeWrapperPass>().getDomTree();
27422742
};
27432743
auto lateLowerGCFrame = LateLowerGCFrame(GetDT);
2744-
return lateLowerGCFrame.runOnFunction(F);
2744+
bool modified = lateLowerGCFrame.runOnFunction(F);
2745+
#ifdef JL_VERIFY_PASSES
2746+
assert(!verifyFunction(F, &errs()));
2747+
#endif
2748+
return modified;
27452749
}
27462750

27472751
PreservedAnalyses LateLowerGC::run(Function &F, FunctionAnalysisManager &AM)
@@ -2751,7 +2755,11 @@ PreservedAnalyses LateLowerGC::run(Function &F, FunctionAnalysisManager &AM)
27512755
};
27522756
auto lateLowerGCFrame = LateLowerGCFrame(GetDT);
27532757
bool CFGModified = false;
2754-
if (lateLowerGCFrame.runOnFunction(F, &CFGModified)) {
2758+
bool modified = lateLowerGCFrame.runOnFunction(F, &CFGModified);
2759+
#ifdef JL_VERIFY_PASSES
2760+
assert(!verifyFunction(F, &errs()));
2761+
#endif
2762+
if (modified) {
27552763
if (CFGModified) {
27562764
return PreservedAnalyses::none();
27572765
} else {

src/llvm-lower-handlers.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <llvm/IR/Module.h>
1818
#include <llvm/IR/Value.h>
1919
#include <llvm/IR/LegacyPassManager.h>
20+
#include <llvm/IR/Verifier.h>
2021
#include <llvm/Pass.h>
2122
#include <llvm/Support/Debug.h>
2223
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
@@ -234,7 +235,11 @@ static bool lowerExcHandlers(Function &F) {
234235

235236
PreservedAnalyses LowerExcHandlers::run(Function &F, FunctionAnalysisManager &AM)
236237
{
237-
if (lowerExcHandlers(F)) {
238+
bool modified = lowerExcHandlers(F);
239+
#ifdef JL_VERIFY_PASSES
240+
assert(!verifyFunction(F, &errs()));
241+
#endif
242+
if (modified) {
238243
return PreservedAnalyses::allInSet<CFGAnalyses>();
239244
}
240245
return PreservedAnalyses::all();
@@ -246,7 +251,11 @@ struct LowerExcHandlersLegacy : public FunctionPass {
246251
LowerExcHandlersLegacy() : FunctionPass(ID)
247252
{}
248253
bool runOnFunction(Function &F) {
249-
return lowerExcHandlers(F);
254+
bool modified = lowerExcHandlers(F);
255+
#ifdef JL_VERIFY_PASSES
256+
assert(!verifyFunction(F, &errs()));
257+
#endif
258+
return modified;
250259
}
251260
};
252261

src/llvm-muladd.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ static bool combineMulAdd(Function &F)
8484
}
8585
}
8686
}
87+
#ifdef JL_VERIFY_PASSES
8788
assert(!verifyFunction(F, &errs()));
89+
#endif
8890
return modified;
8991
}
9092

src/llvm-multiversioning.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,9 @@ static bool runMultiVersioning(Module &M, function_ref<LoopInfo&(Function&)> Get
11341134
// At this point, we should have fixed up all the uses of the cloned functions
11351135
// and collected all the shared/target-specific relocations.
11361136
clone.emit_metadata();
1137-
1137+
#ifdef JL_VERIFY_PASSES
11381138
assert(!verifyModule(M, &errs()));
1139+
#endif
11391140

11401141
return true;
11411142
}

src/llvm-propagate-addrspaces.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,11 @@ struct PropagateJuliaAddrspacesLegacy : FunctionPass {
302302

303303
PropagateJuliaAddrspacesLegacy() : FunctionPass(ID) {}
304304
bool runOnFunction(Function &F) override {
305-
return propagateJuliaAddrspaces(F);
305+
bool modified = propagateJuliaAddrspaces(F);
306+
#ifdef JL_VERIFY_PASSES
307+
assert(!verifyFunction(F, &errs()));
308+
#endif
309+
return modified;
306310
}
307311
};
308312

@@ -314,7 +318,12 @@ Pass *createPropagateJuliaAddrspaces() {
314318
}
315319

316320
PreservedAnalyses PropagateJuliaAddrspacesPass::run(Function &F, FunctionAnalysisManager &AM) {
317-
if (propagateJuliaAddrspaces(F)) {
321+
bool modified = propagateJuliaAddrspaces(F);
322+
323+
#ifdef JL_VERIFY_PASSES
324+
assert(!verifyFunction(F, &errs()));
325+
#endif
326+
if (modified) {
318327
return PreservedAnalyses::allInSet<CFGAnalyses>();
319328
} else {
320329
return PreservedAnalyses::all();

src/llvm-ptls.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <llvm/IR/Constants.h>
1717
#include <llvm/IR/LLVMContext.h>
1818
#include <llvm/IR/MDBuilder.h>
19+
#include <llvm/IR/Verifier.h>
1920

2021
#include <llvm/IR/InlineAsm.h>
2122
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
@@ -356,7 +357,11 @@ struct LowerPTLSLegacy: public ModulePass {
356357
bool imaging_mode;
357358
bool runOnModule(Module &M) override {
358359
LowerPTLS lower(M, imaging_mode);
359-
return lower.run(nullptr);
360+
bool modified = lower.run(nullptr);
361+
#ifdef JL_VERIFY_PASSES
362+
assert(!verifyModule(M, &errs()));
363+
#endif
364+
return modified;
360365
}
361366
};
362367

@@ -371,7 +376,11 @@ static RegisterPass<LowerPTLSLegacy> X("LowerPTLS", "LowerPTLS Pass",
371376
PreservedAnalyses LowerPTLSPass::run(Module &M, ModuleAnalysisManager &AM) {
372377
LowerPTLS lower(M, imaging_mode);
373378
bool CFGModified = false;
374-
if (lower.run(&CFGModified)) {
379+
bool modified = lower.run(&CFGModified);
380+
#ifdef JL_VERIFY_PASSES
381+
assert(!verifyModule(M, &errs()));
382+
#endif
383+
if (modified) {
375384
if (CFGModified) {
376385
return PreservedAnalyses::none();
377386
} else {

src/llvm-remove-addrspaces.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <llvm/IR/Instructions.h>
99
#include <llvm/IR/InstIterator.h>
1010
#include <llvm/IR/LegacyPassManager.h>
11+
#include <llvm/IR/Verifier.h>
1112
#include <llvm/Support/Debug.h>
1213
#include <llvm/Transforms/Utils/Cloning.h>
1314
#include <llvm/Transforms/Utils/ValueMapper.h>
@@ -464,7 +465,11 @@ struct RemoveAddrspacesPassLegacy : public ModulePass {
464465

465466
public:
466467
bool runOnModule(Module &M) override {
467-
return removeAddrspaces(M, ASRemapper);
468+
bool modified = removeAddrspaces(M, ASRemapper);
469+
#ifdef JL_VERIFY_PASSES
470+
assert(!verifyModule(M, &errs()));
471+
#endif
472+
return modified;
468473
}
469474
};
470475

@@ -484,7 +489,11 @@ Pass *createRemoveAddrspacesPass(
484489
RemoveAddrspacesPass::RemoveAddrspacesPass() : RemoveAddrspacesPass(removeAllAddrspaces) {}
485490

486491
PreservedAnalyses RemoveAddrspacesPass::run(Module &M, ModuleAnalysisManager &AM) {
487-
if (removeAddrspaces(M, ASRemapper)) {
492+
bool modified = removeAddrspaces(M, ASRemapper);
493+
#ifdef JL_VERIFY_PASSES
494+
assert(!verifyModule(M, &errs()));
495+
#endif
496+
if (modified) {
488497
return PreservedAnalyses::allInSet<CFGAnalyses>();
489498
} else {
490499
return PreservedAnalyses::all();

src/llvm-simdloop.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ static bool markLoopInfo(Module &M, Function *marker, function_ref<LoopInfo &(Fu
232232
for (Instruction *I : ToDelete)
233233
I->deleteValue();
234234
marker->eraseFromParent();
235-
235+
#ifdef JL_VERIFY_PASSES
236236
assert(!verifyModule(M, &errs()));
237+
#endif
237238
return Changed;
238239
}
239240

0 commit comments

Comments
 (0)