Skip to content

[ConstraintSystem] Add some counters helpful for understanding solver… #17329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,19 @@ bool ConstraintSystem::solve(Expr *const expr,
// Solve the system.
solveRec(solutions, allowFreeTypeVariables);

if (TC.getLangOpts().DebugConstraintSolver) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
log << "---Solver statistics---\n";
log << "Total number of scopes explored: " << solverState->NumStatesExplored << "\n";
log << "Number of leaf scopes explored: " << solverState->leafScopes << "\n";
log << "Maximum depth reached while exploring solutions: " << solverState->maxDepth << "\n";
if (Timer) {
auto timeInMillis =
1000 * Timer->getElapsedProcessTimeInFractionalSeconds();
log << "Time: " << timeInMillis << "ms\n";
}
}

// Filter deduced solutions, try to figure out if there is
// a single best solution to use, if not explicitly disabled
// by constraint system options.
Expand Down
19 changes: 18 additions & 1 deletion lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,15 @@ class ConstraintSystem {
/// \brief Depth of the solution stack.
unsigned depth = 0;

/// \brief Maximum depth reached so far in exploring solutions.
unsigned maxDepth = 0;

/// \brief Count of the number of leaf scopes we've created. These
/// either result in a failure to solve, or in a solution, unlike
/// all the intermediate scopes. They are interesting to track as
/// part of a metric of whether an expression is too complex.
unsigned leafScopes = 0;

/// \brief Whether to record failures or not.
bool recordFixes = false;

Expand Down Expand Up @@ -1198,7 +1207,8 @@ class ConstraintSystem {
/// \param scope The scope to associate with current solver state.
void registerScope(SolverScope *scope) {
++depth;
++NumStatesExplored;
maxDepth = std::max(maxDepth, depth);
scope->scopeNumber = NumStatesExplored++;

CS.incrementScopeCounter();
auto scopeInfo =
Expand All @@ -1217,6 +1227,10 @@ class ConstraintSystem {
void rollback(SolverScope *scope) {
--depth;

unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber;
if (countScopesExplored == 1)
++leafScopes;

SolverScope *savedScope;
// The position of last retired constraint before given scope.
ConstraintList::iterator lastRetiredPos;
Expand Down Expand Up @@ -1445,6 +1459,9 @@ class ConstraintSystem {
/// The previous score.
Score PreviousScore;

/// The scope number of this scope. Set when the scope is registered.
unsigned scopeNumber = 0;

/// Time in fractional seconds at which we entered this scope.
double startTime;

Expand Down