Skip to content

Commit

Permalink
TypeAnalysis speed up Canonicalize (#1747)
Browse files Browse the repository at this point in the history
* TypeAnalysis speed up Canonicalize

* replace old impl
  • Loading branch information
wsmoses authored Feb 21, 2024
1 parent d4cb8b3 commit 638ac37
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions enzyme/Enzyme/TypeAnalysis/TypeTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,22 +702,22 @@ class TypeTree : public std::enable_shared_from_this<TypeTree> {
staging[next][pair.second].insert(pair.first[0]);
}

mapping.clear();
// TypeTree mappings which did not get combined
std::map<const std::vector<int>, ConcreteType> unCombinedToAdd;

for (auto &pair : staging) {
// TypeTree mappings which did get combined into an outer -1
std::map<const std::vector<int>, ConcreteType> combinedToAdd;

for (const auto &pair : staging) {
auto &pnext = pair.first;
for (auto &pair2 : pair.second) {
for (const auto &pair2 : pair.second) {
auto dt = pair2.first;
const auto &set = pair2.second;

// llvm::errs() << " - set: {";
// for(auto s : set) llvm::errs() << s << ", ";
// llvm::errs() << "} len=" << len << "\n";

bool legalCombine = set.count(-1);
bool legalCombine = false;

// See if we can canonicalize the outermost index into a -1
if (!legalCombine) {
if (!set.count(-1)) {
size_t chunk = 1;
if (pnext.size() > 0) {
chunk = dl.getPointerSizeInBits() / 8;
Expand Down Expand Up @@ -745,15 +745,38 @@ class TypeTree : public std::enable_shared_from_this<TypeTree> {
next.push_back(v);

if (legalCombine) {
insert(next, dt, /*intsAreLegalPointerSub*/ true);
combinedToAdd.emplace(next, dt);
} else {
for (auto e : set) {
next[0] = e;
insert(next, dt);
unCombinedToAdd.emplace(next, dt);
}
}
}
}

// If we combined nothing, just return since there are no
// changes.
if (combinedToAdd.size() == 0) {
return;
}

// Non-combined ones do not conflict, since they were already in
// a TT which we can assume contained no conflicts.
mapping = std::move(unCombinedToAdd);
minIndices[0] = -1;

// Fusing several terms into a minus one can create a conflict
// if the prior minus one was already in the map
// time, or also generated by fusion.
// E.g. {-1:Anything, [0]:Pointer} on 8 -> create a [-1]:Pointer
// which conflicts
// Alternatively [-1,-1,-1]:Pointer, and generated a [-1,0,-1] fusion
for (const auto &pair : combinedToAdd) {
insert(pair.first, pair.second);
}

return;
}

/// Keep only pointers (or anything's) to a repeated value (represented by -1)
Expand Down

0 comments on commit 638ac37

Please sign in to comment.