Skip to content

[IRGen] Optimize Builtin.arrayDestroy when count is 1 #72636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,17 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
ptr = IGF.Builder.CreateBitCast(ptr,
valueTy.second.getStorageType()->getPointerTo());
Address array = valueTy.second.getAddressForPointer(ptr);

// If the count is statically known to be a constant 1, then just call the
// type's destroy instead of the array variant.
if (auto ci = dyn_cast<llvm::ConstantInt>(count)) {
if (ci->isOne()) {
bool isOutlined = false;
valueTy.second.destroy(IGF, array, valueTy.first, isOutlined);
return;
}
}

valueTy.second.destroyArray(IGF, array, count, valueTy.first);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/UnsafePointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,8 @@ extension UnsafeMutablePointer where Pointee: ~Copyable {
@discardableResult
public func deinitialize(count: Int) -> UnsafeMutableRawPointer {
_debugPrecondition(count >= 0, "UnsafeMutablePointer.deinitialize with negative count")
// TODO: IRGen optimization when `count` value is statically known to be 1,
// then call `Builtin.destroy(Pointee.self, _rawValue)` instead.
// Note: When count is statically known to be 1 the compiler will optimize
// away a call to swift_arrayDestroy into the type's specific destroy.
Builtin.destroyArray(Pointee.self, _rawValue, count._builtinWordValue)
return UnsafeMutableRawPointer(self)
}
Expand Down
20 changes: 20 additions & 0 deletions test/IRGen/builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,26 @@ func destroyGenArray<T>(_ array: Builtin.RawPointer, count: Builtin.Word, _: T)
Builtin.destroyArray(T.self, array, count)
}

// CHECK-LABEL: define hidden {{.*}}void @"$s8builtins21destroyArraySinglePODyyBpF"(ptr %0)
// CHECK-NOT: call void @swift_arrayDestroy
func destroyArraySinglePOD(_ array: Builtin.RawPointer) {
Builtin.destroyArray(Int.self, array, 1._builtinWordValue)
}

// CHECK-LABEL: define hidden {{.*}}void @"$s8builtins24destroyArraySingleNonPODyyBpF"(ptr %0)
// CHECK-NOT: call void @swift_arrayDestroy
// CHECK: [[TO_DESTROY:%.*]] = load ptr, ptr {{%.*}}
// CHECK: call void @swift_release(ptr [[TO_DESTROY]])
func destroyArraySingleNonPOD(_ array: Builtin.RawPointer) {
Builtin.destroyArray(C.self, array, 1._builtinWordValue)
}

// CHECK-LABEL: define hidden {{.*}}void @"$s8builtins21destroyArraySingleGenyyBp_xmtlF"(ptr %0, ptr %1, ptr %T)
// CHECK-NOT: call void @swift_arrayDestroy
// CHECK: call void {{%.*}}(ptr {{.*}} {{%.*}}, ptr %T)
func destroyArraySingleGen<T>(_ array: Builtin.RawPointer, _: T.Type) {
Builtin.destroyArray(T.self, array, 1._builtinWordValue)
}

// CHECK-LABEL: define hidden {{.*}}void @"$s8builtins12copyPODArray{{[_0-9a-zA-Z]*}}F"(ptr %0, ptr %1, i64 %2)
// check: mul nuw i64 4, %2
Expand Down