Skip to content

Commit 523a1c7

Browse files
authored
Merge pull request #18969 from atrick/fix-tuple-assign
[SILGen] force immeidate LValue assignment cleanups.
2 parents 47228bc + 7f902f5 commit 523a1c7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,12 +3966,18 @@ void SILGenFunction::emitAssignToLValue(SILLocation loc, RValue &&src,
39663966
void SILGenFunction::emitAssignToLValue(SILLocation loc,
39673967
ArgumentSource &&src,
39683968
LValue &&dest) {
3969-
FormalEvaluationScope scope(*this);
3969+
// Enter a FormalEvaluationScope so that formal access to independent LValue
3970+
// components do not overlap. Furthermore, use an ArgumentScope to force
3971+
// cleanup of materialized LValues immediately, before evaluating the next
3972+
// LValue. For example: (x[i], x[j]) = a, b
3973+
ArgumentScope argScope(*this, loc);
39703974

39713975
// If the last component is a getter/setter component, use a special
39723976
// generation pattern that allows us to peephole the emission of the RHS.
3973-
if (trySetterPeephole(*this, loc, std::move(src), std::move(dest)))
3977+
if (trySetterPeephole(*this, loc, std::move(src), std::move(dest))) {
3978+
argScope.pop();
39743979
return;
3980+
}
39753981

39763982
// Otherwise, force the RHS now to preserve evaluation order.
39773983
auto srcLoc = src.getLocation();
@@ -4008,6 +4014,7 @@ void SILGenFunction::emitAssignToLValue(SILLocation loc,
40084014

40094015
// The writeback scope closing will propagate the value back up through the
40104016
// writeback chain.
4017+
argScope.pop();
40114018
}
40124019

40134020
void SILGenFunction::emitCopyLValueInto(SILLocation loc, LValue &&src,

test/SILGen/tuples.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,26 @@ extension P {
156156
return tuple.value.foo()
157157
}
158158
}
159+
160+
// CHECK-LABEL: sil @$S6tuples15testTupleAssign1xySaySiGz_tF : $@convention(thin) (@inout Array<Int>) -> () {
161+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unknown] %0 : $*Array<Int>
162+
// function_ref Array.subscript.nativeOwningMutableAddressor
163+
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$SSayxSiciao : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
164+
// CHECK: [[TUPLE:%.*]] = apply [[ADDRESSOR]]<Int>(%{{.*}}, [[ACCESS]]) : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
165+
// CHECK: ([[PTR:%.*]], [[OBJ:%.*]]) = destructure_tuple [[TUPLE]] : $(UnsafeMutablePointer<Int>, Builtin.NativeObject)
166+
// CHECK: assign %{{.*}} to %{{.*}} : $*Int
167+
// CHECK: end_access [[ACCESS]] : $*Array<Int>
168+
// CHECK: destroy_value [[OBJ]] : $Builtin.NativeObject
169+
//
170+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unknown] %0 : $*Array<Int>
171+
// function_ref Array.subscript.nativeOwningMutableAddressor
172+
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$SSayxSiciao : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
173+
// CHECK: [[TUPLE:%.*]] = apply [[ADDRESSOR]]<Int>(%{{.*}}, [[ACCESS]]) : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
174+
// CHECK: ([[PTR:%.*]], [[OBJ:%.*]]) = destructure_tuple [[TUPLE]] : $(UnsafeMutablePointer<Int>, Builtin.NativeObject)
175+
// CHECK: assign %{{.*}} to %{{.*}} : $*Int
176+
// CHECK: end_access [[ACCESS]] : $*Array<Int>
177+
// CHECK: destroy_value [[OBJ]] : $Builtin.NativeObject
178+
// CHECK-LABEL: } // end sil function '$S6tuples15testTupleAssign1xySaySiGz_tF'
179+
public func testTupleAssign(x: inout [Int]) {
180+
(x[0], x[1]) = (0, 1)
181+
}

0 commit comments

Comments
 (0)