Skip to content

Commit 0de8428

Browse files
committed
Sema: Tail-allocate Constraint::Overload::Choice
OverloadChoice is rather large, 40 bytes.
1 parent a48591d commit 0de8428

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

include/swift/Sema/Constraint.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,10 @@ enum RememberChoice_t : bool {
344344

345345
/// A constraint between two type variables.
346346
class Constraint final : public llvm::ilist_node<Constraint>,
347-
private llvm::TrailingObjects<Constraint, TypeVariableType *, ConstraintFix *> {
347+
private llvm::TrailingObjects<Constraint,
348+
TypeVariableType *,
349+
ConstraintFix *,
350+
OverloadChoice> {
348351
friend TrailingObjects;
349352

350353
/// The kind of constraint.
@@ -438,9 +441,6 @@ class Constraint final : public llvm::ilist_node<Constraint>,
438441
/// The first type
439442
Type First;
440443

441-
/// The overload choice
442-
OverloadChoice Choice;
443-
444444
/// The DC in which the use appears.
445445
DeclContext *UseDC;
446446
} Overload;
@@ -522,6 +522,10 @@ class Constraint final : public llvm::ilist_node<Constraint>,
522522
return HasFix ? 1 : 0;
523523
}
524524

525+
size_t numTrailingObjects(OverloadToken<OverloadChoice>) const {
526+
return Kind == ConstraintKind::BindOverload ? 1 : 0;
527+
}
528+
525529
public:
526530
/// Create a new constraint.
527531
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
@@ -861,7 +865,7 @@ class Constraint final : public llvm::ilist_node<Constraint>,
861865
/// Retrieve the overload choice for an overload-binding constraint.
862866
OverloadChoice getOverloadChoice() const {
863867
assert(Kind == ConstraintKind::BindOverload);
864-
return Overload.Choice;
868+
return *getTrailingObjects<OverloadChoice>();
865869
}
866870

867871
/// Retrieve the DC in which the overload was used.

lib/Sema/Constraint.cpp

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,12 @@ Constraint::Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
241241
: Kind(ConstraintKind::BindOverload), HasFix(fix != nullptr), HasRestriction(false),
242242
IsActive(false), IsDisabled(bool(fix)), IsDisabledForPerformance(false),
243243
RememberChoice(false), IsFavored(false), IsIsolated(false),
244-
NumTypeVariables(typeVars.size()), Overload{type, choice, useDC},
244+
NumTypeVariables(typeVars.size()), Overload{type, useDC},
245245
Locator(locator) {
246246
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
247247
if (fix)
248248
*getTrailingObjects<ConstraintFix *>() = fix;
249+
*getTrailingObjects<OverloadChoice>() = choice;
249250
}
250251

251252
Constraint::Constraint(ConstraintKind kind,
@@ -838,8 +839,9 @@ Constraint *Constraint::create(ConstraintSystem &cs, ConstraintKind kind,
838839
second->is<ProtocolType>());
839840

840841
// Create the constraint.
841-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
842-
typeVars.size(), /*hasFix=*/0);
842+
auto size =
843+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
844+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
843845
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
844846
return ::new (mem) Constraint(kind, first, second, locator, typeVars);
845847
}
@@ -858,8 +860,9 @@ Constraint *Constraint::create(ConstraintSystem &cs, ConstraintKind kind,
858860
if (third->hasTypeVariable())
859861
third->getTypeVariables(typeVars);
860862

861-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
862-
typeVars.size(), /*hasFix=*/0);
863+
auto size =
864+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
865+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
863866
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
864867
return ::new (mem) Constraint(kind,
865868
first, second, third,
@@ -899,8 +902,9 @@ Constraint *Constraint::createMember(ConstraintSystem &cs, ConstraintKind kind,
899902
second->getTypeVariables(typeVars);
900903

901904
// Create the constraint.
902-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
903-
typeVars.size(), /*hasFix=*/0);
905+
auto size =
906+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
907+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
904908
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
905909
return new (mem) Constraint(kind, first, second, member, useDC,
906910
functionRefKind, locator, typeVars);
@@ -920,8 +924,9 @@ Constraint *Constraint::createValueWitness(
920924
second->getTypeVariables(typeVars);
921925

922926
// Create the constraint.
923-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
924-
typeVars.size(), /*hasFix=*/0);
927+
auto size =
928+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
929+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
925930
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
926931
return new (mem) Constraint(kind, first, second, requirement, useDC,
927932
functionRefKind, locator, typeVars);
@@ -947,8 +952,9 @@ Constraint *Constraint::createRestricted(ConstraintSystem &cs,
947952
second->getTypeVariables(typeVars);
948953

949954
// Create the constraint.
950-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
951-
typeVars.size(), /*hasFix=*/0);
955+
auto size =
956+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
957+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
952958
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
953959
return new (mem) Constraint(kind, restriction, first, second, locator,
954960
typeVars);
@@ -965,8 +971,9 @@ Constraint *Constraint::createFixed(ConstraintSystem &cs, ConstraintKind kind,
965971
second->getTypeVariables(typeVars);
966972

967973
// Create the constraint.
968-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
969-
typeVars.size(), fix ? 1 : 0);
974+
auto size =
975+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
976+
typeVars.size(), fix ? 1 : 0, /*hasOverloadChoice=*/0);
970977
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
971978
return new (mem) Constraint(kind, fix, first, second, locator, typeVars);
972979
}
@@ -985,8 +992,9 @@ Constraint *Constraint::createFixedChoice(ConstraintSystem &cs, Type type,
985992
}
986993

987994
// Create the constraint.
988-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
989-
typeVars.size(), fix ? 1 : 0);
995+
auto size =
996+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
997+
typeVars.size(), fix ? 1 : 0, /*hasOverloadChoice=*/1);
990998
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
991999
return new (mem) Constraint(type, choice, useDC, fix, locator, typeVars);
9921000
}
@@ -1056,8 +1064,9 @@ Constraint *Constraint::createDisjunction(ConstraintSystem &cs,
10561064
#endif
10571065

10581066
// Create the disjunction constraint.
1059-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
1060-
typeVars.size(), /*hasFix=*/0);
1067+
auto size =
1068+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
1069+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
10611070
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
10621071
auto disjunction = new (mem)
10631072
Constraint(ConstraintKind::Disjunction, cs.allocateCopy(constraints),
@@ -1076,8 +1085,9 @@ Constraint *Constraint::createConjunction(
10761085
// because each have to be solved in isolation.
10771086

10781087
assert(!constraints.empty() && "Empty conjunction constraint");
1079-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
1080-
typeVars.size(), /*hasFix=*/0);
1088+
auto size =
1089+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
1090+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
10811091
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
10821092
auto conjunction = new (mem)
10831093
Constraint(ConstraintKind::Conjunction, cs.allocateCopy(constraints),
@@ -1097,8 +1107,9 @@ Constraint *Constraint::createApplicableFunction(
10971107
calleeType->getTypeVariables(typeVars);
10981108

10991109
// Create the constraint.
1100-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
1101-
typeVars.size(), /*hasFix=*/0);
1110+
auto size =
1111+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
1112+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
11021113
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
11031114
auto constraint = new (mem) Constraint(
11041115
ConstraintKind::ApplicableFunction, argumentFnType, calleeType, locator,
@@ -1140,8 +1151,9 @@ Constraint *Constraint::createSyntacticElement(ConstraintSystem &cs,
11401151
if (auto contextTy = context.getType())
11411152
contextTy->getTypeVariables(typeVars);
11421153

1143-
auto size = totalSizeToAlloc<TypeVariableType *, ConstraintFix *>(
1144-
typeVars.size(), /*hasFix=*/0);
1154+
auto size =
1155+
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
1156+
typeVars.size(), /*hasFix=*/0, /*hasOverloadChoice=*/0);
11451157
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
11461158
return new (mem) Constraint(node, context, isDiscarded, locator, typeVars);
11471159
}

0 commit comments

Comments
 (0)