Skip to content

Commit

Permalink
Remove duplicate let values (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsharlet authored Jan 15, 2025
1 parent 72d14c1 commit 2cba9a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions builder/simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,20 @@ class simplifier : public node_mutator {
std::vector<sv_type> scoped_values;
scoped_values.reserve(op->lets.size());

std::map<expr, var, node_less> reverse_lets;

bool values_changed = false;
for (const auto& s : op->lets) {
expr_info value_info;
expr value = mutate(s.second, &value_info);

var& name = reverse_lets[value];
if (name.defined()) {
// This value is the same as another let value, use that variable instead.
value = expr(name);
} else {
name = s.first;
}
if (should_substitute(value)) {
value_info = expr_info::substitution(std::move(value));
values_changed = true;
Expand Down
3 changes: 3 additions & 0 deletions builder/test/simplify/simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ TEST(simplify, let) {
ASSERT_THAT(simplify(let::make({{x, y * 2}, {z, y}}, z)), matches(y));
ASSERT_THAT(simplify(let::make({{x, y}, {z, (x + 1) / x}}, (z + 1) / z)),
matches(let::make({{z, (y + 1) / y}}, (z + 1) / z)));

// Duplicate lets
ASSERT_THAT(simplify(let::make({{x, y * 2}, {z, y * 2}}, x + z)), matches(let::make(x, y * 2, x * 2)));
}

TEST(simplify, loop) {
Expand Down

0 comments on commit 2cba9a7

Please sign in to comment.