Skip to content

Commit 4a089ea

Browse files
Merge pull request #62322 from nate-chandler/opaque-values/1/20221128
[TypeLowering] Define "copy into" for opaque vals.
2 parents d52a044 + db9103c commit 4a089ea

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/SIL/IR/TypeLowering.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
using namespace swift;
4444
using namespace Lowering;
4545

46+
// Necessary to straightforwardly write SIL tests that exercise
47+
// OpaqueValueTypeLowering (and MoveOnlyOpaqueValueTypeLowering): the tests can
48+
// be written as though opaque values were enabled to begin but have since been
49+
// lowered out of.
50+
llvm::cl::opt<bool> TypeLoweringForceOpaqueValueLowering(
51+
"type-lowering-force-opaque-value-lowering", llvm::cl::init(false),
52+
llvm::cl::desc("Force TypeLowering to behave as if building with opaque "
53+
"values enabled"));
54+
4655
namespace {
4756
/// A CRTP type visitor for deciding whether the metatype for a type
4857
/// is a singleton type, i.e. whether there can only ever be one
@@ -1941,7 +1950,12 @@ namespace {
19411950
void emitCopyInto(SILBuilder &B, SILLocation loc,
19421951
SILValue src, SILValue dest, IsTake_t isTake,
19431952
IsInitialization_t isInit) const override {
1944-
llvm_unreachable("copy into");
1953+
if (B.getModule().useLoweredAddresses()) {
1954+
B.createCopyAddr(loc, src, dest, isTake, isInit);
1955+
} else {
1956+
SILValue value = emitLoadOfCopy(B, loc, src, isTake);
1957+
emitStoreOfCopy(B, loc, value, dest, isInit);
1958+
}
19451959
}
19461960

19471961
// OpaqueValue store cannot be decoupled from a destroy because it is not
@@ -2066,7 +2080,8 @@ namespace {
20662080

20672081
TypeLowering *handleMoveOnlyAddressOnly(CanType type,
20682082
RecursiveProperties properties) {
2069-
if (!TC.Context.SILOpts.EnableSILOpaqueValues) {
2083+
if (!TC.Context.SILOpts.EnableSILOpaqueValues &&
2084+
!TypeLoweringForceOpaqueValueLowering) {
20702085
auto silType = SILType::getPrimitiveAddressType(type);
20712086
return new (TC)
20722087
MoveOnlyAddressOnlyTypeLowering(silType, properties, Expansion);
@@ -2084,7 +2099,8 @@ namespace {
20842099

20852100
TypeLowering *handleAddressOnly(CanType type,
20862101
RecursiveProperties properties) {
2087-
if (!TC.Context.SILOpts.EnableSILOpaqueValues) {
2102+
if (!TC.Context.SILOpts.EnableSILOpaqueValues &&
2103+
!TypeLoweringForceOpaqueValueLowering) {
20882104
auto silType = SILType::getPrimitiveAddressType(type);
20892105
return new (TC) AddressOnlyTypeLowering(silType, properties,
20902106
Expansion);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -address-lowering -diagnostic-constant-propagation -type-lowering-force-opaque-value-lowering | %FileCheck %s
2+
sil_stage canonical
3+
4+
struct G<T> {
5+
var t: T
6+
}
7+
8+
// CHECK-LABEL: sil [ossa] @checked_cast_same_type : {{.*}} {
9+
// CHECK: copy_addr [take]
10+
// CHECK-LABEL: } // end sil function 'checked_cast_same_type'
11+
sil [ossa] @checked_cast_same_type : $@convention(thin) <T> () -> () {
12+
%addr_src = alloc_stack $G<T>
13+
%addr_dest = alloc_stack $G<T>
14+
apply undef<T>(%addr_src) : $@convention(thin) <U> () -> @out G<U>
15+
unconditional_checked_cast_addr G<T> in %addr_src : $*G<T> to G<T> in %addr_dest : $*G<T>
16+
apply undef<T>(%addr_dest) : $@convention(thin) <U> (@in_guaranteed G<U>) -> ()
17+
destroy_addr %addr_dest : $*G<T>
18+
dealloc_stack %addr_dest : $*G<T>
19+
dealloc_stack %addr_src : $*G<T>
20+
%retval = tuple ()
21+
return %retval : $()
22+
}

0 commit comments

Comments
 (0)