Skip to content

Commit 6b8406c

Browse files
authored
Merge pull request swiftlang#25666 from xedin/rdar-51616307
[AST] Increase size of type variable id field to 20 bits
2 parents b2c0a52 + 00f426d commit 6b8406c

File tree

3 files changed

+309
-37
lines changed

3 files changed

+309
-37
lines changed

include/swift/AST/Types.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,16 @@ class alignas(1 << TypeAlignInBits) TypeBase {
336336
NumProtocols : 16
337337
);
338338

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

343343
/// Type variable options.
344344
Options : 4,
345345

346346
/// Index into the list of type variables, as used by the
347347
/// constraint graph.
348-
GraphIndex : 28
348+
GraphIndex : 20
349349
);
350350

351351
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+3+1+2,
@@ -5231,8 +5231,11 @@ TypeVariableType : public TypeBase {
52315231
TypeVariableType(const ASTContext &C, unsigned ID)
52325232
: TypeBase(TypeKind::TypeVariable, &C,
52335233
RecursiveTypeProperties::HasTypeVariable) {
5234-
// Note: the ID may overflow, but its only used for printing.
5234+
// Note: the ID may overflow (current limit is 2^20 - 1).
52355235
Bits.TypeVariableType.ID = ID;
5236+
if (Bits.TypeVariableType.ID != ID) {
5237+
llvm::report_fatal_error("Type variable id overflow");
5238+
}
52365239
}
52375240

52385241
class Implementation;
@@ -5268,8 +5271,10 @@ TypeVariableType : public TypeBase {
52685271
return reinterpret_cast<Implementation *>(this + 1);
52695272
}
52705273

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

52755280
// Implement isa/cast/dyncast/etc.

lib/Sema/CSGen.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ namespace {
360360
/// expression, attempt to derive a favored type for it.
361361
bool computeFavoredTypeForExpr(Expr *expr, ConstraintSystem &CS) {
362362
LinkedTypeInfo lti;
363-
363+
364364
expr->walk(LinkedExprAnalyzer(lti, CS));
365365

366366
// Link anonymous closure params of the same index.
@@ -381,41 +381,21 @@ namespace {
381381
}
382382
}
383383

384-
// Link integer literal tyvars.
385-
if (lti.intLiteralTyvars.size() > 1) {
386-
auto rep1 = CS.getRepresentative(lti.intLiteralTyvars[0]);
387-
388-
for (size_t i = 1; i < lti.intLiteralTyvars.size(); i++) {
389-
auto rep2 = CS.getRepresentative(lti.intLiteralTyvars[i]);
384+
auto mergeTypeVariables = [&](ArrayRef<TypeVariableType *> typeVars) {
385+
if (typeVars.size() < 2)
386+
return;
390387

388+
auto rep1 = CS.getRepresentative(typeVars.front());
389+
for (unsigned i = 1, n = typeVars.size(); i != n; ++i) {
390+
auto rep2 = CS.getRepresentative(typeVars[i]);
391391
if (rep1 != rep2)
392392
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
393393
}
394-
}
395-
396-
// Link float literal tyvars.
397-
if (lti.floatLiteralTyvars.size() > 1) {
398-
auto rep1 = CS.getRepresentative(lti.floatLiteralTyvars[0]);
399-
400-
for (size_t i = 1; i < lti.floatLiteralTyvars.size(); i++) {
401-
auto rep2 = CS.getRepresentative(lti.floatLiteralTyvars[i]);
402-
403-
if (rep1 != rep2)
404-
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
405-
}
406-
}
407-
408-
// Link string literal tyvars.
409-
if (lti.stringLiteralTyvars.size() > 1) {
410-
auto rep1 = CS.getRepresentative(lti.stringLiteralTyvars[0]);
411-
412-
for (size_t i = 1; i < lti.stringLiteralTyvars.size(); i++) {
413-
auto rep2 = CS.getRepresentative(lti.stringLiteralTyvars[i]);
394+
};
414395

415-
if (rep1 != rep2)
416-
CS.mergeEquivalenceClasses(rep1, rep2, /*updateWorkList*/ false);
417-
}
418-
}
396+
mergeTypeVariables(lti.intLiteralTyvars);
397+
mergeTypeVariables(lti.floatLiteralTyvars);
398+
mergeTypeVariables(lti.stringLiteralTyvars);
419399

420400
if (lti.collectedTypes.size() == 1) {
421401
// TODO: Compute the BCT.

0 commit comments

Comments
 (0)