Skip to content

Commit b00c8b9

Browse files
authored
[PassTimingInfo] Handle nested timers in passes (#70165)
Some pass can call other passes, such as CoroCleanupPass. Make pass timers work with nested passes.
1 parent 2637467 commit b00c8b9

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

llvm/include/llvm/IR/PassTimingInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ class TimePassesHandler {
5555
/// Map of timers for pass invocations
5656
StringMap<TimerVector> TimingData;
5757

58-
/// Currently active pass timer.
59-
Timer *ActivePassTimer = nullptr;
58+
/// Stack of currently active pass timers. Passes can run other
59+
/// passes.
60+
SmallVector<Timer *, 8> PassActiveTimerStack;
6061
/// Stack of currently active analysis timers. Analyses can request other
6162
/// analyses.
6263
SmallVector<Timer *, 8> AnalysisActiveTimerStack;

llvm/lib/IR/PassTimingInfo.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,32 @@ static bool shouldIgnorePass(StringRef PassID) {
252252
void TimePassesHandler::startPassTimer(StringRef PassID) {
253253
if (shouldIgnorePass(PassID))
254254
return;
255-
assert(!ActivePassTimer && "should only have one pass timer at a time");
255+
// Stop the previous pass timer to prevent double counting when a
256+
// pass requests another pass.
257+
if (!PassActiveTimerStack.empty()) {
258+
assert(PassActiveTimerStack.back()->isRunning());
259+
PassActiveTimerStack.back()->stopTimer();
260+
}
256261
Timer &MyTimer = getPassTimer(PassID, /*IsPass*/ true);
257-
ActivePassTimer = &MyTimer;
262+
PassActiveTimerStack.push_back(&MyTimer);
258263
assert(!MyTimer.isRunning());
259264
MyTimer.startTimer();
260265
}
261266

262267
void TimePassesHandler::stopPassTimer(StringRef PassID) {
263268
if (shouldIgnorePass(PassID))
264269
return;
265-
assert(ActivePassTimer);
266-
assert(ActivePassTimer->isRunning());
267-
ActivePassTimer->stopTimer();
268-
ActivePassTimer = nullptr;
270+
assert(!PassActiveTimerStack.empty() && "empty stack in popTimer");
271+
Timer *MyTimer = PassActiveTimerStack.pop_back_val();
272+
assert(MyTimer && "timer should be present");
273+
assert(MyTimer->isRunning());
274+
MyTimer->stopTimer();
275+
276+
// Restart the previously stopped timer.
277+
if (!PassActiveTimerStack.empty()) {
278+
assert(!PassActiveTimerStack.back()->isRunning());
279+
PassActiveTimerStack.back()->startTimer();
280+
}
269281
}
270282

271283
void TimePassesHandler::startAnalysisTimer(StringRef PassID) {

llvm/test/Other/time-passes.ll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
; RUN: opt < %s -disable-output -passes='default<O2>' -time-passes 2>&1 | FileCheck %s --check-prefix=TIME
22
;
33
; For new pass manager, check that -time-passes-per-run emit one report for each pass run.
4-
; RUN: opt < %s -disable-output -passes='instcombine,instcombine,loop-mssa(licm)' -time-passes-per-run 2>&1 | FileCheck %s --check-prefix=TIME --check-prefix=TIME-PER-RUN
4+
; RUN: opt < %s -disable-output -passes='coro-cleanup,function(instcombine,instcombine,loop-mssa(licm))' -time-passes-per-run 2>&1 | FileCheck %s --check-prefix=TIME --check-prefix=TIME-PER-RUN --check-prefix=TIME-PER-RUN-CORO
55
; RUN: opt < %s -disable-output -passes='instcombine,loop-mssa(licm),instcombine,loop-mssa(licm)' -time-passes-per-run 2>&1 | FileCheck %s --check-prefix=TIME --check-prefix=TIME-PER-RUN -check-prefix=TIME-DOUBLE-LICM
66
;
77
; For new pass manager, check that -time-passes emit one report for each pass.
8-
; RUN: opt < %s -disable-output -passes='instcombine,instcombine,loop-mssa(licm)' -time-passes 2>&1 | FileCheck %s --check-prefixes=TIME,TIME-PER-PASS
8+
; RUN: opt < %s -disable-output -passes='coro-cleanup,function(instcombine,instcombine,loop-mssa(licm))' -time-passes 2>&1 | FileCheck %s --check-prefixes=TIME,TIME-PER-PASS,TIME-PER-PASS-CORO
99
; RUN: opt < %s -disable-output -passes='instcombine,loop-mssa(licm),instcombine,loop-mssa(licm)' -time-passes 2>&1 | FileCheck %s --check-prefixes=TIME,TIME-PER-PASS
1010
;
1111
; The following 2 test runs verify -info-output-file interaction (default goes to stderr, '-' goes to stdout).
@@ -30,11 +30,15 @@
3030
; TIME-PER-RUN-DAG: LCSSAPass
3131
; TIME-PER-RUN-DAG: LoopSimplifyPass
3232
; TIME-PER-RUN-DAG: VerifierPass
33+
; TIME-PER-RUN-CORO-DAG: SimplifyCFGPass #1
34+
; TIME-PER-RUN-CORO-DAG: CoroCleanupPass #1
3335
; TIME-PER-PASS-DAG: InstCombinePass
3436
; TIME-PER-PASS-DAG: LICMPass
3537
; TIME-PER-PASS-DAG: LCSSAPass
3638
; TIME-PER-PASS-DAG: LoopSimplifyPass
3739
; TIME-PER-PASS-DAG: VerifierPass
40+
; TIME-PER-PASS-CORO-DAG: SimplifyCFGPass
41+
; TIME-PER-PASS-CORO-DAG: CoroCleanupPass
3842
; TIME-PER-PASS-NOT: InstCombinePass #
3943
; TIME-PER-PASS-NOT: LICMPass #
4044
; TIME-PER-PASS-NOT: LCSSAPass #
@@ -78,3 +82,10 @@ end:
7882
ret void
7983

8084
}
85+
86+
define void @baz_coro() {
87+
%unused = call ptr @llvm.coro.begin(token none, ptr null)
88+
ret void
89+
}
90+
91+
declare ptr @llvm.coro.begin(token, ptr)

0 commit comments

Comments
 (0)