Skip to content

Commit

Permalink
Rewrite IREquality to use a more compact stack instead of deep recurs…
Browse files Browse the repository at this point in the history
…ion (#8198)

* Rewrite IREquality to use a more compact stack instead of deep recursion

Deletes a bunch of code and speeds up lowering time of local laplacian
with 20 pyramid levels by ~2.5%

* clang-tidy

* Fold in the version of equal in IRMatch.h/cpp

* Add missing switch breaks

* Add missing comments

* Elaborate on why we treat NaNs as equal
  • Loading branch information
abadams authored Apr 18, 2024
1 parent 7994e70 commit 4e0b313
Show file tree
Hide file tree
Showing 10 changed files with 660 additions and 944 deletions.
2 changes: 1 addition & 1 deletion src/Associativity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool associative_op_pattern_match(const Expr &e,
debug(5) << "Adding result: " << iter.first << " -> " << iter.second << "\n";
match.emplace(iter.first, iter.second);
} else {
if (!equal(iter.first, match_iter->first) || !equal(iter.second, match_iter->second)) {
if (iter.first != match_iter->first || !equal(iter.second, match_iter->second)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ int static_sign(const Expr &x) {
return -1;
} else {
Expr zero = make_zero(x.type());
if (equal(const_true(), simplify(x > zero))) {
if (is_const_one(simplify(x > zero))) {
return 1;
} else if (equal(const_true(), simplify(x < zero))) {
} else if (is_const_one(simplify(x < zero))) {
return -1;
}
}
Expand Down
18 changes: 4 additions & 14 deletions src/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,23 @@ class GVN : public IRMutator {
Expr expr;
int use_count = 0;
// All consumer Exprs for which this is the last child Expr.
map<ExprWithCompareCache, int> uses;
map<Expr, int, IRGraphDeepCompare> uses;
Entry(const Expr &e)
: expr(e) {
}
};
vector<std::unique_ptr<Entry>> entries;

map<Expr, int, ExprCompare> shallow_numbering, output_numbering;
map<ExprWithCompareCache, int> leaves;
map<Expr, int, IRGraphDeepCompare> leaves;

int number = -1;

IRCompareCache cache;

GVN()
: number(0), cache(8) {
}
int number = 0;

Stmt mutate(const Stmt &s) override {
internal_error << "Can't call GVN on a Stmt: " << s << "\n";
return Stmt();
}

ExprWithCompareCache with_cache(const Expr &e) {
return ExprWithCompareCache(e, &cache);
}

Expr mutate(const Expr &e) override {
// Early out if we've already seen this exact Expr.
{
Expand All @@ -123,7 +113,7 @@ class GVN : public IRMutator {
// that child has an identical parent to this one.

auto &use_map = number == -1 ? leaves : entries[number]->uses;
auto p = use_map.emplace(with_cache(new_e), (int)entries.size());
auto p = use_map.emplace(new_e, (int)entries.size());
auto iter = p.first;
bool novel = p.second;
if (novel) {
Expand Down
Loading

0 comments on commit 4e0b313

Please sign in to comment.