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] Enforce type check for all expressions #3461

Merged
merged 5 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 0 additions & 6 deletions taichi/ir/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ Expr &Expr::operator=(const Expr &o) {
} else if (expr->is_lvalue()) {
current_ast_builder().insert(
std::make_unique<FrontendAssignStmt>(*this, load_if_ptr(o)));
if (this->is<IdExpression>()) {
expr->ret_type = current_ast_builder()
.get_last_stmt()
->cast<FrontendAssignStmt>()
->rhs->ret_type;
}
} else {
TI_ERROR("Cannot assign to non-lvalue: {}", serialize());
}
Expand Down
97 changes: 68 additions & 29 deletions taichi/ir/frontend_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ FrontendSNodeOpStmt::FrontendSNodeOpStmt(SNodeOpType op_type,
FrontendAssignStmt::FrontendAssignStmt(const Expr &lhs, const Expr &rhs)
: lhs(lhs), rhs(rhs) {
TI_ASSERT(lhs->is_lvalue());
if (lhs.is<IdExpression>() && lhs->ret_type == PrimitiveType::unknown) {
lhs.expr->ret_type = rhs->ret_type;
}
}

IRNode *FrontendContext::root() {
Expand Down Expand Up @@ -128,10 +131,12 @@ void UnaryOpExpression::serialize(std::ostream &ss) {
}

void UnaryOpExpression::type_check() {
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (operand->ret_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(operand->ret_type != PrimitiveType::unknown,
ailzhang marked this conversation as resolved.
Show resolved Hide resolved
"[{}] was not type-checked", operand.serialize());
if (!operand->ret_type->is<PrimitiveType>())
throw std::runtime_error(
fmt::format("TypeError: unsupported operand type(s) for '{}': '{}'",
unary_op_type_name(type), operand->ret_type->to_string()));
if ((type == UnaryOpType::floor || type == UnaryOpType::ceil ||
is_trigonometric(type)) &&
!is_real(operand->ret_type))
Expand Down Expand Up @@ -159,10 +164,10 @@ void UnaryOpExpression::flatten(FlattenContext *ctx) {
void BinaryOpExpression::type_check() {
auto lhs_type = lhs->ret_type;
auto rhs_type = rhs->ret_type;
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (lhs_type == PrimitiveType::unknown || rhs_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(lhs_type != PrimitiveType::unknown,
"[{}] was not type-checked", lhs.serialize());
TI_ASSERT_INFO(rhs_type != PrimitiveType::unknown,
"[{}] was not type-checked", rhs.serialize());
auto error = [&]() {
throw std::runtime_error(fmt::format(
"TypeError: unsupported operand type(s) for '{}': '{}' and '{}'",
Expand Down Expand Up @@ -204,9 +209,12 @@ void TernaryOpExpression::type_check() {
auto op1_type = op1->ret_type;
auto op2_type = op2->ret_type;
auto op3_type = op3->ret_type;
if (op1_type == PrimitiveType::unknown ||
op2_type == PrimitiveType::unknown || op3_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(op1_type != PrimitiveType::unknown,
"[{}] was not type-checked", op1.serialize());
TI_ASSERT_INFO(op2_type != PrimitiveType::unknown,
"[{}] was not type-checked", op2.serialize());
TI_ASSERT_INFO(op3_type != PrimitiveType::unknown,
"[{}] was not type-checked", op3.serialize());
auto error = [&]() {
throw std::runtime_error(fmt::format(
"TypeError: unsupported operand type(s) for '{}': '{}', '{}' and '{}'",
Expand All @@ -230,6 +238,16 @@ void TernaryOpExpression::flatten(FlattenContext *ctx) {
stmt = ctx->back_stmt();
}

void InternalFuncCallExpression::type_check() {
for (auto &arg : args) {
TI_ASSERT_INFO(arg->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", arg.serialize());
// no arg type compatibility check for now due to lack of specification
}
// internal func calls have default return type
ret_type = PrimitiveType::i32;
}

void InternalFuncCallExpression::flatten(FlattenContext *ctx) {
std::vector<Stmt *> args_stmts(args.size());
for (int i = 0; i < (int)args.size(); ++i) {
Expand All @@ -240,6 +258,20 @@ void InternalFuncCallExpression::flatten(FlattenContext *ctx) {
stmt = ctx->back_stmt();
}

void ExternalFuncCallExpression::type_check() {
for (auto &arg : args) {
TI_ASSERT_INFO(arg->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", arg.serialize());
// no arg type compatibility check for now due to lack of specification
}
for (auto &output : outputs) {
TI_ASSERT_INFO(output->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", output.serialize());
// no output type compatibility check for now due to lack of specification
}
// external func calls have no return type for now
}

void ExternalFuncCallExpression::flatten(FlattenContext *ctx) {
TI_ASSERT((int)(so_func != nullptr) + (int)(!asm_source.empty()) +
(int)(!bc_filename.empty()) ==
Expand Down Expand Up @@ -295,10 +327,8 @@ void GlobalPtrExpression::type_check() {
} else if (var.is<ExternalTensorExpression>()) {
for (int i = 0; i < indices.exprs.size(); i++) {
auto &expr = indices.exprs[i];
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (expr->ret_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(expr->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", expr.serialize());
if (!is_integral(expr->ret_type))
throw std::runtime_error(
fmt::format("TypeError: indices must be integers, however '{}' is "
Expand Down Expand Up @@ -441,11 +471,10 @@ void TensorElementExpression::flatten(FlattenContext *ctx) {
}

void RangeAssumptionExpression::type_check() {
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (input->ret_type == PrimitiveType::unknown ||
base->ret_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(input->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", input.serialize());
TI_ASSERT_INFO(base->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", base.serialize());
if (!input->ret_type->is<PrimitiveType>() ||
!base->ret_type->is<PrimitiveType>() || input->ret_type != base->ret_type)
throw std::runtime_error(
Expand All @@ -464,10 +493,8 @@ void RangeAssumptionExpression::flatten(FlattenContext *ctx) {
}

void LoopUniqueExpression::type_check() {
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (input->ret_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(input->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", input.serialize());
if (!input->ret_type->is<PrimitiveType>())
throw std::runtime_error(fmt::format(
"TypeError: unsupported operand type(s) for 'loop_unique': '{}'",
Expand Down Expand Up @@ -516,11 +543,10 @@ void IdExpression::flatten(FlattenContext *ctx) {
}

void AtomicOpExpression::type_check() {
// TODO: assert no unknowns after type_check for all expressions are
// implemented
if (dest->ret_type == PrimitiveType::unknown ||
val->ret_type == PrimitiveType::unknown)
return;
TI_ASSERT_INFO(dest->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", dest.serialize());
TI_ASSERT_INFO(val->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", val.serialize());
auto error = [&]() {
throw std::runtime_error(fmt::format(
"TypeError: unsupported operand type(s) for 'atomic_{}': '{}' and '{}'",
Expand Down Expand Up @@ -685,6 +711,19 @@ void ExternalTensorShapeAlongAxisExpression::flatten(FlattenContext *ctx) {
stmt = ctx->back_stmt();
}

void FuncCallExpression::type_check() {
for (auto &arg : args.exprs) {
TI_ASSERT_INFO(arg->ret_type != PrimitiveType::unknown,
"[{}] was not type-checked", arg.serialize());
// no arg type compatibility check for now due to lack of specification
}
TI_ASSERT_INFO(func->rets.size() <= 1,
"Too many (> 1) return values for FuncCallExpression");
if (func->rets.size() == 1) {
ret_type = func->rets[0].dt;
}
}

void FuncCallExpression::flatten(FlattenContext *ctx) {
std::vector<Stmt *> stmt_args;
for (auto &arg : args.exprs) {
Expand Down
6 changes: 6 additions & 0 deletions taichi/ir/frontend_ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ class InternalFuncCallExpression : public Expression {
}
}

void type_check() override;

void serialize(std::ostream &ss) override {
ss << "internal call " << func_name << '(';
std::string args_str;
Expand Down Expand Up @@ -395,6 +397,8 @@ class ExternalFuncCallExpression : public Expression {
outputs(outputs) {
}

void type_check() override;

void serialize(std::ostream &ss) override {
if (so_func != nullptr) {
ss << fmt::format("so {:x} (", (uint64)so_func);
Expand Down Expand Up @@ -750,6 +754,8 @@ class FuncCallExpression : public Expression {
Function *func;
ExprGroup args;

void type_check() override;

void serialize(std::ostream &ss) override;

FuncCallExpression(Function *func, const ExprGroup &args)
Expand Down
7 changes: 7 additions & 0 deletions tests/cpp/ir/frontend_type_inference_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,12 @@ TEST(FrontendTypeInference, LoopUnique) {
EXPECT_EQ(loop_unique->ret_type, PrimitiveType::i64);
}

TEST(FrontendTypeInference, InternalFuncCall) {
auto internal_func_call =
Expr::make<InternalFuncCallExpression>("do_nothing", std::vector<Expr>{});
internal_func_call->type_check();
EXPECT_EQ(internal_func_call->ret_type, PrimitiveType::i32);
}

} // namespace lang
} // namespace taichi