Skip to content

[DestroyAddrHoisting] Don't destructure NE aggs. #81854

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 3 commits into from
May 30, 2025
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
2 changes: 1 addition & 1 deletion include/swift/SIL/MemAccessUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ struct AccessPathWithBase {
//
// Returns false if the access path couldn't be computed.
bool visitProductLeafAccessPathNodes(
SILValue address, TypeExpansionContext tec, SILModule &module,
SILValue address, TypeExpansionContext tec, SILFunction &function,
std::function<void(AccessPath::PathNode, SILType)> visitor);

inline AccessPath AccessPath::compute(SILValue address) {
Expand Down
8 changes: 5 additions & 3 deletions lib/SIL/Utils/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ AccessPathWithBase AccessPathWithBase::computeInScope(SILValue address) {
}

bool swift::visitProductLeafAccessPathNodes(
SILValue address, TypeExpansionContext tec, SILModule &module,
SILValue address, TypeExpansionContext tec, SILFunction &function,
std::function<void(AccessPath::PathNode, SILType)> visitor) {
auto rootPath = AccessPath::compute(address);
if (!rootPath.isValid()) {
Expand All @@ -1461,15 +1461,17 @@ bool swift::visitProductLeafAccessPathNodes(
visitor(AccessPath::PathNode(node), silType);
continue;
}
if (decl->isCxxNonTrivial()) {
if (decl->isCxxNonTrivial() || !silType.isEscapable(function) ||
silType.isMoveOnly()) {
visitor(AccessPath::PathNode(node), silType);
continue;
}
unsigned index = 0;
for (auto *field : decl->getStoredProperties()) {
auto *fieldNode = node->getChild(index);
worklist.push_back(
{silType.getFieldType(field, module, tec), fieldNode});
{silType.getFieldType(field, function.getModule(), tec),
fieldNode});
++index;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Transforms/DestroyAddrHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ bool HoistDestroys::foldBarrier(SILInstruction *barrier,
SmallPtrSet<AccessPath::PathNode, 16> trivialLeaves;

bool succeeded = visitProductLeafAccessPathNodes(
storageRoot, typeExpansionContext, module,
storageRoot, typeExpansionContext, *function,
[&](AccessPath::PathNode node, SILType ty) {
if (ty.isTrivial(*function))
return;
Expand Down Expand Up @@ -765,7 +765,7 @@ bool HoistDestroys::checkFoldingBarrier(
bool alreadySawLeaf = false;
bool alreadySawTrivialSubleaf = false;
auto succeeded = visitProductLeafAccessPathNodes(
address, typeExpansionContext, module,
address, typeExpansionContext, *function,
[&](AccessPath::PathNode node, SILType ty) {
if (ty.isTrivial(*function)) {
bool inserted = !trivialLeaves.insert(node).second;
Expand Down
60 changes: 58 additions & 2 deletions test/SILOptimizer/hoist_destroy_addr.sil
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// RUN: %target-sil-opt -sil-print-types -opt-mode=none -enable-sil-verify-all %s -compute-side-effects -destroy-addr-hoisting | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECKDEB --check-prefix=CHECK-DEB
// RUN: %target-sil-opt -sil-print-types -opt-mode=speed -enable-sil-verify-all %s -compute-side-effects -destroy-addr-hoisting | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECKOPT --check-prefix=CHECK-OPT
// RUN: %target-sil-opt -sil-print-types -enable-experimental-feature LifetimeDependence -opt-mode=none -enable-sil-verify-all %s -compute-side-effects -destroy-addr-hoisting | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECKDEB --check-prefix=CHECK-DEB
// RUN: %target-sil-opt -sil-print-types -enable-experimental-feature LifetimeDependence -opt-mode=speed -enable-sil-verify-all %s -compute-side-effects -destroy-addr-hoisting | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECKOPT --check-prefix=CHECK-OPT
//
// TODO: migrate the remaining tests from destroy_hoisting.sil.

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence

sil_stage canonical

Expand Down Expand Up @@ -1224,3 +1225,58 @@ bb0(%0 : @owned $Nontrivial):
%14 = tuple ()
return %14 : $()
}

struct Regular {
var storage: Builtin.NativeObject
}
struct NEGutless: ~Escapable {
let ptr: UnsafeRawPointer?
}
struct NEMarker : ~Escapable {}
struct NEAggregate: ~Escapable {
var ne: NEGutless
let regular: Regular

@lifetime(copy ne) init(ne: NEMarker)
}

// CHECK-LABEL: sil [ossa] @no_destructure_nonescapable : {{.*}} {
// CHECK: bb0([[AGGREGATE:%[^,]+]] :
// CHECK: destroy_addr [[AGGREGATE]]
// CHECK-LABEL: } // end sil function 'no_destructure_nonescapable'
sil [ossa] @no_destructure_nonescapable : $@convention(method) (@in NEAggregate) -> () {
bb0(%aggregate : $*NEAggregate):
%regular = struct_element_addr %aggregate, #NEAggregate.regular
%regular_copy = alloc_stack $Regular
copy_addr %regular to [init] %regular_copy
destroy_addr %regular_copy
dealloc_stack %regular_copy
destroy_addr %aggregate
%retval = tuple ()
return %retval
}

struct NCGutless: ~Copyable {
let ptr: UnsafeRawPointer?
}
struct NCAggregate: ~Copyable {
var ne: NCGutless
let regular: Regular
deinit {}
}

// CHECK-LABEL: sil [ossa] @no_destructure_noncopyable : {{.*}} {
// CHECK: bb0([[AGGREGATE:%[^,]+]] :
// CHECK: destroy_addr [[AGGREGATE]]
// CHECK-LABEL: } // end sil function 'no_destructure_noncopyable'
sil [ossa] @no_destructure_noncopyable : $@convention(method) (@in NCAggregate) -> () {
bb0(%aggregate : $*NCAggregate):
%regular = struct_element_addr %aggregate, #NCAggregate.regular
%regular_copy = alloc_stack $Regular
copy_addr %regular to [init] %regular_copy
destroy_addr %regular_copy
dealloc_stack %regular_copy
destroy_addr %aggregate
%retval = tuple ()
return %retval
}