Skip to content

Commit 5f12ffc

Browse files
committed
[Stats] Simplify FrontendStatsTracer uses and formatter-definitions.
1 parent db28140 commit 5f12ffc

File tree

9 files changed

+116
-99
lines changed

9 files changed

+116
-99
lines changed

include/swift/Basic/Statistic.h

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace swift {
5858
class Decl;
5959
class Expr;
6060
class SILFunction;
61+
class FrontendStatsTracer;
6162

6263
class UnifiedStatsReporter {
6364

@@ -98,25 +99,6 @@ class UnifiedStatsReporter {
9899
virtual ~TraceFormatter();
99100
};
100101

101-
struct FrontendStatsTracer
102-
{
103-
UnifiedStatsReporter *Reporter;
104-
llvm::TimeRecord SavedTime;
105-
StringRef EventName;
106-
const void *Entity;
107-
const TraceFormatter *Formatter;
108-
FrontendStatsTracer(StringRef EventName,
109-
const void *Entity,
110-
const TraceFormatter *Formatter,
111-
UnifiedStatsReporter *Reporter);
112-
FrontendStatsTracer();
113-
FrontendStatsTracer(FrontendStatsTracer&& other);
114-
FrontendStatsTracer& operator=(FrontendStatsTracer&&);
115-
~FrontendStatsTracer();
116-
FrontendStatsTracer(const FrontendStatsTracer&) = delete;
117-
FrontendStatsTracer& operator=(const FrontendStatsTracer&) = delete;
118-
};
119-
120102
struct FrontendStatsEvent
121103
{
122104
uint64_t TimeUSec;
@@ -172,16 +154,69 @@ class UnifiedStatsReporter {
172154
AlwaysOnFrontendCounters &getFrontendCounters();
173155
AlwaysOnFrontendRecursiveSharedTimers &getFrontendRecursiveSharedTimers();
174156
void noteCurrentProcessExitStatus(int);
175-
// We declare 4 explicit overloads here, but the _definitions_ live in the
176-
// upper-level files (in libswiftAST or libswiftSIL) that provide the types
177-
// being traced. If you want to trace those types, it's assumed you're linking
178-
// with the object files that define the tracer.
179-
FrontendStatsTracer getStatsTracer(StringRef EventName, const Decl *D);
180-
FrontendStatsTracer getStatsTracer(StringRef EventName, const clang::Decl*D);
181-
FrontendStatsTracer getStatsTracer(StringRef EventName, const Expr *E);
182-
FrontendStatsTracer getStatsTracer(StringRef EventName, const SILFunction *F);
183157
void saveAnyFrontendStatsEvents(FrontendStatsTracer const &T, bool IsEntry);
184158
};
185159

160+
// This is a non-nested type just to make it less work to write at call sites.
161+
class FrontendStatsTracer
162+
{
163+
FrontendStatsTracer(UnifiedStatsReporter *Reporter,
164+
StringRef EventName,
165+
const void *Entity,
166+
const UnifiedStatsReporter::TraceFormatter *Formatter);
167+
168+
// In the general case we do not know how to format an entity for tracing.
169+
template<typename T> static
170+
const UnifiedStatsReporter::TraceFormatter *getTraceFormatter() {
171+
return nullptr;
172+
}
173+
174+
public:
175+
UnifiedStatsReporter *Reporter;
176+
llvm::TimeRecord SavedTime;
177+
StringRef EventName;
178+
const void *Entity;
179+
const UnifiedStatsReporter::TraceFormatter *Formatter;
180+
FrontendStatsTracer();
181+
FrontendStatsTracer(FrontendStatsTracer&& other);
182+
FrontendStatsTracer& operator=(FrontendStatsTracer&&);
183+
~FrontendStatsTracer();
184+
FrontendStatsTracer(const FrontendStatsTracer&) = delete;
185+
FrontendStatsTracer& operator=(const FrontendStatsTracer&) = delete;
186+
187+
/// These are the convenience constructors you want to be calling throughout
188+
/// the compiler: they select an appropriate trace formatter for the provided
189+
/// entity type, and produce a tracer that's either active or inert depending
190+
/// on whether the provided \p Reporter is null (nullptr means "tracing is
191+
/// disabled").
192+
FrontendStatsTracer(UnifiedStatsReporter *Reporter, StringRef EventName,
193+
const Decl *D);
194+
FrontendStatsTracer(UnifiedStatsReporter *Reporter, StringRef EventName,
195+
const clang::Decl *D);
196+
FrontendStatsTracer(UnifiedStatsReporter *Reporter, StringRef EventName,
197+
const Expr *E);
198+
FrontendStatsTracer(UnifiedStatsReporter *Reporter, StringRef EventName,
199+
const SILFunction *F);
200+
};
201+
202+
// In particular cases, we do know how to format traced entities: we declare
203+
// explicit specializations of getTraceFormatter() here, matching the overloaded
204+
// constructors of FrontendStatsTracer above, where the _definitions_ live in
205+
// the upper-level files (in libswiftAST or libswiftSIL), and provide tracing
206+
// for those entity types. If you want to trace those types, it's assumed you're
207+
// linking with the object files that define the tracer.
208+
209+
template<> const UnifiedStatsReporter::TraceFormatter*
210+
FrontendStatsTracer::getTraceFormatter<const Decl *>();
211+
212+
template<> const UnifiedStatsReporter::TraceFormatter*
213+
FrontendStatsTracer::getTraceFormatter<const clang::Decl *>();
214+
215+
template<> const UnifiedStatsReporter::TraceFormatter*
216+
FrontendStatsTracer::getTraceFormatter<const Expr *>();
217+
218+
template<> const UnifiedStatsReporter::TraceFormatter*
219+
FrontendStatsTracer::getTraceFormatter<const SILFunction *>();
220+
186221
}
187222
#endif // SWIFT_BASIC_STATISTIC_H

lib/AST/Decl.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5683,12 +5683,8 @@ struct DeclTraceFormatter : public UnifiedStatsReporter::TraceFormatter {
56835683

56845684
static DeclTraceFormatter TF;
56855685

5686-
UnifiedStatsReporter::FrontendStatsTracer
5687-
UnifiedStatsReporter::getStatsTracer(StringRef EventName, const Decl *D) {
5688-
if (LastTracedFrontendCounters)
5689-
// Return live tracer object.
5690-
return FrontendStatsTracer(EventName, D, &TF, this);
5691-
else
5692-
// Return inert tracer object.
5693-
return FrontendStatsTracer();
5686+
template<>
5687+
const UnifiedStatsReporter::TraceFormatter*
5688+
FrontendStatsTracer::getTraceFormatter<const Decl *>() {
5689+
return &TF;
56945690
}

lib/AST/Expr.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,12 +2297,8 @@ struct ExprTraceFormatter : public UnifiedStatsReporter::TraceFormatter {
22972297

22982298
static ExprTraceFormatter TF;
22992299

2300-
UnifiedStatsReporter::FrontendStatsTracer
2301-
UnifiedStatsReporter::getStatsTracer(StringRef EventName, const Expr *E) {
2302-
if (LastTracedFrontendCounters)
2303-
// Return live tracer object.
2304-
return FrontendStatsTracer(EventName, E, &TF, this);
2305-
else
2306-
// Return inert tracer object.
2307-
return FrontendStatsTracer();
2300+
template<>
2301+
const UnifiedStatsReporter::TraceFormatter*
2302+
FrontendStatsTracer::getTraceFormatter<const Expr *>() {
2303+
return &TF;
23082304
}

lib/Basic/Statistic.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,31 +258,41 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
258258
OS.flush();
259259
}
260260

261-
UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
262-
StringRef EventName,
263-
const void *Entity,
264-
const TraceFormatter *Formatter,
265-
UnifiedStatsReporter *Reporter)
266-
: Reporter(Reporter),
267-
SavedTime(llvm::TimeRecord::getCurrentTime()),
268-
EventName(EventName),
269-
Entity(Entity),
270-
Formatter(Formatter)
271-
{
272-
if (Reporter)
261+
FrontendStatsTracer::FrontendStatsTracer(
262+
UnifiedStatsReporter *Reporter, StringRef EventName, const void *Entity,
263+
const UnifiedStatsReporter::TraceFormatter *Formatter)
264+
: Reporter(Reporter), SavedTime(), EventName(EventName), Entity(Entity),
265+
Formatter(Formatter) {
266+
if (Reporter) {
267+
SavedTime = llvm::TimeRecord::getCurrentTime();
273268
Reporter->saveAnyFrontendStatsEvents(*this, true);
269+
}
274270
}
275271

276-
UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer()
277-
: Reporter(nullptr),
278-
Entity(nullptr),
279-
Formatter(nullptr)
280-
{
281-
}
272+
FrontendStatsTracer::FrontendStatsTracer() = default;
273+
274+
FrontendStatsTracer::FrontendStatsTracer(UnifiedStatsReporter *R, StringRef S,
275+
const Decl *D)
276+
: FrontendStatsTracer(R, S, D, getTraceFormatter<const Decl *>())
277+
{}
278+
279+
FrontendStatsTracer::FrontendStatsTracer(UnifiedStatsReporter *R, StringRef S,
280+
const Expr *E)
281+
: FrontendStatsTracer(R, S, E, getTraceFormatter<const Expr *>())
282+
{}
283+
284+
FrontendStatsTracer::FrontendStatsTracer(UnifiedStatsReporter *R, StringRef S,
285+
const clang::Decl *D)
286+
: FrontendStatsTracer(R, S, D, getTraceFormatter<const clang::Decl *>())
287+
{}
288+
289+
FrontendStatsTracer::FrontendStatsTracer(UnifiedStatsReporter *R, StringRef S,
290+
const SILFunction *F)
291+
: FrontendStatsTracer(R, S, F, getTraceFormatter<const SILFunction *>())
292+
{}
282293

283-
UnifiedStatsReporter::FrontendStatsTracer&
284-
UnifiedStatsReporter::FrontendStatsTracer::operator=(
285-
FrontendStatsTracer&& other)
294+
FrontendStatsTracer&
295+
FrontendStatsTracer::operator=(FrontendStatsTracer&& other)
286296
{
287297
Reporter = other.Reporter;
288298
SavedTime = other.SavedTime;
@@ -293,8 +303,7 @@ UnifiedStatsReporter::FrontendStatsTracer::operator=(
293303
return *this;
294304
}
295305

296-
UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
297-
FrontendStatsTracer&& other)
306+
FrontendStatsTracer::FrontendStatsTracer(FrontendStatsTracer&& other)
298307
: Reporter(other.Reporter),
299308
SavedTime(other.SavedTime),
300309
EventName(other.EventName),
@@ -304,7 +313,7 @@ UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
304313
other.Reporter = nullptr;
305314
}
306315

307-
UnifiedStatsReporter::FrontendStatsTracer::~FrontendStatsTracer()
316+
FrontendStatsTracer::~FrontendStatsTracer()
308317
{
309318
if (Reporter)
310319
Reporter->saveAnyFrontendStatsEvents(*this, false);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7875,11 +7875,8 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
78757875
if (!ClangDecl)
78767876
return nullptr;
78777877

7878-
UnifiedStatsReporter::FrontendStatsTracer Tracer;
7879-
if (SwiftContext.Stats)
7880-
Tracer = SwiftContext.Stats->getStatsTracer("import-clang-decl",
7881-
ClangDecl);
7882-
7878+
FrontendStatsTracer StatsTracer(SwiftContext.Stats,
7879+
"import-clang-decl", ClangDecl);
78837880
clang::PrettyStackTraceDecl trace(ClangDecl, clang::SourceLocation(),
78847881
Instance->getSourceManager(), "importing");
78857882

@@ -8633,13 +8630,8 @@ struct ClangDeclTraceFormatter : public UnifiedStatsReporter::TraceFormatter {
86338630

86348631
static ClangDeclTraceFormatter TF;
86358632

8636-
UnifiedStatsReporter::FrontendStatsTracer
8637-
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
8638-
const clang::Decl *D) {
8639-
if (LastTracedFrontendCounters)
8640-
// Return live tracer object.
8641-
return FrontendStatsTracer(EventName, D, &TF, this);
8642-
else
8643-
// Return inert tracer object.
8644-
return FrontendStatsTracer();
8633+
template<>
8634+
const UnifiedStatsReporter::TraceFormatter*
8635+
FrontendStatsTracer::getTraceFormatter<const clang::Decl *>() {
8636+
return &TF;
86458637
}

lib/SIL/SILFunction.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,8 @@ struct SILFunctionTraceFormatter : public UnifiedStatsReporter::TraceFormatter {
515515

516516
static SILFunctionTraceFormatter TF;
517517

518-
UnifiedStatsReporter::FrontendStatsTracer
519-
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
520-
const SILFunction *F) {
521-
if (LastTracedFrontendCounters)
522-
// Return live tracer object.
523-
return FrontendStatsTracer(EventName, F, &TF, this);
524-
else
525-
// Return inert tracer object.
526-
return FrontendStatsTracer();
518+
template<>
519+
const UnifiedStatsReporter::TraceFormatter*
520+
FrontendStatsTracer::getTraceFormatter<const SILFunction *>() {
521+
return &TF;
527522
}

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,7 @@ void SILGenModule::emitFunction(FuncDecl *fd) {
707707
emitAbstractFuncDecl(fd);
708708

709709
if (hasSILBody(fd)) {
710-
UnifiedStatsReporter::FrontendStatsTracer Tracer;
711-
if (getASTContext().Stats)
712-
Tracer = getASTContext().Stats->getStatsTracer("emit-SIL", fd);
710+
FrontendStatsTracer Tracer(getASTContext().Stats, "emit-SIL", fd);
713711
PrettyStackTraceDecl stackTrace("emitting SIL for", fd);
714712

715713
SILDeclRef constant(decl);

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,9 +3949,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39493949
: TC(TC), IsFirstPass(IsFirstPass), IsSecondPass(IsSecondPass) {}
39503950

39513951
void visit(Decl *decl) {
3952-
UnifiedStatsReporter::FrontendStatsTracer Tracer;
3953-
if (TC.Context.Stats)
3954-
Tracer = TC.Context.Stats->getStatsTracer("typecheck-decl", decl);
3952+
FrontendStatsTracer StatsTracer(TC.Context.Stats, "typecheck-decl", decl);
39553953
PrettyStackTraceDecl StackTrace("type-checking", decl);
39563954

39573955
DeclVisitor<DeclChecker>::visit(decl);

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,7 @@ static void typeCheckFunctionsAndExternalDecls(TypeChecker &TC) {
420420
// but that gets tricky with synthesized function bodies.
421421
if (AFD->isBodyTypeChecked()) continue;
422422

423-
UnifiedStatsReporter::FrontendStatsTracer Tracer;
424-
if (TC.Context.Stats)
425-
Tracer = TC.Context.Stats->getStatsTracer("typecheck-fn", AFD);
423+
FrontendStatsTracer StatsTracer(TC.Context.Stats, "typecheck-fn", AFD);
426424
PrettyStackTraceDecl StackEntry("type-checking", AFD);
427425
TC.typeCheckAbstractFunctionBody(AFD);
428426

0 commit comments

Comments
 (0)