Skip to content

Commit 7bd7300

Browse files
committed
RequirementMachine: Add a couple of simple counters
1 parent 0a5b462 commit 7bd7300

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

include/swift/AST/RewriteSystem.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/LayoutConstraint.h"
1919
#include "swift/AST/ProtocolGraph.h"
2020
#include "swift/AST/Types.h"
21+
#include "swift/Basic/Statistic.h"
2122
#include "llvm/ADT/FoldingSet.h"
2223
#include "llvm/ADT/PointerUnion.h"
2324
#include "llvm/ADT/SmallVector.h"
@@ -352,7 +353,10 @@ class RewriteContext final {
352353
RewriteContext &operator=(RewriteContext &&) = delete;
353354

354355
public:
355-
RewriteContext() {}
356+
/// Statistical counters.
357+
UnifiedStatsReporter *Stats;
358+
359+
RewriteContext(UnifiedStatsReporter *stats) : Stats(stats) {}
356360

357361
Term getTermForType(CanType paramType,
358362
const ProtocolDecl *proto);
@@ -420,6 +424,7 @@ class Rule final {
420424
///
421425
/// Out-of-line methods are documented in RewriteSystem.cpp.
422426
class RewriteSystem final {
427+
/// Rewrite context for memory allocation.
423428
RewriteContext &Context;
424429

425430
/// The rules added so far, including rules from our client, as well

include/swift/Basic/Statistics.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ FRONTEND_STATISTIC(Sema, NumAccessorsSynthesized)
220220
/// Number of synthesized accessor bodies.
221221
FRONTEND_STATISTIC(Sema, NumAccessorBodiesSynthesized)
222222

223+
/// Number of requirement machines constructed. Rough proxy for
224+
/// amount of work the requirement machine does analyzing type signatures.
225+
FRONTEND_STATISTIC(Sema, NumRequirementMachines)
226+
227+
/// Number of requirement machines constructed. Rough proxy for
228+
/// amount of work the requirement machine does analyzing type signatures.
229+
FRONTEND_STATISTIC(Sema, NumRequirementMachineCompletionSteps)
230+
223231
/// Number of generic signature builders constructed. Rough proxy for
224232
/// amount of work the GSB does analyzing type signatures.
225233
FRONTEND_STATISTIC(Sema, NumGenericSignatureBuilders)

lib/AST/RequirementMachine.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,12 @@ struct RequirementMachine::Implementation {
207207
RewriteSystem System;
208208
bool Complete = false;
209209

210-
Implementation() : System(Context) {}
210+
Implementation(ASTContext &ctx)
211+
: Context(ctx.Stats), System(Context) {}
211212
};
212213

213214
RequirementMachine::RequirementMachine(ASTContext &ctx) : Context(ctx) {
214-
Impl = new Implementation();
215+
Impl = new Implementation(ctx);
215216
}
216217

217218
RequirementMachine::~RequirementMachine() {
@@ -223,6 +224,9 @@ void RequirementMachine::addGenericSignature(CanGenericSignature sig) {
223224

224225
auto *Stats = Context.Stats;
225226

227+
if (Stats)
228+
++Stats->getFrontendCounters().NumRequirementMachines;
229+
226230
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
227231

228232
if (Context.LangOpts.DebugRequirementMachine) {

lib/AST/RewriteSystem.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/AST/RewriteSystem.h"
14+
#include "swift/Basic/Defer.h"
1415
#include "llvm/ADT/FoldingSet.h"
1516
#include "llvm/Support/raw_ostream.h"
1617
#include <algorithm>
@@ -815,6 +816,15 @@ void RewriteSystem::processMergedAssociatedTypes() {
815816
RewriteSystem::CompletionResult
816817
RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
817818
unsigned maxDepth) {
819+
unsigned steps = 0;
820+
821+
SWIFT_DEFER {
822+
if (Context.Stats) {
823+
Context.Stats->getFrontendCounters()
824+
.NumRequirementMachineCompletionSteps += steps;
825+
}
826+
};
827+
818828
// The worklist must be processed in first-in-first-out order, to ensure
819829
// that we resolve all overlaps among the initial set of rules before
820830
// moving on to overlaps between rules introduced by completion.
@@ -881,7 +891,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
881891
continue;
882892

883893
// Check if we've already done too much work.
884-
if (--maxIterations == 0)
894+
if (++steps >= maxIterations)
885895
return CompletionResult::MaxIterations;
886896

887897
const auto &newRule = Rules[i];

0 commit comments

Comments
 (0)