@@ -682,7 +682,7 @@ ConstraintSystem::SolverState::~SolverState() {
682682 // Update the "largest" statistics if this system is larger than the
683683 // previous one.
684684 // FIXME: This is not at all thread-safe.
685- if (NumStatesExplored > LargestNumStatesExplored .getValue ()) {
685+ if (NumSolverScopes > LargestNumSolverScopes .getValue ()) {
686686 LargestSolutionAttemptNumber = SolutionAttempt-1 ;
687687 ++LargestSolutionAttemptNumber;
688688 #define CS_STATISTIC (Name, Description ) \
@@ -695,17 +695,17 @@ ConstraintSystem::SolverState::~SolverState() {
695695
696696ConstraintSystem::SolverScope::SolverScope (ConstraintSystem &cs)
697697 : cs(cs),
698- numTypeVariables (cs.TypeVariables.size()),
699- numTrailChanges (cs.solverState->Trail.size()),
698+ startTypeVariables (cs.TypeVariables.size()),
699+ startTrailSteps (cs.solverState->Trail.size()),
700700 scopeNumber(cs.solverState->beginScope ()),
701701 moved(0 ) {
702702 ASSERT (!cs.failedConstraint && " Unexpected failed constraint!" );
703703}
704704
705705ConstraintSystem::SolverScope::SolverScope (SolverScope &&other)
706706 : cs(other.cs),
707- numTypeVariables (other.numTypeVariables ),
708- numTrailChanges (other.numTrailChanges ),
707+ startTypeVariables (other.startTypeVariables ),
708+ startTrailSteps (other.startTrailSteps ),
709709 scopeNumber(other.scopeNumber),
710710 moved(0 ) {
711711 other.moved = 1 ;
@@ -720,7 +720,7 @@ ConstraintSystem::SolverScope::~SolverScope() {
720720 return ;
721721
722722 // Roll back introduced type variables.
723- truncate (cs.TypeVariables , numTypeVariables );
723+ truncate (cs.TypeVariables , startTypeVariables );
724724
725725 // Move any remaining active constraints into the inactive list.
726726 if (!cs.ActiveConstraints .empty ()) {
@@ -731,16 +731,43 @@ ConstraintSystem::SolverScope::~SolverScope() {
731731 cs.ActiveConstraints );
732732 }
733733
734+ uint64_t endTrailSteps = cs.solverState ->Trail .size ();
735+
734736 // Roll back changes to the constraint system.
735- cs.solverState ->Trail .undo (numTrailChanges );
737+ cs.solverState ->Trail .undo (startTrailSteps );
736738
737739 // Update statistics.
738- cs.solverState ->endScope (scopeNumber);
740+ cs.solverState ->endScope (scopeNumber,
741+ startTrailSteps,
742+ endTrailSteps);
739743
740744 // Clear out other "failed" state.
741745 cs.failedConstraint = nullptr ;
742746}
743747
748+ unsigned ConstraintSystem::SolverState::beginScope () {
749+ ++depth;
750+ maxDepth = std::max (maxDepth, depth);
751+
752+ CS.incrementScopeCounter ();
753+
754+ return NumSolverScopes++;
755+ }
756+
757+ // / Update statistics when a scope ends.
758+ void ConstraintSystem::SolverState::endScope (unsigned scopeNumber,
759+ uint64_t startTrailSteps,
760+ uint64_t endTrailSteps) {
761+ ASSERT (depth > 0 );
762+ --depth;
763+
764+ NumTrailSteps += (endTrailSteps - startTrailSteps);
765+
766+ unsigned countSolverScopes = NumSolverScopes - scopeNumber;
767+ if (countSolverScopes == 1 )
768+ CS.incrementLeafScopes ();
769+ }
770+
744771// / Solve the system of constraints.
745772// /
746773// / \param allowFreeTypeVariables How to bind free type variables in
@@ -795,7 +822,7 @@ bool ConstraintSystem::Candidate::solve(
795822 ConstraintSystem cs (DC, std::nullopt );
796823
797824 // Set up expression type checker timer for the candidate.
798- cs.Timer . emplace (E, cs );
825+ cs.startExpressionTimer (E );
799826
800827 // Generate constraints for the new system.
801828 if (auto generatedExpr = cs.generateConstraints (E, DC)) {
@@ -1494,7 +1521,7 @@ ConstraintSystem::solveImpl(SyntacticElementTarget &target,
14941521
14951522 // Set up the expression type checker timer.
14961523 if (Expr *expr = target.getAsExpr ())
1497- Timer. emplace (expr, * this );
1524+ startExpressionTimer (expr);
14981525
14991526 if (generateConstraints (target, allowFreeTypeVariables))
15001527 return SolutionResult::forError ();
@@ -1529,7 +1556,8 @@ bool ConstraintSystem::solve(SmallVectorImpl<Solution> &solutions,
15291556 if (isDebugMode ()) {
15301557 auto &log = llvm::errs ();
15311558 log << " \n ---Solver statistics---\n " ;
1532- log << " Total number of scopes explored: " << solverState->NumStatesExplored << " \n " ;
1559+ log << " Total number of scopes explored: " << solverState->NumSolverScopes << " \n " ;
1560+ log << " Total number of trail steps: " << solverState->NumTrailSteps << " \n " ;
15331561 log << " Maximum depth reached while exploring solutions: " << solverState->maxDepth << " \n " ;
15341562 if (Timer) {
15351563 auto timeInMillis =
@@ -1679,7 +1707,7 @@ bool ConstraintSystem::solveForCodeCompletion(
16791707 setContextualInfo (expr, target.getExprContextualTypeInfo ());
16801708
16811709 // Set up the expression type checker timer.
1682- Timer. emplace (expr, * this );
1710+ startExpressionTimer (expr);
16831711
16841712 shrink (expr);
16851713 }
0 commit comments