Skip to content

Minor constraint system stuff #16911

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 3 commits into from
May 30, 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
11 changes: 4 additions & 7 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -472,8 +472,6 @@ ConstraintSystem::SolverState::~SolverState() {
ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
: cs(cs), CGScope(cs.CG)
{
++cs.solverState->depth;

resolvedOverloadSets = cs.resolvedOverloadSets;
numTypeVariables = cs.TypeVariables.size();
numSavedBindings = cs.solverState->savedBindings.size();
Expand All @@ -485,16 +483,15 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
numDefaultedConstraints = cs.DefaultedConstraints.size();
numCheckedConformances = cs.CheckedConformances.size();
PreviousScore = cs.CurrentScore;
if (cs.Timer) {
startTime = cs.Timer->getElapsedProcessTimeInFractionalSeconds();
}

cs.solverState->registerScope(this);
assert(!cs.failedConstraint && "Unexpected failed constraint!");

++cs.solverState->NumStatesExplored;
}

ConstraintSystem::SolverScope::~SolverScope() {
--cs.solverState->depth;

// Erase the end of various lists.
cs.resolvedOverloadSets = resolvedOverloadSets;
truncate(cs.TypeVariables, numTypeVariables);
Expand Down
43 changes: 29 additions & 14 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -1144,19 +1144,6 @@ class ConstraintSystem {
#define CS_STATISTIC(Name, Description) unsigned Name = 0;
#include "ConstraintSolverStats.def"

/// \brief Register given scope to be tracked by the current solver state,
/// this helps to make sure that all of the retired/generated constraints
/// are dealt with correctly when the life time of the scope ends.
///
/// \param scope The scope to associate with current solver state.
void registerScope(SolverScope *scope) {
CS.incrementScopeCounter();
auto scopeInfo =
std::make_tuple(scope, retiredConstraints.begin(),
generatedConstraints.size());
scopes.push_back(scopeInfo);
}

/// \brief Check whether there are any retired constraints present.
bool hasRetiredConstraints() const {
return !retiredConstraints.empty();
Expand Down Expand Up @@ -1204,6 +1191,22 @@ class ConstraintSystem {
}
}

/// \brief Register given scope to be tracked by the current solver state,
/// this helps to make sure that all of the retired/generated constraints
/// are dealt with correctly when the life time of the scope ends.
///
/// \param scope The scope to associate with current solver state.
void registerScope(SolverScope *scope) {
++depth;
++NumStatesExplored;

CS.incrementScopeCounter();
auto scopeInfo =
std::make_tuple(scope, retiredConstraints.begin(),
generatedConstraints.size());
scopes.push_back(scopeInfo);
}

/// \brief Restore all of the retired/generated constraints to the state
/// before given scope. This is required because retired constraints have
/// to be re-introduced to the system in order of arrival (LIFO) and list
Expand All @@ -1212,6 +1215,8 @@ class ConstraintSystem {
///
/// \param scope The solver scope to rollback.
void rollback(SolverScope *scope) {
--depth;

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

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

/// Constraint graph scope associated with this solver scope.
ConstraintGraphScope CGScope;

Expand All @@ -1451,6 +1459,13 @@ class ConstraintSystem {
public:
explicit SolverScope(ConstraintSystem &cs);
~SolverScope();

Optional<double> getElapsedTimeInFractionalSeconds() {
if (!cs.Timer)
return None;

return cs.Timer->getElapsedProcessTimeInFractionalSeconds() - startTime;
}
};

ConstraintSystem(TypeChecker &tc, DeclContext *dc,
Expand Down