10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
+ #include " clang/AST/Decl.h"
14
+ #include " clang/Basic/SourceLocation.h"
15
+ #include " clang/Basic/SourceManager.h"
13
16
#include " swift/Basic/Statistic.h"
17
+ #include " swift/AST/Decl.h"
18
+ #include " swift/AST/Expr.h"
19
+ #include " swift/SIL/SILFunction.h"
14
20
#include " swift/Driver/DependencyGraph.h"
15
21
#include " llvm/Config/config.h"
16
22
#include " llvm/Support/FileSystem.h"
@@ -128,6 +134,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
128
134
StringRef OptType,
129
135
StringRef Directory,
130
136
SourceManager *SM,
137
+ clang::SourceManager *CSM,
131
138
bool TraceEvents)
132
139
: UnifiedStatsReporter(ProgramName,
133
140
auxName (ModuleName,
@@ -136,14 +143,15 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
136
143
OutputType,
137
144
OptType),
138
145
Directory,
139
- SM, TraceEvents)
146
+ SM, CSM, TraceEvents)
140
147
{
141
148
}
142
149
143
150
UnifiedStatsReporter::UnifiedStatsReporter (StringRef ProgramName,
144
151
StringRef AuxName,
145
152
StringRef Directory,
146
153
SourceManager *SM,
154
+ clang::SourceManager *CSM,
147
155
bool TraceEvents)
148
156
: currentProcessExitStatusSet(false ),
149
157
currentProcessExitStatus(EXIT_FAILURE),
@@ -153,7 +161,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
153
161
Timer(make_unique<NamedRegionTimer>(AuxName,
154
162
" Building Target" ,
155
163
ProgramName, " Running Program" )),
156
- SourceMgr(SM)
164
+ SourceMgr(SM),
165
+ ClangSourceMgr(CSM)
157
166
{
158
167
path::append (StatsFilename, makeStatsFileName (ProgramName, AuxName));
159
168
path::append (TraceFilename, makeTraceFileName (ProgramName, AuxName));
@@ -250,13 +259,13 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
250
259
}
251
260
252
261
UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer (
253
- StringRef Name ,
254
- SourceRange const &Range ,
262
+ StringRef EventName ,
263
+ TraceEntity Entity ,
255
264
UnifiedStatsReporter *Reporter)
256
265
: Reporter(Reporter),
257
266
SavedTime(llvm::TimeRecord::getCurrentTime()),
258
- Name(Name ),
259
- Range(Range )
267
+ EventName(EventName ),
268
+ Entity(Entity )
260
269
{
261
270
if (Reporter)
262
271
Reporter->saveAnyFrontendStatsEvents (*this , true );
@@ -273,8 +282,8 @@ UnifiedStatsReporter::FrontendStatsTracer::operator=(
273
282
{
274
283
Reporter = other.Reporter ;
275
284
SavedTime = other.SavedTime ;
276
- Name = other.Name ;
277
- Range = other.Range ;
285
+ EventName = other.EventName ;
286
+ Entity = other.Entity ;
278
287
other.Reporter = nullptr ;
279
288
return *this ;
280
289
}
@@ -283,8 +292,8 @@ UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
283
292
FrontendStatsTracer&& other)
284
293
: Reporter(other.Reporter),
285
294
SavedTime (other.SavedTime),
286
- Name (other.Name ),
287
- Range (other.Range )
295
+ EventName (other.EventName ),
296
+ Entity (other.Entity )
288
297
{
289
298
other.Reporter = nullptr ;
290
299
}
@@ -296,17 +305,46 @@ UnifiedStatsReporter::FrontendStatsTracer::~FrontendStatsTracer()
296
305
}
297
306
298
307
UnifiedStatsReporter::FrontendStatsTracer
299
- UnifiedStatsReporter::getStatsTracer (StringRef N ,
300
- SourceRange const &R )
308
+ UnifiedStatsReporter::getStatsTracer (StringRef EventName ,
309
+ const Decl *D )
301
310
{
302
311
if (LastTracedFrontendCounters)
303
312
// Return live tracer object.
304
- return FrontendStatsTracer (N, R , this );
313
+ return FrontendStatsTracer (EventName, D , this );
305
314
else
306
315
// Return inert tracer object.
307
316
return FrontendStatsTracer ();
308
317
}
309
318
319
+ UnifiedStatsReporter::FrontendStatsTracer
320
+ UnifiedStatsReporter::getStatsTracer (StringRef EventName,
321
+ const Expr *E) {
322
+ if (LastTracedFrontendCounters)
323
+ return FrontendStatsTracer (EventName, E, this );
324
+ else
325
+ return FrontendStatsTracer ();
326
+ }
327
+
328
+ UnifiedStatsReporter::FrontendStatsTracer
329
+ UnifiedStatsReporter::getStatsTracer (StringRef EventName,
330
+ const clang::Decl *D)
331
+ {
332
+ if (LastTracedFrontendCounters)
333
+ return FrontendStatsTracer (EventName, D, this );
334
+ else
335
+ return FrontendStatsTracer ();
336
+ }
337
+
338
+ UnifiedStatsReporter::FrontendStatsTracer
339
+ UnifiedStatsReporter::getStatsTracer (StringRef EventName,
340
+ const SILFunction *F)
341
+ {
342
+ if (LastTracedFrontendCounters)
343
+ return FrontendStatsTracer (EventName, F, this );
344
+ else
345
+ return FrontendStatsTracer ();
346
+ }
347
+
310
348
void
311
349
UnifiedStatsReporter::saveAnyFrontendStatsEvents (
312
350
FrontendStatsTracer const & T,
@@ -321,13 +359,14 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
321
359
auto &C = getFrontendCounters ();
322
360
#define FRONTEND_STATISTIC (TY, NAME ) \
323
361
do { \
362
+ auto total = C.NAME ; \
324
363
auto delta = C.NAME - LastTracedFrontendCounters->NAME ; \
325
364
static char const *name = #TY " ." #NAME; \
326
365
if (delta != 0 ) { \
327
366
LastTracedFrontendCounters->NAME = C.NAME ; \
328
367
FrontendStatsEvents.emplace_back (FrontendStatsEvent { \
329
- NowUS, LiveUS, IsEntry, T.Name , name, \
330
- delta, C. NAME , T.Range }); \
368
+ NowUS, LiveUS, IsEntry, T.EventName , name, \
369
+ delta, total , T.Entity }); \
331
370
} \
332
371
} while (0 );
333
372
#include " swift/Basic/Statistics.def"
@@ -343,6 +382,57 @@ UnifiedStatsReporter::AlwaysOnFrontendRecursiveSharedTimers::
343
382
dummyInstanceVariableToGetConstructorToParse (0 ) {
344
383
}
345
384
385
+ static inline void
386
+ printTraceEntityName (raw_ostream &OS,
387
+ UnifiedStatsReporter::TraceEntity E) {
388
+ if (auto const *CD = E.dyn_cast <const clang::Decl*>()) {
389
+ if (auto const *ND = dyn_cast<const clang::NamedDecl>(CD)) {
390
+ ND->printName (OS);
391
+ }
392
+ } else if (auto const *D = E.dyn_cast <const Decl*>()) {
393
+ if (auto const *VD = dyn_cast<const ValueDecl>(D)) {
394
+ VD->getFullName ().print (OS, false );
395
+ }
396
+ } else if (auto const *X = E.dyn_cast <const Expr*>()) {
397
+ // Exprs don't have names
398
+ } else if (auto const *F = E.dyn_cast <const SILFunction*>()) {
399
+ OS << F->getName ();
400
+ }
401
+ }
402
+
403
+ static inline void
404
+ printClangShortLoc (raw_ostream &OS,
405
+ clang::SourceManager *CSM,
406
+ clang::SourceLocation L) {
407
+ if (!L.isValid () || !L.isFileID ())
408
+ return ;
409
+ auto PLoc = CSM->getPresumedLoc (L);
410
+ OS << llvm::sys::path::filename (PLoc.getFilename ())
411
+ << ' :' << PLoc.getLine ()
412
+ << ' :' << PLoc.getColumn ();
413
+ }
414
+
415
+ static inline void
416
+ printTraceEntityLoc (raw_ostream &OS,
417
+ SourceManager *SM,
418
+ clang::SourceManager *CSM,
419
+ UnifiedStatsReporter::TraceEntity E) {
420
+ if (auto const *CD = E.dyn_cast <const clang::Decl*>()) {
421
+ if (CSM) {
422
+ auto Range = CD->getSourceRange ();
423
+ printClangShortLoc (OS, CSM, Range.getBegin ());
424
+ OS << ' -' ;
425
+ printClangShortLoc (OS, CSM, Range.getEnd ());
426
+ }
427
+ } else if (auto const *D = E.dyn_cast <const Decl*>()) {
428
+ D->getSourceRange ().print (OS, *SM, false );
429
+ } else if (auto const *X = E.dyn_cast <const Expr*>()) {
430
+ X->getSourceRange ().print (OS, *SM, false );
431
+ } else if (auto const *F = E.dyn_cast <const SILFunction*>()) {
432
+ F->getLocation ().getSourceRange ().print (OS, *SM, false );
433
+ }
434
+ }
435
+
346
436
UnifiedStatsReporter::~UnifiedStatsReporter ()
347
437
{
348
438
// If nobody's marked this process as successful yet,
@@ -419,7 +509,7 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
419
509
return ;
420
510
}
421
511
tstream << " Time,Live,IsEntry,EventName,CounterName,"
422
- << " CounterDelta,CounterValue,SourceRange \n " ;
512
+ << " CounterDelta,CounterValue,EntityName,EntityRange \n " ;
423
513
for (auto const &E : FrontendStatsEvents) {
424
514
tstream << E.TimeUSec << ' ,'
425
515
<< E.LiveUSec << ' ,'
@@ -429,7 +519,10 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
429
519
<< E.CounterDelta << ' ,'
430
520
<< E.CounterValue << ' ,' ;
431
521
tstream << ' "' ;
432
- E.SourceRange .print (tstream, *SourceMgr, false );
522
+ printTraceEntityName (tstream, E.Entity );
523
+ tstream << ' "' << ' ,' ;
524
+ tstream << ' "' ;
525
+ printTraceEntityLoc (tstream, SourceMgr, ClangSourceMgr, E.Entity );
433
526
tstream << ' "' << ' \n ' ;
434
527
}
435
528
}
0 commit comments