Skip to content

Commit 3e25467

Browse files
committed
[Sema] Add counter to track number of constraints considered by each edge contraction attempt
1 parent e021691 commit 3e25467

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

include/swift/Basic/Statistics.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ FRONTEND_STATISTIC(Sema, NumConformancesDeserialized)
149149
/// expression typechecker did".
150150
FRONTEND_STATISTIC(Sema, NumConstraintScopes)
151151

152+
/// Number of constraints considered per attempt to
153+
/// contract constraint graph edges.
154+
/// This is a measure of complexity of the contraction algorithm.
155+
FRONTEND_STATISTIC(Sema, NumConstraintsConsideredForEdgeContraction)
156+
152157
/// Number of declarations that were deserialized. A rough proxy for the amount
153158
/// of material loaded from other modules.
154159
FRONTEND_STATISTIC(Sema, NumDeclsDeserialized)

lib/Sema/ConstraintGraph.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ConstraintGraph.h"
1919
#include "ConstraintGraphScope.h"
2020
#include "ConstraintSystem.h"
21+
#include "swift/Basic/Statistic.h"
2122
#include "llvm/Support/Debug.h"
2223
#include "llvm/Support/SaveAndRestore.h"
2324
#include <algorithm>
@@ -27,6 +28,8 @@
2728
using namespace swift;
2829
using namespace constraints;
2930

31+
#define DEBUG_TYPE "ConstraintGraph"
32+
3033
#pragma mark Graph construction/destruction
3134

3235
ConstraintGraph::ConstraintGraph(ConstraintSystem &cs) : CS(cs) { }
@@ -650,7 +653,9 @@ static bool shouldContractEdge(ConstraintKind kind) {
650653

651654
bool ConstraintGraph::contractEdges() {
652655
SmallVector<Constraint *, 16> constraints;
653-
CS.findConstraints(constraints, [](const Constraint &constraint) {
656+
CS.findConstraints(constraints, [&](const Constraint &constraint) {
657+
// Track how many constraints did contraction algorithm iterated over.
658+
incrementConstraintsPerContractionCounter();
654659
return shouldContractEdge(constraint.getKind());
655660
});
656661

@@ -767,6 +772,14 @@ void ConstraintGraph::optimize() {
767772
while (contractEdges()) {}
768773
}
769774

775+
void ConstraintGraph::incrementConstraintsPerContractionCounter() {
776+
SWIFT_FUNC_STAT;
777+
auto &context = CS.getASTContext();
778+
if (context.Stats)
779+
context.Stats->getFrontendCounters()
780+
.NumConstraintsConsideredForEdgeContraction++;
781+
}
782+
770783
#pragma mark Debugging output
771784

772785
void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent) {

lib/Sema/ConstraintGraph.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ class ConstraintGraph {
332332
/// Constraints that are "orphaned" because they contain no type variables.
333333
SmallVector<Constraint *, 4> OrphanedConstraints;
334334

335+
/// Increment the number of constraints considered per attempt
336+
/// to contract constrant graph edges.
337+
void incrementConstraintsPerContractionCounter();
338+
335339
/// The kind of change made to the graph.
336340
enum class ChangeKind {
337341
/// Added a type variable.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %scale-test --begin 0 --end 25000 --step 1000 --typecheck --select incrementConstraintsPerContractionCounter %s
2+
// REQUIRES: OS=macosx
3+
// REQUIRES: asserts
4+
5+
let _: [Int] = [
6+
%for i in range(0, N):
7+
1,
8+
%end
9+
1
10+
]

0 commit comments

Comments
 (0)