Skip to content

[SILGen] force immeidate LValue assignment cleanups. #18969

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 1 commit into from
Aug 25, 2018
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
11 changes: 9 additions & 2 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3966,12 +3966,18 @@ void SILGenFunction::emitAssignToLValue(SILLocation loc, RValue &&src,
void SILGenFunction::emitAssignToLValue(SILLocation loc,
ArgumentSource &&src,
LValue &&dest) {
FormalEvaluationScope scope(*this);
// Enter a FormalEvaluationScope so that formal access to independent LValue
// components do not overlap. Furthermore, use an ArgumentScope to force
// cleanup of materialized LValues immediately, before evaluating the next
// LValue. For example: (x[i], x[j]) = a, b
ArgumentScope argScope(*this, loc);

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

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

// The writeback scope closing will propagate the value back up through the
// writeback chain.
argScope.pop();
}

void SILGenFunction::emitCopyLValueInto(SILLocation loc, LValue &&src,
Expand Down
23 changes: 23 additions & 0 deletions test/SILGen/tuples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,26 @@ extension P {
return tuple.value.foo()
}
}

// CHECK-LABEL: sil @$S6tuples15testTupleAssign1xySaySiGz_tF : $@convention(thin) (@inout Array<Int>) -> () {
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unknown] %0 : $*Array<Int>
// function_ref Array.subscript.nativeOwningMutableAddressor
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$SSayxSiciao : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
// CHECK: [[TUPLE:%.*]] = apply [[ADDRESSOR]]<Int>(%{{.*}}, [[ACCESS]]) : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
// CHECK: ([[PTR:%.*]], [[OBJ:%.*]]) = destructure_tuple [[TUPLE]] : $(UnsafeMutablePointer<Int>, Builtin.NativeObject)
// CHECK: assign %{{.*}} to %{{.*}} : $*Int
// CHECK: end_access [[ACCESS]] : $*Array<Int>
// CHECK: destroy_value [[OBJ]] : $Builtin.NativeObject
//
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unknown] %0 : $*Array<Int>
// function_ref Array.subscript.nativeOwningMutableAddressor
// CHECK: [[ADDRESSOR:%.*]] = function_ref @$SSayxSiciao : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
// CHECK: [[TUPLE:%.*]] = apply [[ADDRESSOR]]<Int>(%{{.*}}, [[ACCESS]]) : $@convention(method) <τ_0_0> (Int, @inout Array<τ_0_0>) -> (UnsafeMutablePointer<τ_0_0>, @owned Builtin.NativeObject)
// CHECK: ([[PTR:%.*]], [[OBJ:%.*]]) = destructure_tuple [[TUPLE]] : $(UnsafeMutablePointer<Int>, Builtin.NativeObject)
// CHECK: assign %{{.*}} to %{{.*}} : $*Int
// CHECK: end_access [[ACCESS]] : $*Array<Int>
// CHECK: destroy_value [[OBJ]] : $Builtin.NativeObject
// CHECK-LABEL: } // end sil function '$S6tuples15testTupleAssign1xySaySiGz_tF'
public func testTupleAssign(x: inout [Int]) {
(x[0], x[1]) = (0, 1)
}