Skip to content

Commit 9bf4fe8

Browse files
authored
Merge pull request swiftlang#35903 from xedin/cs-incremental-bindings
[ConstraintSystem] Compute bindings incrementally
2 parents 88c73c6 + 8b8e002 commit 9bf4fe8

16 files changed

+786
-456
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 172 additions & 128 deletions
Large diffs are not rendered by default.

include/swift/Sema/ConstraintGraph.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Basic/LLVM.h"
2222
#include "swift/AST/Identifier.h"
2323
#include "swift/AST/Type.h"
24+
#include "swift/Sema/CSBindings.h"
2425
#include "llvm/ADT/ArrayRef.h"
2526
#include "llvm/ADT/DenseSet.h"
2627
#include "llvm/ADT/DenseMap.h"
@@ -47,7 +48,8 @@ class ConstraintSystem;
4748
/// A single node in the constraint graph, which represents a type variable.
4849
class ConstraintGraphNode {
4950
public:
50-
explicit ConstraintGraphNode(TypeVariableType *typeVar) : TypeVar(typeVar) { }
51+
explicit ConstraintGraphNode(ConstraintGraph &CG, TypeVariableType *typeVar)
52+
: CG(CG), TypeVar(typeVar) {}
5153

5254
ConstraintGraphNode(const ConstraintGraphNode&) = delete;
5355
ConstraintGraphNode &operator=(const ConstraintGraphNode&) = delete;
@@ -75,6 +77,8 @@ class ConstraintGraphNode {
7577
/// as this type variable.
7678
ArrayRef<TypeVariableType *> getEquivalenceClass() const;
7779

80+
inference::PotentialBindings &getCurrentBindings();
81+
7882
private:
7983
/// Determines whether the type variable associated with this node
8084
/// is a representative of an equivalence class.
@@ -98,6 +102,9 @@ class ConstraintGraphNode {
98102
/// Add the given type variables to this node's equivalence class.
99103
void addToEquivalenceClass(ArrayRef<TypeVariableType *> typeVars);
100104

105+
/// Remove N last members from equivalence class of the current type variable.
106+
void truncateEquivalenceClass(unsigned prevSize);
107+
101108
/// Add a type variable related to this type variable through fixed
102109
/// binding.
103110
void addReferencedVar(TypeVariableType *typeVar);
@@ -112,9 +119,46 @@ class ConstraintGraphNode {
112119
/// Remove a type variable which used to reference this type variable.
113120
void removeReferencedBy(TypeVariableType *typeVar);
114121

122+
/// Binding Inference {
123+
124+
/// Infer bindings from the given constraint and notify referenced variables
125+
/// about its arrival (if requested). This happens every time a new constraint
126+
/// gets added to a constraint graph node.
127+
void introduceToInference(Constraint *constraint, bool notifyReferencedVars);
128+
129+
/// Forget about the given constraint. This happens every time a constraint
130+
/// gets removed for a constraint graph.
131+
void retractFromInference(Constraint *constraint, bool notifyReferencedVars);
132+
133+
/// Re-evaluate the given constraint. This happens when there are changes
134+
/// in associated type variables e.g. bound/unbound to/from a fixed type,
135+
/// equivalence class changes.
136+
void reintroduceToInference(Constraint *constraint, bool notifyReferencedVars);
137+
138+
/// Drop all previously collected bindings and re-infer based on the
139+
/// current set constraints associated with this equivalence class.
140+
void resetBindingSet();
141+
142+
/// Notify all of the type variables that have this one (or any member of
143+
/// its equivalence class) referenced in their fixed type.
144+
///
145+
/// This is a traversal up the reference change which triggers constraint
146+
/// re-introduction to affected type variables.
147+
///
148+
/// This is useful in situations when type variable gets bound and unbound,
149+
/// or equivalence class changes.
150+
void notifyReferencingVars() const;
151+
/// }
152+
153+
/// The constraint graph this node belongs to.
154+
ConstraintGraph &CG;
155+
115156
/// The type variable this node represents.
116157
TypeVariableType *TypeVar;
117158

159+
/// The set of bindings associated with this type variable.
160+
llvm::Optional<inference::PotentialBindings> Bindings;
161+
118162
/// The vector of constraints that mention this type variable, in a stable
119163
/// order for iteration.
120164
SmallVector<Constraint *, 2> Constraints;

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,12 +4633,11 @@ class ConstraintSystem {
46334633
ConstraintKind bodyResultConstraintKind,
46344634
ConstraintLocatorBuilder locator);
46354635

4636-
Optional<PotentialBindings> determineBestBindings();
4636+
Optional<BindingSet> determineBestBindings();
46374637

4638-
/// Infer bindings for the given type variable based on current
4638+
/// Get bindings for the given type variable based on current
46394639
/// state of the constraint system.
4640-
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,
4641-
bool finalize = true);
4640+
BindingSet getBindingsFor(TypeVariableType *typeVar, bool finalize = true);
46424641

46434642
private:
46444643
/// Add a constraint to the constraint system.
@@ -5406,7 +5405,7 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
54065405
public:
54075406
using Element = TypeVariableBinding;
54085407

5409-
TypeVarBindingProducer(PotentialBindings &bindings);
5408+
TypeVarBindingProducer(BindingSet &bindings);
54105409

54115410
/// Retrieve a set of bindings available in the current state.
54125411
ArrayRef<Binding> getCurrentBindings() const { return Bindings; }

0 commit comments

Comments
 (0)