Skip to content

Commit 4504065

Browse files
committed
Tweak the emission of _ = X to not emit a temporary.
1 parent 94dc4a6 commit 4504065

File tree

5 files changed

+14
-27
lines changed

5 files changed

+14
-27
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,6 +4314,19 @@ static void emitSimpleAssignment(SILGenFunction &SGF, SILLocation loc,
43144314
// also prevents us from getting into that case.
43154315
if (dest->getType()->isEqual(srcLoad->getSubExpr()->getType())) {
43164316
assert(!dest->getType()->is<TupleType>());
4317+
4318+
dest = dest->getSemanticsProvidingExpr();
4319+
if (isa<DiscardAssignmentExpr>(dest)) {
4320+
// The logical thing to do here would be emitIgnoredExpr, but that
4321+
// changed some test results in a way I wanted to avoid, so instead
4322+
// we're doing this.
4323+
FormalEvaluationScope writeback(SGF);
4324+
auto srcLV = SGF.emitLValue(srcLoad->getSubExpr(),
4325+
SGFAccessKind::IgnoredRead);
4326+
(void) SGF.emitLoadOfLValue(loc, std::move(srcLV), SGFContext());
4327+
return;
4328+
}
4329+
43174330
FormalEvaluationScope writeback(SGF);
43184331
auto destLV = SGF.emitLValue(dest, SGFAccessKind::Write);
43194332
auto srcLV = SGF.emitLValue(srcLoad->getSubExpr(),

test/SILGen/class_bound_protocols.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ func takesInheritsMutatingMethod(x: inout InheritsMutatingMethod,
169169
// CHECK-NEXT: dealloc_stack [[TEMPORARY]] : $*@opened("{{.*}}") InheritsMutatingMethod
170170
x.mutateMe()
171171

172-
// CHECK-NEXT: [[RESULT_BOX:%.*]] = alloc_stack $Value
173-
// CHECK-NEXT: [[RESULT:%.*]] = mark_uninitialized [var] [[RESULT_BOX]] : $*Value
174172
// CHECK-NEXT: [[X_ADDR:%.*]] = begin_access [read] [unknown] %0 : $*InheritsMutatingMethod
175173
// CHECK-NEXT: [[X_VALUE:%.*]] = load [copy] [[X_ADDR]] : $*InheritsMutatingMethod
176174
// CHECK-NEXT: [[X_PAYLOAD:%.*]] = open_existential_ref [[X_VALUE]] : $InheritsMutatingMethod to $@opened("{{.*}}") InheritsMutatingMethod
@@ -192,9 +190,7 @@ func takesInheritsMutatingMethod(x: inout InheritsMutatingMethod,
192190
// CHECK-NEXT: end_borrow
193191
// CHECK-NEXT: destroy_addr
194192
// CHECK-NEXT: end_access [[X_ADDR]] : $*InheritsMutatingMethod
195-
// CHECK-NEXT: assign [[RESULT_VALUE]] to [[RESULT]] : $*Value
196193
// CHECK-NEXT: dealloc_stack [[TEMPORARY]] : $*@opened("{{.*}}") InheritsMutatingMethod
197-
// CHECK-NEXT: dealloc_stack [[RESULT_BOX]] : $*Value
198194
_ = x.mutatingCounter
199195

200196
// CHECK-NEXT: [[X_ADDR:%.*]] = begin_access [modify] [unknown] %0 : $*InheritsMutatingMethod

test/SILGen/lifetime.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -768,15 +768,3 @@ func tuple_explosion() {
768768
// CHECK-NOT: tuple_extract [[TUPLE]]
769769
// CHECK-NOT: destroy_value [[TUPLE]]
770770
}
771-
772-
class C {
773-
var v = ""
774-
// CHECK-LABEL: sil hidden @$s8lifetime1CC18ignored_assignment{{[_0-9a-zA-Z]*}}F
775-
func ignored_assignment() {
776-
// CHECK: [[STRING:%.*]] = alloc_stack $String
777-
// CHECK: [[UNINIT:%.*]] = mark_uninitialized [var] [[STRING]]
778-
// CHECK: assign {{%.*}} to [[UNINIT]]
779-
// CHECK: destroy_addr [[UNINIT]]
780-
_ = self.v
781-
}
782-
}

test/SILGen/properties.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,9 +1211,7 @@ protocol NonmutatingProtocol {
12111211
}
12121212

12131213
// sil hidden @$s10properties19overlappingLoadExpr1cyAA13ReferenceTypeCz_tF : $@convention(thin) (@inout ReferenceType) -> () {
1214-
// CHECK: [[RESULT:%.*]] = alloc_stack $Int
1215-
// CHECK-NEXT: [[UNINIT:%.*]] = mark_uninitialized [var] [[RESULT]] : $*Int
1216-
// CHECK-NEXT: [[C_INOUT:%.*]] = begin_access [read] [unknown] %0 : $*ReferenceType
1214+
// CHECK: [[C_INOUT:%.*]] = begin_access [read] [unknown] %0 : $*ReferenceType
12171215
// CHECK-NEXT: [[C:%.*]] = load [copy] [[C_INOUT:%.*]] : $*ReferenceType
12181216
// CHECK-NEXT: end_access [[C_INOUT]] : $*ReferenceType
12191217
// CHECK-NEXT: [[C_BORROW:%.*]] = begin_borrow [[C]]
@@ -1231,13 +1229,11 @@ protocol NonmutatingProtocol {
12311229
// CHECK-NEXT: [[GETTER:%.*]] = witness_method $@opened("{{.*}}") NonmutatingProtocol, #NonmutatingProtocol.x!getter.1 : <Self where Self : NonmutatingProtocol> (Self) -> () -> Int, [[C_FIELD_PAYLOAD]] : $*@opened("{{.*}}") NonmutatingProtocol : $@convention(witness_method: NonmutatingProtocol) <τ_0_0 where τ_0_0 : NonmutatingProtocol> (@in_guaranteed τ_0_0) -> Int
12321230
// CHECK-NEXT: [[RESULT_VALUE:%.*]] = apply [[GETTER]]<@opened("{{.*}}") NonmutatingProtocol>([[C_FIELD_BORROW]]) : $@convention(witness_method: NonmutatingProtocol) <τ_0_0 where τ_0_0 : NonmutatingProtocol> (@in_guaranteed τ_0_0) -> Int
12331231
// CHECK-NEXT: destroy_addr [[C_FIELD_BORROW]]
1234-
// CHECK-NEXT: assign [[RESULT_VALUE]] to [[UNINIT]] : $*Int
12351232
// CHECK-NEXT: destroy_addr [[C_FIELD_COPY]] : $*@opened("{{.*}}") NonmutatingProtocol
12361233
// CHECK-NEXT: dealloc_stack [[C_FIELD_BORROW]]
12371234
// CHECK-NEXT: dealloc_stack [[C_FIELD_COPY]] : $*@opened("{{.*}}") NonmutatingProtocol
12381235
// CHECK-NEXT: destroy_addr [[C_FIELD_BOX]] : $*NonmutatingProtocol
12391236
// CHECK-NEXT: dealloc_stack [[C_FIELD_BOX]] : $*NonmutatingProtocol
1240-
// CHECK-NEXT: dealloc_stack [[RESULT]] : $*Int
12411237
// CHECK-NEXT: tuple ()
12421238
// CHECK-NEXT: return
12431239

test/SILOptimizer/access_marker_verify.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,10 @@ func testCaptureLocal() -> ()->() {
223223
// CHECK: end_access
224224
// CHECK: [[CAPTURE:%.*]] = copy_value %0 : ${ var Int }
225225
// CHECK: partial_apply [callee_guaranteed] %{{.*}}([[CAPTURE]]) : $@convention(thin) (@guaranteed { var Int }) -> ()
226-
// CHECK: alloc_stack $Int
227-
// CHECK: [[UNINIT:%.*]] = mark_uninitialized [var]
228226
// CHECK: begin_access [read] [unknown] [[PROJ]]
229227
// CHECK: [[VAL:%.*]] = load [trivial]
230228
// CHECK: end_access
231229
// CHECK-NOT: begin_access
232-
// CHECK: assign [[VAL]] to [[UNINIT]] : $*Int
233230
// CHECK: return {{.*}} : $@callee_guaranteed () -> ()
234231
// CHECK-LABEL: } // end sil function '$s20access_marker_verify16testCaptureLocalyycyF'
235232

@@ -948,15 +945,12 @@ func testLocalExistential() {
948945
// CHECK: partial_apply [callee_guaranteed] %{{.*}}([[PROJ]]) : $@convention(thin) (@inout_aliasable P) -> ()
949946
// CHECK-NOT: begin_access
950947
// CHECK: apply
951-
// CHECK: [[TMP:%.*]] = alloc_stack $P
952-
// CHECK: [[UNINIT:%.*]] = mark_uninitialized [var] [[TMP]] : $*P
953948
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[PROJ]] : $*P
954949
// CHECK: [[COPY:%.*]] = alloc_stack $P
955950
// CHECK-NOT: begin_access
956951
// CHECK: copy_addr [[ACCESS]] to [initialization] [[COPY]] : $*P
957952
// CHECK: end_access
958953
// CHECK-NOT: begin_access
959-
// CHECK: copy_addr [take] [[COPY]] to [[UNINIT]] : $*P
960954
// CHECK-LABEL: } // end sil function '$s20access_marker_verify20testLocalExistentialyyF'
961955

962956
// --- address-only argument.

0 commit comments

Comments
 (0)