@@ -160,7 +160,8 @@ struct Timer {
160
160
};
161
161
162
162
struct PassTiming : public PassInstrumentation {
163
- PassTiming (PassDisplayMode displayMode) : displayMode(displayMode) {}
163
+ PassTiming (std::unique_ptr<PassManager::PassTimingConfig> config)
164
+ : config(std::move(config)) {}
164
165
~PassTiming () override { print (); }
165
166
166
167
// / Setup the instrumentation hooks.
@@ -231,8 +232,8 @@ struct PassTiming : public PassInstrumentation {
231
232
// / A stack of the currently active pass timers per thread.
232
233
DenseMap<uint64_t , SmallVector<Timer *, 4 >> activeThreadTimers;
233
234
234
- // / The display mode to use when printing the timing results.
235
- PassDisplayMode displayMode ;
235
+ // / The configuration object to use when printing the timing results.
236
+ std::unique_ptr<PassManager::PassTimingConfig> config ;
236
237
237
238
// / A mapping of pipeline timers that need to be merged into the parent
238
239
// / collection. The timers are mapped to the parent info to merge into.
@@ -353,28 +354,37 @@ void PassTiming::print() {
353
354
return ;
354
355
355
356
assert (rootTimers.size () == 1 && " expected one remaining root timer" );
356
- auto &rootTimer = rootTimers.begin ()->second ;
357
- auto os = llvm::CreateInfoOutputFile ();
358
-
359
- // Print the timer header.
360
- TimeRecord totalTime = rootTimer->getTotalTime ();
361
- printTimerHeader (*os, totalTime);
362
-
363
- // Defer to a specialized printer for each display mode.
364
- switch (displayMode) {
365
- case PassDisplayMode::List:
366
- printResultsAsList (*os, rootTimer.get (), totalTime);
367
- break ;
368
- case PassDisplayMode::Pipeline:
369
- printResultsAsPipeline (*os, rootTimer.get (), totalTime);
370
- break ;
371
- }
372
- printTimeEntry (*os, 0 , " Total" , totalTime, totalTime);
373
- os->flush ();
374
357
375
- // Reset root timers.
376
- rootTimers.clear ();
377
- activeThreadTimers.clear ();
358
+ auto printCallback = [&](raw_ostream &os) {
359
+ auto &rootTimer = rootTimers.begin ()->second ;
360
+ // Print the timer header.
361
+ TimeRecord totalTime = rootTimer->getTotalTime ();
362
+ printTimerHeader (os, totalTime);
363
+ // Defer to a specialized printer for each display mode.
364
+ switch (config->getDisplayMode ()) {
365
+ case PassDisplayMode::List:
366
+ printResultsAsList (os, rootTimer.get (), totalTime);
367
+ break ;
368
+ case PassDisplayMode::Pipeline:
369
+ printResultsAsPipeline (os, rootTimer.get (), totalTime);
370
+ break ;
371
+ }
372
+ printTimeEntry (os, 0 , " Total" , totalTime, totalTime);
373
+ os.flush ();
374
+
375
+ // Reset root timers.
376
+ rootTimers.clear ();
377
+ activeThreadTimers.clear ();
378
+ };
379
+
380
+ config->printTiming (printCallback);
381
+ }
382
+
383
+ // The default implementation for printTiming uses
384
+ // `llvm::CreateInfoOutputFile()` as stream, it can be overridden by clients
385
+ // to customize the output.
386
+ void PassManager::PassTimingConfig::printTiming (PrintCallbackFn printCallback) {
387
+ printCallback (*llvm::CreateInfoOutputFile ());
378
388
}
379
389
380
390
// / Print the timing result in list mode.
@@ -449,16 +459,21 @@ void PassTiming::printResultsAsPipeline(raw_ostream &os, Timer *root,
449
459
printTimer (0 , topLevelTimer.second .get ());
450
460
}
451
461
462
+ // Out-of-line as key function.
463
+ PassManager::PassTimingConfig::~PassTimingConfig () {}
464
+
452
465
// ===----------------------------------------------------------------------===//
453
466
// PassManager
454
467
// ===----------------------------------------------------------------------===//
455
468
456
469
// / Add an instrumentation to time the execution of passes and the computation
457
470
// / of analyses.
458
- void PassManager::enableTiming (PassDisplayMode displayMode ) {
471
+ void PassManager::enableTiming (std::unique_ptr<PassTimingConfig> config ) {
459
472
// Check if pass timing is already enabled.
460
473
if (passTiming)
461
474
return ;
462
- addInstrumentation (std::make_unique<PassTiming>(displayMode));
475
+ if (!config)
476
+ config = std::make_unique<PassManager::PassTimingConfig>();
477
+ addInstrumentation (std::make_unique<PassTiming>(std::move (config)));
463
478
passTiming = true ;
464
479
}
0 commit comments