Skip to content

Commit 83f95ae

Browse files
committed
Sema: Allocate PreparedOverload in solver arena
1 parent 7916ed9 commit 83f95ae

File tree

3 files changed

+160
-99
lines changed

3 files changed

+160
-99
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SyntacticElementTarget;
6767

6868
// PreparedOverload.h
6969
struct DeclReferenceType;
70-
struct PreparedOverload;
70+
class PreparedOverload;
7171
struct PreparedOverloadBuilder;
7272

7373
} // end namespace constraints
@@ -4906,18 +4906,23 @@ class ConstraintSystem {
49064906
void recordResolvedOverload(ConstraintLocator *locator,
49074907
SelectedOverload choice);
49084908

4909+
/// Build and allocate a prepared overload in the solver arena.
4910+
const PreparedOverload *prepareOverload(ConstraintLocator *locator,
4911+
OverloadChoice choice,
4912+
DeclContext *useDC);
4913+
49094914
/// Populate the prepared overload with all type variables and constraints
49104915
/// that are to be introduced into the constraint system when this choice
49114916
/// is taken.
49124917
DeclReferenceType
4913-
prepareOverload(ConstraintLocator *locator,
4914-
OverloadChoice choice,
4915-
DeclContext *useDC,
4916-
PreparedOverloadBuilder *preparedOverload);
4918+
prepareOverloadImpl(ConstraintLocator *locator,
4919+
OverloadChoice choice,
4920+
DeclContext *useDC,
4921+
PreparedOverloadBuilder *preparedOverload);
49174922

49184923
void replayChanges(
4919-
ConstraintLocatorBuilder locator,
4920-
PreparedOverload preparedOverload);
4924+
ConstraintLocator *locator,
4925+
const PreparedOverload *preparedOverload);
49214926

49224927
/// Resolve the given overload set to the given choice.
49234928
void resolveOverload(ConstraintLocator *locator, Type boundType,

include/swift/Sema/PreparedOverload.h

Lines changed: 102 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#define SWIFT_SEMA_PREPAREDOVERLOAD_H
1414

1515
#include "swift/AST/PropertyWrappers.h"
16+
#include "llvm/ADT/ArrayRef.h"
1617
#include "llvm/ADT/PointerIntPair.h"
1718
#include "llvm/ADT/SmallVector.h"
19+
#include "llvm/Support/TrailingObjects.h"
1820

1921
namespace swift {
2022

@@ -59,77 +61,118 @@ struct DeclReferenceType {
5961
/// variable.
6062
using OpenedType = std::pair<GenericTypeParamType *, TypeVariableType *>;
6163

62-
/// A "pre-cooked" representation of all type variables and constraints
63-
/// that are generated as part of an overload choice.
64-
struct PreparedOverload {
65-
/// A change to be introduced into the constraint system when this
66-
/// overload choice is chosen.
67-
struct Change {
68-
enum ChangeKind : unsigned {
69-
/// A generic parameter was opened to a type variable.
70-
AddedTypeVariable,
64+
/// A change to be introduced into the constraint system when this
65+
/// overload choice is chosen.
66+
struct PreparedOverloadChange {
67+
enum ChangeKind : unsigned {
68+
/// A generic parameter was opened to a type variable.
69+
AddedTypeVariable,
7170

72-
/// A generic requirement was opened to a constraint.
73-
AddedConstraint,
71+
/// A generic requirement was opened to a constraint.
72+
AddedConstraint,
7473

75-
/// A mapping of generic parameter types to type variables
76-
/// was recorded.
77-
OpenedTypes,
74+
/// A mapping of generic parameter types to type variables
75+
/// was recorded.
76+
OpenedTypes,
7877

79-
/// An existential type was opened.
80-
OpenedExistentialType,
78+
/// An existential type was opened.
79+
OpenedExistentialType,
8180

82-
/// A pack expansion type was opened.
83-
OpenedPackExpansionType,
81+
/// A pack expansion type was opened.
82+
OpenedPackExpansionType,
8483

85-
/// A property wrapper was applied to a parameter.
86-
AppliedPropertyWrapper,
84+
/// A property wrapper was applied to a parameter.
85+
AppliedPropertyWrapper,
8786

88-
/// A fix was recorded because a property wrapper application failed.
89-
AddedFix
90-
};
87+
/// A fix was recorded because a property wrapper application failed.
88+
AddedFix
89+
};
9190

92-
/// The kind of change.
93-
ChangeKind Kind;
91+
/// The kind of change.
92+
ChangeKind Kind;
9493

95-
union {
96-
/// For ChangeKind::AddedTypeVariable.
97-
TypeVariableType *TypeVar;
94+
union {
95+
/// For ChangeKind::AddedTypeVariable.
96+
TypeVariableType *TypeVar;
9897

99-
/// For ChangeKind::AddedConstraint.
100-
Constraint *TheConstraint;
101-
102-
/// For ChangeKind::OpenedTypes.
103-
struct {
104-
const OpenedType *Data;
105-
size_t Count;
106-
} Replacements;
107-
108-
/// For ChangeKind::OpenedExistentialType.
109-
ExistentialArchetypeType *TheExistential;
110-
111-
/// For ChangeKind::OpenedPackExpansionType.
112-
struct {
113-
PackExpansionType *TheExpansion;
114-
TypeVariableType *TypeVar;
115-
} PackExpansion;
116-
117-
/// For ChangeKind::AppliedPropertyWrapper.
118-
struct {
119-
TypeBase *WrapperType;
120-
PropertyWrapperInitKind InitKind;
121-
} PropertyWrapper;
122-
123-
/// For ChangeKind::Fix.
124-
struct {
125-
ConstraintFix *TheFix;
126-
unsigned Impact;
127-
} Fix;
128-
};
98+
/// For ChangeKind::AddedConstraint.
99+
Constraint *TheConstraint;
100+
101+
/// For ChangeKind::OpenedTypes.
102+
struct {
103+
const OpenedType *Data;
104+
size_t Count;
105+
} Replacements;
106+
107+
/// For ChangeKind::OpenedExistentialType.
108+
ExistentialArchetypeType *TheExistential;
109+
110+
/// For ChangeKind::OpenedPackExpansionType.
111+
struct {
112+
PackExpansionType *TheExpansion;
113+
TypeVariableType *TypeVar;
114+
} PackExpansion;
115+
116+
/// For ChangeKind::AppliedPropertyWrapper.
117+
struct {
118+
TypeBase *WrapperType;
119+
PropertyWrapperInitKind InitKind;
120+
} PropertyWrapper;
121+
122+
/// For ChangeKind::Fix.
123+
struct {
124+
ConstraintFix *TheFix;
125+
unsigned Impact;
126+
} Fix;
129127
};
128+
};
129+
130+
/// A "pre-cooked" representation of all type variables and constraints
131+
/// that are generated as part of an overload choice.
132+
class PreparedOverload final :
133+
public llvm::TrailingObjects<PreparedOverload, PreparedOverloadChange> {
130134

131-
ArrayRef<Change> Changes;
135+
public:
136+
using Change = PreparedOverloadChange;
137+
138+
private:
139+
size_t Count;
132140
DeclReferenceType DeclType;
141+
142+
size_t numTrailingObjects(OverloadToken<Change>) const {
143+
return Count;
144+
}
145+
146+
public:
147+
PreparedOverload(const DeclReferenceType &declType, ArrayRef<Change> changes)
148+
: Count(changes.size()), DeclType(declType) {
149+
std::uninitialized_copy(changes.begin(), changes.end(),
150+
getTrailingObjects<Change>());
151+
}
152+
153+
Type getOpenedType() const {
154+
return DeclType.openedType;
155+
}
156+
157+
Type getAdjustedOpenedType() const {
158+
return DeclType.adjustedOpenedType;
159+
}
160+
161+
Type getReferenceType() const {
162+
return DeclType.referenceType;
163+
}
164+
165+
Type getAdjustedReferenceType() const {
166+
return DeclType.adjustedReferenceType;
167+
}
168+
169+
Type getThrownErrorTypeOnAccess() const {
170+
return DeclType.thrownErrorTypeOnAccess;
171+
}
172+
173+
ArrayRef<Change> getChanges() const {
174+
return ArrayRef<Change>(getTrailingObjects<Change>(), Count);
175+
}
133176
};
134177

135178
struct PreparedOverloadBuilder {

lib/Sema/TypeOfReference.cpp

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,9 +2498,9 @@ void ConstraintSystem::recordResolvedOverload(ConstraintLocator *locator,
24982498
}
24992499

25002500
void ConstraintSystem::replayChanges(
2501-
ConstraintLocatorBuilder locator,
2502-
PreparedOverload preparedOverload) {
2503-
for (auto change : preparedOverload.Changes) {
2501+
ConstraintLocator *locator,
2502+
const PreparedOverload *preparedOverload) {
2503+
for (auto change : preparedOverload->getChanges()) {
25042504
switch (change.Kind) {
25052505
case PreparedOverload::Change::AddedTypeVariable:
25062506
addTypeVariable(change.TypeVar);
@@ -2511,20 +2511,18 @@ void ConstraintSystem::replayChanges(
25112511
break;
25122512

25132513
case PreparedOverload::Change::OpenedTypes: {
2514-
auto *locatorPtr = getConstraintLocator(locator);
25152514
ArrayRef<OpenedType> replacements(
25162515
change.Replacements.Data,
25172516
change.Replacements.Count);
25182517

25192518
// FIXME: Get rid of this conditional.
2520-
if (getOpenedTypes(locatorPtr).empty())
2521-
recordOpenedType(locatorPtr, replacements);
2519+
if (getOpenedTypes(locator).empty())
2520+
recordOpenedType(locator, replacements);
25222521
break;
25232522
}
25242523

25252524
case PreparedOverload::Change::OpenedExistentialType: {
2526-
auto *locatorPtr = getConstraintLocator(locator);
2527-
recordOpenedExistentialType(locatorPtr,
2525+
recordOpenedExistentialType(locator,
25282526
change.TheExistential);
25292527
break;
25302528
}
@@ -2536,8 +2534,7 @@ void ConstraintSystem::replayChanges(
25362534
break;
25372535

25382536
case PreparedOverload::Change::AppliedPropertyWrapper: {
2539-
auto *locatorPtr = getConstraintLocator(locator);
2540-
Expr *anchor = getAsExpr(locatorPtr->getAnchor());
2537+
Expr *anchor = getAsExpr(locator->getAnchor());
25412538
applyPropertyWrapper(anchor,
25422539
{ Type(change.PropertyWrapper.WrapperType),
25432540
change.PropertyWrapper.InitKind });
@@ -2558,10 +2555,10 @@ void ConstraintSystem::replayChanges(
25582555
/// FIXME: As a transitional mechanism, if preparedOverload is nullptr, this
25592556
/// immediately performs all operations.
25602557
DeclReferenceType
2561-
ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2562-
OverloadChoice choice,
2563-
DeclContext *useDC,
2564-
PreparedOverloadBuilder *preparedOverload) {
2558+
ConstraintSystem::prepareOverloadImpl(ConstraintLocator *locator,
2559+
OverloadChoice choice,
2560+
DeclContext *useDC,
2561+
PreparedOverloadBuilder *preparedOverload) {
25652562
// If we refer to a top-level decl with special type-checking semantics,
25662563
// handle it now.
25672564
auto semantics =
@@ -2584,6 +2581,25 @@ ConstraintSystem::prepareOverload(ConstraintLocator *locator,
25842581
}
25852582
}
25862583

2584+
const PreparedOverload *
2585+
ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2586+
OverloadChoice choice,
2587+
DeclContext *useDC) {
2588+
ASSERT(!PreparingOverload);
2589+
PreparingOverload = true;
2590+
2591+
PreparedOverloadBuilder builder;
2592+
auto declRefType = prepareOverloadImpl(locator, choice, useDC, &builder);
2593+
2594+
PreparingOverload = false;
2595+
2596+
size_t count = builder.Changes.size();
2597+
auto size = PreparedOverload::totalSizeToAlloc<PreparedOverload::Change>(count);
2598+
auto mem = Allocator.Allocate(size, alignof(PreparedOverload));
2599+
2600+
return new (mem) PreparedOverload(declRefType, builder.Changes);
2601+
}
2602+
25872603
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
25882604
Type boundType, OverloadChoice choice,
25892605
DeclContext *useDC) {
@@ -2602,30 +2618,27 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
26022618
case OverloadChoiceKind::DynamicMemberLookup:
26032619
case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
26042620
// FIXME: Transitional hack
2605-
bool enablePreparedOverloads = false;
2621+
bool enablePreparedOverloads = true;
26062622

2607-
PreparedOverloadBuilder preparedOverload;
26082623
if (enablePreparedOverloads) {
2609-
ASSERT(!PreparingOverload);
2610-
PreparingOverload = true;
2611-
}
2612-
2613-
auto declRefType = prepareOverload(locator, choice, useDC,
2614-
(enablePreparedOverloads
2615-
? &preparedOverload
2616-
: nullptr));
2624+
auto *preparedOverload = prepareOverload(locator, choice, useDC);
2625+
replayChanges(locator, preparedOverload);
2626+
2627+
openedType = preparedOverload->getOpenedType();
2628+
adjustedOpenedType = preparedOverload->getAdjustedOpenedType();
2629+
refType = preparedOverload->getReferenceType();
2630+
adjustedRefType = preparedOverload->getAdjustedReferenceType();
2631+
thrownErrorTypeOnAccess = preparedOverload->getThrownErrorTypeOnAccess();
2632+
} else {
2633+
auto declRefType = prepareOverloadImpl(locator, choice, useDC, nullptr);
26172634

2618-
if (enablePreparedOverloads) {
2619-
PreparingOverload = false;
2620-
PreparedOverload result{preparedOverload.Changes, declRefType};
2621-
replayChanges(locator, result);
2635+
openedType = declRefType.openedType;
2636+
adjustedOpenedType = declRefType.adjustedOpenedType;
2637+
refType = declRefType.referenceType;
2638+
adjustedRefType = declRefType.adjustedReferenceType;
2639+
thrownErrorTypeOnAccess = declRefType.thrownErrorTypeOnAccess;
26222640
}
26232641

2624-
openedType = declRefType.openedType;
2625-
adjustedOpenedType = declRefType.adjustedOpenedType;
2626-
refType = declRefType.referenceType;
2627-
adjustedRefType = declRefType.adjustedReferenceType;
2628-
thrownErrorTypeOnAccess = declRefType.thrownErrorTypeOnAccess;
26292642
break;
26302643
}
26312644

0 commit comments

Comments
 (0)