Skip to content

Commit bed95dd

Browse files
committed
Sema: Simplify PotentialBindings updates a little
1 parent 340a60b commit bed95dd

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

Diff for: include/swift/Sema/ConstraintGraph.h

-9
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,6 @@ class ConstraintGraphNode {
146146

147147
/// Binding Inference {
148148

149-
/// Infer bindings from the given constraint and notify referenced variables
150-
/// about its arrival (if requested). This happens every time a new constraint
151-
/// gets added to a constraint graph node.
152-
void introduceToInference(Constraint *constraint);
153-
154-
/// Forget about the given constraint. This happens every time a constraint
155-
/// gets removed for a constraint graph.
156-
void retractFromInference(Constraint *constraint);
157-
158149
/// Perform graph updates that must be undone after we bind a fixed type
159150
/// to a type variable.
160151
void retractFromInference(Type fixedType);

Diff for: lib/Sema/ConstraintGraph.cpp

+31-47
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,10 @@ void ConstraintGraphNode::notifyReferencingVars(
230230

231231
void ConstraintGraphNode::notifyReferencedVars(
232232
llvm::function_ref<void(ConstraintGraphNode &)> notification) const {
233-
for (auto *fixedBinding : getReferencedVars()) {
234-
notification(CG[fixedBinding]);
233+
for (auto *referencedVar : getReferencedVars()) {
234+
auto *repr = referencedVar->getImpl().getRepresentative(/*record=*/nullptr);
235+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
236+
notification(CG[repr]);
235237
}
236238
}
237239

@@ -285,30 +287,6 @@ void ConstraintGraphNode::removeReferencedBy(TypeVariableType *typeVar) {
285287
}
286288
}
287289

288-
void ConstraintGraphNode::introduceToInference(Constraint *constraint) {
289-
if (forRepresentativeVar()) {
290-
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
291-
if (!fixedType)
292-
getPotentialBindings().infer(CG.getConstraintSystem(), TypeVar, constraint);
293-
} else {
294-
auto *repr =
295-
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
296-
CG[repr].introduceToInference(constraint);
297-
}
298-
}
299-
300-
void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
301-
if (forRepresentativeVar()) {
302-
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
303-
if (!fixedType)
304-
getPotentialBindings().retract(CG.getConstraintSystem(), TypeVar,constraint);
305-
} else {
306-
auto *repr =
307-
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
308-
CG[repr].retractFromInference(constraint);
309-
}
310-
}
311-
312290
void ConstraintGraphNode::updateFixedType(
313291
Type fixedType,
314292
llvm::function_ref<void (ConstraintGraphNode &,
@@ -328,7 +306,11 @@ void ConstraintGraphNode::updateFixedType(
328306
fixedType->getTypeVariables(referencedVars);
329307

330308
for (auto *referencedVar : referencedVars) {
331-
auto &node = CG[referencedVar];
309+
auto *repr = referencedVar->getImpl().getRepresentative(/*record=*/nullptr);
310+
if (repr->getImpl().getFixedType(/*record=*/nullptr))
311+
continue;
312+
313+
auto &node = CG[repr];
332314

333315
// Newly referred vars need to re-introduce all constraints associated
334316
// with this type variable since they are now going to be used in
@@ -341,18 +323,20 @@ void ConstraintGraphNode::updateFixedType(
341323
}
342324

343325
void ConstraintGraphNode::retractFromInference(Type fixedType) {
326+
auto &cs = CG.getConstraintSystem();
344327
return updateFixedType(
345328
fixedType,
346-
[](ConstraintGraphNode &node, Constraint *constraint) {
347-
node.retractFromInference(constraint);
329+
[&cs](ConstraintGraphNode &node, Constraint *constraint) {
330+
node.getPotentialBindings().retract(cs, node.getTypeVariable(), constraint);
348331
});
349332
}
350333

351334
void ConstraintGraphNode::introduceToInference(Type fixedType) {
335+
auto &cs = CG.getConstraintSystem();
352336
return updateFixedType(
353337
fixedType,
354-
[](ConstraintGraphNode &node, Constraint *constraint) {
355-
node.introduceToInference(constraint);
338+
[&cs](ConstraintGraphNode &node, Constraint *constraint) {
339+
node.getPotentialBindings().infer(cs, node.getTypeVariable(), constraint);
356340
});
357341
}
358342

@@ -377,13 +361,13 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
377361

378362
addConstraint(typeVar, constraint);
379363

380-
auto &node = (*this)[typeVar];
381-
382-
node.introduceToInference(constraint);
364+
auto *repr = typeVar->getImpl().getRepresentative(/*record=*/nullptr);
365+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
366+
(*this)[repr].getPotentialBindings().infer(CS, repr, constraint);
383367

384368
if (isUsefulForReferencedVars(constraint)) {
385-
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
386-
referencedVar.introduceToInference(constraint);
369+
(*this)[typeVar].notifyReferencedVars([&](ConstraintGraphNode &node) {
370+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
387371
});
388372
}
389373
}
@@ -415,14 +399,13 @@ void ConstraintGraph::removeConstraint(Constraint *constraint) {
415399
// For the nodes corresponding to each type variable...
416400
auto referencedTypeVars = constraint->getTypeVariables();
417401
for (auto typeVar : referencedTypeVars) {
418-
// Find the node for this type variable.
419-
auto &node = (*this)[typeVar];
420-
421-
node.retractFromInference(constraint);
402+
auto *repr = typeVar->getImpl().getRepresentative(/*record=*/nullptr);
403+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
404+
(*this)[repr].getPotentialBindings().retract(CS, repr, constraint);
422405

423406
if (isUsefulForReferencedVars(constraint)) {
424-
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
425-
referencedVar.retractFromInference(constraint);
407+
(*this)[typeVar].notifyReferencedVars([&](ConstraintGraphNode &node) {
408+
node.getPotentialBindings().retract(CS, node.getTypeVariable(), constraint);
426409
});
427410
}
428411

@@ -468,7 +451,7 @@ void ConstraintGraph::mergeNodesPre(TypeVariableType *typeVar2) {
468451

469452
node.notifyReferencingVars(
470453
[&](ConstraintGraphNode &node, Constraint *constraint) {
471-
node.retractFromInference(constraint);
454+
node.getPotentialBindings().retract(CS, node.getTypeVariable(), constraint);
472455
});
473456
}
474457
}
@@ -498,19 +481,20 @@ void ConstraintGraph::mergeNodes(TypeVariableType *typeVar1,
498481
auto &node = (*this)[newMember];
499482

500483
for (auto *constraint : node.getConstraints()) {
501-
repNode.introduceToInference(constraint);
484+
if (!typeVar1->getImpl().getFixedType(/*record=*/nullptr))
485+
repNode.getPotentialBindings().infer(CS, typeVar1, constraint);
502486

503487
if (!isUsefulForReferencedVars(constraint))
504488
continue;
505489

506-
repNode.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
507-
referencedVar.introduceToInference(constraint);
490+
repNode.notifyReferencedVars([&](ConstraintGraphNode &node) {
491+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
508492
});
509493
}
510494

511495
node.notifyReferencingVars(
512496
[&](ConstraintGraphNode &node, Constraint *constraint) {
513-
node.introduceToInference(constraint);
497+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
514498
});
515499
}
516500
}

0 commit comments

Comments
 (0)