Skip to content

Commit 388e357

Browse files
committed
[Stats] Add UnifiedStatsReporter tracing/profiling machinery to request-evaluator.
1 parent a0587d1 commit 388e357

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

include/swift/AST/Evaluator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/AnyValue.h"
2323
#include "swift/Basic/CycleDiagnosticKind.h"
2424
#include "swift/Basic/Defer.h"
25+
#include "swift/Basic/Statistic.h"
2526
#include "llvm/ADT/DenseMap.h"
2627
#include "llvm/ADT/Optional.h"
2728
#include "llvm/ADT/SetVector.h"
@@ -338,7 +339,8 @@ class Evaluator {
338339

339340
PrettyStackTraceRequest<Request> prettyStackTrace(request);
340341

341-
/// Update statistics.
342+
// Trace and/or count statistics.
343+
FrontendStatsTracer statsTracer = make_tracer(stats, request);
342344
if (stats) reportEvaluatedRequest(*stats, request);
343345

344346
return getRequestFunction<Request>()(request, *this);

include/swift/AST/SimpleRequest.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/DiagnosticEngine.h"
2121
#include "swift/AST/DiagnosticsCommon.h"
2222
#include "swift/Basic/SimpleDisplay.h"
23+
#include "swift/Basic/Statistic.h"
2324
#include "swift/Basic/TypeID.h"
2425
#include "llvm/ADT/Hashing.h"
2526
#include "llvm/ADT/STLExtras.h"
@@ -160,8 +161,12 @@ class SimpleRequest {
160161
out << TypeID<Derived>::getName();
161162
simple_display(out, request.storage);
162163
}
163-
};
164164

165+
friend FrontendStatsTracer
166+
make_tracer(UnifiedStatsReporter *Reporter, const Derived &request) {
167+
return make_tracer(Reporter, TypeID<Derived>::getName(), request.storage);
168+
}
169+
};
165170
}
166171

167172
#endif // SWIFT_BASIC_SIMPLEREQUEST_H

include/swift/Basic/Statistic.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/Basic/Timer.h"
2020

2121
#include <thread>
22+
#include <tuple>
2223

2324
#define SWIFT_FUNC_STAT \
2425
do { \
@@ -329,5 +330,68 @@ inline FrontendStatsTracer::FrontendStatsTracer(UnifiedStatsReporter *R,
329330
const TypeRepr *TR)
330331
: FrontendStatsTracer(R, S, TR, getTraceFormatter<const TypeRepr *>()) {}
331332

333+
/// Utilities for constructing TraceFormatters from entities in the request-evaluator:
334+
335+
template <typename T>
336+
typename std::enable_if<
337+
std::is_constructible<FrontendStatsTracer, UnifiedStatsReporter *,
338+
StringRef, const T *>::value,
339+
FrontendStatsTracer>::type
340+
make_tracer_direct(UnifiedStatsReporter *Reporter, StringRef Name, T *Value) {
341+
return FrontendStatsTracer(Reporter, Name, static_cast<const T *>(Value));
342+
}
343+
344+
template <typename T>
345+
typename std::enable_if<
346+
std::is_constructible<FrontendStatsTracer, UnifiedStatsReporter *,
347+
StringRef, const T *>::value,
348+
FrontendStatsTracer>::type
349+
make_tracer_direct(UnifiedStatsReporter *Reporter, StringRef Name,
350+
const T *Value) {
351+
return FrontendStatsTracer(Reporter, Name, Value);
352+
}
353+
354+
template <typename T>
355+
typename std::enable_if<
356+
!std::is_constructible<FrontendStatsTracer, UnifiedStatsReporter *,
357+
StringRef, const T *>::value,
358+
FrontendStatsTracer>::type
359+
make_tracer_direct(UnifiedStatsReporter *Reporter, StringRef Name, T *Value) {
360+
return FrontendStatsTracer(Reporter, Name);
361+
}
362+
363+
template <typename T>
364+
typename std::enable_if<!std::is_pointer<T>::value, FrontendStatsTracer>::type
365+
make_tracer_direct(UnifiedStatsReporter *Reporter, StringRef Name, T Value) {
366+
return FrontendStatsTracer(Reporter, Name);
367+
}
368+
369+
template <typename T> struct is_pointerunion : std::false_type {};
370+
template <typename T, typename U>
371+
struct is_pointerunion<llvm::PointerUnion<T, U>> : std::true_type {};
372+
373+
template <typename T, typename U>
374+
FrontendStatsTracer make_tracer_pointerunion(UnifiedStatsReporter *Reporter,
375+
StringRef Name,
376+
llvm::PointerUnion<T, U> Value) {
377+
if (Value.template is<T>())
378+
return make_tracer_direct(Reporter, Name, Value.template get<T>());
379+
else
380+
return make_tracer_direct(Reporter, Name, Value.template get<U>());
381+
}
382+
383+
template <typename T>
384+
typename std::enable_if<!is_pointerunion<T>::value, FrontendStatsTracer>::type
385+
make_tracer_pointerunion(UnifiedStatsReporter *Reporter, StringRef Name,
386+
T Value) {
387+
return make_tracer_direct(Reporter, Name, Value);
388+
}
389+
390+
template <typename First, typename... Rest>
391+
FrontendStatsTracer make_tracer(UnifiedStatsReporter *Reporter, StringRef Name,
392+
std::tuple<First, Rest...> Value) {
393+
return make_tracer_pointerunion(Reporter, Name, std::get<0>(Value));
394+
}
395+
332396
} // namespace swift
333397
#endif // SWIFT_BASIC_STATISTIC_H

test/Misc/stats_dir_tracer.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// RUN: %target-swiftc_driver -o %t/main -module-name main -stats-output-dir %t %s -trace-stats-events
33
// RUN: %FileCheck -input-file %t/*.csv %s
44

5-
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumTypesDeserialized",[0-9]+,[0-9]+,"Call","\[.*stats_dir_tracer.swift:9:5 - line:9:18\]"}}
6-
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumConstraintScopes",[0-9]+,[0-9]+,"IntegerLiteral","\[.*stats_dir_tracer.swift:22:13 - line:22:13\]"}}
5+
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumTypesDeserialized",[0-9]+,[0-9]+,"Call","\[.*stats_dir_tracer.swift.*\]"}}
6+
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumConstraintScopes",[0-9]+,[0-9]+,"Sequence","\[.*stats_dir_tracer.swift.*\]"}}
7+
// CHECK: {{[0-9]+,[0-9]+,"exit","SuperclassDeclRequest","Sema.SuperclassDeclRequest",[0-9]+,[0-9]+,"Bar","\[.*stats_dir_tracer.swift.*\]"}}
78

89
public func foo() {
910
print("hello")

0 commit comments

Comments
 (0)