Skip to content

NFC: [MemAccessUtils] Tweaked API. #71157

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
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
7 changes: 4 additions & 3 deletions include/swift/SIL/MemAccessUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,10 @@ struct AccessPathWithBase {
//
// The "product leaves" are the leaves obtained by only looking through type
// products (structs and tuples) and NOT type sums (enums).
void visitProductLeafAccessPathNodes(
AccessPath rootPath, SILValue address, TypeExpansionContext tec,
SILModule &module,
//
// Returns false if the access path couldn't be computed.
bool visitProductLeafAccessPathNodes(
SILValue address, TypeExpansionContext tec, SILModule &module,
std::function<void(AccessPath::PathNode, SILType)> visitor);

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

void swift::visitProductLeafAccessPathNodes(
AccessPath rootPath, SILValue address, TypeExpansionContext tec,
SILModule &module,
bool swift::visitProductLeafAccessPathNodes(
SILValue address, TypeExpansionContext tec, SILModule &module,
std::function<void(AccessPath::PathNode, SILType)> visitor) {
assert(rootPath.isValid());
assert(AccessPath::compute(address) == rootPath);
auto rootPath = AccessPath::compute(address);
if (!rootPath.isValid()) {
return false;
}
SmallVector<std::pair<SILType, IndexTrieNode *>, 32> worklist;
auto *node = rootPath.getPathNode().node;
worklist.push_back({address->getType(), node});
Expand Down Expand Up @@ -1474,6 +1475,7 @@ void swift::visitProductLeafAccessPathNodes(
visitor(AccessPath::PathNode(node), silType);
}
}
return true;
}

void AccessPath::Index::print(raw_ostream &os) const {
Expand Down
43 changes: 20 additions & 23 deletions lib/SILOptimizer/Transforms/DestroyAddrHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,14 +560,6 @@ bool HoistDestroys::rewriteDestroys(const AccessStorage &storage,
bool HoistDestroys::foldBarrier(SILInstruction *barrier,
const AccessStorage &storage,
const DeinitBarriers &deinitBarriers) {
auto rootPath = AccessPath::compute(storageRoot);
if (!rootPath.isValid()) {
// [invalid_access_path] The access path to storageRoot isn't understood.
// It can't be determined whether all of its leaves have been visited, so
// foldability can't be determined. Bail.
return false;
}

// The load [copy]s which will be folded into load [take]s if folding is
// possible.
llvm::SmallVector<LoadInst *, 4> loads;
Expand Down Expand Up @@ -599,13 +591,19 @@ bool HoistDestroys::foldBarrier(SILInstruction *barrier,
// it.
SmallPtrSet<AccessPath::PathNode, 16> trivialLeaves;

visitProductLeafAccessPathNodes(rootPath, storageRoot, typeExpansionContext,
module,
[&](AccessPath::PathNode node, SILType ty) {
if (ty.isTrivial(*function))
return;
leaves.insert(node);
});
bool succeeded = visitProductLeafAccessPathNodes(
storageRoot, typeExpansionContext, module,
[&](AccessPath::PathNode node, SILType ty) {
if (ty.isTrivial(*function))
return;
leaves.insert(node);
});
if (!succeeded) {
// [invalid_access_path] The access path to storageRoot isn't understood.
// It can't be determined whether all of its leaves have been visited, so
// foldability can't be determined. Bail.
return false;
}

for (auto *instruction = barrier; instruction != nullptr;
instruction = instruction->getPreviousInstruction()) {
Expand Down Expand Up @@ -752,16 +750,10 @@ bool HoistDestroys::checkFoldingBarrier(
// of the root storage which would be folded if folding were possible.
// Find its nontrivial product leaves and remove them from the set of
// leaves of the root storage which we're wating to see.
auto rootPath = AccessPath::compute(address);
// [invalid_access_path] The access path to storageRoot was understood, and
// address has identical storage to its storage. The access path to address
// must be valid.
assert(rootPath.isValid());

bool alreadySawLeaf = false;
bool alreadySawTrivialSubleaf = false;
visitProductLeafAccessPathNodes(
rootPath, address, typeExpansionContext, module,
auto succeeded = visitProductLeafAccessPathNodes(
address, typeExpansionContext, module,
[&](AccessPath::PathNode node, SILType ty) {
if (ty.isTrivial(*function)) {
bool inserted = !trivialLeaves.insert(node).second;
Expand All @@ -771,6 +763,11 @@ bool HoistDestroys::checkFoldingBarrier(
bool erased = leaves.erase(node);
alreadySawLeaf = alreadySawLeaf || !erased;
});
(void)succeeded;
// [invalid_access_path] The access path to storageRoot was understood, and
// address has identical storage to its storage. The access path to address
// must be valid.
assert(succeeded);
if (alreadySawLeaf) {
// We saw this non-trivial product leaf already. That means there are
// multiple load [copy]s or copy_addrs of at least one product leaf
Expand Down