Skip to content

Commit ec554da

Browse files
authored
Merge pull request #81861 from kavon/kavon/avoid-double-copy
SILGen: avoid double copies with 'copy' expr
2 parents 0654dc7 + 32f1ebc commit ec554da

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7222,7 +7222,9 @@ RValue RValueEmitter::visitCopyExpr(CopyExpr *E, SGFContext C) {
72227222
}
72237223

72247224
if (subType.isLoadable(SGF.F)) {
7225-
ManagedValue mv = SGF.emitRValue(subExpr).getAsSingleValue(SGF, subExpr);
7225+
ManagedValue mv =
7226+
SGF.emitRValue(subExpr, SGFContext::AllowImmediatePlusZero)
7227+
.getAsSingleValue(SGF, subExpr);
72267228
if (mv.getType().isTrivial(SGF.F))
72277229
return RValue(SGF, {mv}, subType.getASTType());
72287230
{

test/SILGen/copy_expr.swift

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ struct ContainKlass {
2929
// CHECK: [[X:%.*]] = apply {{%.*}}({{%.*}}) : $@convention(method) (@thin ContainKlass.Type) -> @owned ContainKlass
3030
// CHECK: [[MOVE:%.*]] = move_value [lexical] [var_decl] [[X]]
3131
// CHECK: [[BORROW:%.*]] = begin_borrow [[MOVE]]
32-
// CHECK: [[COPY_BORROW:%.*]] = copy_value [[BORROW]]
33-
// CHECK: explicit_copy_value [[COPY_BORROW]]
32+
// CHECK: [[COPY_BORROW:%.*]] = explicit_copy_value [[BORROW]]
3433
// CHECK: } // end sil function '$s9copy_expr22testCopyLoadableRValueyyF'
3534
func testCopyLoadableRValue() {
3635
let x = ContainKlass()
@@ -121,43 +120,35 @@ func testCopyAddressOnlyLValueArg<T : P>(_ x: inout T) {
121120
// CHECK: [[X:%.*]] = begin_borrow [[MOVE]]
122121
//
123122
// Calling consumeFunc.
124-
// CHECK: [[COPY_X:%.*]] = copy_value [[X]]
125-
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[COPY_X]]
123+
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[X]]
126124
// CHECK: [[FUNC:%.*]] = function_ref @$s9copy_expr12ContainKlassV11consumeFuncyyF : $@convention(method) (@owned ContainKlass) -> ()
127125
// CHECK: apply [[FUNC]]([[EXPLICIT_COPY_X]])
128-
// CHECK: destroy_value [[COPY_X]]
129126
//
130127
// Calling borrowingFunc.
131128
// CHECK: [[X:%.*]] = begin_borrow [[MOVE]]
132-
// CHECK: [[COPY_X:%.*]] = copy_value [[X]]
133-
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[COPY_X]]
129+
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[X]]
134130
// CHECK: [[FUNC:%.*]] = function_ref @$s9copy_expr12ContainKlassV13borrowingFuncyyF : $@convention(method) (@guaranteed ContainKlass) -> ()
135131
// CHECK: apply [[FUNC]]([[EXPLICIT_COPY_X]])
136132
// CHECK: destroy_value [[EXPLICIT_COPY_X]]
137-
// CHECK: destroy_value [[COPY_X]]
138133
//
139134
// Calling computedK. It is borrowed.
140135
// CHECK: [[X:%.*]] = begin_borrow [[MOVE]]
141-
// CHECK: [[COPY_X:%.*]] = copy_value [[X]]
142-
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[COPY_X]]
136+
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[X]]
143137
// CHECK: [[BORROW_EXPLICIT_COPY_X:%.*]] = begin_borrow [[EXPLICIT_COPY_X]]
144138
// CHECK: [[FUNC:%.*]] = function_ref @$s9copy_expr12ContainKlassV9computedKAA0D0Cvg : $@convention(method) (@guaranteed ContainKlass) -> @owned Klass
145139
// CHECK: apply [[FUNC]]([[BORROW_EXPLICIT_COPY_X]])
146140
// CHECK: end_borrow [[BORROW_EXPLICIT_COPY_X]]
147141
// CHECK: destroy_value [[EXPLICIT_COPY_X]]
148-
// CHECK: destroy_value [[COPY_X]]
149142
//
150143
// Calling computed getter.
151144
// CHECK: [[X:%.*]] = begin_borrow [[MOVE]]
152-
// CHECK: [[COPY_X:%.*]] = copy_value [[X]]
153-
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[COPY_X]]
145+
// CHECK: [[EXPLICIT_COPY_X:%.*]] = explicit_copy_value [[X]]
154146
// CHECK: [[BORROW_EXPLICIT_COPY_X:%.*]] = begin_borrow [[EXPLICIT_COPY_X]]
155147
// CHECK: [[COPY_BORROW_EXPLICIT_COPY_X:%.*]] = copy_value [[BORROW_EXPLICIT_COPY_X]]
156148
// CHECK: [[FUNC:%.*]] = function_ref @$s9copy_expr12ContainKlassV18consumingComputedKAA0D0Cvg : $@convention(method) (@owned ContainKlass) -> @owned Klass
157149
// CHECK: apply [[FUNC]]([[COPY_BORROW_EXPLICIT_COPY_X]])
158150
// CHECK: end_borrow [[BORROW_EXPLICIT_COPY_X]]
159151
// CHECK: destroy_value [[EXPLICIT_COPY_X]]
160-
// CHECK: destroy_value [[COPY_X]]
161152
// CHECK: } // end sil function '$s9copy_expr31testCallMethodOnLoadableLetCopyyyF'
162153
func testCallMethodOnLoadableLetCopy() {
163154
let x = ContainKlass()

test/SILGen/copy_operator.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ class Klass {}
1111
// CHECK-LABEL: sil [ossa] @$s8moveonly7useCopyyAA5KlassCADF : {{.*}} {
1212
// CHECK: bb0([[ARG:%.*]] :
1313
// CHECK-NEXT: debug_value
14-
// CHECK-NEXT: [[COPY:%[^,]+]] = copy_value [[ARG]]
15-
// CHECK-NEXT: [[EXPLICIT_COPY:%[^,]+]] = explicit_copy_value [[COPY]]
16-
// CHECK-NEXT: destroy_value [[COPY]]
14+
// CHECK-NEXT: [[EXPLICIT_COPY:%[^,]+]] = explicit_copy_value [[ARG]]
1715
// CHECK-NEXT: return [[EXPLICIT_COPY]]
1816
// CHECK-LABEL: } // end sil function '$s8moveonly7useCopyyAA5KlassCADF'
1917

2018
// CHECK-SIL-LABEL: sil @$s8moveonly7useCopyyAA5KlassCADF : {{.*}} {
2119
// CHECK-SIL: bb0([[ARG:%.*]] : $Klass):
2220
// CHECK-SIL-NEXT: debug_value
2321
// CHECK-SIL-NEXT: strong_retain [[ARG]]
24-
// CHECK-SIL-NEXT: strong_retain [[ARG]]
25-
// CHECK-SIL-NEXT: strong_release [[ARG]]
2622
// CHECK-SIL-NEXT: return [[ARG]] : $Klass
2723
// CHECK-SIL-LABEL: } // end sil function '$s8moveonly7useCopyyAA5KlassCADF'
2824

@@ -39,18 +35,14 @@ public func useCopy(_ k: Klass) -> Klass {
3935
// CHECK-LABEL: sil [ossa] @$s8moveonly7useCopyyxxRlzClF : $@convention(thin) <T where T : AnyObject> (@guaranteed T) -> @owned T {
4036
// CHECK: bb0([[ARG:%.*]] :
4137
// CHECK-NEXT: debug_value
42-
// CHECK-NEXT: [[COPY:%[^,]+]] = copy_value [[ARG]]
43-
// CHECK-NEXT: [[EXPLICIT_COPY:%[^,]+]] = explicit_copy_value [[COPY]]
44-
// CHECK-NEXT: destroy_value [[COPY]]
38+
// CHECK-NEXT: [[EXPLICIT_COPY:%[^,]+]] = explicit_copy_value [[ARG]]
4539
// CHECK-NEXT: return [[EXPLICIT_COPY]]
4640
// CHECK-LABEL: } // end sil function '$s8moveonly7useCopyyxxRlzClF'
4741

4842
// CHECK-SIL-LABEL: sil @$s8moveonly7useCopyyxxRlzClF : {{.*}} {
4943
// CHECK-SIL: bb0([[ARG:%.*]] :
5044
// CHECK-SIL-NEXT: debug_value
5145
// CHECK-SIL-NEXT: strong_retain [[ARG]]
52-
// CHECK-SIL-NEXT: strong_retain [[ARG]]
53-
// CHECK-SIL-NEXT: strong_release [[ARG]]
5446
// CHECK-SIL-NEXT: return [[ARG]]
5547
// CHECK-SIL-LABEL: } // end sil function '$s8moveonly7useCopyyxxRlzClF'
5648

0 commit comments

Comments
 (0)