Skip to content

Commit d538894

Browse files
committed
[AddressLowering] Handle copyable_to_moveonly.
The _value version of the instruction lowers to the _address version.
1 parent 99df40a commit d538894

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

lib/SIL/Utils/InstWrappers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ bool ForwardingOperation::hasSameRepresentation() const {
4343
return false;
4444

4545
case SILInstructionKind::ConvertFunctionInst:
46+
case SILInstructionKind::CopyableToMoveOnlyWrapperValueInst:
4647
case SILInstructionKind::DestructureTupleInst:
4748
case SILInstructionKind::DestructureStructInst:
4849
case SILInstructionKind::InitExistentialRefInst:

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ static Operand *getReusedStorageOperand(SILValue value) {
10091009
default:
10101010
break;
10111011

1012+
case ValueKind::CopyableToMoveOnlyWrapperValueInst:
10121013
case ValueKind::MoveOnlyWrapperToCopyableValueInst:
10131014
case ValueKind::OpenExistentialValueInst:
10141015
case ValueKind::OpenExistentialBoxValueInst:
@@ -3410,6 +3411,19 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
34103411

34113412
void visitBeginBorrowInst(BeginBorrowInst *borrow);
34123413

3414+
void visitCopyableToMoveOnlyWrapperValueInst(
3415+
CopyableToMoveOnlyWrapperValueInst *inst) {
3416+
assert(use == getReusedStorageOperand(inst));
3417+
assert(inst->getType().isAddressOnly(*pass.function));
3418+
SILValue srcVal = inst->getOperand();
3419+
SILValue srcAddr = pass.valueStorageMap.getStorage(srcVal).storageAddress;
3420+
3421+
auto destAddr =
3422+
builder.createCopyableToMoveOnlyWrapperAddr(inst->getLoc(), srcAddr);
3423+
3424+
markRewritten(inst, destAddr);
3425+
}
3426+
34133427
void visitEndBorrowInst(EndBorrowInst *end) {}
34143428

34153429
void visitFixLifetimeInst(FixLifetimeInst *fli) {

test/SILOptimizer/address_lowering.sil

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ sil [ossa] @getT : $@convention(thin) <T> () -> @out T
100100
sil [ossa] @getKlass : $@convention(thin) () -> (@owned Klass)
101101
sil [ossa] @borrowKlass : $(@in_guaranteed Klass) -> ()
102102
sil [ossa] @borrowT : $@convention(thin) <T> (@in_guaranteed T) -> ()
103+
sil [ossa] @borrowMoveOnlyT : $@convention(thin) <T> (@in_guaranteed @moveOnly T) -> ()
103104
sil [ossa] @getPair : $@convention(thin) <T> () -> @out Pair<T>
104105
sil [ossa] @getOwned : $@convention(thin) <T : AnyObject> () -> (@owned T)
105106
sil [ossa] @takeGuaranteedObject : $@convention(thin) (@guaranteed AnyObject) -> ()
@@ -2345,6 +2346,34 @@ exit:
23452346
return %retval : $()
23462347
}
23472348

2349+
// CHECK-LABEL: sil [ossa] @test_copyable_to_moveonlywrapper_1_owned : {{.*}} {
2350+
// CHECK: bb0([[TMO:%[^,]+]] :
2351+
// CHECK: [[T:%[^,]+]] = copyable_to_moveonlywrapper_addr [[TMO]]
2352+
// CHECK: destroy_addr [[T]]
2353+
// CHECK-LABEL: } // end sil function 'test_copyable_to_moveonlywrapper_1_owned'
2354+
sil [ossa] @test_copyable_to_moveonlywrapper_1_owned : $@convention(thin) <T> (@in T) -> () {
2355+
entry(%t : @owned $T):
2356+
%tmo = copyable_to_moveonlywrapper [owned] %t : $T
2357+
destroy_value %tmo : $@moveOnly T
2358+
%retval = tuple ()
2359+
return %retval : $()
2360+
}
2361+
2362+
// CHECK-LABEL: sil [ossa] @test_copyable_to_moveonlywrapper_2_guaranteed : {{.*}} {
2363+
// CHECK: bb0([[TMO:%[^,]+]] :
2364+
// CHECK: [[T:%[^,]+]] = copyable_to_moveonlywrapper_addr [[TMO]]
2365+
// CHECK: [[BORROW_T:%[^,]+]] = function_ref @borrowMoveOnlyT
2366+
// CHECK: apply [[BORROW_T]]<T>([[T]])
2367+
// CHECK-LABEL: } // end sil function 'test_copyable_to_moveonlywrapper_2_guaranteed'
2368+
sil [ossa] @test_copyable_to_moveonlywrapper_2_guaranteed : $@convention(thin) <T> (@in_guaranteed T) -> () {
2369+
entry(%t : @guaranteed $T):
2370+
%tmo = copyable_to_moveonlywrapper [guaranteed] %t : $T
2371+
%borrowT = function_ref @borrowMoveOnlyT : $@convention(thin) <T> (@in_guaranteed @moveOnly T) -> ()
2372+
apply %borrowT<T>(%tmo) : $@convention(thin) <T> (@in_guaranteed @moveOnly T) -> ()
2373+
%retval = tuple ()
2374+
return %retval : $()
2375+
}
2376+
23482377
enum PairEnum<T> {
23492378
case it(T, T)
23502379
}

0 commit comments

Comments
 (0)