Skip to content

[ConstraintGraph] Optimize edge contraction #36220

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
Mar 3, 2021
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
55 changes: 39 additions & 16 deletions lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,26 +1298,49 @@ ConstraintGraph::computeConnectedComponents(
return cc.getComponents();
}

bool ConstraintGraph::contractEdges() {
// Current constraint system doesn't have any closure expressions
// associated with it so there is nothing to here.
if (CS.ClosureTypes.empty())
return false;

/// For a given constraint kind, decide if we should attempt to eliminate its
/// edge in the graph.
static bool shouldContractEdge(ConstraintKind kind) {
switch (kind) {
case ConstraintKind::BindParam:
return true;
// For a given constraint kind, decide if we should attempt to eliminate its
// edge in the graph.
auto shouldContractEdge = [](ConstraintKind kind) {
switch (kind) {
case ConstraintKind::BindParam:
return true;

default:
return false;
}
}
default:
return false;
}
};

bool ConstraintGraph::contractEdges() {
SmallVector<Constraint *, 16> constraints;
CS.findConstraints(constraints, [&](const Constraint &constraint) {
// Track how many constraints did contraction algorithm iterated over.
incrementConstraintsPerContractionCounter();
return shouldContractEdge(constraint.getKind());
});
for (const auto &closure : CS.ClosureTypes) {
for (const auto &param : closure.second->getParams()) {
auto paramTy = param.getPlainType()->getAs<TypeVariableType>();
if (!paramTy)
continue;

// This closure is not currently in scope.
if (!CS.TypeVariables.count(paramTy))
break;

// Nothing to contract here since outside parameter
// is already bound to a concrete type.
if (CS.getFixedType(paramTy))
continue;

for (auto *constraint : (*this)[paramTy].getConstraints()) {
// Track how many constraints did contraction algorithm iterated over.
incrementConstraintsPerContractionCounter();

if (shouldContractEdge(constraint->getKind()))
constraints.push_back(constraint);
}
}
}

bool didContractEdges = false;
for (auto *constraint : constraints) {
Expand Down

This file was deleted.