Skip to content

Commit fe39e69

Browse files
Merge pull request #71138 from nate-chandler/alias-analysis-builtin
[DestroyAddrHoisting] Don't fold for invalid paths.
2 parents b975981 + 7219914 commit fe39e69

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

include/swift/SIL/MemAccessUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ struct AccessPathWithBase {
12721272
// The "product leaves" are the leaves obtained by only looking through type
12731273
// products (structs and tuples) and NOT type sums (enums).
12741274
void visitProductLeafAccessPathNodes(
1275-
SILValue address, TypeExpansionContext tec, SILModule &module,
1275+
AccessPath rootPath, SILValue address, TypeExpansionContext tec,
1276+
SILModule &module,
12761277
std::function<void(AccessPath::PathNode, SILType)> visitor);
12771278

12781279
inline AccessPath AccessPath::compute(SILValue address) {

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,12 @@ AccessPathWithBase AccessPathWithBase::computeInScope(SILValue address) {
14361436
}
14371437

14381438
void swift::visitProductLeafAccessPathNodes(
1439-
SILValue address, TypeExpansionContext tec, SILModule &module,
1439+
AccessPath rootPath, SILValue address, TypeExpansionContext tec,
1440+
SILModule &module,
14401441
std::function<void(AccessPath::PathNode, SILType)> visitor) {
1442+
assert(rootPath.isValid());
1443+
assert(AccessPath::compute(address) == rootPath);
14411444
SmallVector<std::pair<SILType, IndexTrieNode *>, 32> worklist;
1442-
auto rootPath = AccessPath::compute(address);
14431445
auto *node = rootPath.getPathNode().node;
14441446
worklist.push_back({address->getType(), node});
14451447
while (!worklist.empty()) {

lib/SILOptimizer/Transforms/DestroyAddrHoisting.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ bool HoistDestroys::rewriteDestroys(const AccessStorage &storage,
560560
bool HoistDestroys::foldBarrier(SILInstruction *barrier,
561561
const AccessStorage &storage,
562562
const DeinitBarriers &deinitBarriers) {
563+
auto rootPath = AccessPath::compute(storageRoot);
564+
if (!rootPath.isValid()) {
565+
// [invalid_access_path] The access path to storageRoot isn't understood.
566+
// It can't be determined whether all of its leaves have been visited, so
567+
// foldability can't be determined. Bail.
568+
return false;
569+
}
563570

564571
// The load [copy]s which will be folded into load [take]s if folding is
565572
// possible.
@@ -592,7 +599,8 @@ bool HoistDestroys::foldBarrier(SILInstruction *barrier,
592599
// it.
593600
SmallPtrSet<AccessPath::PathNode, 16> trivialLeaves;
594601

595-
visitProductLeafAccessPathNodes(storageRoot, typeExpansionContext, module,
602+
visitProductLeafAccessPathNodes(rootPath, storageRoot, typeExpansionContext,
603+
module,
596604
[&](AccessPath::PathNode node, SILType ty) {
597605
if (ty.isTrivial(*function))
598606
return;
@@ -744,10 +752,16 @@ bool HoistDestroys::checkFoldingBarrier(
744752
// of the root storage which would be folded if folding were possible.
745753
// Find its nontrivial product leaves and remove them from the set of
746754
// leaves of the root storage which we're wating to see.
755+
auto rootPath = AccessPath::compute(address);
756+
// [invalid_access_path] The access path to storageRoot was understood, and
757+
// address has identical storage to its storage. The access path to address
758+
// must be valid.
759+
assert(rootPath.isValid());
760+
747761
bool alreadySawLeaf = false;
748762
bool alreadySawTrivialSubleaf = false;
749763
visitProductLeafAccessPathNodes(
750-
address, typeExpansionContext, module,
764+
rootPath, address, typeExpansionContext, module,
751765
[&](AccessPath::PathNode node, SILType ty) {
752766
if (ty.isTrivial(*function)) {
753767
bool inserted = !trivialLeaves.insert(node).second;

test/SILOptimizer/hoist_destroy_addr.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,3 +1179,23 @@ entry(%addr : $*MoE):
11791179
%retval = tuple ()
11801180
return %retval : $()
11811181
}
1182+
1183+
sil @getPointer : $@convention(thin) () -> Builtin.RawPointer
1184+
1185+
struct Nontrivial {
1186+
var guts: Builtin.AnyObject
1187+
}
1188+
1189+
sil [ossa] @rdar121327964 : $@convention(method) (@owned Nontrivial) -> () {
1190+
bb0(%0 : @owned $Nontrivial):
1191+
%6 = function_ref @getPointer : $@convention(thin) () -> Builtin.RawPointer
1192+
%7 = apply %6() : $@convention(thin) () -> Builtin.RawPointer
1193+
%8 = pointer_to_address %7 : $Builtin.RawPointer to [strict] $*Nontrivial
1194+
%9 = copy_value %0 : $Nontrivial
1195+
%10 = begin_access [modify] [dynamic] %8 : $*Nontrivial
1196+
store %9 to [assign] %10 : $*Nontrivial
1197+
end_access %10 : $*Nontrivial
1198+
destroy_value %0 : $Nontrivial
1199+
%14 = tuple ()
1200+
return %14 : $()
1201+
}

0 commit comments

Comments
 (0)