Skip to content

Commit 8d36026

Browse files
Merge pull request #70986 from nate-chandler/opaque-values/20240117/2
[AddressLowering] Handle instructions that wrap and unwrap in @moveOnly.
2 parents 728d680 + d538894 commit 8d36026

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

lib/SIL/Utils/InstWrappers.cpp

Lines changed: 2 additions & 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:
@@ -51,6 +52,7 @@ bool ForwardingOperation::hasSameRepresentation() const {
5152
case SILInstructionKind::OpenExistentialRefInst:
5253
case SILInstructionKind::OpenExistentialValueInst:
5354
case SILInstructionKind::MarkUnresolvedNonCopyableValueInst:
55+
case SILInstructionKind::MoveOnlyWrapperToCopyableValueInst:
5456
case SILInstructionKind::MarkUninitializedInst:
5557
case SILInstructionKind::StructExtractInst:
5658
case SILInstructionKind::TupleExtractInst:

lib/SILOptimizer/Mandatory/AddressLowering.cpp

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

1012+
case ValueKind::CopyableToMoveOnlyWrapperValueInst:
1013+
case ValueKind::MoveOnlyWrapperToCopyableValueInst:
10121014
case ValueKind::OpenExistentialValueInst:
10131015
case ValueKind::OpenExistentialBoxValueInst:
10141016
case ValueKind::UncheckedEnumDataInst:
@@ -3409,6 +3411,19 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
34093411

34103412
void visitBeginBorrowInst(BeginBorrowInst *borrow);
34113413

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+
34123427
void visitEndBorrowInst(EndBorrowInst *end) {}
34133428

34143429
void visitFixLifetimeInst(FixLifetimeInst *fli) {
@@ -3495,6 +3510,19 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
34953510

34963511
void visitMoveValueInst(MoveValueInst *mvi);
34973512

3513+
void visitMoveOnlyWrapperToCopyableValueInst(
3514+
MoveOnlyWrapperToCopyableValueInst *inst) {
3515+
assert(use == getReusedStorageOperand(inst));
3516+
assert(inst->getType().isAddressOnly(*pass.function));
3517+
SILValue srcVal = inst->getOperand();
3518+
SILValue srcAddr = pass.valueStorageMap.getStorage(srcVal).storageAddress;
3519+
3520+
auto destAddr =
3521+
builder.createMoveOnlyWrapperToCopyableAddr(inst->getLoc(), srcAddr);
3522+
3523+
markRewritten(inst, destAddr);
3524+
}
3525+
34983526
void visitReturnInst(ReturnInst *returnInst) {
34993527
// Returns are rewritten for any function with indirect results after
35003528
// opaque value rewriting.

test/SILOptimizer/address_lowering.sil

Lines changed: 57 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
}
@@ -2552,6 +2581,34 @@ entry(%t : @owned $@moveOnly T):
25522581
return %retval : $()
25532582
}
25542583

2584+
// CHECK-LABEL: sil [ossa] @test_moveonlywrapper_to_copyable_1_owned : {{.*}} {
2585+
// CHECK: bb0([[TMO:%[^,]+]] :
2586+
// CHECK: [[T:%[^,]+]] = moveonlywrapper_to_copyable_addr [[TMO]]
2587+
// CHECK: destroy_addr [[T]]
2588+
// CHECK-LABEL: } // end sil function 'test_moveonlywrapper_to_copyable_1_owned'
2589+
sil [ossa] @test_moveonlywrapper_to_copyable_1_owned : $@convention(thin) <T> (@in @moveOnly T) -> () {
2590+
entry(%tmo : @owned $@moveOnly T):
2591+
%t = moveonlywrapper_to_copyable [owned] %tmo : $@moveOnly T
2592+
destroy_value %t : $T
2593+
%retval = tuple ()
2594+
return %retval : $()
2595+
}
2596+
2597+
// CHECK-LABEL: sil [ossa] @test_moveonlywrapper_to_copyable_2_guaranteed : {{.*}} {
2598+
// CHECK: bb0([[TMO:%[^,]+]] :
2599+
// CHECK: [[T:%[^,]+]] = moveonlywrapper_to_copyable_addr [[TMO]]
2600+
// CHECK: [[BORROW_T:%[^,]+]] = function_ref @borrowT
2601+
// CHECK: apply [[BORROW_T]]<T>([[T]])
2602+
// CHECK-LABEL: } // end sil function 'test_moveonlywrapper_to_copyable_2_guaranteed'
2603+
sil [ossa] @test_moveonlywrapper_to_copyable_2_guaranteed : $@convention(thin) <T> (@in_guaranteed @moveOnly T) -> () {
2604+
entry(%tmo : @guaranteed $@moveOnly T):
2605+
%t = moveonlywrapper_to_copyable [guaranteed] %tmo : $@moveOnly T
2606+
%borrowT = function_ref @borrowT : $@convention(thin) <T> (@in_guaranteed T) -> ()
2607+
apply %borrowT<T>(%t) : $@convention(thin) <T> (@in_guaranteed T) -> ()
2608+
%retval = tuple ()
2609+
return %retval : $()
2610+
}
2611+
25552612
// CHECK-LABEL: sil [ossa] @test_open_pack_element_dominance : $@convention(thin) <each T> (@pack_guaranteed Pack{repeat each T}, Builtin.Word) -> () {
25562613
// CHECK: bb0([[PACK:%[^,]+]] :
25572614
// CHECK-SAME: [[RAW_INDEX:%[^,]+]] :

0 commit comments

Comments
 (0)