Skip to content

Commit 31ebe66

Browse files
authored
Revert "Use global TimerGroups for both new pass manager and old pass manager timers" (#131173)
Reverts #130375 Causes breakages, e.g. https://lab.llvm.org/buildbot/#/builders/160/builds/14607
1 parent aa612f3 commit 31ebe66

File tree

6 files changed

+36
-48
lines changed

6 files changed

+36
-48
lines changed

clang/test/Misc/time-passes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
// NPM: InstCombinePass{{$}}
2020
// NPM-NOT: InstCombinePass #
2121
// TIME: Total{{$}}
22+
// NPM: Pass execution timing report
2223

2324
int foo(int x, int y) { return x + y; }

llvm/include/llvm/IR/PassTimingInfo.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ Timer *getPassTimer(Pass *);
3939
/// This class implements -time-passes functionality for new pass manager.
4040
/// It provides the pass-instrumentation callbacks that measure the pass
4141
/// execution time. They collect timing info into individual timers as
42-
/// passes are being run.
42+
/// passes are being run. At the end of its life-time it prints the resulting
43+
/// timing report.
4344
class TimePassesHandler {
4445
/// Value of this type is capable of uniquely identifying pass invocations.
4546
/// It is a pair of string Pass-Identifier (which for now is common
4647
/// to all the instance of a given pass) + sequential invocation counter.
4748
using PassInvocationID = std::pair<StringRef, unsigned>;
4849

4950
/// Groups of timers for passes and analyses.
50-
TimerGroup &PassTG =
51-
NamedRegionTimer::getNamedTimerGroup(PassGroupName, PassGroupDesc);
52-
TimerGroup &AnalysisTG = NamedRegionTimer::getNamedTimerGroup(
53-
AnalysisGroupName, AnalysisGroupDesc);
51+
TimerGroup PassTG;
52+
TimerGroup AnalysisTG;
5453

5554
using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
5655
/// Map of timers for pass invocations
@@ -72,15 +71,12 @@ class TimePassesHandler {
7271
bool PerRun;
7372

7473
public:
75-
static constexpr StringRef PassGroupName = "pass";
76-
static constexpr StringRef AnalysisGroupName = "analysis";
77-
static constexpr StringRef PassGroupDesc = "Pass execution timing report";
78-
static constexpr StringRef AnalysisGroupDesc =
79-
"Analysis execution timing report";
80-
8174
TimePassesHandler();
8275
TimePassesHandler(bool Enabled, bool PerRun = false);
8376

77+
/// Destructor handles the print action if it has not been handled before.
78+
~TimePassesHandler() { print(); }
79+
8480
/// Prints out timing information and then resets the timers.
8581
void print();
8682

llvm/include/llvm/Support/Timer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,6 @@ struct NamedRegionTimer : public TimeRegion {
169169
explicit NamedRegionTimer(StringRef Name, StringRef Description,
170170
StringRef GroupName,
171171
StringRef GroupDescription, bool Enabled = true);
172-
173-
// Create or get a TimerGroup stored in the same global map owned by
174-
// NamedRegionTimer.
175-
static TimerGroup &getNamedTimerGroup(StringRef GroupName,
176-
StringRef GroupDescription);
177172
};
178173

179174
/// The TimerGroup class is used to group together related timers into a single

llvm/lib/IR/PassTimingInfo.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ class PassTimingInfo {
6363
private:
6464
StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
6565
DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
66-
TimerGroup *PassTG = nullptr;
66+
TimerGroup TG;
6767

6868
public:
69+
/// Default constructor for yet-inactive timeinfo.
70+
/// Use \p init() to activate it.
71+
PassTimingInfo();
72+
73+
/// Print out timing information and release timers.
74+
~PassTimingInfo();
75+
6976
/// Initializes the static \p TheTimeInfo member to a non-null value when
7077
/// -time-passes is enabled. Leaves it null otherwise.
7178
///
@@ -87,6 +94,14 @@ class PassTimingInfo {
8794

8895
static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
8996

97+
PassTimingInfo::PassTimingInfo() : TG("pass", "Pass execution timing report") {}
98+
99+
PassTimingInfo::~PassTimingInfo() {
100+
// Deleting the timers accumulates their info into the TG member.
101+
// Then TG member is (implicitly) deleted, actually printing the report.
102+
TimingData.clear();
103+
}
104+
90105
void PassTimingInfo::init() {
91106
if (TheTimeInfo || !TimePassesIsEnabled)
92107
return;
@@ -95,16 +110,12 @@ void PassTimingInfo::init() {
95110
// This guarantees that the object will be constructed after static globals,
96111
// thus it will be destroyed before them.
97112
static ManagedStatic<PassTimingInfo> TTI;
98-
if (!TTI->PassTG)
99-
TTI->PassTG = &NamedRegionTimer::getNamedTimerGroup(
100-
TimePassesHandler::PassGroupName, TimePassesHandler::PassGroupDesc);
101113
TheTimeInfo = &*TTI;
102114
}
103115

104116
/// Prints out timing information and then resets the timers.
105117
void PassTimingInfo::print(raw_ostream *OutStream) {
106-
assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
107-
PassTG->print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
118+
TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
108119
}
109120

110121
Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
@@ -113,8 +124,7 @@ Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
113124
// Appending description with a pass-instance number for all but the first one
114125
std::string PassDescNumbered =
115126
num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
116-
assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
117-
return new Timer(PassID, PassDescNumbered, *PassTG);
127+
return new Timer(PassID, PassDescNumbered, TG);
118128
}
119129

120130
Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
@@ -183,7 +193,9 @@ Timer &TimePassesHandler::getPassTimer(StringRef PassID, bool IsPass) {
183193
}
184194

185195
TimePassesHandler::TimePassesHandler(bool Enabled, bool PerRun)
186-
: Enabled(Enabled), PerRun(PerRun) {}
196+
: PassTG("pass", "Pass execution timing report"),
197+
AnalysisTG("analysis", "Analysis execution timing report"),
198+
Enabled(Enabled), PerRun(PerRun) {}
187199

188200
TimePassesHandler::TimePassesHandler()
189201
: TimePassesHandler(TimePassesIsEnabled, TimePassesPerRun) {}

llvm/lib/Support/Timer.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,15 @@ class Name2PairMap {
222222
StringRef GroupDescription) {
223223
sys::SmartScopedLock<true> L(timerLock());
224224

225-
std::pair<TimerGroup *, Name2TimerMap> &GroupEntry =
226-
getGroupEntry(GroupName, GroupDescription);
227-
Timer &T = GroupEntry.second[Name];
228-
if (!T.isInitialized())
229-
T.init(Name, Description, *GroupEntry.first);
230-
return T;
231-
}
232-
233-
TimerGroup &getTimerGroup(StringRef GroupName, StringRef GroupDescription) {
234-
return *getGroupEntry(GroupName, GroupDescription).first;
235-
}
225+
std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
236226

237-
private:
238-
std::pair<TimerGroup *, Name2TimerMap> &
239-
getGroupEntry(StringRef GroupName, StringRef GroupDescription) {
240-
std::pair<TimerGroup *, Name2TimerMap> &GroupEntry = Map[GroupName];
241227
if (!GroupEntry.first)
242228
GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
243229

244-
return GroupEntry;
230+
Timer &T = GroupEntry.second[Name];
231+
if (!T.isInitialized())
232+
T.init(Name, Description, *GroupEntry.first);
233+
return T;
245234
}
246235
};
247236

@@ -255,11 +244,6 @@ NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
255244
: &namedGroupedTimers().get(Name, Description, GroupName,
256245
GroupDescription)) {}
257246

258-
TimerGroup &NamedRegionTimer::getNamedTimerGroup(StringRef GroupName,
259-
StringRef GroupDescription) {
260-
return namedGroupedTimers().getTimerGroup(GroupName, GroupDescription);
261-
}
262-
263247
//===----------------------------------------------------------------------===//
264248
// TimerGroup Implementation
265249
//===----------------------------------------------------------------------===//

llvm/unittests/IR/TimePassesTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ TEST(TimePassesTest, CustomOut) {
161161
PI.runBeforePass(Pass2, M);
162162
PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
163163

164-
// Clear and generate report again.
165-
TimePasses->print();
164+
// Generate report by deleting the handler.
165+
TimePasses.reset();
166166

167167
// There should be Pass2 in this report and no Pass1.
168168
EXPECT_FALSE(TimePassesStr.str().empty());

0 commit comments

Comments
 (0)