Skip to content

Commit 820ca35

Browse files
committed
[SILGen] Used move_value for @noImplicitCopy.
Replace the pattern of begin_borrow [lexical] + copy_value with move_value [lexical] to avoid introducing a spurious copy to begin with.
1 parent e48c438 commit 820ca35

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

lib/SILGen/SILGenDecl.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -843,10 +843,9 @@ class LetValueInitialization : public Initialization {
843843

844844
// If we have a no implicit copy lexical, emit the instruction stream so
845845
// that the move checker knows to check this variable.
846-
value = SGF.B.createBeginBorrow(
847-
PrologueLoc, value,
848-
/*isLexical*/ true, /*hasPointerEscape=*/false, /*fromVarDecl=*/true);
849-
value = SGF.B.createCopyValue(PrologueLoc, value);
846+
value =
847+
SGF.B.createMoveValue(PrologueLoc, value, /*IisLexical*/ true,
848+
/*hasPointerEscape=*/false, /*fromVarDecl=*/true);
850849
value = SGF.B.createOwnedCopyableToMoveOnlyWrapperValue(PrologueLoc, value);
851850
return SGF.B.createMarkUnresolvedNonCopyableValueInst(
852851
PrologueLoc, value,
@@ -2192,14 +2191,10 @@ void SILGenFunction::destroyLocalVariable(SILLocation silLoc, VarDecl *vd) {
21922191

21932192
if (auto *copyToMove = dyn_cast<CopyableToMoveOnlyWrapperValueInst>(
21942193
mvi->getOperand())) {
2195-
if (auto *cvi = dyn_cast<CopyValueInst>(copyToMove->getOperand())) {
2196-
if (auto *bbi = dyn_cast<BeginBorrowInst>(cvi->getOperand())) {
2197-
if (bbi->isLexical()) {
2198-
B.emitDestroyValueOperation(silLoc, mvi);
2199-
B.createEndBorrow(silLoc, bbi);
2200-
B.emitDestroyValueOperation(silLoc, bbi->getOperand());
2201-
return;
2202-
}
2194+
if (auto *move = dyn_cast<MoveValueInst>(copyToMove->getOperand())) {
2195+
if (move->isLexical()) {
2196+
B.emitDestroyValueOperation(silLoc, mvi);
2197+
return;
22032198
}
22042199
}
22052200
}

test/SILGen/noimplicitcopy.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ func callClosureIntOwned() {
337337
// NOTE: MOW expands to MOVEONLYWRAPPED
338338
//
339339
// CHECK-LABEL: sil hidden [ossa] @$s14noimplicitcopy10printKlassyyF : $@convention(thin) () -> () {
340-
// CHECK: [[X:%.*]] = begin_borrow [lexical] [var_decl] {{%[0-9]+}} : $Klass
341-
// CHECK: [[X_COPY:%.*]] = copy_value [[X]]
340+
// CHECK: [[X:%.*]] = move_value [lexical] [var_decl] {{%[0-9]+}} : $Klass
342341
// CHECK: [[X_MOVEONLYWRAPPED:%.*]] = copyable_to_moveonlywrapper [owned] [[X_COPY]]
343342
// CHECK: [[X_MOVEONLYWRAPPED_MARKED:%.*]] = mark_unresolved_non_copyable_value [consumable_and_assignable] [[X_MOVEONLYWRAPPED]]
344343
// CHECK: [[BORROWED_X_MOVEONLYWRAPPED_MARKED:%.*]] = begin_borrow [[X_MOVEONLYWRAPPED_MARKED]]

test/SILOptimizer/noimplicitcopy_borrow_to_destructure_transform_diagnostics.sil

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ sil [ossa] @aggStructConsumeField : $@convention(thin) (@guaranteed AggStruct) -
2323
bb0(%0 : @guaranteed $AggStruct):
2424
debug_value %0 : $AggStruct, let, name "x", argno 1
2525
%2 = copy_value %0 : $AggStruct
26-
%3 = begin_borrow [lexical] %2 : $AggStruct
27-
%4 = copy_value %3 : $AggStruct
28-
%5 = copyable_to_moveonlywrapper [owned] %4 : $AggStruct
26+
%3 = move_value [lexical] [var_decl] %2 : $AggStruct
27+
%5 = copyable_to_moveonlywrapper [owned] %3 : $AggStruct
2928
%6 = mark_unresolved_non_copyable_value [consumable_and_assignable] %5 : $@moveOnly AggStruct
3029
debug_value %6 : $@moveOnly AggStruct, let, name "x2"
3130
%8 = begin_borrow %6 : $@moveOnly AggStruct
@@ -52,8 +51,6 @@ bb2:
5251

5352
bb3:
5453
destroy_value %6 : $@moveOnly AggStruct
55-
end_borrow %3 : $AggStruct
56-
destroy_value %2 : $AggStruct
5754
%64 = tuple ()
5855
return %64 : $()
5956
}

0 commit comments

Comments
 (0)