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

Lower linearized into a series of adds and muls. #509

Merged
merged 4 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 26 additions & 4 deletions taichi/transforms/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,6 @@ class BasicBlockSimplify : public IRVisitor {
}

void visit(LinearizeStmt *stmt) override {
if (is_done(stmt))
return;

if (stmt->inputs.size() && stmt->inputs.back()->is<IntegerOffsetStmt>()) {
yuanming-hu marked this conversation as resolved.
Show resolved Hide resolved
auto previous_offset = stmt->inputs.back()->as<IntegerOffsetStmt>();
// push forward offset
Expand Down Expand Up @@ -691,7 +688,32 @@ class BasicBlockSimplify : public IRVisitor {
}
Copy link
Member

Choose a reason for hiding this comment

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

The CSE here

    for (int i = 0; i < current_stmt_id; i++) {
      auto &bstmt = block->statements[i];
      if (stmt->ret_type == bstmt->ret_type) {
        auto &bstmt_data = *bstmt;
        if (typeid(bstmt_data) == typeid(*stmt)) {
          auto bstmt_ = bstmt->as<LinearizeStmt>();
          if (identical_vectors(bstmt_->inputs, stmt->inputs) &&
              identical_vectors(bstmt_->strides, stmt->strides)) {
            stmt->replace_with(bstmt.get());
            stmt->parent->erase(current_stmt_id);
            throw IRModified();
          }
        }
      }
    }

can be removed now.

}
}
set_done(stmt);

// Lower into a series of adds and muls.
// Need typecheck afterwards.
if (stmt->inputs.empty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Actually, no need to specialize when you have empty inputs. Empty inputs should simply be treated as inputs with zero lengths. Then you can merge two clauses of this if stmt. The extra 0 + i can be optimized by your alg_simp pass.

auto zero = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(0));
stmt->replace_with(zero.get());
stmt->insert_before_me(std::move(zero));
stmt->parent->erase(stmt);
throw IRModified();
} else {
auto sumptr = stmt->inputs.back();
auto stride_product = 1;
for (int i = (int)stmt->inputs.size() - 2; i >= 0; i--) {
stride_product *= stmt->strides[i + 1];
auto stride_stmt = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(stride_product));
auto mul = Stmt::make<BinaryOpStmt>(BinaryOpType::mul, stmt->inputs[i], stride_stmt.get());
auto sum = Stmt::make<BinaryOpStmt>(BinaryOpType::add, sumptr, mul.get());
sumptr = sum.get();
stmt->insert_before_me(std::move(stride_stmt));
stmt->insert_before_me(std::move(mul));
stmt->insert_before_me(std::move(sum));
}
stmt->replace_with(sumptr);
stmt->parent->erase(stmt);
Copy link
Member

Choose a reason for hiding this comment

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

You can do irpass::typecheck(stmt->parent) if necessary.

throw IRModified();
}
}

void visit(SNodeLookupStmt *stmt) override {
Expand Down
35 changes: 35 additions & 0 deletions tests/cpp/test_alg_simp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,39 @@ TI_TEST("simplify_multiply_zero_fast_math") {
TI_CHECK((*block)[0]->is<GlobalTemporaryStmt>());
}

TI_TEST("simplify_linearized_with_trivial_inputs") {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe create a test_simplify.cpp file for this test?

auto block = std::make_unique<Block>();

auto get_root = block->push_back<GetRootStmt>();
auto linearized_empty =
block->push_back<LinearizeStmt>(std::vector<Stmt*>(), std::vector<int>());
SNode root(0, SNodeType::root);
root.insert_children(SNodeType::dense);
auto lookup =
block->push_back<SNodeLookupStmt>(&root, get_root, linearized_empty, false, std::vector<Stmt*>());
auto get_child = block->push_back<GetChStmt>(lookup, 0);
auto zero = block->push_back<ConstStmt>(TypedConstant(0));
auto linearized_zero =
block->push_back<LinearizeStmt>(std::vector<Stmt*>(2, zero), std::vector<int>({8, 4}));
auto lookup2 =
block->push_back<SNodeLookupStmt>(&*root.ch[0], get_child, linearized_zero, true, std::vector<Stmt*>());

irpass::typecheck(block.get());
// irpass::print(block.get());
TI_CHECK(block->size() == 7);

irpass::simplify(block.get()); // should lower linearized
// irpass::print(block.get());
// TI_CHECK(block->size() == 8);

irpass::typecheck(block.get()); // necessary here
// irpass::print(block.get());

irpass::constant_fold(block.get());
irpass::alg_simp(block.get(), CompileConfig());
irpass::die(block.get()); // should eliminate consts
// irpass::print(block.get());
TI_CHECK(block->size() == 5); // get root, const 0, lookup, get child, lookup
}

TLANG_NAMESPACE_END