Skip to content

[move-only] Change type lowering to ensure we use {retain,release}_value instead of strong_{retain,release} #61711

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
4 changes: 4 additions & 0 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ class SILType {
/// wrapped type.
bool isMoveOnly() const;

/// Is this a type that is a first class move only type. This returns false
/// for a move only wrapped type.
bool isPureMoveOnly() const;

/// Returns true if and only if this type is a first class move only
/// type. NOTE: Returns false if the type is a move only wrapped type.
bool isMoveOnlyType() const;
Expand Down
7 changes: 7 additions & 0 deletions lib/SIL/IR/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,10 @@ bool SILType::isMoveOnlyType() const {
return true;
return false;
}

bool SILType::isPureMoveOnly() const {
if (auto *nom = getNominalOrBoundGenericNominal())
if (nom->isMoveOnly())
return true;
return false;
}
110 changes: 108 additions & 2 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,110 @@ namespace {
}
};

/// A lowering for loadable but non-trivial struct types.
class MoveOnlyLoadableStructTypeLowering final
: public LoadableAggTypeLowering<MoveOnlyLoadableStructTypeLowering,
VarDecl *> {
using Super =
LoadableAggTypeLowering<MoveOnlyLoadableStructTypeLowering, VarDecl *>;

public:
MoveOnlyLoadableStructTypeLowering(CanType type,
RecursiveProperties properties,
TypeExpansionContext forExpansion)
: LoadableAggTypeLowering(type, properties, forExpansion) {}

SILValue emitRValueProject(SILBuilder &B, SILLocation loc,
SILValue structValue, VarDecl *field,
const TypeLowering &fieldLowering) const {
return B.createStructExtract(loc, structValue, field,
fieldLowering.getLoweredType());
}

void destructureAggregate(
SILBuilder &B, SILLocation loc, SILValue aggValue, bool skipTrivial,
function_ref<void(unsigned childIndex, SILValue childValue,
const TypeLowering &childLowering)>
visitor) const {
if (!B.hasOwnership())
return Super::destructureAggregate(B, loc, aggValue, skipTrivial,
visitor);

auto *dsi = B.createDestructureStruct(loc, aggValue);
for (auto pair : llvm::enumerate(dsi->getResults())) {
SILValue childValue = pair.value();
auto &childLowering =
B.getFunction().getTypeLowering(childValue->getType());
if (skipTrivial && childLowering.isTrivial())
continue;
visitor(pair.index(), childValue, childLowering);
}
}

SILValue rebuildAggregate(SILBuilder &B, SILLocation loc,
ArrayRef<SILValue> values) const override {
return B.createStruct(loc, getLoweredType(), values);
}

private:
void lowerChildren(TypeConverter &TC,
SmallVectorImpl<Child> &children) const override {
auto silTy = getLoweredType();
auto structDecl = silTy.getStructOrBoundGenericStruct();
assert(structDecl);

for (auto prop : structDecl->getStoredProperties()) {
SILType propTy = silTy.getFieldType(prop, TC, getExpansionContext());
auto &propTL = TC.getTypeLowering(propTy, getExpansionContext());
children.push_back(Child{prop, propTL});
}
}
};

/// A lowering for loadable but non-trivial enum types.
class MoveOnlyLoadableEnumTypeLowering final
: public NonTrivialLoadableTypeLowering {
public:
MoveOnlyLoadableEnumTypeLowering(CanType type,
RecursiveProperties properties,
TypeExpansionContext forExpansion)
: NonTrivialLoadableTypeLowering(SILType::getPrimitiveObjectType(type),
properties, IsNotReferenceCounted,
forExpansion) {}

SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
if (B.getFunction().hasOwnership())
return B.createCopyValue(loc, value);
B.createRetainValue(loc, value, B.getDefaultAtomicity());
return value;
}

SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
TypeExpansionKind style) const override {
if (B.getFunction().hasOwnership())
return B.createCopyValue(loc, value);
B.createRetainValue(loc, value, B.getDefaultAtomicity());
return value;
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
if (B.getFunction().hasOwnership()) {
B.createDestroyValue(loc, value);
return;
}
B.createReleaseValue(loc, value, B.getDefaultAtomicity());
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
TypeExpansionKind style) const override {
// Enums, we never want to expand.
return emitDestroyValue(B, loc, value);
}
};

/// A type lowering for `@differentiable(_linear)` function types.
class LinearDifferentiableSILFunctionTypeLowering final
: public LoadableAggTypeLowering<
Expand Down Expand Up @@ -2146,7 +2250,8 @@ namespace {
properties.setNonTrivial();
if (properties.isAddressOnly())
return handleMoveOnlyAddressOnly(structType, properties);
return handleMoveOnlyReference(structType, properties);
return new (TC) MoveOnlyLoadableStructTypeLowering(
structType, properties, Expansion);
}

return handleAggregateByProperties<LoadableStructTypeLowering>(structType,
Expand Down Expand Up @@ -2223,7 +2328,8 @@ namespace {
properties.setNonTrivial();
if (properties.isAddressOnly())
return handleMoveOnlyAddressOnly(enumType, properties);
return handleMoveOnlyReference(enumType, properties);
return new (TC)
MoveOnlyLoadableEnumTypeLowering(enumType, properties, Expansion);
}

return handleAggregateByProperties<LoadableEnumTypeLowering>(enumType,
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4248,6 +4248,7 @@ RValue CallEmission::applyEnumElementConstructor(SGFContext C) {
uncurriedLoc, std::move(payload),
SGF.getLoweredType(formalResultType),
element, uncurriedContext);

return RValue(SGF, uncurriedLoc, formalResultType, resultMV);
}

Expand Down
19 changes: 15 additions & 4 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,21 @@ class LetValueInitialization : public Initialization {
SILValue value, bool wasPlusOne) {
// If we have none...
if (value->getOwnershipKind() == OwnershipKind::None) {
// ... and we don't have a no implicit copy trivial type, just return
// value.
if (!SGF.getASTContext().LangOpts.Features.count(Feature::MoveOnly) ||
!vd->isNoImplicitCopy() || !value->getType().isTrivial(SGF.F))
// If we don't have move only features enabled, just return, we are done.
if (!SGF.getASTContext().LangOpts.Features.count(Feature::MoveOnly))
return value;

// Then check if we have a pure move only type. In that case, we need to
// insert a no implicit copy
if (value->getType().isPureMoveOnly()) {
value = SGF.B.createMoveValue(PrologueLoc, value, /*isLexical*/ true);
return SGF.B.createMarkMustCheckInst(
PrologueLoc, value, MarkMustCheckInst::CheckKind::NoImplicitCopy);
}

// Otherwise, if we don't have a no implicit copy trivial type, just
// return value.
if (!vd->isNoImplicitCopy() || !value->getType().isTrivial(SGF.F))
return value;

// Otherwise, we have a no implicit copy trivial type, so wrap it in the
Expand Down
Loading