Skip to content

[AddressLowering] Handle move_value. #67217

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 2 commits into from
Jul 11, 2023
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
27 changes: 22 additions & 5 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ static Operand *getProjectedDefOperand(SILValue value) {

return nullptr;

case ValueKind::MoveValueInst:
return &cast<MoveValueInst>(value)->getOperandRef();

case ValueKind::MultipleValueInstructionResult: {
SILInstruction *destructure =
cast<MultipleValueInstructionResult>(value)->getParent();
Expand Down Expand Up @@ -3050,6 +3053,9 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
}
}

template <typename Introducer>
void visitLifetimeIntroducer(Introducer *introducer);

void visitBeginBorrowInst(BeginBorrowInst *borrow);

void visitEndBorrowInst(EndBorrowInst *end) {}
Expand Down Expand Up @@ -3125,6 +3131,8 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
// types.
void visitOpenExistentialValueInst(OpenExistentialValueInst *openExistential);

void visitMoveValueInst(MoveValueInst *mvi);

void visitReturnInst(ReturnInst *returnInst) {
// Returns are rewritten for any function with indirect results after
// opaque value rewriting.
Expand Down Expand Up @@ -3263,15 +3271,16 @@ void UseRewriter::rewriteDestructure(SILInstruction *destructure) {
}
}

void UseRewriter::visitBeginBorrowInst(BeginBorrowInst *borrow) {
assert(use == getProjectedDefOperand(borrow));
template <typename Introducer>
void UseRewriter::visitLifetimeIntroducer(Introducer *introducer) {
assert(use == getProjectedDefOperand(introducer));

// Mark the value as rewritten and use the operand's storage.
auto address = pass.valueStorageMap.getStorage(use->get()).storageAddress;
markRewritten(borrow, address);
markRewritten(introducer, address);

// Borrows are irrelevant unless they are marked lexical.
if (borrow->isLexical()) {
// Lifetime introducers are irrelevant unless they are marked lexical.
if (introducer->isLexical()) {
if (auto base = getAccessBase(address)) {
if (auto *allocStack = dyn_cast<AllocStackInst>(base)) {
allocStack->setIsLexical();
Expand All @@ -3287,6 +3296,14 @@ void UseRewriter::visitBeginBorrowInst(BeginBorrowInst *borrow) {
}
}

void UseRewriter::visitBeginBorrowInst(BeginBorrowInst *borrow) {
visitLifetimeIntroducer(borrow);
}

void UseRewriter::visitMoveValueInst(MoveValueInst *mvi) {
visitLifetimeIntroducer(mvi);
}

// Opening an opaque existential. Rewrite the opened existentials here on
// the use-side because it may produce either loadable or address-only
// types.
Expand Down
54 changes: 54 additions & 0 deletions test/SILOptimizer/address_lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,60 @@ entry(%addr : $*T):
return %retval : $()
}

// The move_value is lexical so the corresponding alloc_stack must be marked
// lexical too.
// CHECK-LABEL: sil [ossa] @testMoveValueLexical : {{.*}} {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'testMoveValueLexical'
sil [ossa] @testMoveValueLexical : $@convention(thin) <T> () -> () {
%getT = function_ref @getT : $@convention(thin) <T> () -> @out T
%instance = apply %getT<T>() : $@convention(thin) <T> () -> @out T
%moved = move_value [lexical] %instance : $T
destroy_value %moved : $T
%retval = tuple ()
return %retval : $()
}

// Verify that the temporary storage is lexical and moved out of
// (copy_addr [take]).
// CHECK-LABEL: sil [ossa] @testMoveValueLexicalAndReturn : {{.*}} {
// CHECK: {{bb[0-9]+}}([[RETVAL:%[^,]+]] :
// CHECK: [[STACK:%[^,]+]] = alloc_stack [lexical]
// CHECK: copy_addr [take] [[STACK]] to [init] [[RETVAL]]
// CHECK-LABEL: } // end sil function 'testMoveValueLexicalAndReturn'
sil [ossa] @testMoveValueLexicalAndReturn : $@convention(thin) <T> () -> @out T {
%getT = function_ref @getT : $@convention(thin) <T> () -> @out T
%instance = apply %getT<T>() : $@convention(thin) <T> () -> @out T
%moved = move_value [lexical] %instance : $T
return %moved : $T
}

// Verify that a move_value gets no stack storage if it's from an argument.
// CHECK-LABEL: sil [ossa] @testMoveValueLexicalArgument : {{.*}} {
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
// CHECK: destroy_addr [[INSTANCE]]
// CHECK-LABEL: } // end sil function 'testMoveValueLexicalArgument'
sil [ossa] @testMoveValueLexicalArgument : $@convention(thin) <T> (@in T) -> () {
entry(%instance : @owned $T):
%moved = move_value [lexical] %instance : $T
destroy_value %moved : $T
%retval = tuple ()
return %retval : $()
}

// Only lexical move_values should mark the alloc_stack lexical.
// CHECK-LABEL: sil [ossa] @testMoveValueNonlexical : {{.*}} {
// CHECK-NOT: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'testMoveValueNonlexical'
sil [ossa] @testMoveValueNonlexical : $@convention(thin) <T> () -> () {
%getT = function_ref @getT : $@convention(thin) <T> () -> @out T
%instance = apply %getT<T>() : $@convention(thin) <T> () -> @out T
%moved = move_value %instance : $T
destroy_value %moved : $T
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil hidden [ossa] @testOpaqueYield :
// CHECK: bb0(%0 : @guaranteed $TestGeneric<T>):
// CHECK: [[REF:%.*]] = ref_element_addr %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric
Expand Down