Skip to content

[CSBindings] Cache already computed type variable bindings #19852

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 11 additions & 7 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ Optional<ConstraintSystem::PotentialBindings>
ConstraintSystem::determineBestBindings() {
// Look for potential type variable bindings.
Optional<PotentialBindings> bestBindings;
llvm::SmallDenseMap<TypeVariableType *, PotentialBindings> cache;

// First, let's collect all of the possible bindings.
for (auto *typeVar : getTypeVariables()) {
if (typeVar->getImpl().hasRepresentativeOrFixed())
continue;

if (Bindings.count(typeVar) > 0)
continue;

if (auto bindings = getPotentialBindings(typeVar))
cache.insert({typeVar, std::move(bindings)});
Bindings.insert({typeVar, std::move(bindings)});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to assert that the insertion succeeded?

}

// Now let's see if we could infer something for related type
// variables based on other bindings.
for (auto *typeVar : getTypeVariables()) {
auto cachedBindings = cache.find(typeVar);
if (cachedBindings == cache.end())
if (typeVar->getImpl().hasRepresentativeOrFixed())
continue;

auto cachedBindings = Bindings.find(typeVar);
if (cachedBindings == Bindings.end())
continue;

auto &bindings = cachedBindings->getSecond();
Expand All @@ -62,8 +66,8 @@ ConstraintSystem::determineBestBindings() {
if (!tv)
continue;

auto relatedBindings = cache.find(tv);
if (relatedBindings == cache.end())
auto relatedBindings = Bindings.find(tv);
if (relatedBindings == Bindings.end())
continue;

for (auto &binding : relatedBindings->getSecond().Bindings) {
Expand Down
1 change: 0 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,6 @@ ConstraintSystem::matchTypesBindTypeVar(
}

assignFixedType(typeVar, type);

return getTypeMatchSuccess();
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ void ConstraintSystem::addTypeVariableConstraintsToWorkList(
constraint);
constraint->setActive(true);
}

// Invalidate pre-computed bindings associated
// with this type variable (if any).
invalidateBindings(typeVar);
}

/// Retrieve a dynamic result signature for the given declaration.
Expand Down
29 changes: 29 additions & 0 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ class ConstraintSystem {
/// \param constraint The newly generated constraint.
void addGeneratedConstraint(Constraint *constraint) {
generatedConstraints.push_back(constraint);
CS.invalidateBindings(constraint->getTypeVariables());
}

/// \brief Erase given constraint from the list of generated constraints
Expand Down Expand Up @@ -2940,6 +2941,12 @@ class ConstraintSystem {
}
};

/// Maps type variables to their bindings. Bindings are invalidated
/// and have to be re-computed if new constraints are introduced or
/// in relation to a particular set of type variables or type variables
/// are assigned fixed types or merged together.
llvm::DenseMap<TypeVariableType *, PotentialBindings> Bindings;

Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type,
bool *isNilLiteral = nullptr);
Optional<PotentialBindings> determineBestBindings();
Expand Down Expand Up @@ -3228,6 +3235,28 @@ class ConstraintSystem {
SmallVectorImpl<unsigned> &Ordering,
SmallVectorImpl<unsigned> &PartitionBeginning);

void invalidateBindings(ArrayRef<TypeVariableType *> typeVars) {
llvm::SmallPtrSet<TypeVariableType *, 8> visitedVars;
for (auto *typeVar : typeVars) {
if (typeVar != typeVar->getImpl().getRepresentative(nullptr) ||
visitedVars.count(typeVar) > 0)
continue;

// If there was no binding, there is no need to disturb
// other type variables.
Bindings.erase(typeVar);
visitedVars.insert(typeVar);

for (auto *eqClassVar : CG[typeVar].getEquivalenceClass()) {
for (auto *adjacentVar : CG[eqClassVar].getAdjacencies()) {
auto *var = getRepresentative(adjacentVar);
if (visitedVars.insert(var).second)
Bindings.erase(var);
}
}
}
}

LLVM_ATTRIBUTE_DEPRECATED(
void dump() LLVM_ATTRIBUTE_USED,
"only for use within the debugger");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %scale-test --begin 1 --end 20 --step 1 --select NumLeafScopes %s
// REQUIRES: OS=macosx
// REQUIRES: asserts

% array_elements = 20

let _ = [
%for i in range(0, N):
${i} : [
%for j in range(array_elements):
"e${j}",
%end
],
%end
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %scale-test --invert-result --begin 1 --end 5 --step 1 --select NumLeafScopes %s --expected-exit-code 1
// RUN: %scale-test --invert-result --begin 1 --end 4 --step 1 --select NumLeafScopes %s --expected-exit-code 1
// REQUIRES: OS=macosx
// REQUIRES: asserts

Expand Down