Skip to content

[4.2] [ConstraintSystem] Attempt to select disjunctions that split constraint systems. #16813

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 2 commits into from
May 24, 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
76 changes: 75 additions & 1 deletion lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1802,15 +1802,89 @@ static bool shouldSkipDisjunctionChoice(ConstraintSystem &cs,
return false;
}

// Attempt to find a disjunction of bind constraints where all options
// in the disjunction are binding the same type variable, and where
// that type variable appears as the right hand side of a conversion
// constraint.
//
// Trying these bindings early can make it possible to split the
// constraint system into multiple ones.
static Constraint *selectDisjunctionBindingConversionResultType(
ConstraintSystem &cs, SmallVectorImpl<Constraint *> &disjunctions) {

// Collect any disjunctions that simply attempt bindings for a
// type variable.
SmallVector<Constraint *, 8> bindingDisjunctions;
for (auto *disjunction : disjunctions) {
llvm::Optional<TypeVariableType *> commonTypeVariable;
if (llvm::all_of(
disjunction->getNestedConstraints(),
[&](Constraint *bindingConstraint) {
if (bindingConstraint->getKind() != ConstraintKind::Bind)
return false;

auto *tv =
bindingConstraint->getFirstType()->getAs<TypeVariableType>();
// Only do this for simple type variable bindings, not for
// bindings like: ($T1) -> $T2 bind String -> Int
if (!tv)
return false;

if (!commonTypeVariable.hasValue())
commonTypeVariable = tv;

if (commonTypeVariable.getValue() != tv)
return false;

return true;
})) {
bindingDisjunctions.push_back(disjunction);
}
}

for (auto *disjunction : bindingDisjunctions) {
auto nested = disjunction->getNestedConstraints();
assert(!nested.empty());
auto *tv = cs.simplifyType(nested[0]->getFirstType())
->getRValueType()
->getAs<TypeVariableType>();
assert(tv);

SmallVector<Constraint *, 8> constraints;
cs.getConstraintGraph().gatherConstraints(
tv, constraints, ConstraintGraph::GatheringKind::EquivalenceClass);

for (auto *constraint : constraints) {
if (constraint->getKind() != ConstraintKind::Conversion)
continue;

auto toType =
cs.simplifyType(constraint->getSecondType())->getRValueType();
auto *toTV = toType->getAs<TypeVariableType>();
if (tv != toTV)
continue;

return disjunction;
}
}

return nullptr;
}

Constraint *ConstraintSystem::selectDisjunction(
SmallVectorImpl<Constraint *> &disjunctions) {
if (disjunctions.empty())
return nullptr;

auto *disjunction =
selectDisjunctionBindingConversionResultType(*this, disjunctions);
if (disjunction)
return disjunction;

// Pick the smallest disjunction.
// FIXME: This heuristic isn't great, but it helped somewhat for
// overload sets.
auto disjunction = disjunctions[0];
disjunction = disjunctions[0];
auto bestSize = disjunction->countActiveNestedConstraints();
if (bestSize > 2) {
for (auto contender : llvm::makeArrayRef(disjunctions).slice(1)) {
Expand Down
23 changes: 23 additions & 0 deletions validation-test/Sema/type_checker_perf/fast/rdar40344044.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %scale-test --begin 3 --end 20 --step 1 --select incrementScopeCounter %s
// REQUIRES: OS=macosx
// REQUIRES: asserts

protocol P {}
class C : P {}
class D : P {}

class Test {
let c: C! = C()
let d: D! = D()
var a: [P]! = []

func test() {
a = [
c,
%for i in range(0, N):
c,
%end
d
]
}
}