Skip to content

Commit df8ab6e

Browse files
committed
SILGen: Don't copy_addr [take] trivial address-only values.
This is a new case that comes up with `InlineArray`, since an `InlineArray` with unknown count but known trivial element type is trivial but still address-only due to its unknown size. We are inconsistent about whether we emit formal copies or not of these values; they should generally be unnecessary as long as the memory location of a value is sufficiently long-lived, but the SIL verifier still reasonably considers a `[take]` as an invalidation of the memory, even though at runtime a take is a no-op. Since the take is unnecessary, we can just not take when we copy out of a trivial address location. Fixes #84141 | rdar://160007939.
1 parent 96900f9 commit df8ab6e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5234,7 +5234,9 @@ void SILGenFunction::emitSemanticStore(SILLocation loc,
52345234
assert(!silConv.useLoweredAddresses() ||
52355235
(dest->getType().isAddressOnly(F) == rvalue->getType().isAddress()));
52365236
if (rvalue->getType().isAddress()) {
5237-
B.createCopyAddr(loc, rvalue, dest, IsTake, isInit);
5237+
B.createCopyAddr(loc, rvalue, dest,
5238+
rvalue->getType().isTrivial(F) ? IsNotTake : IsTake,
5239+
isInit);
52385240
} else {
52395241
emitUnloweredStoreOfCopy(B, loc, rvalue, dest, isInit);
52405242
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-emit-silgen -verify -disable-availability-checking %s
2+
3+
public enum StreamYieldResult<let count: Int>: Sendable {
4+
case literal(buffer: InlineArray<count, UInt8>)
5+
case end(buffer: InlineArray<count, UInt8>, endIndex: Int)
6+
7+
public func buffer() -> InlineArray<count, UInt8> {
8+
switch self {
9+
case .literal(let b):
10+
return b
11+
case .end(let b, _):
12+
return b
13+
}
14+
}
15+
}

test/SILGen/variadic-generic-tuples.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public struct Container<each T> {
102102
// Finally, the actual assignment.
103103
// CHECK-NEXT: [[ACCESS:%.*]] = begin_access [modify] [unknown] %1 :
104104
// CHECK-NEXT: [[FIELD:%.*]] = struct_element_addr [[ACCESS]] : $*Container<repeat each T>, #Container.storage
105-
// CHECK-NEXT: copy_addr [take] [[COPY2]] to [[FIELD]] : $*(repeat Stored<each T>)
105+
// CHECK-NEXT: copy_addr [[COPY2]] to [[FIELD]] : $*(repeat Stored<each T>)
106106
// CHECK-NEXT: end_access [[ACCESS]]
107107
// Clean up.
108108
// CHECK-NEXT: dealloc_stack [[COPY2]]

0 commit comments

Comments
 (0)