Skip to content

Commit 7321b10

Browse files
authored
Merge pull request #17329 from rudkx/improve-too-complex-metric
[ConstraintSystem] Add some counters helpful for understanding solver…
2 parents e53ec62 + 93dbb54 commit 7321b10

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,19 @@ bool ConstraintSystem::solve(Expr *const expr,
14231423
// Solve the system.
14241424
solveRec(solutions, allowFreeTypeVariables);
14251425

1426+
if (TC.getLangOpts().DebugConstraintSolver) {
1427+
auto &log = getASTContext().TypeCheckerDebug->getStream();
1428+
log << "---Solver statistics---\n";
1429+
log << "Total number of scopes explored: " << solverState->NumStatesExplored << "\n";
1430+
log << "Number of leaf scopes explored: " << solverState->leafScopes << "\n";
1431+
log << "Maximum depth reached while exploring solutions: " << solverState->maxDepth << "\n";
1432+
if (Timer) {
1433+
auto timeInMillis =
1434+
1000 * Timer->getElapsedProcessTimeInFractionalSeconds();
1435+
log << "Time: " << timeInMillis << "ms\n";
1436+
}
1437+
}
1438+
14261439
// Filter deduced solutions, try to figure out if there is
14271440
// a single best solution to use, if not explicitly disabled
14281441
// by constraint system options.

lib/Sema/ConstraintSystem.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,15 @@ class ConstraintSystem {
11241124
/// \brief Depth of the solution stack.
11251125
unsigned depth = 0;
11261126

1127+
/// \brief Maximum depth reached so far in exploring solutions.
1128+
unsigned maxDepth = 0;
1129+
1130+
/// \brief Count of the number of leaf scopes we've created. These
1131+
/// either result in a failure to solve, or in a solution, unlike
1132+
/// all the intermediate scopes. They are interesting to track as
1133+
/// part of a metric of whether an expression is too complex.
1134+
unsigned leafScopes = 0;
1135+
11271136
/// \brief Whether to record failures or not.
11281137
bool recordFixes = false;
11291138

@@ -1198,7 +1207,8 @@ class ConstraintSystem {
11981207
/// \param scope The scope to associate with current solver state.
11991208
void registerScope(SolverScope *scope) {
12001209
++depth;
1201-
++NumStatesExplored;
1210+
maxDepth = std::max(maxDepth, depth);
1211+
scope->scopeNumber = NumStatesExplored++;
12021212

12031213
CS.incrementScopeCounter();
12041214
auto scopeInfo =
@@ -1217,6 +1227,10 @@ class ConstraintSystem {
12171227
void rollback(SolverScope *scope) {
12181228
--depth;
12191229

1230+
unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber;
1231+
if (countScopesExplored == 1)
1232+
++leafScopes;
1233+
12201234
SolverScope *savedScope;
12211235
// The position of last retired constraint before given scope.
12221236
ConstraintList::iterator lastRetiredPos;
@@ -1445,6 +1459,9 @@ class ConstraintSystem {
14451459
/// The previous score.
14461460
Score PreviousScore;
14471461

1462+
/// The scope number of this scope. Set when the scope is registered.
1463+
unsigned scopeNumber = 0;
1464+
14481465
/// Time in fractional seconds at which we entered this scope.
14491466
double startTime;
14501467

0 commit comments

Comments
 (0)