Skip to content

Commit ed966e7

Browse files
committed
RequirementMachine: Add histograms for symbol kinds and term length
1 parent 5f298a8 commit ed966e7

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
using namespace swift;
2020
using namespace rewriting;
2121

22+
RewriteContext::RewriteContext(ASTContext &ctx)
23+
: Context(ctx),
24+
Stats(ctx.Stats),
25+
SymbolHistogram(Symbol::NumKinds),
26+
TermHistogram(4, /*Start=*/1) {
27+
}
28+
2229
Term RewriteContext::getTermForType(CanType paramType,
2330
const ProtocolDecl *proto) {
2431
return Term::get(getMutableTermForType(paramType, proto), *this);
@@ -271,4 +278,16 @@ Type RewriteContext::getRelativeTypeForTerm(
271278
return getTypeForSymbolRange(
272279
term.begin() + prefix.size(), term.end(), genericParam,
273280
{ }, protos, Context);
281+
}
282+
283+
/// We print stats in the destructor, which should get executed at the end of
284+
/// a compilation job.
285+
RewriteContext::~RewriteContext() {
286+
if (Context.LangOpts.AnalyzeRequirementMachine) {
287+
llvm::dbgs() << "--- Requirement Machine Statistics ---\n";
288+
llvm::dbgs() << "\n* Symbol kind:\n";
289+
SymbolHistogram.dump(llvm::dbgs(), Symbol::Kinds);
290+
llvm::dbgs() << "\n* Term length:\n";
291+
TermHistogram.dump(llvm::dbgs());
292+
}
274293
}

lib/AST/RequirementMachine/RewriteContext.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "swift/Basic/Statistic.h"
1919
#include "llvm/ADT/FoldingSet.h"
2020
#include "llvm/Support/Allocator.h"
21-
#include "RewriteSystem.h"
21+
#include "Histogram.h"
22+
#include "Symbol.h"
23+
#include "Term.h"
2224

2325
namespace swift {
2426

@@ -50,10 +52,14 @@ class RewriteContext final {
5052
ASTContext &Context;
5153

5254
public:
53-
/// Statistical counters.
55+
/// Statistics.
5456
UnifiedStatsReporter *Stats;
5557

56-
RewriteContext(ASTContext &ctx) : Context(ctx), Stats(ctx.Stats) {}
58+
/// Histograms.
59+
Histogram SymbolHistogram;
60+
Histogram TermHistogram;
61+
62+
explicit RewriteContext(ASTContext &ctx);
5763

5864
Term getTermForType(CanType paramType, const ProtocolDecl *proto);
5965

@@ -73,6 +79,8 @@ class RewriteContext final {
7379
Type getRelativeTypeForTerm(
7480
const MutableTerm &term, const MutableTerm &prefix,
7581
const ProtocolGraph &protos) const;
82+
83+
~RewriteContext();
7684
};
7785

7886
} // end namespace rewriting

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
using namespace swift;
2424
using namespace rewriting;
2525

26+
RewriteSystem::RewriteSystem(RewriteContext &ctx)
27+
: Context(ctx) {
28+
DebugSimplify = false;
29+
DebugAdd = false;
30+
DebugMerge = false;
31+
DebugCompletion = false;
32+
}
33+
2634
void Rule::dump(llvm::raw_ostream &out) const {
2735
out << LHS << " => " << RHS;
2836
if (deleted)

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,7 @@ class RewriteSystem final {
134134
unsigned DebugCompletion : 1;
135135

136136
public:
137-
explicit RewriteSystem(RewriteContext &ctx) : Context(ctx) {
138-
DebugSimplify = false;
139-
DebugAdd = false;
140-
DebugMerge = false;
141-
DebugCompletion = false;
142-
}
137+
explicit RewriteSystem(RewriteContext &ctx);
143138

144139
RewriteSystem(const RewriteSystem &) = delete;
145140
RewriteSystem(RewriteSystem &&) = delete;

lib/AST/RequirementMachine/Symbol.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
using namespace swift;
2525
using namespace rewriting;
2626

27+
const StringRef Symbol::Kinds[] = {
28+
"protocol",
29+
"assocty",
30+
"generic",
31+
"name",
32+
"layout",
33+
"super",
34+
"concrete"
35+
};
36+
2737
/// Symbols are uniqued and immutable, stored as a single pointer;
2838
/// the Storage type is the allocated backing storage.
2939
struct Symbol::Storage final
@@ -209,6 +219,7 @@ Symbol Symbol::forName(Identifier name,
209219
#endif
210220

211221
ctx.Symbols.InsertNode(symbol, insertPos);
222+
ctx.SymbolHistogram.add(unsigned(Kind::Name));
212223

213224
return symbol;
214225
}
@@ -237,6 +248,7 @@ Symbol Symbol::forProtocol(const ProtocolDecl *proto,
237248
#endif
238249

239250
ctx.Symbols.InsertNode(symbol, insertPos);
251+
ctx.SymbolHistogram.add(unsigned(Kind::Protocol));
240252

241253
return symbol;
242254
}
@@ -280,6 +292,7 @@ Symbol Symbol::forAssociatedType(ArrayRef<const ProtocolDecl *> protos,
280292
#endif
281293

282294
ctx.Symbols.InsertNode(symbol, insertPos);
295+
ctx.SymbolHistogram.add(unsigned(Kind::AssociatedType));
283296

284297
return symbol;
285298
}
@@ -310,6 +323,7 @@ Symbol Symbol::forGenericParam(GenericTypeParamType *param,
310323
#endif
311324

312325
ctx.Symbols.InsertNode(symbol, insertPos);
326+
ctx.SymbolHistogram.add(unsigned(Kind::GenericParam));
313327

314328
return symbol;
315329
}
@@ -336,6 +350,7 @@ Symbol Symbol::forLayout(LayoutConstraint layout,
336350
#endif
337351

338352
ctx.Symbols.InsertNode(symbol, insertPos);
353+
ctx.SymbolHistogram.add(unsigned(Kind::Layout));
339354

340355
return symbol;
341356
}
@@ -367,6 +382,7 @@ Symbol Symbol::forSuperclass(CanType type, ArrayRef<Term> substitutions,
367382
#endif
368383

369384
ctx.Symbols.InsertNode(symbol, insertPos);
385+
ctx.SymbolHistogram.add(unsigned(Kind::Superclass));
370386

371387
return symbol;
372388
}
@@ -397,6 +413,7 @@ Symbol Symbol::forConcreteType(CanType type, ArrayRef<Term> substitutions,
397413
#endif
398414

399415
ctx.Symbols.InsertNode(symbol, insertPos);
416+
ctx.SymbolHistogram.add(unsigned(Kind::ConcreteType));
400417

401418
return symbol;
402419
}

lib/AST/RequirementMachine/Symbol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class Symbol final {
109109
ConcreteType,
110110
};
111111

112+
static const unsigned NumKinds = 7;
113+
114+
static const StringRef Kinds[];
115+
112116
private:
113117
friend class RewriteContext;
114118

lib/AST/RequirementMachine/Term.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Term Term::get(const MutableTerm &mutableTerm, RewriteContext &ctx) {
101101
term->getElements()[i] = mutableTerm[i];
102102

103103
ctx.Terms.InsertNode(term, insertPos);
104+
ctx.TermHistogram.add(size);
104105

105106
return term;
106107
}

0 commit comments

Comments
 (0)