Skip to content

Commit 7ba2b76

Browse files
committed
Basic: Add -analyze-request-evaluator flag to dump request cache statistics
1 parent ca9c09f commit 7ba2b76

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

include/swift/AST/Evaluator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ class Evaluator {
283283
return activeRequests.count(ActiveRequest(request));
284284
}
285285

286+
void dump(llvm::raw_ostream &out) { cache.dump(out); }
287+
286288
private:
287289
/// Diagnose a cycle detected in the evaluation of the given
288290
/// request.

include/swift/AST/RequestCache.h

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/DependencyCollector.h"
1919
#include "llvm/ADT/DenseMap.h"
2020
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Support/raw_ostream.h"
2122

2223
#ifndef SWIFT_AST_REQUEST_CACHE_H
2324
#define SWIFT_AST_REQUEST_CACHE_H
@@ -162,14 +163,22 @@ class RequestKey<Request, typename detail::TupleHasDenseMapInfo<
162163
class PerRequestCache {
163164
void *Storage;
164165
std::function<void(void *)> Deleter;
166+
std::function<void(llvm::raw_ostream &out, void *)> Dumper;
165167

166-
PerRequestCache(void *storage, std::function<void(void *)> deleter)
167-
: Storage(storage), Deleter(deleter) {}
168+
PerRequestCache(void *storage,
169+
std::function<void(void *)> deleter,
170+
std::function<void(llvm::raw_ostream &out, void *)> dumper)
171+
: Storage(storage), Deleter(deleter), Dumper(dumper) {}
168172

169173
public:
170-
PerRequestCache() : Storage(nullptr), Deleter([](void *) {}) {}
174+
PerRequestCache()
175+
: Storage(nullptr),
176+
Deleter([](void *) {}),
177+
Dumper([](llvm::raw_ostream &, void *) {}) {}
171178
PerRequestCache(PerRequestCache &&other)
172-
: Storage(other.Storage), Deleter(std::move(other.Deleter)) {
179+
: Storage(other.Storage),
180+
Deleter(std::move(other.Deleter)),
181+
Dumper(std::move(other.Dumper)) {
173182
other.Storage = nullptr;
174183
}
175184

@@ -190,7 +199,17 @@ class PerRequestCache {
190199
llvm::DenseMap<RequestKey<Request>,
191200
typename Request::OutputType>;
192201
return PerRequestCache(new Map(),
193-
[](void *ptr) { delete static_cast<Map *>(ptr); });
202+
[](void *ptr) { delete static_cast<Map *>(ptr); },
203+
[](llvm::raw_ostream &out, void *storage) {
204+
out << TypeID<Request>::getName() << "\t";
205+
if (auto *map = static_cast<Map *>(storage)) {
206+
out << map->size() << "\t"
207+
<< llvm::capacity_in_bytes(*map);
208+
} else {
209+
out << "0\t0";
210+
}
211+
out << "\n";
212+
});
194213
}
195214

196215
template <typename Request>
@@ -204,11 +223,28 @@ class PerRequestCache {
204223
return static_cast<Map *>(Storage);
205224
}
206225

226+
template <typename Request>
227+
std::pair<size_t, size_t>
228+
size() const {
229+
using Map =
230+
llvm::DenseMap<RequestKey<Request>,
231+
typename Request::OutputType>;
232+
if (!Storage)
233+
return std::make_pair(0, 0);
234+
235+
auto map = static_cast<Map *>(Storage);
236+
return std::make_pair(map.size(), llvm::capacity_in_bytes(map));
237+
}
238+
207239
bool isNull() const { return !Storage; }
208240
~PerRequestCache() {
209241
if (Storage)
210242
Deleter(Storage);
211243
}
244+
245+
void dump(llvm::raw_ostream &out) {
246+
Dumper(out, Storage);
247+
}
212248
};
213249

214250
/// Data structure for caching results of requests. Sharded by the type ID
@@ -275,6 +311,15 @@ class RequestCache {
275311
void clear() {
276312
#define SWIFT_TYPEID_ZONE(Name, Id) Name##ZoneCache.clear();
277313
#include "swift/Basic/TypeIDZones.def"
314+
#undef SWIFT_TYPEID_ZONE
315+
}
316+
317+
void dump(llvm::raw_ostream &out) {
318+
#define SWIFT_TYPEID_ZONE(Name, Id) \
319+
for (auto &entry : Name##ZoneCache) { \
320+
entry.dump(out); \
321+
}
322+
#include "swift/Basic/TypeIDZones.def"
278323
#undef SWIFT_TYPEID_ZONE
279324
}
280325
};

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ namespace swift {
531531
ASTVerifierOverrideKind ASTVerifierOverride =
532532
ASTVerifierOverrideKind::NoOverride;
533533

534+
/// Dumps request evaluator cache statistics at the end of compilation.
535+
bool AnalyzeRequestEvaluator = false;
536+
534537
/// Enables dumping rewrite systems from the requirement machine.
535538
bool DumpRequirementMachine = false;
536539

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ def dump_macro_expansions : Flag<["-"], "dump-macro-expansions">,
415415
def emit_macro_expansion_files : Separate<["-"], "emit-macro-expansion-files">,
416416
HelpText<"Specify when to emit macro expansion file: 'none', 'debug', or 'diagnostics'">;
417417

418+
def analyze_request_evaluator : Flag<["-"], "analyze-request-evaluator">,
419+
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
420+
HelpText<"Print out request evaluator cache statistics at the end of the compilation job">;
421+
418422
def analyze_requirement_machine : Flag<["-"], "analyze-requirement-machine">,
419423
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
420424
HelpText<"Print out requirement machine statistics at the end of the compilation job">;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ ASTContext::ASTContext(
804804
}
805805

806806
ASTContext::~ASTContext() {
807+
if (LangOpts.AnalyzeRequestEvaluator)
808+
evaluator.dump(llvm::dbgs());
807809
getImpl().~Implementation();
808810
}
809811

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
14721472
Opts.DisableSubstSILFunctionTypes =
14731473
Args.hasArg(OPT_disable_subst_sil_function_types);
14741474

1475+
Opts.AnalyzeRequestEvaluator = Args.hasArg(
1476+
OPT_analyze_request_evaluator);
1477+
14751478
Opts.DumpRequirementMachine = Args.hasArg(
14761479
OPT_dump_requirement_machine);
14771480
Opts.AnalyzeRequirementMachine = Args.hasArg(

0 commit comments

Comments
 (0)