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

[Opt] Constant folding for BitExtractStmt #1307

Merged
merged 3 commits into from
Jun 23, 2020
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
27 changes: 25 additions & 2 deletions taichi/transforms/constant_fold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class ConstantFold : public BasicStmtVisitor {
auto evaluated =
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(new_constant));
stmt->replace_with(evaluated.get());
modifier.insert_before(stmt, VecStatement(std::move(evaluated)));
modifier.insert_before(stmt, std::move(evaluated));
modifier.erase(stmt);
}
}
Expand All @@ -177,11 +177,34 @@ class ConstantFold : public BasicStmtVisitor {
auto evaluated =
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(new_constant));
stmt->replace_with(evaluated.get());
modifier.insert_before(stmt, VecStatement(std::move(evaluated)));
modifier.insert_before(stmt, std::move(evaluated));
modifier.erase(stmt);
}
}

void visit(BitExtractStmt *stmt) override {
auto input = stmt->input->cast<ConstStmt>();
if (!input)
return;
Comment on lines +187 to +188
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (!input)
return;
if (!input || input->element_type() == DataType::i32)
return;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why not do constant folding when the input is i32?

if (stmt->width() != 1)
return;
std::unique_ptr<Stmt> result_stmt;
if (is_signed(input->val[0].dt)) {
auto result = (input->val[0].val_int() >> stmt->bit_begin) &
((1LL << (stmt->bit_end - stmt->bit_begin)) - 1);
result_stmt = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(
TypedConstant(input->val[0].dt, result)));
} else {
auto result = (input->val[0].val_uint() >> stmt->bit_begin) &
((1LL << (stmt->bit_end - stmt->bit_begin)) - 1);
result_stmt = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(
TypedConstant(input->val[0].dt, result)));
}
stmt->replace_with(result_stmt.get());
modifier.insert_before(stmt, std::move(result_stmt));
modifier.erase(stmt);
}

static bool run(IRNode *node) {
ConstantFold folder;
bool modified = false;
Expand Down
2 changes: 1 addition & 1 deletion taichi/transforms/type_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class TypeCheck : public IRVisitor {
}

void visit(BitExtractStmt *stmt) {
stmt->ret_type.data_type = DataType::i32;
stmt->ret_type = stmt->input->ret_type;
}

void visit(LinearizeStmt *stmt) {
Expand Down