@@ -1045,13 +1045,15 @@ struct Score {
1045
1045
friend Score operator -(const Score &x, const Score &y) {
1046
1046
Score result;
1047
1047
for (unsigned i = 0 ; i != NumScoreKinds; ++i) {
1048
+ ASSERT (x.Data [i] >= y.Data [i]);
1048
1049
result.Data [i] = x.Data [i] - y.Data [i];
1049
1050
}
1050
1051
return result;
1051
1052
}
1052
1053
1053
1054
friend Score &operator -=(Score &x, const Score &y) {
1054
1055
for (unsigned i = 0 ; i != NumScoreKinds; ++i) {
1056
+ ASSERT (x.Data [i] >= y.Data [i]);
1055
1057
x.Data [i] -= y.Data [i];
1056
1058
}
1057
1059
return x;
@@ -2531,98 +2533,47 @@ class ConstraintSystem {
2531
2533
// / The number of the solution attempts we're looking at.
2532
2534
unsigned SolutionAttempt;
2533
2535
2534
- // / Refers to the innermost partial solution scope.
2535
- SolverScope *PartialSolutionScope = nullptr ;
2536
+ // / The number of fixes in the innermost partial solution scope.
2537
+ unsigned numPartialSolutionFixes = 0 ;
2536
2538
2537
2539
// Statistics
2538
2540
#define CS_STATISTIC (Name, Description ) unsigned Name = 0 ;
2539
2541
#include " ConstraintSolverStats.def"
2540
2542
2541
- // / Check whether there are any retired constraints present.
2542
- bool hasRetiredConstraints () const {
2543
- return !retiredConstraints.empty ();
2544
- }
2545
-
2546
2543
// / Mark given constraint as retired along current solver path.
2547
2544
// /
2548
2545
// / \param constraint The constraint to retire temporarily.
2549
2546
void retireConstraint (Constraint *constraint) {
2550
- retiredConstraints.push_front (constraint);
2551
- }
2552
-
2553
- // / Iterate over all of the retired constraints registered with
2554
- // / current solver state.
2555
- // /
2556
- // / \param processor The processor function to be applied to each of
2557
- // / the constraints retrieved.
2558
- void forEachRetired (llvm::function_ref<void (Constraint &)> processor) {
2559
- for (auto &constraint : retiredConstraints)
2560
- processor (constraint);
2547
+ Trail.recordChange (SolverTrail::Change::RetiredConstraint (constraint));
2561
2548
}
2562
2549
2563
2550
// / Add new "generated" constraint along the current solver path.
2564
2551
// /
2565
2552
// / \param constraint The newly generated constraint.
2566
2553
void addGeneratedConstraint (Constraint *constraint) {
2567
- assert (constraint && " Null generated constraint?" );
2568
- generatedConstraints.push_back (constraint);
2554
+ Trail.recordChange (SolverTrail::Change::GeneratedConstraint (constraint));
2569
2555
}
2570
2556
2571
- // / Register given scope to be tracked by the current solver state,
2572
- // / this helps to make sure that all of the retired/generated constraints
2573
- // / are dealt with correctly when the life time of the scope ends.
2557
+ // / Update statistics when a scope begins.
2574
2558
// /
2575
2559
// / \param scope The scope to associate with current solver state.
2576
- void registerScope (SolverScope *scope) {
2560
+ void beginScope (SolverScope *scope) {
2577
2561
++depth;
2578
2562
maxDepth = std::max (maxDepth, depth);
2579
2563
scope->scopeNumber = NumStatesExplored++;
2580
2564
2581
2565
CS.incrementScopeCounter ();
2582
- auto scopeInfo =
2583
- std::make_tuple (scope, retiredConstraints.begin (),
2584
- generatedConstraints.size ());
2585
- scopes.push_back (scopeInfo);
2586
2566
}
2587
2567
2588
- // / Restore all of the retired/generated constraints to the state
2589
- // / before given scope. This is required because retired constraints have
2590
- // / to be re-introduced to the system in order of arrival (LIFO) and list
2591
- // / of the generated constraints has to be truncated back to the
2592
- // / original size.
2568
+ // / Update statistics when a scope ends.
2593
2569
// /
2594
2570
// / \param scope The solver scope to rollback.
2595
- void rollback (SolverScope *scope) {
2571
+ void endScope (SolverScope *scope) {
2596
2572
--depth;
2597
2573
2598
2574
unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber ;
2599
2575
if (countScopesExplored == 1 )
2600
2576
CS.incrementLeafScopes ();
2601
-
2602
- SolverScope *savedScope;
2603
- // The position of last retired constraint before given scope.
2604
- ConstraintList::iterator lastRetiredPos;
2605
- // The original number of generated constraints before given scope.
2606
- unsigned numGenerated;
2607
-
2608
- std::tie (savedScope, lastRetiredPos, numGenerated) =
2609
- scopes.pop_back_val ();
2610
-
2611
- assert (savedScope == scope && " Scope rollback not in LIFO order!" );
2612
-
2613
- // Restore all of the retired constraints.
2614
- CS.InactiveConstraints .splice (CS.InactiveConstraints .end (),
2615
- retiredConstraints,
2616
- retiredConstraints.begin (), lastRetiredPos);
2617
-
2618
- // And remove all of the generated constraints.
2619
- auto genStart = generatedConstraints.begin () + numGenerated,
2620
- genEnd = generatedConstraints.end ();
2621
- for (auto genI = genStart; genI != genEnd; ++genI) {
2622
- CS.InactiveConstraints .erase (ConstraintList::iterator (*genI));
2623
- }
2624
-
2625
- generatedConstraints.erase (genStart, genEnd);
2626
2577
}
2627
2578
2628
2579
// / Check whether constraint system is allowed to form solutions
@@ -2649,29 +2600,12 @@ class ConstraintSystem {
2649
2600
}
2650
2601
2651
2602
private:
2652
- // / The list of constraints that have been retired along the
2653
- // / current path, this list is used in LIFO fashion when
2654
- // / constraints are added back to the circulation.
2655
- ConstraintList retiredConstraints;
2603
+ // / Depth of the solution stack.
2604
+ unsigned depth = 0 ;
2656
2605
2657
2606
// / The set of constraints which were active at the time of this state
2658
2607
// / creating, it's used to re-activate them on destruction.
2659
2608
SmallVector<Constraint *, 4 > activeConstraints;
2660
-
2661
- // / The current set of generated constraints.
2662
- SmallVector<Constraint *, 4 > generatedConstraints;
2663
-
2664
- // / The collection which holds association between solver scope
2665
- // / and position of the last retired constraint and number of
2666
- // / constraints generated before registration of given scope,
2667
- // / this helps to rollback all of the constraints retired/generated
2668
- // / each of the registered scopes correct (LIFO) order.
2669
- llvm::SmallVector<
2670
- std::tuple<SolverScope *, ConstraintList::iterator, unsigned >, 4 > scopes;
2671
-
2672
-
2673
- // / Depth of the solution stack.
2674
- unsigned depth = 0 ;
2675
2609
};
2676
2610
2677
2611
class CacheExprTypes : public ASTWalker {
@@ -2850,14 +2784,6 @@ class ConstraintSystem {
2850
2784
// / The length of \c Trail.
2851
2785
unsigned numTrailChanges;
2852
2786
2853
- // / The length of \c Fixes.
2854
- // /
2855
- // / FIXME: Remove this.
2856
- unsigned numFixes;
2857
-
2858
- // / The previous score.
2859
- Score PreviousScore;
2860
-
2861
2787
// / The scope number of this scope. Set when the scope is registered.
2862
2788
unsigned scopeNumber = 0 ;
2863
2789
@@ -2903,7 +2829,8 @@ class ConstraintSystem {
2903
2829
// / This operation is used to take a solution computed based on some
2904
2830
// / subset of the constraints and then apply it back to the
2905
2831
// / constraint system for further exploration.
2906
- void applySolution (const Solution &solution);
2832
+ void replaySolution (const Solution &solution,
2833
+ bool shouldIncreaseScore=true );
2907
2834
2908
2835
// FIXME: Perhaps these belong on ConstraintSystem itself.
2909
2836
friend std::optional<BraceStmt *>
@@ -5531,10 +5458,13 @@ class ConstraintSystem {
5531
5458
5532
5459
public:
5533
5460
// / Increase the score of the given kind for the current (partial) solution
5534
- // / along the.
5461
+ // / along the current solver path .
5535
5462
void increaseScore (ScoreKind kind, ConstraintLocatorBuilder Locator,
5536
5463
unsigned value = 1 );
5537
5464
5465
+ // / Primitive form of the above. Records a change in the trail.
5466
+ void increaseScore (ScoreKind kind, unsigned value);
5467
+
5538
5468
// / Determine whether this solution is guaranteed to be worse than the best
5539
5469
// / solution found so far.
5540
5470
bool worseThanBestSolution () const ;
0 commit comments