Skip to content

Commit bf666d1

Browse files
Merge pull request #67687 from nate-chandler/opaque-values/20230802/1/no-temporary-for-lvalue-keypath-base
[OpaqueValues] Skip temporary when loading l-value base.
2 parents a11baaf + 4c6fc1a commit bf666d1

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,10 +2180,15 @@ static ManagedValue
21802180
makeBaseConsumableMaterializedRValue(SILGenFunction &SGF,
21812181
SILLocation loc, ManagedValue base) {
21822182
if (base.isLValue()) {
2183-
auto tmp = SGF.emitTemporaryAllocation(loc, base.getType());
2184-
SGF.B.createCopyAddr(loc, base.getValue(), tmp,
2185-
IsNotTake, IsInitialization);
2186-
return SGF.emitManagedBufferWithCleanup(tmp);
2183+
if (SGF.useLoweredAddresses()) {
2184+
auto tmp = SGF.emitTemporaryAllocation(loc, base.getType());
2185+
SGF.B.createCopyAddr(loc, base.getValue(), tmp, IsNotTake,
2186+
IsInitialization);
2187+
return SGF.emitManagedBufferWithCleanup(tmp);
2188+
}
2189+
return SGF.emitLoad(loc, base.getValue(),
2190+
SGF.getTypeLowering(base.getType()), SGFContext(),
2191+
IsNotTake);
21872192
}
21882193

21892194
bool isBorrowed = base.isPlusZeroRValueOrTrivial()

test/SILGen/opaque_values_silgen.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class C {}
88

9+
struct MyInt {
10+
var int: Int
11+
}
12+
913
func genericInout<T>(_: inout T) {}
1014

1115
func hasVarArg(_ args: Any...) {}
@@ -678,3 +682,20 @@ func FormClassKeyPath() {
678682
}
679683
_ = \Q.q
680684
}
685+
686+
// CHECK-LABEL: sil {{.*}}[ossa] @UseGetterOnInout : {{.*}} {
687+
// CHECK: bb0([[CONTAINER_ADDR:%[^,]+]] :
688+
// CHECK: [[KEYPATH:%[^,]+]] = keypath $WritableKeyPath<MyInt, Int>, (root $MyInt; stored_property #MyInt.int : $Int)
689+
// CHECK: [[CONTAINER_ACCESS:%[^,]+]] = begin_access [read] [unknown] [[CONTAINER_ADDR]]
690+
// CHECK: [[KEYPATH_UP:%[^,]+]] = upcast [[KEYPATH]]
691+
// CHECK: [[CONTAINER:%[^,]+]] = load [trivial] [[CONTAINER_ACCESS]]
692+
// CHECK: [[GETTER:%[^,]+]] = function_ref @swift_getAtKeyPath
693+
// CHECK: [[VALUE:%[^,]+]] = apply [[GETTER]]<MyInt, Int>([[CONTAINER]], [[KEYPATH_UP]])
694+
// CHECK: end_access [[CONTAINER_ACCESS]]
695+
// CHECK: destroy_value [[KEYPATH_UP]]
696+
// CHECK: return [[VALUE]] : $Int
697+
// CHECK-LABEL: } // end sil function 'UseGetterOnInout'
698+
@_silgen_name("UseGetterOnInout")
699+
func getInout(_ i: inout MyInt) -> Int {
700+
return i[keyPath: \MyInt.int]
701+
}

0 commit comments

Comments
 (0)