Skip to content

[AST] Increase size of type variable id field to 20 bits #25666

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 4 commits into from
Jun 24, 2019
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
17 changes: 11 additions & 6 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,16 @@ class alignas(1 << TypeAlignInBits) TypeBase {
NumProtocols : 16
);

SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 64-NumTypeBaseBits,
SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 20+4+20,
/// The unique number assigned to this type variable.
ID : 32 - NumTypeBaseBits,
ID : 20,

/// Type variable options.
Options : 4,

/// Index into the list of type variables, as used by the
/// constraint graph.
GraphIndex : 28
GraphIndex : 20
);

SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+3+1+2,
Expand Down Expand Up @@ -5224,8 +5224,11 @@ TypeVariableType : public TypeBase {
TypeVariableType(const ASTContext &C, unsigned ID)
: TypeBase(TypeKind::TypeVariable, &C,
RecursiveTypeProperties::HasTypeVariable) {
// Note: the ID may overflow, but its only used for printing.
// Note: the ID may overflow (current limit is 2^20 - 1).
Bits.TypeVariableType.ID = ID;
if (Bits.TypeVariableType.ID != ID) {
llvm::report_fatal_error("Type variable id overflow");
}
}

class Implementation;
Expand Down Expand Up @@ -5261,8 +5264,10 @@ TypeVariableType : public TypeBase {
return reinterpret_cast<Implementation *>(this + 1);
}

/// Type variable IDs are not globally unique and are only meant as a visual
/// aid when dumping AST.
/// Type variable IDs are not globally unique and are
/// used in equivalence class merging (so representative
/// is always a type variable with smaller id), as well
/// as a visual aid when dumping AST.
unsigned getID() const { return Bits.TypeVariableType.ID; }

// Implement isa/cast/dyncast/etc.
Expand Down
42 changes: 11 additions & 31 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ namespace {
/// expression, attempt to derive a favored type for it.
bool computeFavoredTypeForExpr(Expr *expr, ConstraintSystem &CS) {
LinkedTypeInfo lti;

expr->walk(LinkedExprAnalyzer(lti, CS));

// Link anonymous closure params of the same index.
Expand All @@ -381,41 +381,21 @@ namespace {
}
}

// Link integer literal tyvars.
if (lti.intLiteralTyvars.size() > 1) {
auto rep1 = CS.getRepresentative(lti.intLiteralTyvars[0]);

for (size_t i = 1; i < lti.intLiteralTyvars.size(); i++) {
auto rep2 = CS.getRepresentative(lti.intLiteralTyvars[i]);
auto mergeTypeVariables = [&](ArrayRef<TypeVariableType *> typeVars) {
if (typeVars.size() < 2)
return;

auto rep1 = CS.getRepresentative(typeVars.front());
for (unsigned i = 1, n = typeVars.size(); i != n; ++i) {
auto rep2 = CS.getRepresentative(typeVars[i]);
if (rep1 != rep2)
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
}
}

// Link float literal tyvars.
if (lti.floatLiteralTyvars.size() > 1) {
auto rep1 = CS.getRepresentative(lti.floatLiteralTyvars[0]);

for (size_t i = 1; i < lti.floatLiteralTyvars.size(); i++) {
auto rep2 = CS.getRepresentative(lti.floatLiteralTyvars[i]);

if (rep1 != rep2)
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
}
}

// Link string literal tyvars.
if (lti.stringLiteralTyvars.size() > 1) {
auto rep1 = CS.getRepresentative(lti.stringLiteralTyvars[0]);

for (size_t i = 1; i < lti.stringLiteralTyvars.size(); i++) {
auto rep2 = CS.getRepresentative(lti.stringLiteralTyvars[i]);
};

if (rep1 != rep2)
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
}
}
mergeTypeVariables(lti.intLiteralTyvars);
mergeTypeVariables(lti.floatLiteralTyvars);
mergeTypeVariables(lti.stringLiteralTyvars);

if (lti.collectedTypes.size() == 1) {
// TODO: Compute the BCT.
Expand Down
Loading