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

[ir][refactor] Pass a context object to Expression::flatten() #901

Merged
merged 2 commits into from
May 1, 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
36 changes: 18 additions & 18 deletions taichi/ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,15 @@ bool UnaryOpExpression::is_cast() const {
return unary_op_is_cast(type);
}

void UnaryOpExpression::flatten(VecStatement &ret) {
operand->flatten(ret);
void UnaryOpExpression::flatten(FlattenContext *ctx) {
operand->flatten(ctx);
auto unary = std::make_unique<UnaryOpStmt>(type, operand->stmt);
if (is_cast()) {
unary->cast_type = cast_type;
}
stmt = unary.get();
stmt->tb = tb;
ret.push_back(std::move(unary));
ctx->push_back(std::move(unary));
}

ExternalPtrStmt::ExternalPtrStmt(const LaneAttribute<Stmt *> &base_ptrs,
Expand Down Expand Up @@ -459,22 +459,22 @@ std::string GlobalPtrExpression::serialize() {
return s;
}

void GlobalPtrExpression::flatten(VecStatement &ret) {
void GlobalPtrExpression::flatten(FlattenContext *ctx) {
std::vector<Stmt *> index_stmts;
for (int i = 0; i < (int)indices.size(); i++) {
indices.exprs[i]->flatten(ret);
indices.exprs[i]->flatten(ctx);
index_stmts.push_back(indices.exprs[i]->stmt);
}
if (var.is<GlobalVariableExpression>()) {
ret.push_back(std::make_unique<GlobalPtrStmt>(
ctx->push_back(std::make_unique<GlobalPtrStmt>(
var.cast<GlobalVariableExpression>()->snode, index_stmts));
} else {
TI_ASSERT(var.is<ExternalTensorExpression>());
var->flatten(ret);
ret.push_back(std::make_unique<ExternalPtrStmt>(
var->flatten(ctx);
ctx->push_back(std::make_unique<ExternalPtrStmt>(
var.cast<ExternalTensorExpression>()->stmt, index_stmts));
}
stmt = ret.back().get();
stmt = ctx->back_stmt();
}

GetChStmt::GetChStmt(Stmt *input_ptr, int chid)
Expand Down Expand Up @@ -858,10 +858,10 @@ std::string SNodeOpExpression::serialize() {
}
}

void SNodeOpExpression::flatten(VecStatement &ret) {
void SNodeOpExpression::flatten(FlattenContext *ctx) {
std::vector<Stmt *> indices_stmt;
for (int i = 0; i < (int)indices.size(); i++) {
indices[i]->flatten(ret);
indices[i]->flatten(ctx);
indices_stmt.push_back(indices[i]->stmt);
}
if (op_type == SNodeOpType::is_active) {
Expand All @@ -871,24 +871,24 @@ void SNodeOpExpression::flatten(VecStatement &ret) {
snode->type != SNodeType::hash &&
snode->type != SNodeType::bitmasked,
"ti.is_active only works on pointer, hash or bitmasked nodes.");
ret.push_back<SNodeOpStmt>(SNodeOpType::is_active, snode, indices_stmt);
ctx->push_back<SNodeOpStmt>(SNodeOpType::is_active, snode, indices_stmt);
} else {
auto ptr = ret.push_back<GlobalPtrStmt>(snode, indices_stmt);
auto ptr = ctx->push_back<GlobalPtrStmt>(snode, indices_stmt);
if (op_type == SNodeOpType::append) {
value->flatten(ret);
ret.push_back<SNodeOpStmt>(SNodeOpType::append, snode, ptr,
ret.back().get());
value->flatten(ctx);
ctx->push_back<SNodeOpStmt>(SNodeOpType::append, snode, ptr,
ctx->back_stmt());
TI_ERROR_IF(snode->type != SNodeType::dynamic,
"ti.append only works on dynamic nodes.");
TI_ERROR_IF(snode->ch.size() != 1,
"ti.append only works on single-child dynamic nodes.");
TI_ERROR_IF(data_type_size(snode->ch[0]->dt) != 4,
"ti.append only works on i32/f32 nodes.");
} else if (op_type == SNodeOpType::length) {
ret.push_back<SNodeOpStmt>(SNodeOpType::length, snode, ptr, nullptr);
ctx->push_back<SNodeOpStmt>(SNodeOpType::length, snode, ptr, nullptr);
}
}
stmt = ret.back().get();
stmt = ctx->back_stmt();
}

std::unique_ptr<ConstStmt> ConstStmt::copy() {
Expand Down
112 changes: 65 additions & 47 deletions taichi/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,31 @@ class Expression {
std::string tb;
std::map<std::string, std::string> attributes;

struct FlattenContext {
VecStatement stmts;
Block *current_block = nullptr;

inline Stmt *push_back(pStmt &&stmt) {
return stmts.push_back(std::move(stmt));
}

template <typename T, typename... Args>
T *push_back(Args &&... args) {
return stmts.push_back<T>(std::forward<Args>(args)...);
}

Stmt *back_stmt() {
return stmts.back().get();
}
};

Expression() {
stmt = nullptr;
}

virtual std::string serialize() = 0;

virtual void flatten(VecStatement &ret) {
virtual void flatten(FlattenContext *ctx) {
TI_NOT_IMPLEMENTED;
};

Expand Down Expand Up @@ -938,10 +956,10 @@ class ArgLoadExpression : public Expression {
return fmt::format("arg[{}]", arg_id);
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
auto ran = std::make_unique<ArgLoadStmt>(arg_id);
ret.push_back(std::move(ran));
stmt = ret.back().get();
ctx->push_back(std::move(ran));
stmt = ctx->back_stmt();
}
};

Expand Down Expand Up @@ -1008,10 +1026,10 @@ class RandExpression : public Expression {
return fmt::format("rand<{}>()", data_type_name(dt));
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
auto ran = std::make_unique<RandStmt>(dt);
ret.push_back(std::move(ran));
stmt = ret.back().get();
ctx->push_back(std::move(ran));
stmt = ctx->back_stmt();
}
};

Expand All @@ -1030,7 +1048,7 @@ class UnaryOpExpression : public Expression {

std::string serialize() override;

void flatten(VecStatement &ret) override;
void flatten(FlattenContext *ctx) override;
};

class BinaryOpStmt : public Stmt {
Expand Down Expand Up @@ -1104,14 +1122,14 @@ class BinaryOpExpression : public Expression {
binary_op_type_symbol(type), rhs->serialize());
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
// if (stmt)
// return;
lhs->flatten(ret);
rhs->flatten(ret);
ret.push_back(std::make_unique<BinaryOpStmt>(type, lhs->stmt, rhs->stmt));
ret.back()->tb = tb;
stmt = ret.back().get();
lhs->flatten(ctx);
rhs->flatten(ctx);
ctx->push_back(std::make_unique<BinaryOpStmt>(type, lhs->stmt, rhs->stmt));
ctx->stmts.back()->tb = tb;
stmt = ctx->back_stmt();
}
};

Expand All @@ -1135,15 +1153,15 @@ class TernaryOpExpression : public Expression {
op1->serialize(), op2->serialize(), op3->serialize());
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
// if (stmt)
// return;
op1->flatten(ret);
op2->flatten(ret);
op3->flatten(ret);
ret.push_back(
op1->flatten(ctx);
op2->flatten(ctx);
op3->flatten(ctx);
ctx->push_back(
std::make_unique<TernaryOpStmt>(type, op1->stmt, op2->stmt, op3->stmt));
stmt = ret.back().get();
stmt = ctx->back_stmt();
}
};

Expand Down Expand Up @@ -1197,10 +1215,10 @@ class ExternalTensorExpression : public Expression {
return fmt::format("{}d_ext_arr", dim);
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
auto ptr = Stmt::make<ArgLoadStmt>(arg_id, true);
ret.push_back(std::move(ptr));
stmt = ret.back().get();
ctx->push_back(std::move(ptr));
stmt = ctx->back_stmt();
}
};

Expand Down Expand Up @@ -1236,11 +1254,11 @@ class GlobalVariableExpression : public Expression {
return "#" + ident.name();
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
TI_ASSERT(snode->num_active_indices == 0);
auto ptr = Stmt::make<GlobalPtrStmt>(LaneAttribute<SNode *>(snode),
std::vector<Stmt *>());
ret.push_back(std::move(ptr));
ctx->push_back(std::move(ptr));
}
};

Expand All @@ -1255,7 +1273,7 @@ class GlobalPtrExpression : public Expression {

std::string serialize() override;

void flatten(VecStatement &ret) override;
void flatten(FlattenContext *ctx) override;

bool is_lvalue() const override {
return true;
Expand Down Expand Up @@ -1871,7 +1889,7 @@ class EvalExpression : public Expression {
return fmt::format("%{}", stmt_id);
}

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
stmt = stmt_ptr;
}
};
Expand All @@ -1894,12 +1912,12 @@ class RangeAssumptionExpression : public Expression {
base.serialize(), high);
}

void flatten(VecStatement &ret) override {
input->flatten(ret);
base->flatten(ret);
ret.push_back(
void flatten(FlattenContext *ctx) override {
input->flatten(ctx);
base->flatten(ctx);
ctx->push_back(
Stmt::make<RangeAssumptionStmt>(input->stmt, base->stmt, low, high));
stmt = ret.back().get();
stmt = ctx->back_stmt();
}
};

Expand All @@ -1919,10 +1937,10 @@ class IdExpression : public Expression {
return id.name();
}

void flatten(VecStatement &ret) override {
ret.push_back(std::make_unique<LocalLoadStmt>(
LocalAddress(current_block->lookup_var(id), 0)));
stmt = ret.back().get();
void flatten(FlattenContext *ctx) override {
ctx->push_back(std::make_unique<LocalLoadStmt>(
LocalAddress(ctx->current_block->lookup_var(id), 0)));
stmt = ctx->back_stmt();
}

bool is_lvalue() const override {
Expand All @@ -1945,12 +1963,12 @@ class AtomicOpExpression : public Expression {

std::string serialize() override;

void flatten(VecStatement &ret) override {
void flatten(FlattenContext *ctx) override {
// FrontendAtomicStmt is the correct place to flatten sub-exprs like |dest|
// and |val| (See LowerAST). This class only wraps the frontend atomic_op()
// stmt as an expression.
ret.push_back<FrontendAtomicStmt>(op_type, dest, val);
stmt = ret.back().get();
ctx->push_back<FrontendAtomicStmt>(op_type, dest, val);
stmt = ctx->back_stmt();
}
};

Expand All @@ -1974,7 +1992,7 @@ class SNodeOpExpression : public Expression {

std::string serialize() override;

void flatten(VecStatement &ret) override;
void flatten(FlattenContext *ctx) override;
};

class GlobalLoadExpression : public Expression {
Expand All @@ -1987,10 +2005,10 @@ class GlobalLoadExpression : public Expression {
return "gbl load " + ptr.serialize();
}

void flatten(VecStatement &ret) override {
ptr->flatten(ret);
ret.push_back(std::make_unique<GlobalLoadStmt>(ptr->stmt));
stmt = ret.back().get();
void flatten(FlattenContext *ctx) override {
ptr->flatten(ctx);
ctx->push_back(std::make_unique<GlobalLoadStmt>(ptr->stmt));
stmt = ctx->back_stmt();
}
};

Expand All @@ -2006,9 +2024,9 @@ class ConstExpression : public Expression {
return val.stringify();
}

void flatten(VecStatement &ret) override {
ret.push_back(Stmt::make<ConstStmt>(val));
stmt = ret.back().get();
void flatten(FlattenContext *ctx) override {
ctx->push_back(Stmt::make<ConstStmt>(val));
stmt = ctx->back_stmt();
}
};

Expand Down
Loading