Skip to content

Commit c8db9f5

Browse files
committed
Sema: Remove last heap allocation of Scope
1 parent e9add92 commit c8db9f5

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,8 +2143,6 @@ class ConstraintSystem {
21432143
friend struct ClosureIsolatedByPreconcurrency;
21442144
friend class SolverTrail;
21452145

2146-
class SolverScope;
2147-
21482146
/// Expressions that are known to be unevaluated.
21492147
/// Note: this is only used to support ObjCSelectorExpr at the moment.
21502148
llvm::SmallPtrSet<Expr *, 2> UnevaluatedRootExprs;
@@ -2555,23 +2553,21 @@ class ConstraintSystem {
25552553
}
25562554

25572555
/// Update statistics when a scope begins.
2558-
///
2559-
/// \param scope The scope to associate with current solver state.
2560-
void beginScope(SolverScope *scope) {
2556+
unsigned beginScope() {
25612557
++depth;
25622558
maxDepth = std::max(maxDepth, depth);
2563-
scope->scopeNumber = NumStatesExplored++;
25642559

25652560
CS.incrementScopeCounter();
2561+
2562+
return NumStatesExplored++;
25662563
}
25672564

25682565
/// Update statistics when a scope ends.
2569-
///
2570-
/// \param scope The solver scope to rollback.
2571-
void endScope(SolverScope *scope) {
2566+
void endScope(unsigned scopeNumber) {
2567+
ASSERT(depth > 0);
25722568
--depth;
25732569

2574-
unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber;
2570+
unsigned countScopesExplored = NumStatesExplored - scopeNumber;
25752571
if (countScopesExplored == 1)
25762572
CS.incrementLeafScopes();
25772573
}
@@ -2775,7 +2771,7 @@ class ConstraintSystem {
27752771
/// this object is destroyed.
27762772
///
27772773
///
2778-
class SolverScope {
2774+
struct SolverScope {
27792775
ConstraintSystem &cs;
27802776

27812777
/// The length of \c TypeVariables.
@@ -2785,15 +2781,19 @@ class ConstraintSystem {
27852781
unsigned numTrailChanges;
27862782

27872783
/// The scope number of this scope. Set when the scope is registered.
2788-
unsigned scopeNumber = 0;
2784+
unsigned scopeNumber : 31;
2785+
2786+
/// A moved-from scope skips doing anything in the destructor.
2787+
unsigned moved : 1;
2788+
2789+
explicit SolverScope(ConstraintSystem &cs);
27892790

27902791
SolverScope(const SolverScope &) = delete;
2791-
SolverScope &operator=(const SolverScope &) = delete;
2792+
SolverScope(SolverScope &&other);
27922793

2793-
friend class ConstraintSystem;
2794+
SolverScope &operator=(const SolverScope &) = delete;
2795+
SolverScope &operator=(SolverScope &&) = delete;
27942796

2795-
public:
2796-
explicit SolverScope(ConstraintSystem &cs);
27972797
~SolverScope();
27982798
};
27992799

lib/Sema/CSSolver.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,21 +702,32 @@ ConstraintSystem::SolverState::~SolverState() {
702702
}
703703

704704
ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
705-
: cs(cs) {
706-
numTrailChanges = cs.solverState->Trail.size();
707-
708-
numTypeVariables = cs.TypeVariables.size();
705+
: cs(cs),
706+
numTypeVariables(cs.TypeVariables.size()),
707+
numTrailChanges(cs.solverState->Trail.size()),
708+
scopeNumber(cs.solverState->beginScope()),
709+
moved(0) {
710+
ASSERT(!cs.failedConstraint && "Unexpected failed constraint!");
711+
}
709712

710-
cs.solverState->beginScope(this);
711-
assert(!cs.failedConstraint && "Unexpected failed constraint!");
713+
ConstraintSystem::SolverScope::SolverScope(SolverScope &&other)
714+
: cs(other.cs),
715+
numTypeVariables(other.numTypeVariables),
716+
numTrailChanges(other.numTrailChanges),
717+
scopeNumber(other.scopeNumber),
718+
moved(0) {
719+
other.moved = 1;
712720
}
713721

714722
ConstraintSystem::SolverScope::~SolverScope() {
723+
if (moved)
724+
return;
725+
715726
// Don't attempt to rollback from an incorrect state.
716727
if (cs.inInvalidState())
717728
return;
718729

719-
// Erase the end of various lists.
730+
// Roll back introduced type variables.
720731
truncate(cs.TypeVariables, numTypeVariables);
721732

722733
// Move any remaining active constraints into the inactive list.
@@ -731,7 +742,8 @@ ConstraintSystem::SolverScope::~SolverScope() {
731742
// Roll back changes to the constraint system.
732743
cs.solverState->Trail.undo(numTrailChanges);
733744

734-
cs.solverState->endScope(this);
745+
// Update statistics.
746+
cs.solverState->endScope(scopeNumber);
735747

736748
// Clear out other "failed" state.
737749
cs.failedConstraint = nullptr;

lib/Sema/CSStep.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ template <typename P> class BindingStep : public SolverStep {
519519
/// being attempted, helps to rewind state of the
520520
/// constraint system back to original before attempting
521521
/// next binding, if any.
522-
std::optional<std::pair<std::unique_ptr<Scope>, typename P::Element>>
522+
std::optional<std::pair<Scope, typename P::Element>>
523523
ActiveChoice;
524524

525525
BindingStep(ConstraintSystem &cs, P producer,
@@ -548,16 +548,14 @@ template <typename P> class BindingStep : public SolverStep {
548548
}
549549

550550
{
551-
auto &trail = CS.solverState->Trail;
552-
unsigned size = trail.size();
553-
554-
auto scope = std::make_unique<Scope>(CS);
551+
Scope scope(CS);
555552
if (attempt(*choice)) {
556553
ActiveChoice.emplace(std::move(scope), *choice);
557554

558555
if (CS.isDebugMode()) {
559-
trail.dumpActiveScopeChanges(
560-
llvm::errs(), size, CS.solverState->getCurrentIndent());
556+
CS.solverState->Trail.dumpActiveScopeChanges(
557+
llvm::errs(), ActiveChoice->first.numTrailChanges,
558+
CS.solverState->getCurrentIndent());
561559
}
562560

563561
return suspend(std::make_unique<SplitterStep>(CS, Solutions));

0 commit comments

Comments
 (0)