Skip to content

Commit 9587a3a

Browse files
authored
Merge pull request swiftlang#23170 from slavapestov/enable-sil-resilience-expansion
Start using the best resilience expansion in SIL
2 parents 1f6ffa6 + d069cc4 commit 9587a3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+551
-253
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,13 @@ class SILBuilder {
198198
SILModule &getModule() const { return C.Module; }
199199
ASTContext &getASTContext() const { return getModule().getASTContext(); }
200200
const Lowering::TypeLowering &getTypeLowering(SILType T) const {
201-
// FIXME: Expansion
202-
return getModule().Types.getTypeLowering(T,
203-
ResilienceExpansion::Minimal);
201+
auto expansion = ResilienceExpansion::Maximal;
202+
// If there's no current SILFunction, we're inserting into a global
203+
// variable initializer.
204+
if (F)
205+
expansion = F->getResilienceExpansion();
206+
207+
return getModule().Types.getTypeLowering(T, expansion);
204208
}
205209

206210
void setOpenedArchetypesTracker(SILOpenedArchetypesTracker *Tracker) {
@@ -2158,15 +2162,7 @@ class SILBuilder {
21582162
if (!SILModuleConventions(M).useLoweredAddresses())
21592163
return true;
21602164

2161-
// FIXME: Just call getTypeLowering() here, and move this code there
2162-
2163-
auto expansion = ResilienceExpansion::Maximal;
2164-
// If there's no current SILFunction, we're inserting into a global
2165-
// variable initializer.
2166-
if (F)
2167-
expansion = F->getResilienceExpansion();
2168-
2169-
return M.Types.getTypeLowering(Ty, expansion).isLoadable();
2165+
return getTypeLowering(Ty).isLoadable();
21702166
}
21712167

21722168
void appendOperandTypeName(SILType OpdTy, llvm::SmallString<16> &Name) {

include/swift/SIL/SILType.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,46 +252,25 @@ class SILType {
252252

253253
/// True if the type, or the referenced type of an address type, is loadable.
254254
/// This is the opposite of isAddressOnly.
255-
bool isLoadable(SILModule &M) const {
256-
return !isAddressOnly(M);
257-
}
258-
259-
/// Like isLoadable(SILModule), but specific to a function.
260-
///
261-
/// This takes the resilience expansion of the function into account. If the
262-
/// type is not loadable in general (because it's resilient), it still might
263-
/// be loadable inside a resilient function in the module.
264-
/// In other words: isLoadable(SILModule) is the conservative default, whereas
265-
/// isLoadable(SILFunction) might give a more optimistic result.
266255
bool isLoadable(const SILFunction &F) const {
267256
return !isAddressOnly(F);
268257
}
269258

270259
/// True if either:
271260
/// 1) The type, or the referenced type of an address type, is loadable.
272261
/// 2) The SIL Module conventions uses lowered addresses
273-
bool isLoadableOrOpaque(SILModule &M) const;
274-
275-
/// Like isLoadableOrOpaque(SILModule), but takes the resilience expansion of
276-
/// \p F into account (see isLoadable(SILFunction)).
277262
bool isLoadableOrOpaque(const SILFunction &F) const;
278263

279264
/// True if the type, or the referenced type of an address type, is
280265
/// address-only. This is the opposite of isLoadable.
281-
bool isAddressOnly(SILModule &M) const;
282-
283-
/// Like isAddressOnly(SILModule), but takes the resilience expansion of
284-
/// \p F into account (see isLoadable(SILFunction)).
285266
bool isAddressOnly(const SILFunction &F) const;
286267

287268
/// True if the type, or the referenced type of an address type, is trivial,
288269
/// meaning it is loadable and can be trivially copied, moved or detroyed.
289270
bool isTrivial(const SILFunction &F) const;
290271

291272
/// True if the type, or the referenced type of an address type, is known to
292-
/// be a scalar reference-counted type. If this is false, then some part of
293-
/// the type may be opaque. It may become reference counted later after
294-
/// specialization.
273+
/// be a scalar reference-counted type.
295274
bool isReferenceCounted(SILModule &M) const;
296275

297276
/// Returns true if the referenced type is a function type that never

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class AliasAnalysis : public SILAnalysis {
137137
SILType TBAAType2 = SILType());
138138

139139
/// Returns True if memory of type \p T1 and \p T2 may alias.
140-
bool typesMayAlias(SILType T1, SILType T2);
140+
bool typesMayAlias(SILType T1, SILType T2, const SILFunction &F);
141141

142142
virtual void handleDeleteNotification(SILNode *node) override {
143143
assert(node->isRepresentativeSILNodeInObject());

include/swift/SILOptimizer/Analysis/ArraySemantic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ArraySemanticsCall {
151151

152152
/// Replace a call to append(contentsOf: ) with a series of
153153
/// append(element: ) calls.
154-
bool replaceByAppendingValues(SILModule &M, SILFunction *AppendFn,
154+
bool replaceByAppendingValues(SILFunction *AppendFn,
155155
SILFunction *ReserveFn,
156156
const llvm::SmallVectorImpl<SILValue> &Vals,
157157
SubstitutionMap Subs);

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ class ReabstractionInfo {
156156
}
157157

158158
ResilienceExpansion getResilienceExpansion() const {
159-
// FIXME: Expansion
160-
return ResilienceExpansion::Minimal;
159+
return (Serialized
160+
? ResilienceExpansion::Minimal
161+
: ResilienceExpansion::Maximal);
161162
}
162163

163164
/// Returns true if the \p ParamIdx'th (non-result) formal parameter is

lib/SIL/SILFunction.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,45 +228,34 @@ bool SILFunction::isNoReturnFunction() const {
228228

229229
const TypeLowering &
230230
SILFunction::getTypeLowering(AbstractionPattern orig, Type subst) {
231-
// FIXME: Expansion
232231
return getModule().Types.getTypeLowering(orig, subst,
233-
ResilienceExpansion::Minimal);
232+
getResilienceExpansion());
234233
}
235234

236235
const TypeLowering &SILFunction::getTypeLowering(Type t) const {
237-
// FIXME: Expansion
238-
return getModule().Types.getTypeLowering(t, ResilienceExpansion::Minimal);
236+
return getModule().Types.getTypeLowering(t, getResilienceExpansion());
239237
}
240238

241239
SILType
242240
SILFunction::getLoweredType(AbstractionPattern orig, Type subst) const {
243-
// FIXME: Expansion
244241
return getModule().Types.getLoweredType(orig, subst,
245-
ResilienceExpansion::Minimal);
242+
getResilienceExpansion());
246243
}
247244

248245
SILType SILFunction::getLoweredType(Type t) const {
249-
// FIXME: Expansion
250-
return getModule().Types.getLoweredType(t,
251-
ResilienceExpansion::Minimal);
246+
return getModule().Types.getLoweredType(t, getResilienceExpansion());
252247
}
253248

254249
SILType SILFunction::getLoweredLoadableType(Type t) const {
255-
// FIXME: Expansion
256-
return getModule().Types.getLoweredLoadableType(t,
257-
ResilienceExpansion::Minimal);
250+
return getModule().Types.getLoweredLoadableType(t, getResilienceExpansion());
258251
}
259252

260253
const TypeLowering &SILFunction::getTypeLowering(SILType type) const {
261-
// FIXME: Expansion
262-
return getModule().Types.getTypeLowering(type,
263-
ResilienceExpansion::Minimal);
254+
return getModule().Types.getTypeLowering(type, getResilienceExpansion());
264255
}
265256

266257
bool SILFunction::isTypeABIAccessible(SILType type) const {
267-
// FIXME: Expansion
268-
return getModule().isTypeABIAccessible(type,
269-
ResilienceExpansion::Minimal);
258+
return getModule().isTypeABIAccessible(type, getResilienceExpansion());
270259
}
271260

272261
SILBasicBlock *SILFunction::createBasicBlock() {

lib/SIL/SILFunctionType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,9 @@ static CanSILFunctionType getSILFunctionType(
10051005
// from the function to which the argument is attached.
10061006
if (constant && !constant->isDefaultArgGenerator()) {
10071007
if (auto function = constant->getAnyFunctionRef()) {
1008-
// FIXME: Expansion
1009-
auto expansion = ResilienceExpansion::Minimal;
1008+
auto expansion = ResilienceExpansion::Maximal;
1009+
if (constant->isSerialized())
1010+
expansion = ResilienceExpansion::Minimal;
10101011
lowerCaptureContextParameters(M, *function, genericSig, expansion,
10111012
inputs);
10121013
}

lib/SIL/SILType.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ SILType SILType::getSILTokenType(const ASTContext &C) {
8181
}
8282

8383
bool SILType::isTrivial(const SILFunction &F) const {
84-
// FIXME: Should just call F.getTypeLowering()
85-
return F.getModule().Types.getTypeLowering(*this,
86-
ResilienceExpansion::Minimal).isTrivial();
84+
return F.getTypeLowering(*this).isTrivial();
8785
}
8886

8987
bool SILType::isReferenceCounted(SILModule &M) const {
@@ -177,28 +175,13 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, SILModule &M) const {
177175
return SILType(loweredTy, getCategory());
178176
}
179177

180-
bool SILType::isLoadableOrOpaque(SILModule &M) const {
181-
return isLoadable(M) || !SILModuleConventions(M).useLoweredAddresses();
182-
}
183-
184178
bool SILType::isLoadableOrOpaque(const SILFunction &F) const {
185179
SILModule &M = F.getModule();
186-
return isLoadable(F) ||
187-
!SILModuleConventions(M).useLoweredAddresses();
188-
}
189-
190-
/// True if the type, or the referenced type of an address type, is
191-
/// address-only. For example, it could be a resilient struct or something of
192-
/// unknown size.
193-
bool SILType::isAddressOnly(SILModule &M) const {
194-
return M.Types.getTypeLowering(*this, ResilienceExpansion::Minimal)
195-
.isAddressOnly();
180+
return isLoadable(F) || !SILModuleConventions(M).useLoweredAddresses();
196181
}
197182

198183
bool SILType::isAddressOnly(const SILFunction &F) const {
199-
// FIXME: Should just call F.getTypeLowering()
200-
return F.getModule().Types.getTypeLowering(*this,
201-
F.getResilienceExpansion()).isAddressOnly();
184+
return F.getTypeLowering(*this).isAddressOnly();
202185
}
203186

204187
SILType SILType::substGenericArgs(SILModule &M,

lib/SILGen/RValue.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ExplodeTupleValue
8585

8686
void visitType(CanType formalType, ManagedValue v) {
8787
// If we have a loadable type that has not been loaded, actually load it.
88-
if (!v.getType().isObject() && v.getType().isLoadable(SGF.getModule())) {
88+
if (!v.getType().isObject() && v.getType().isLoadable(SGF.F)) {
8989
if (v.isPlusOne(SGF)) {
9090
v = SGF.B.createLoadTake(loc, v);
9191
} else {
@@ -390,7 +390,7 @@ static void verifyHelper(ArrayRef<ManagedValue> values,
390390
auto result = Optional<ValueOwnershipKind>(ValueOwnershipKind::Any);
391391
Optional<bool> sameHaveCleanups;
392392
for (ManagedValue v : values) {
393-
assert((!SGF || !v.getType().isLoadable(SGF.get()->getModule()) ||
393+
assert((!SGF || !v.getType().isLoadable(SGF.get()->F) ||
394394
v.getType().isObject()) &&
395395
"All loadable values in an RValue must be an object");
396396

@@ -803,7 +803,7 @@ SILType RValue::getLoweredType(SILGenFunction &SGF) const & {
803803

804804
SILType RValue::getLoweredImplodedTupleType(SILGenFunction &SGF) const & {
805805
SILType loweredType = getLoweredType(SGF);
806-
if (loweredType.isAddressOnly(SGF.getModule()) &&
806+
if (loweredType.isAddressOnly(SGF.F) &&
807807
SGF.silConv.useLoweredAddresses())
808808
return loweredType.getAddressType();
809809
return loweredType.getObjectType();

lib/SILGen/SILGenApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,7 +4643,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
46434643
SGFContext C) {
46444644
// Easy case -- no payload
46454645
if (!payload) {
4646-
if (enumTy.isLoadable(SGM.M) || !silConv.useLoweredAddresses()) {
4646+
if (enumTy.isLoadable(F) || !silConv.useLoweredAddresses()) {
46474647
return emitManagedRValueWithCleanup(
46484648
B.createEnum(loc, SILValue(), element, enumTy.getObjectType()));
46494649
}
@@ -4688,7 +4688,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
46884688
}
46894689

46904690
// Loadable with payload
4691-
if (enumTy.isLoadable(SGM.M) || !silConv.useLoweredAddresses()) {
4691+
if (enumTy.isLoadable(F) || !silConv.useLoweredAddresses()) {
46924692
if (!payloadMV) {
46934693
// If the payload was indirect, we already evaluated it and
46944694
// have a single value. Otherwise, evaluate the payload.
@@ -5278,7 +5278,7 @@ ArgumentSource AccessorBaseArgPreparer::prepareAccessorAddressBaseArg() {
52785278
// If the base is currently an address, we may have to copy it.
52795279
if (shouldLoadBaseAddress()) {
52805280
if (selfParam.isConsumed() ||
5281-
base.getType().isAddressOnly(SGF.getModule())) {
5281+
base.getType().isAddressOnly(SGF.F)) {
52825282
// The load can only be a take if the base is a +1 rvalue.
52835283
auto shouldTake = IsTake_t(base.hasCleanup());
52845284

0 commit comments

Comments
 (0)