Skip to content

Commit 29541fa

Browse files
committed
SILGen: use builtin prepareInitialization instead of zeroInitializer when generating SIL for Builtin.emplace
`zeroInitializer` is blocking optimizations, but it is not needed because the closure of `Builtin.emplace` will initialize the memory anyway. We only need to tell mandatory passes that this memory should be treated as initialized.
1 parent 9052652 commit 29541fa

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,11 +2165,13 @@ static ManagedValue emitBuiltinEmplace(SILGenFunction &SGF,
21652165

21662166
auto buffer = dest->getAddressForInPlaceInitialization(SGF, loc);
21672167

2168-
// Zero-initialize the buffer.
2169-
// Aside from providing a modicum of predictability if the memory isn't
2170-
// actually initialized, this also serves to communicate to DI that the memory
2168+
// Mark the buffer as initializedto communicate to DI that the memory
21712169
// is considered initialized from this point.
2172-
SGF.B.createZeroInitAddr(loc, buffer);
2170+
auto markInit = getBuiltinValueDecl(Ctx, Ctx.getIdentifier("prepareInitialization"));
2171+
SGF.B.createBuiltin(loc, markInit->getBaseIdentifier(),
2172+
SILType::getEmptyTupleType(Ctx),
2173+
SubstitutionMap(),
2174+
buffer);
21732175

21742176
SILValue bufferPtr = SGF.B.createAddressToPointer(loc, buffer,
21752177
SILType::getPrimitiveObjectType(SGF.getASTContext().TheRawPointerType),

test/SILGen/emplace.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Loadable {
1818
// CHECK-LABEL: sil {{.*}} @$s{{.*}}_in_place_loadable
1919
func emplace_in_place_loadable() -> Loadable {
2020
// CHECK: [[TMP:%.*]] = alloc_stack $Loadable
21-
// CHECK-NEXT: builtin "zeroInitializer"([[TMP]])
21+
// CHECK-NEXT: builtin "prepareInitialization"([[TMP]])
2222
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[TMP]]
2323
// CHECK-NEXT: apply {{.*}}([[PTR]])
2424
// CHECK-NEXT: load [take] [[TMP]]
@@ -29,7 +29,7 @@ func emplace_in_place_loadable() -> Loadable {
2929
// CHECK-LABEL: sil {{.*}} @$s{{.*}}_in_place_ao
3030
// CHECK: bb0([[OUT:%.*]] : $*AO):
3131
func emplace_in_place_ao() -> AO {
32-
// CHECK: builtin "zeroInitializer"([[OUT]])
32+
// CHECK: builtin "prepareInitialization"([[OUT]])
3333
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[OUT]]
3434
// CHECK-NEXT: apply [[FN:%.*]]([[PTR]])
3535
// CHECK-NEXT: destroy_value [[FN]]
@@ -42,7 +42,7 @@ func emplace_in_place_ao() -> AO {
4242
// CHECK: bb0([[INOUT:%.*]] : $*Loadable):
4343
func emplace_assign_loadable(_ x: inout Loadable) {
4444
// CHECK: [[TMP:%.*]] = alloc_stack $Loadable
45-
// CHECK-NEXT: builtin "zeroInitializer"([[TMP]])
45+
// CHECK-NEXT: builtin "prepareInitialization"([[TMP]])
4646
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[TMP]]
4747
// CHECK-NEXT: apply {{.*}}([[PTR]])
4848
// CHECK-NEXT: [[RESULT:%.*]] = load [take] [[TMP]]
@@ -57,7 +57,7 @@ func emplace_assign_loadable(_ x: inout Loadable) {
5757
// CHECK: bb0([[INOUT:%.*]] : $*AO):
5858
func emplace_assign_ao(_ x: inout AO) {
5959
// CHECK: [[TMP:%.*]] = alloc_stack $AO
60-
// CHECK-NEXT: builtin "zeroInitializer"([[TMP]])
60+
// CHECK-NEXT: builtin "prepareInitialization"([[TMP]])
6161
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[TMP]]
6262
// CHECK-NEXT: apply {{.*}}([[PTR]])
6363
// CHECK-NEXT: [[WRITE:%.*]] = begin_access [modify] [unknown] [[INOUT]]
@@ -70,7 +70,7 @@ func emplace_assign_ao(_ x: inout AO) {
7070
// CHECK-LABEL: sil {{.*}} @$s{{.*}}_ignore_loadable
7171
func emplace_ignore_loadable() {
7272
// CHECK: [[TMP:%.*]] = alloc_stack $Loadable
73-
// CHECK-NEXT: builtin "zeroInitializer"([[TMP]])
73+
// CHECK-NEXT: builtin "prepareInitialization"([[TMP]])
7474
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[TMP]]
7575
// CHECK-NEXT: apply {{.*}}([[PTR]])
7676
// CHECK-NEXT: [[RESULT:%.*]] = load [take] [[TMP]]
@@ -83,7 +83,7 @@ func emplace_ignore_loadable() {
8383
// CHECK-LABEL: sil {{.*}} @$s{{.*}}_ignore_ao
8484
func emplace_ignore_ao() {
8585
// CHECK: [[TMP:%.*]] = alloc_stack $AO
86-
// CHECK-NEXT: builtin "zeroInitializer"([[TMP]])
86+
// CHECK-NEXT: builtin "prepareInitialization"([[TMP]])
8787
// CHECK-NEXT: [[PTR:%.*]] = address_to_pointer {{.*}} [[TMP]]
8888
// CHECK-NEXT: apply {{.*}}([[PTR]])
8989
// CHECK-NEXT: ignored_use [[TMP]]

0 commit comments

Comments
 (0)