Skip to content

Commit 3a8d8ee

Browse files
Refactored TimeTraceProfilerEntry to allow easier extensibility. Implemented inserting Instant Events with a single function.
1 parent 6d3e447 commit 3a8d8ee

File tree

6 files changed

+255
-174
lines changed

6 files changed

+255
-174
lines changed

clang/lib/Sema/Sema.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace sema {
160160
class SemaPPCallbacks : public PPCallbacks {
161161
Sema *S = nullptr;
162162
llvm::SmallVector<SourceLocation, 8> IncludeStack;
163-
llvm::SmallVector<llvm::TimeTraceProfilerEntry *, 8> ProfilerStack;
163+
llvm::SmallVector<std::shared_ptr<llvm::DurableEvent>, 8> ProfilerStack;
164164

165165
public:
166166
void set(Sema &S) { this->S = &S; }
@@ -192,8 +192,10 @@ class SemaPPCallbacks : public PPCallbacks {
192192
}
193193
case ExitFile:
194194
if (!IncludeStack.empty()) {
195-
if (llvm::timeTraceProfilerEnabled())
196-
llvm::timeTraceProfilerEnd(ProfilerStack.pop_back_val());
195+
if (llvm::timeTraceProfilerEnabled()) {
196+
auto E = ProfilerStack.pop_back_val();
197+
llvm::timeTraceProfilerEnd(E);
198+
}
197199

198200
S->DiagnoseNonDefaultPragmaAlignPack(
199201
Sema::PragmaAlignPackDiagnoseKind::ChangedStateAtExit,

clang/lib/Sema/SemaExpr.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18049,21 +18049,18 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
1804918049
// Notify the consumer that a function was implicitly instantiated.
1805018050
Consumer.HandleCXXImplicitFunctionInstantiation(Func);
1805118051

18052-
llvm::TimeTraceScope TimeScope(
18053-
"DeferInstantiation",
18054-
[&]() {
18055-
llvm::TimeTraceMetadata M;
18056-
llvm::raw_string_ostream OS(M.Detail);
18057-
Func->getNameForDiagnostic(OS, getPrintingPolicy(),
18058-
/*Qualified=*/true);
18059-
if (llvm::isTimeTraceVerbose()) {
18060-
auto Loc = SourceMgr.getExpansionLoc(Func->getLocation());
18061-
M.File = SourceMgr.getFilename(Loc);
18062-
M.Line = SourceMgr.getExpansionLineNumber(Loc);
18063-
}
18064-
return M;
18065-
},
18066-
llvm::TimeTraceEventType::InstantEvent);
18052+
llvm::timeTraceProfilerInsert("DeferInstantiation", [&]() {
18053+
llvm::TimeTraceMetadata M;
18054+
llvm::raw_string_ostream OS(M.Detail);
18055+
Func->getNameForDiagnostic(OS, getPrintingPolicy(),
18056+
/*Qualified=*/true);
18057+
if (llvm::isTimeTraceVerbose()) {
18058+
auto Loc = SourceMgr.getExpansionLoc(Func->getLocation());
18059+
M.File = SourceMgr.getFilename(Loc);
18060+
M.Line = SourceMgr.getExpansionLineNumber(Loc);
18061+
}
18062+
return M;
18063+
});
1806718064
}
1806818065
}
1806918066
} else {

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,22 +4984,18 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
49844984
PendingInstantiations.push_back(
49854985
std::make_pair(Function, PointOfInstantiation));
49864986

4987-
llvm::TimeTraceScope TimeScope(
4988-
"DeferInstantiation",
4989-
[&]() {
4990-
llvm::TimeTraceMetadata M;
4991-
llvm::raw_string_ostream OS(M.Detail);
4992-
Function->getNameForDiagnostic(OS, getPrintingPolicy(),
4993-
/*Qualified=*/true);
4994-
if (llvm::isTimeTraceVerbose()) {
4995-
auto Loc = SourceMgr.getExpansionLoc(Function->getLocation());
4996-
M.File = SourceMgr.getFilename(Loc);
4997-
M.Line = SourceMgr.getExpansionLineNumber(Loc);
4998-
}
4999-
return M;
5000-
},
5001-
llvm::TimeTraceEventType::InstantEvent);
5002-
4987+
llvm::timeTraceProfilerInsert("DeferInstantiation", [&]() {
4988+
llvm::TimeTraceMetadata M;
4989+
llvm::raw_string_ostream OS(M.Detail);
4990+
Function->getNameForDiagnostic(OS, getPrintingPolicy(),
4991+
/*Qualified=*/true);
4992+
if (llvm::isTimeTraceVerbose()) {
4993+
auto Loc = SourceMgr.getExpansionLoc(Function->getLocation());
4994+
M.File = SourceMgr.getFilename(Loc);
4995+
M.Line = SourceMgr.getExpansionLineNumber(Loc);
4996+
}
4997+
return M;
4998+
});
50034999
} else if (TSK == TSK_ImplicitInstantiation) {
50045000
if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() &&
50055001
!getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) {

llvm/include/llvm/Support/TimeProfiler.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,17 @@ struct TimeTraceMetadata {
9494
bool isEmpty() const { return Detail.empty() && File.empty(); }
9595
};
9696

97+
struct TimeTraceProfilerEntry;
98+
struct InstantEvent;
99+
struct DurableEvent;
100+
struct CompleteEvent;
101+
struct AsyncEvent;
102+
97103
struct TimeTraceProfiler;
98104
TimeTraceProfiler *getTimeTraceProfilerInstance();
99105

100106
bool isTimeTraceVerbose();
101107

102-
struct TimeTraceProfilerEntry;
103-
104108
/// Initialize the time trace profiler.
105109
/// This sets up the global \p TimeTraceProfilerInstance
106110
/// variable to be the profiler instance.
@@ -136,13 +140,13 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
136140
/// Profiler copies the string data, so the pointers can be given into
137141
/// temporaries. Time sections can be hierarchical; every Begin must have a
138142
/// matching End pair but they can nest.
139-
TimeTraceProfilerEntry *timeTraceProfilerBegin(StringRef Name,
140-
StringRef Detail);
141-
TimeTraceProfilerEntry *
143+
std::shared_ptr<DurableEvent> timeTraceProfilerBegin(StringRef Name,
144+
StringRef Detail);
145+
std::shared_ptr<DurableEvent>
142146
timeTraceProfilerBegin(StringRef Name,
143147
llvm::function_ref<std::string()> Detail);
144148

145-
TimeTraceProfilerEntry *
149+
std::shared_ptr<DurableEvent>
146150
timeTraceProfilerBegin(StringRef Name,
147151
llvm::function_ref<TimeTraceMetadata()> MetaData);
148152

@@ -151,16 +155,17 @@ timeTraceProfilerBegin(StringRef Name,
151155
/// separately from other traces. See
152156
/// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1
153157
/// for more details.
154-
TimeTraceProfilerEntry *timeTraceAsyncProfilerBegin(StringRef Name,
155-
StringRef Detail);
158+
std::shared_ptr<DurableEvent> timeTraceAsyncProfilerBegin(StringRef Name,
159+
StringRef Detail);
156160

157161
// Mark an instant event.
158-
TimeTraceProfilerEntry *timeTraceInstantEventProfilerBegin(StringRef Name,
162+
void timeTraceProfilerInsert(StringRef Name,
159163
llvm::function_ref<TimeTraceMetadata()> Metadata);
164+
void timeTraceProfilerInsert(StringRef Name, StringRef Detail);
160165

161166
/// Manually end the last time section.
162167
void timeTraceProfilerEnd();
163-
void timeTraceProfilerEnd(TimeTraceProfilerEntry *E);
168+
void timeTraceProfilerEnd(std::shared_ptr<DurableEvent> &E);
164169

165170
/// The TimeTraceScope is a helper class to call the begin and end functions
166171
/// of the time trace profiler. When the object is constructed, it begins
@@ -187,26 +192,17 @@ class TimeTraceScope {
187192
Entry = timeTraceProfilerBegin(Name, Detail);
188193
}
189194
TimeTraceScope(StringRef Name,
190-
llvm::function_ref<TimeTraceMetadata()> Metadata, TimeTraceEventType Et = TimeTraceEventType::CompleteEvent) {
191-
if (getTimeTraceProfilerInstance() == nullptr)
192-
return;
193-
assert((Et == TimeTraceEventType::InstantEvent ||
194-
Et == TimeTraceEventType::CompleteEvent) &&
195-
"Event Type not supported.");
196-
197-
if (Et == TimeTraceEventType::CompleteEvent) {
195+
llvm::function_ref<TimeTraceMetadata()> Metadata) {
196+
if (getTimeTraceProfilerInstance() != nullptr)
198197
Entry = timeTraceProfilerBegin(Name, Metadata);
199-
} else {
200-
Entry = timeTraceInstantEventProfilerBegin(Name, Metadata);
201-
}
202198
}
203199
~TimeTraceScope() {
204200
if (getTimeTraceProfilerInstance() != nullptr)
205201
timeTraceProfilerEnd(Entry);
206202
}
207203

208204
private:
209-
TimeTraceProfilerEntry *Entry = nullptr;
205+
std::shared_ptr<DurableEvent> Entry = nullptr;
210206
};
211207

212208
} // end namespace llvm

0 commit comments

Comments
 (0)