Skip to content

Commit 1938b5c

Browse files
Merge pull request #62565 from nate-chandler/opaque-values/2/20221213
[OpaqueValues] Emit addr-only patterns.
2 parents 6fff307 + d2af343 commit 1938b5c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,13 @@ void PatternMatchEmission::
24492449
emitAddressOnlyInitialization(VarDecl *dest, SILValue value) {
24502450
auto found = Temporaries.find(dest);
24512451
assert(found != Temporaries.end());
2452-
SGF.B.createCopyAddr(dest, value, found->second, IsNotTake, IsInitialization);
2452+
if (SGF.useLoweredAddresses()) {
2453+
SGF.B.createCopyAddr(dest, value, found->second, IsNotTake,
2454+
IsInitialization);
2455+
return;
2456+
}
2457+
auto copy = SGF.B.createCopyValue(dest, value);
2458+
SGF.B.createStore(dest, copy, found->second, StoreOwnershipQualifier::Init);
24532459
}
24542460

24552461
/// Emit all the shared case statements.

test/SILGen/opaque_values_silgen.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,42 @@ enum TestEnum<T> {
478478
}
479479
}
480480

481+
public enum EnumWithTwoSameAddressOnlyPayloads<T> {
482+
case nope
483+
case yes(T)
484+
case and(T)
485+
486+
// CHECK-LABEL: sil [ossa] @EnumWithTwoSameAddressOnlyPayloads_getPayload : {{.*}} {
487+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
488+
// CHECK: [[RESULT_STORAGE:%[^,]+]] = alloc_stack $T
489+
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
490+
// CHECK: switch_enum [[COPY]] : $EnumWithTwoSameAddressOnlyPayloads<T>,
491+
// CHECK-SAME: case #EnumWithTwoSameAddressOnlyPayloads.nope!enumelt: {{bb[0-9]+}},
492+
// CHECK-SAME: case #EnumWithTwoSameAddressOnlyPayloads.yes!enumelt: [[YES_BLOCK:bb[0-9]+]],
493+
// CHECK-SAME: case #EnumWithTwoSameAddressOnlyPayloads.and!enumelt: [[AND_BLOCK:bb[0-9]+]]
494+
// CHECK: [[YES_BLOCK]]([[YES_VALUE:%[^,]+]] :
495+
// CHECK: [[YES_LIFETIME:%[^,]+]] = begin_borrow [lexical] [[YES_VALUE]]
496+
// CHECK: [[YES_COPY:%[^,]+]] = copy_value [[YES_LIFETIME]]
497+
// CHECK: store [[YES_COPY]] to [init] [[RESULT_STORAGE]]
498+
// CHECK: [[AND_BLOCK]]([[AND_VALUE:%[^,]+]] :
499+
// CHECK: [[AND_LIFETIME:%[^,]+]] = begin_borrow [lexical] [[AND_VALUE]]
500+
// CHECK: [[AND_COPY:%[^,]+]] = copy_value [[AND_LIFETIME]]
501+
// CHECK: store [[AND_COPY]] to [init] [[RESULT_STORAGE]]
502+
// CHECK-LABEL: } // end sil function 'EnumWithTwoSameAddressOnlyPayloads_getPayload'
503+
public var getPayload: T? {
504+
@_silgen_name("EnumWithTwoSameAddressOnlyPayloads_getPayload")
505+
get {
506+
switch self {
507+
case .nope:
508+
return nil
509+
case .yes(let t), .and(let t):
510+
return t
511+
}
512+
}
513+
}
514+
}
515+
516+
481517
// Verify exit block arguments are ordered correctly.
482518
//
483519
// CHECK-LABEL: sil private [ossa] @$s20opaque_values_silgen19duplicate_with_int49condition5valueSi_S2ix_xttSb_xtlFSi_S2ix_xttyXEfU_ : {{.*}} {

0 commit comments

Comments
 (0)