Skip to content

[semantic-arc] Change emitCopyValue entrypoints to return the copied SILValue. #5444

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
35 changes: 18 additions & 17 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,43 +280,44 @@ class TypeLowering {
/// Emit a lowered 'retain_value' operation.
///
/// This type must be loadable.
virtual void emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const = 0;
virtual SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const = 0;

/// Emit a lowered 'retain_value' operation.
///
/// This type must be loadable.
void emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
SILValue value) const {
emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
SILValue emitLoweredCopyValueShallow(SILBuilder &B, SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value, LoweringStyle::Shallow);
}

/// Emit a lowered 'retain_value' operation.
///
/// This type must be loadable.
void emitLoweredRetainValueDeepNoEnum(SILBuilder &B, SILLocation loc,
SILValue value) const {
emitLoweredCopyValue(B, loc, value, LoweringStyle::DeepNoEnum);
SILValue emitLoweredCopyValueDeepNoEnum(SILBuilder &B, SILLocation loc,
SILValue value) const {
return emitLoweredCopyValue(B, loc, value, LoweringStyle::DeepNoEnum);
}

/// Given a primitively loaded value of this type (which must be
/// loadable), +1 it.
/// loadable), Perform a copy of this value. This is equivalent to performing
/// +1 on class values.
///
/// This should be used for duplicating a value from place to place
/// with exactly the same semantics. For example, it performs an
/// unowned_retain on a value of [unknown] type. It is therefore
/// not necessarily the right thing to do on a semantic load.
virtual void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const = 0;
virtual SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const = 0;

void emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const {
SILValue emitLoweredCopyChildValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const {
if (style != LoweringStyle::Shallow) {
emitLoweredCopyValue(B, loc, value, style);
return emitLoweredCopyValue(B, loc, value, style);
} else {
emitCopyValue(B, loc, value);
return emitCopyValue(B, loc, value);
}
}

Expand Down
71 changes: 46 additions & 25 deletions lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,17 @@ namespace {
// Trivial
}

void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
// Trivial
return value;
}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
// Trivial
return value;
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
Expand Down Expand Up @@ -615,6 +618,9 @@ namespace {
IsNotReferenceCounted) {
}

virtual SILValue rebuildAggregate(SILBuilder &B, SILLocation loc,
ArrayRef<SILValue> values) const = 0;

ArrayRef<Child> getChildren(SILModule &M) const {
if (Children.data() == nullptr) {
SmallVector<Child, 4> children;
Expand Down Expand Up @@ -657,22 +663,31 @@ namespace {
});
}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue aggValue) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue aggValue) const override {
B.createRetainValue(loc, aggValue, Atomicity::Atomic);
return aggValue;
}

void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue aggValue,
LoweringStyle style) const override {
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue aggValue,
LoweringStyle style) const override {
llvm::SmallVector<SILValue, 8> loweredChildValues;
for (auto &child : getChildren(B.getModule())) {
auto &childLowering = child.getLowering();
SILValue childValue = asImpl().emitRValueProject(B, loc, aggValue,
child.getIndex(),
childLowering);
if (!childLowering.isTrivial()) {
childLowering.emitLoweredCopyChildValue(B, loc, childValue, style);
SILValue loweredChildValue = childLowering.emitLoweredCopyChildValue(
B, loc, childValue, style);
loweredChildValues.push_back(loweredChildValue);
} else {
loweredChildValues.push_back(childValue);
}
}

return rebuildAggregate(B, loc, loweredChildValues);
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
Expand Down Expand Up @@ -713,7 +728,7 @@ namespace {
}

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

Expand Down Expand Up @@ -748,7 +763,7 @@ namespace {
}

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

Expand All @@ -773,14 +788,16 @@ namespace {
: NonTrivialLoadableTypeLowering(SILType::getPrimitiveObjectType(type),
IsNotReferenceCounted) {}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
B.createRetainValue(loc, value, Atomicity::Atomic);
return value;
}

void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
B.createRetainValue(loc, value, Atomicity::Atomic);
return value;
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
Expand All @@ -802,9 +819,10 @@ namespace {
LeafLoadableTypeLowering(SILType type, IsReferenceCounted_t isRefCounted)
: NonTrivialLoadableTypeLowering(type, isRefCounted) {}

void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
emitCopyValue(B, loc, value);
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
return emitCopyValue(B, loc, value);
}

void emitLoweredDestroyValue(SILBuilder &B, SILLocation loc, SILValue value,
Expand All @@ -820,10 +838,11 @@ namespace {
ReferenceTypeLowering(SILType type)
: LeafLoadableTypeLowering(type, IsReferenceCounted) {}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
if (!isa<FunctionRefInst>(value))
B.createStrongRetain(loc, value, Atomicity::Atomic);
return value;
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
Expand All @@ -838,9 +857,10 @@ namespace {
LoadableUnownedTypeLowering(SILType type)
: LeafLoadableTypeLowering(type, IsReferenceCounted) {}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
B.createUnownedRetain(loc, value, Atomicity::Atomic);
return value;
}

void emitDestroyValue(SILBuilder &B, SILLocation loc,
Expand Down Expand Up @@ -883,13 +903,14 @@ namespace {
B.emitDestroyAddrAndFold(loc, value);
}

void emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
SILValue emitCopyValue(SILBuilder &B, SILLocation loc,
SILValue value) const override {
llvm_unreachable("type is not loadable!");
}

void emitLoweredCopyValue(SILBuilder &B, SILLocation loc, SILValue value,
LoweringStyle style) const override {
SILValue emitLoweredCopyValue(SILBuilder &B, SILLocation loc,
SILValue value,
LoweringStyle style) const override {
llvm_unreachable("type is not loadable!");
}

Expand Down
Loading