Skip to content

Commit 8bd32dd

Browse files
authored
Merge pull request #25716 from xedin/rdar-51616307-5.1
[5.1][AST] Increase size of type variable id field to 20 bits
2 parents b04cff1 + b86a295 commit 8bd32dd

File tree

3 files changed

+309
-34
lines changed

3 files changed

+309
-34
lines changed

include/swift/AST/Types.h

Lines changed: 11 additions & 3 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+3+21,
340340
/// The unique number assigned to this type variable.
341-
ID : 32 - NumTypeBaseBits,
341+
ID : 20,
342342

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

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

351351
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+3+1+2,
@@ -5227,7 +5227,11 @@ class TypeVariableType : public TypeBase {
52275227
TypeVariableType(const ASTContext &C, unsigned ID)
52285228
: TypeBase(TypeKind::TypeVariable, &C,
52295229
RecursiveTypeProperties::HasTypeVariable) {
5230+
// Note: the ID may overflow (current limit is 2^20 - 1).
52305231
Bits.TypeVariableType.ID = ID;
5232+
if (Bits.TypeVariableType.ID != ID) {
5233+
llvm::report_fatal_error("Type variable id overflow");
5234+
}
52315235
}
52325236

52335237
class Implementation;
@@ -5263,6 +5267,10 @@ class TypeVariableType : public TypeBase {
52635267
return reinterpret_cast<Implementation *>(this + 1);
52645268
}
52655269

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

52685276
// 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)