Skip to content

Commit 5352263

Browse files
committed
[OpaqueValues] Skip temp when setting at key-path.
When setting a value at a key-path, in opaque values mode, don't create a temporary and store the new value into it. That is necessary when using lowered addresses because intrinsic's signature is ``` sil @swift_setAtWritableKeyPath : $@convention(thin) <τ_0_0, τ_0_1> (@inout τ_0_0, @guaranteed WritableKeyPath<τ_0_0, τ_0_1>, @in τ_0_1) -> () ``` but is incorrect in opaque values mode where values are passed directly to `@in` parameters.
1 parent 53da675 commit 5352263

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ namespace {
23032303

23042304
auto setValue =
23052305
std::move(value).getAsSingleValue(SGF, origType, loweredTy);
2306-
if (!setValue.getType().isAddress()) {
2306+
if (SGF.useLoweredAddresses() && !setValue.getType().isAddress()) {
23072307
setValue = setValue.materialize(SGF, loc);
23082308
}
23092309

test/SILGen/opaque_values_silgen.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,18 @@ func giveKeyPathString() {
647647
@backDeployed(before: SwiftStdlib 5.8)
648648
public func backDeployingReturningGeneric<T>(_ t: T) throws -> T { t }
649649
#endif
650+
651+
// CHECK-LABEL: sil {{.*}}[ossa] @SetIntoContainerAtKeyPath : {{.*}} {
652+
// CHECK: bb0([[CONTAINER_ADDR:%[^,]+]] : {{.*}}, [[KP:%[^,]+]] : {{.*}}, [[VALUE:%[^,]+]] :
653+
// CHECK: [[KP_COPY:%[^,]+]] = copy_value [[KP]] : $WritableKeyPath<Container, Field>
654+
// CHECK: [[VALUE_COPY:%[^,]+]] = copy_value [[VALUE]] : $Field
655+
// CHECK: [[CONTAINER_ACCESS:%[^,]+]] = begin_access [modify] [unknown] [[CONTAINER_ADDR]] : $*Container
656+
// CHECK: [[SETTER:%[^,]+]] = function_ref @swift_setAtWritableKeyPath
657+
// CHECK: apply [[SETTER]]<Container, Field>([[CONTAINER_ACCESS]], [[KP_COPY]], [[VALUE_COPY]])
658+
// CHECK: end_access [[CONTAINER_ACCESS]] : $*Container
659+
// CHECK: destroy_value [[KP_COPY]]
660+
// CHECK-LABEL: } // end sil function 'SetIntoContainerAtKeyPath'
661+
@_silgen_name("SetIntoContainerAtKeyPath")
662+
func set<Container, Field>(into container: inout Container, at keyPath: WritableKeyPath<Container, Field>, _ value: Field) {
663+
container[keyPath: keyPath] = value
664+
}

0 commit comments

Comments
 (0)