Skip to content

Commit c1258f5

Browse files
committed
SILGen: use builtin prepareInitialization instead of zeroInitializer when generating a value constructor for an empty non-copyable type
`zeroInitializer` is blocking optimizations. It is not needed because the constructor will initialize the memory anyway. We only need to tell mandatory passes that this memory should be treated as initialized.
1 parent adc5d67 commit c1258f5

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/SILGen/SILGenConstructor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,13 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
738738
// move only structs are non-trivial, so we need to handle this here.
739739
if (nominal->getAttrs().hasAttribute<RawLayoutAttr>()) {
740740
// Raw memory is not directly decomposable, but we still want to mark
741-
// it as initialized. Use a zero initializer.
742-
B.createZeroInitAddr(ctor, selfLV.getLValueAddress());
741+
// it as initialized.
742+
auto prepInit = getBuiltinValueDecl(getASTContext(),
743+
getASTContext().getIdentifier("prepareInitialization"));
744+
B.createBuiltin(ctor, prepInit->getBaseIdentifier(),
745+
SILType::getEmptyTupleType(getASTContext()),
746+
SubstitutionMap(),
747+
selfLV.getLValueAddress());
743748
} else if (isa<StructDecl>(nominal)
744749
&& lowering.getLoweredType().isMoveOnly()
745750
&& nominal->getStoredProperties().empty()) {

test/SILOptimizer/moveonly_raw_layout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Lock: ~Copyable {
2020
// CHECK-NEXT: sil{{.*}} @[[INIT:\$.*4LockV.*fC]] :
2121
init() {
2222
// CHECK-NOT: destroy_addr
23-
// CHECK: builtin "zeroInitializer"({{%.*}} : $*Lock)
23+
// CHECK: builtin "prepareInitialization"({{%.*}} : $*Lock)
2424
// CHECK-NOT: destroy_addr
2525
// CHECK: [[F:%.*]] = function_ref @init_lock
2626
// CHECK: apply [[F]](

test/SILOptimizer/stdlib/Cell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct Cell<T: ~Copyable>: ~Copyable {
2020

2121
// CHECK-LABEL: sil {{.*}} @$s4CellAAVAARi_zrlEyAByxGxcfC : $@convention(method) <T where T : ~Copyable> (@in T, @thin Cell<T>.Type) -> @out Cell<T> {
2222
// CHECK: bb0({{%.*}} : $*Cell<T>, [[VALUE:%.*]] : $*T, {{%.*}} : $@thin Cell<T>.Type):
23-
// CHECK: {{%.*}} = builtin "zeroInitializer"([[SELF:%.*]] : $*Cell<T>) : $()
23+
// CHECK: {{%.*}} = builtin "prepareInitialization"([[SELF:%.*]] : $*Cell<T>) : $()
2424
// CHECK-NEXT: [[RAW_LAYOUT_ADDR:%.*]] = builtin "addressOfRawLayout"<Cell<T>>([[SELF]] : $*Cell<T>) : $Builtin.RawPointer
2525
// CHECK-NEXT: [[POINTER:%.*]] = struct $UnsafeMutablePointer<T> ([[RAW_LAYOUT_ADDR]] : $Builtin.RawPointer)
2626
// Calling 'UnsafeMutablePointer<T>.initialize(to:)'

0 commit comments

Comments
 (0)