Skip to content

Start using the best resilience expansion in SIL #23170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@ class SILBuilder {
SILModule &getModule() const { return C.Module; }
ASTContext &getASTContext() const { return getModule().getASTContext(); }
const Lowering::TypeLowering &getTypeLowering(SILType T) const {
// FIXME: Expansion
return getModule().Types.getTypeLowering(T,
ResilienceExpansion::Minimal);
auto expansion = ResilienceExpansion::Maximal;
// If there's no current SILFunction, we're inserting into a global
// variable initializer.
if (F)
expansion = F->getResilienceExpansion();

return getModule().Types.getTypeLowering(T, expansion);
}

void setOpenedArchetypesTracker(SILOpenedArchetypesTracker *Tracker) {
Expand Down Expand Up @@ -2158,15 +2162,7 @@ class SILBuilder {
if (!SILModuleConventions(M).useLoweredAddresses())
return true;

// FIXME: Just call getTypeLowering() here, and move this code there

auto expansion = ResilienceExpansion::Maximal;
// If there's no current SILFunction, we're inserting into a global
// variable initializer.
if (F)
expansion = F->getResilienceExpansion();

return M.Types.getTypeLowering(Ty, expansion).isLoadable();
return getTypeLowering(Ty).isLoadable();
}

void appendOperandTypeName(SILType OpdTy, llvm::SmallString<16> &Name) {
Expand Down
23 changes: 1 addition & 22 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,46 +252,25 @@ class SILType {

/// True if the type, or the referenced type of an address type, is loadable.
/// This is the opposite of isAddressOnly.
bool isLoadable(SILModule &M) const {
return !isAddressOnly(M);
}

/// Like isLoadable(SILModule), but specific to a function.
///
/// This takes the resilience expansion of the function into account. If the
/// type is not loadable in general (because it's resilient), it still might
/// be loadable inside a resilient function in the module.
/// In other words: isLoadable(SILModule) is the conservative default, whereas
/// isLoadable(SILFunction) might give a more optimistic result.
bool isLoadable(const SILFunction &F) const {
return !isAddressOnly(F);
}

/// True if either:
/// 1) The type, or the referenced type of an address type, is loadable.
/// 2) The SIL Module conventions uses lowered addresses
bool isLoadableOrOpaque(SILModule &M) const;

/// Like isLoadableOrOpaque(SILModule), but takes the resilience expansion of
/// \p F into account (see isLoadable(SILFunction)).
bool isLoadableOrOpaque(const SILFunction &F) const;

/// True if the type, or the referenced type of an address type, is
/// address-only. This is the opposite of isLoadable.
bool isAddressOnly(SILModule &M) const;

/// Like isAddressOnly(SILModule), but takes the resilience expansion of
/// \p F into account (see isLoadable(SILFunction)).
bool isAddressOnly(const SILFunction &F) const;

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

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

/// Returns true if the referenced type is a function type that never
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Analysis/AliasAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class AliasAnalysis : public SILAnalysis {
SILType TBAAType2 = SILType());

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

virtual void handleDeleteNotification(SILNode *node) override {
assert(node->isRepresentativeSILNodeInObject());
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Analysis/ArraySemantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ArraySemanticsCall {

/// Replace a call to append(contentsOf: ) with a series of
/// append(element: ) calls.
bool replaceByAppendingValues(SILModule &M, SILFunction *AppendFn,
bool replaceByAppendingValues(SILFunction *AppendFn,
SILFunction *ReserveFn,
const llvm::SmallVectorImpl<SILValue> &Vals,
SubstitutionMap Subs);
Expand Down
5 changes: 3 additions & 2 deletions include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ class ReabstractionInfo {
}

ResilienceExpansion getResilienceExpansion() const {
// FIXME: Expansion
return ResilienceExpansion::Minimal;
return (Serialized
? ResilienceExpansion::Minimal
: ResilienceExpansion::Maximal);
}

/// Returns true if the \p ParamIdx'th (non-result) formal parameter is
Expand Down
25 changes: 7 additions & 18 deletions lib/SIL/SILFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,45 +228,34 @@ bool SILFunction::isNoReturnFunction() const {

const TypeLowering &
SILFunction::getTypeLowering(AbstractionPattern orig, Type subst) {
// FIXME: Expansion
return getModule().Types.getTypeLowering(orig, subst,
ResilienceExpansion::Minimal);
getResilienceExpansion());
}

const TypeLowering &SILFunction::getTypeLowering(Type t) const {
// FIXME: Expansion
return getModule().Types.getTypeLowering(t, ResilienceExpansion::Minimal);
return getModule().Types.getTypeLowering(t, getResilienceExpansion());
}

SILType
SILFunction::getLoweredType(AbstractionPattern orig, Type subst) const {
// FIXME: Expansion
return getModule().Types.getLoweredType(orig, subst,
ResilienceExpansion::Minimal);
getResilienceExpansion());
}

SILType SILFunction::getLoweredType(Type t) const {
// FIXME: Expansion
return getModule().Types.getLoweredType(t,
ResilienceExpansion::Minimal);
return getModule().Types.getLoweredType(t, getResilienceExpansion());
}

SILType SILFunction::getLoweredLoadableType(Type t) const {
// FIXME: Expansion
return getModule().Types.getLoweredLoadableType(t,
ResilienceExpansion::Minimal);
return getModule().Types.getLoweredLoadableType(t, getResilienceExpansion());
}

const TypeLowering &SILFunction::getTypeLowering(SILType type) const {
// FIXME: Expansion
return getModule().Types.getTypeLowering(type,
ResilienceExpansion::Minimal);
return getModule().Types.getTypeLowering(type, getResilienceExpansion());
}

bool SILFunction::isTypeABIAccessible(SILType type) const {
// FIXME: Expansion
return getModule().isTypeABIAccessible(type,
ResilienceExpansion::Minimal);
return getModule().isTypeABIAccessible(type, getResilienceExpansion());
}

SILBasicBlock *SILFunction::createBasicBlock() {
Expand Down
5 changes: 3 additions & 2 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,9 @@ static CanSILFunctionType getSILFunctionType(
// from the function to which the argument is attached.
if (constant && !constant->isDefaultArgGenerator()) {
if (auto function = constant->getAnyFunctionRef()) {
// FIXME: Expansion
auto expansion = ResilienceExpansion::Minimal;
auto expansion = ResilienceExpansion::Maximal;
if (constant->isSerialized())
expansion = ResilienceExpansion::Minimal;
lowerCaptureContextParameters(M, *function, genericSig, expansion,
inputs);
}
Expand Down
23 changes: 3 additions & 20 deletions lib/SIL/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ SILType SILType::getSILTokenType(const ASTContext &C) {
}

bool SILType::isTrivial(const SILFunction &F) const {
// FIXME: Should just call F.getTypeLowering()
return F.getModule().Types.getTypeLowering(*this,
ResilienceExpansion::Minimal).isTrivial();
return F.getTypeLowering(*this).isTrivial();
}

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

bool SILType::isLoadableOrOpaque(SILModule &M) const {
return isLoadable(M) || !SILModuleConventions(M).useLoweredAddresses();
}

bool SILType::isLoadableOrOpaque(const SILFunction &F) const {
SILModule &M = F.getModule();
return isLoadable(F) ||
!SILModuleConventions(M).useLoweredAddresses();
}

/// True if the type, or the referenced type of an address type, is
/// address-only. For example, it could be a resilient struct or something of
/// unknown size.
bool SILType::isAddressOnly(SILModule &M) const {
return M.Types.getTypeLowering(*this, ResilienceExpansion::Minimal)
.isAddressOnly();
return isLoadable(F) || !SILModuleConventions(M).useLoweredAddresses();
}

bool SILType::isAddressOnly(const SILFunction &F) const {
// FIXME: Should just call F.getTypeLowering()
return F.getModule().Types.getTypeLowering(*this,
F.getResilienceExpansion()).isAddressOnly();
return F.getTypeLowering(*this).isAddressOnly();
}

SILType SILType::substGenericArgs(SILModule &M,
Expand Down
6 changes: 3 additions & 3 deletions lib/SILGen/RValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ExplodeTupleValue

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

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

SILType RValue::getLoweredImplodedTupleType(SILGenFunction &SGF) const & {
SILType loweredType = getLoweredType(SGF);
if (loweredType.isAddressOnly(SGF.getModule()) &&
if (loweredType.isAddressOnly(SGF.F) &&
SGF.silConv.useLoweredAddresses())
return loweredType.getAddressType();
return loweredType.getObjectType();
Expand Down
6 changes: 3 additions & 3 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4669,7 +4669,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
SGFContext C) {
// Easy case -- no payload
if (!payload) {
if (enumTy.isLoadable(SGM.M) || !silConv.useLoweredAddresses()) {
if (enumTy.isLoadable(F) || !silConv.useLoweredAddresses()) {
return emitManagedRValueWithCleanup(
B.createEnum(loc, SILValue(), element, enumTy.getObjectType()));
}
Expand Down Expand Up @@ -4714,7 +4714,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
}

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

Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ ManagedValue SILGenBuilder::createOptionalSome(SILLocation loc,

ManagedValue SILGenBuilder::createManagedOptionalNone(SILLocation loc,
SILType type) {
if (!type.isAddressOnly(getModule()) || !SGF.silConv.useLoweredAddresses()) {
if (!type.isAddressOnly(getFunction()) ||
!SGF.silConv.useLoweredAddresses()) {
SILValue noneValue = createOptionalNone(loc, type);
return ManagedValue::forUnmanaged(noneValue);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,

// This can happen if the value is resilient in the calling convention
// but not resilient locally.
if (argType.isLoadable(SGF.SGM.M) && argType.isAddress()) {
if (argType.isLoadable(SGF.F) && argType.isAddress()) {
if (mvArg.isPlusOne(SGF))
mvArg = SGF.B.createLoadTake(loc, mvArg);
else
Expand Down Expand Up @@ -140,7 +140,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,

// Emit the indirect return argument, if any.
SILValue resultSlot;
if (selfTy.isAddressOnly(SGF.SGM.M) && SGF.silConv.useLoweredAddresses()) {
if (SILModuleConventions::isReturnedIndirectlyInSIL(selfTy, SGF.SGM.M)) {
auto &AC = SGF.getASTContext();
auto VD = new (AC) ParamDecl(VarDecl::Specifier::InOut,
SourceLoc(), SourceLoc(),
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void TupleInitialization::copyOrInitValueInto(SILGenFunction &SGF,
SILType fieldType) -> ManagedValue {
ManagedValue elt =
SGF.B.createTupleElementAddr(loc, value, i, fieldType);
if (!fieldType.isAddressOnly(SGF.F.getModule())) {
if (!fieldType.isAddressOnly(SGF.F)) {
return SGF.B.createLoadTake(loc, elt);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4424,7 +4424,7 @@ ManagedValue SILGenFunction::emitBindOptional(SILLocation loc,

// If optValue was loadable, we emitted a switch_enum. In such a case, return
// the argument from hasValueBB.
if (optValue.getType().isLoadable(F.getModule())) {
if (optValue.getType().isLoadable(F)) {
return emitManagedRValueWithCleanup(hasValueBB->getArgument(0));
}

Expand Down
3 changes: 1 addition & 2 deletions lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,

auto *vd = capture.getDecl();

// FIXME: Expansion
auto expansion = ResilienceExpansion::Minimal;
auto expansion = F.getResilienceExpansion();
switch (SGM.Types.getDeclCaptureKind(capture, expansion)) {
case CaptureKind::None:
break;
Expand Down
Loading