Skip to content

Commit bfc6590

Browse files
committed
[PassManager] Run PassInstrumentation after analysis invalidation
This allows instrumentation to inspect cached analyses to verify them. The CGSCC PassInstrumentation previously ran `runAfterPass()` on the original SCC, but really it should be running on UpdatedC when relevant since that's the relevant SCC after the pass. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D146096
1 parent 942bc18 commit bfc6590

File tree

6 files changed

+28
-36
lines changed

6 files changed

+28
-36
lines changed

llvm/include/llvm/IR/PassManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,14 @@ class PassManager : public PassInfoMixin<
516516

517517
PreservedAnalyses PassPA = Pass->run(IR, AM, ExtraArgs...);
518518

519-
// Call onto PassInstrumentation's AfterPass callbacks immediately after
520-
// running the pass.
521-
PI.runAfterPass<IRUnitT>(*Pass, IR, PassPA);
522-
523519
// Update the analysis manager as each pass runs and potentially
524520
// invalidates analyses.
525521
AM.invalidate(IR, PassPA);
526522

523+
// Call onto PassInstrumentation's AfterPass callbacks immediately after
524+
// running the pass.
525+
PI.runAfterPass<IRUnitT>(*Pass, IR, PassPA);
526+
527527
// Finally, intersect the preserved analyses to compute the aggregate
528528
// preserved set for this pass manager.
529529
PA.intersect(std::move(PassPA));

llvm/lib/Analysis/CGSCCPassManager.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,
8686

8787
PreservedAnalyses PassPA = Pass->run(*C, AM, G, UR);
8888

89-
if (UR.InvalidatedSCCs.count(C))
90-
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
91-
else
92-
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
93-
9489
// Update the SCC if necessary.
9590
C = UR.UpdatedC ? UR.UpdatedC : C;
9691
if (UR.UpdatedC) {
@@ -107,6 +102,7 @@ PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,
107102
// If the CGSCC pass wasn't able to provide a valid updated SCC, the
108103
// current SCC may simply need to be skipped if invalid.
109104
if (UR.InvalidatedSCCs.count(C)) {
105+
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
110106
LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n");
111107
break;
112108
}
@@ -117,6 +113,8 @@ PassManager<LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,
117113
// Update the analysis manager as each pass runs and potentially
118114
// invalidates analyses.
119115
AM.invalidate(*C, PassPA);
116+
117+
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
120118
}
121119

122120
// Before we mark all of *this* SCC's analyses as preserved below, intersect
@@ -276,11 +274,6 @@ ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
276274

277275
PreservedAnalyses PassPA = Pass->run(*C, CGAM, CG, UR);
278276

279-
if (UR.InvalidatedSCCs.count(C))
280-
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
281-
else
282-
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
283-
284277
// Update the SCC and RefSCC if necessary.
285278
C = UR.UpdatedC ? UR.UpdatedC : C;
286279

@@ -301,6 +294,7 @@ ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
301294
// If the CGSCC pass wasn't able to provide a valid updated SCC,
302295
// the current SCC may simply need to be skipped if invalid.
303296
if (UR.InvalidatedSCCs.count(C)) {
297+
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
304298
LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n");
305299
break;
306300
}
@@ -316,6 +310,8 @@ ModuleToPostOrderCGSCCPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) {
316310
// processed.
317311
CGAM.invalidate(*C, PassPA);
318312

313+
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
314+
319315
// The pass may have restructured the call graph and refined the
320316
// current SCC and/or RefSCC. We need to update our current SCC and
321317
// RefSCC pointers to follow these. Also, when the current SCC is
@@ -408,25 +404,27 @@ PreservedAnalyses DevirtSCCRepeatedPass::run(LazyCallGraph::SCC &InitialC,
408404

409405
PreservedAnalyses PassPA = Pass->run(*C, AM, CG, UR);
410406

411-
if (UR.InvalidatedSCCs.count(C))
412-
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
413-
else
414-
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
415-
416407
PA.intersect(PassPA);
417408

418-
// If the SCC structure has changed, bail immediately and let the outer
419-
// CGSCC layer handle any iteration to reflect the refined structure.
420-
if (UR.UpdatedC && UR.UpdatedC != C)
421-
break;
422-
423409
// If the CGSCC pass wasn't able to provide a valid updated SCC, the
424410
// current SCC may simply need to be skipped if invalid.
425411
if (UR.InvalidatedSCCs.count(C)) {
412+
PI.runAfterPassInvalidated<LazyCallGraph::SCC>(*Pass, PassPA);
426413
LLVM_DEBUG(dbgs() << "Skipping invalidated root or island SCC!\n");
427414
break;
428415
}
429416

417+
// Update the analysis manager with each run and intersect the total set
418+
// of preserved analyses so we're ready to iterate.
419+
AM.invalidate(*C, PassPA);
420+
421+
PI.runAfterPass<LazyCallGraph::SCC>(*Pass, *C, PassPA);
422+
423+
// If the SCC structure has changed, bail immediately and let the outer
424+
// CGSCC layer handle any iteration to reflect the refined structure.
425+
if (UR.UpdatedC && UR.UpdatedC != C)
426+
break;
427+
430428
assert(C->begin() != C->end() && "Cannot have an empty SCC!");
431429

432430
// Check whether any of the handles were devirtualized.
@@ -490,10 +488,6 @@ PreservedAnalyses DevirtSCCRepeatedPass::run(LazyCallGraph::SCC &InitialC,
490488

491489
// Move over the new call counts in preparation for iterating.
492490
CallCounts = std::move(NewCallCounts);
493-
494-
// Update the analysis manager with each run and intersect the total set
495-
// of preserved analyses so we're ready to iterate.
496-
AM.invalidate(*C, PassPA);
497491
}
498492

499493
// Note that we don't add any preserved entries here unlike a more normal
@@ -539,13 +533,14 @@ PreservedAnalyses CGSCCToFunctionPassAdaptor::run(LazyCallGraph::SCC &C,
539533
continue;
540534

541535
PreservedAnalyses PassPA = Pass->run(F, FAM);
542-
PI.runAfterPass<Function>(*Pass, F, PassPA);
543536

544537
// We know that the function pass couldn't have invalidated any other
545538
// function's analyses (that's the contract of a function pass), so
546539
// directly handle the function analysis manager's invalidation here.
547540
FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
548541

542+
PI.runAfterPass<Function>(*Pass, F, PassPA);
543+
549544
// Then intersect the preserved set so that invalidation of module
550545
// analyses will eventually occur when the module pass completes.
551546
PA.intersect(std::move(PassPA));

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Error MachineFunctionPassManager::run(Module &M,
9191

9292
// TODO: EmitSizeRemarks
9393
PreservedAnalyses PassPA = P->run(MF, MFAM);
94-
PI.runAfterPass(*P, MF, PassPA);
9594
MFAM.invalidate(MF, PassPA);
95+
PI.runAfterPass(*P, MF, PassPA);
9696
}
9797
}
9898
} while (true);

llvm/lib/IR/PassManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,14 @@ PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M,
122122
continue;
123123

124124
PreservedAnalyses PassPA = Pass->run(F, FAM);
125-
PI.runAfterPass(*Pass, F, PassPA);
126125

127126
// We know that the function pass couldn't have invalidated any other
128127
// function's analyses (that's the contract of a function pass), so
129128
// directly handle the function analysis manager's invalidation here.
130129
FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
131130

131+
PI.runAfterPass(*Pass, F, PassPA);
132+
132133
// Then intersect the preserved set so that invalidation of module
133134
// analyses will eventually occur when the module pass completes.
134135
PA.intersect(std::move(PassPA));

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,6 @@ void PreservedCFGCheckerInstrumentation::registerCallbacks(
10981098
if (!F)
10991099
return;
11001100

1101-
if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
1102-
!PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
1103-
return;
1104-
11051101
auto CheckCFG = [](StringRef Pass, StringRef FuncName,
11061102
const CFG &GraphBefore, const CFG &GraphAfter) {
11071103
if (GraphAfter == GraphBefore)

llvm/test/Other/scc-deleted-printer.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
; RUN: -passes=inline -print-before-all -print-after-all -print-module-scope | FileCheck %s
55

66
; CHECK: IR Dump Before InlinerPass on (tester, foo)
7-
; CHECK: IR Dump After InlinerPass on (tester, foo) (invalidated)
7+
; CHECK: IR Dump After InlinerPass on (tester, foo)
88
; CHECK: IR Dump Before InlinerPass on (tester)
99
; CHECK: IR Dump After InlinerPass on (tester)
1010

0 commit comments

Comments
 (0)