Skip to content
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

[NFC] Add isSSA to LazyLocalGraph, and use it in OptimizeAddedConstants #6952

Merged
merged 5 commits into from
Sep 18, 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
32 changes: 32 additions & 0 deletions src/ir/LocalGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,38 @@ void LazyLocalGraph::computeGetInfluences() const {
doComputeGetInfluences(*locations, *getInfluences);
}

bool LazyLocalGraph::computeSSA(Index index) const {
// We must never repeat work.
assert(!SSAIndexes.count(index));

if (!flower) {
makeFlower();
}

// Similar logic to LocalGraph::computeSSAIndexes(), but optimized for the
// case of a single index.

// All the sets for this index that we've seen. We'll add all relevant ones,
// and exit if we see more than one.
SmallUnorderedSet<LocalSet*, 2> sets;
for (auto* set : flower->setsByIndex[index]) {
sets.insert(set);
if (sets.size() > 1) {
return SSAIndexes[index] = false;
}
}
for (auto* get : flower->getsByIndex[index]) {
for (auto* set : getSets(get)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can looking at the sets for a get find sets that weren't already found by looking directly at the sets above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gets may also be reading from nullptr (the value at the function entry, for which there is no LocalSet).

sets.insert(set);
if (sets.size() > 1) {
return SSAIndexes[index] = false;
}
}
}
// Finally, check that we have 1 and not 0 sets.
return SSAIndexes[index] = (sets.size() == 1);
}

void LazyLocalGraph::computeLocations() const {
// We must never repeat work.
assert(!locations);
Expand Down
15 changes: 15 additions & 0 deletions src/ir/local-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ struct LazyLocalGraph : public LocalGraphBase {
}
return (*getInfluences)[get];
}
bool isSSA(Index index) const {
auto iter = SSAIndexes.find(index);
if (iter == SSAIndexes.end()) {
auto ret = computeSSA(index);
// The result must have been memoized.
assert(SSAIndexes.count(index));
return ret;
}
return iter->second;
}

const Locations& getLocations() const {
if (!locations) {
Expand All @@ -229,6 +239,9 @@ struct LazyLocalGraph : public LocalGraphBase {
// include sets of other indexes, so there is no simple way to lazify that
// computation.
mutable std::optional<GetInfluencesMap> getInfluences;
// A map if indexes to a bool indicating if the index is SSA. If there is no
// entry, it has not yet been computed.
mutable std::unordered_map<Index, bool> SSAIndexes;
mutable std::optional<Locations> locations;

// Compute the sets for a get and store them on getSetsMap.
Expand All @@ -237,6 +250,8 @@ struct LazyLocalGraph : public LocalGraphBase {
void computeSetInfluences(LocalSet* set) const;
// Compute influences for all gets and store them on getInfluences.
void computeGetInfluences() const;
// Compute whether an index is SSA and store that on SSAIndexes.
bool computeSSA(Index index) const;
// Compute locations and store them on getInfluences.
void computeLocations() const;

Expand Down
12 changes: 5 additions & 7 deletions src/passes/OptimizeAddedConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ template<typename P, typename T> class MemoryAccessOptimizer {
MemoryAccessOptimizer(P* parent,
T* curr,
Module* module,
LocalGraph* localGraph)
LazyLocalGraph* localGraph)
: parent(parent), curr(curr), module(module), localGraph(localGraph) {
memory64 = module->getMemory(curr->memory)->is64();
}
Expand Down Expand Up @@ -133,7 +133,7 @@ template<typename P, typename T> class MemoryAccessOptimizer {
P* parent;
T* curr;
Module* module;
LocalGraph* localGraph;
LazyLocalGraph* localGraph;
bool memory64;

void optimizeConstantPointer() {
Expand Down Expand Up @@ -326,9 +326,7 @@ struct OptimizeAddedConstants
helperIndexes.clear();
propagatable.clear();
if (propagate) {
localGraph = std::make_unique<LocalGraph>(func, getModule());
localGraph->computeSetInfluences();
localGraph->computeSSAIndexes();
localGraph = std::make_unique<LazyLocalGraph>(func, getModule());
findPropagatable();
}
Super::doWalkFunction(func);
Expand Down Expand Up @@ -362,7 +360,7 @@ struct OptimizeAddedConstants
private:
bool propagated;

std::unique_ptr<LocalGraph> localGraph;
std::unique_ptr<LazyLocalGraph> localGraph;

// Whether a set is propagatable.
std::set<LocalSet*> propagatable;
Expand All @@ -379,7 +377,7 @@ struct OptimizeAddedConstants
// but if x has other uses, then avoid doing so - we'll be doing that add
// anyhow, so the load/store offset trick won't actually help.
GetParents parents(getFunction()->body);
for (auto& [location, _] : localGraph->locations) {
for (auto& [location, _] : localGraph->getLocations()) {
if (auto* set = location->dynCast<LocalSet>()) {
if (auto* add = set->value->dynCast<Binary>()) {
if (add->op == AddInt32) {
Expand Down
Loading