Skip to content

Commit 7f24773

Browse files
authored
Merge pull request #80774 from jckarter/trivial-copy-operator-6.2
[6.2] SILGen: Emit `copy x` on a trivial value as a trivial copy.
2 parents bfe3fe7 + ae09d4d commit 7f24773

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7179,6 +7179,14 @@ RValue RValueEmitter::visitCopyExpr(CopyExpr *E, SGFContext C) {
71797179
auto address = SGF.emitAddressOfLValue(subExpr, std::move(lv));
71807180

71817181
if (subType.isLoadable(SGF.F)) {
7182+
// Trivial types don't undergo any lifetime analysis, so simply load
7183+
// the value.
7184+
if (subType.isTrivial(SGF.F)
7185+
&& !address.getType().isMoveOnlyWrapped()) {
7186+
return RValue(SGF, {SGF.B.createLoadCopy(E, address)},
7187+
subType.getASTType());
7188+
}
7189+
71827190
// Use a formal access load borrow so this closes in the writeback scope
71837191
// above.
71847192
ManagedValue value = SGF.B.createFormalAccessLoadBorrow(E, address);

test/SILGen/copy_expr.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,9 @@ func testCallMethodOnAddressOnlyInOutCopy<T : P>(_ x: inout T) {
439439
_ = (copy x).computedK
440440
_ = (copy x).consumingComputedK
441441
}
442+
443+
struct Trivial: BitwiseCopyable { var x: Int }
444+
445+
func copyTrivial(x: inout Trivial) -> Trivial {
446+
return copy x
447+
}

0 commit comments

Comments
 (0)