Skip to content

Commit

Permalink
[refactor] Remove redundant codegen of floordiv (#6135)
Browse files Browse the repository at this point in the history
Issue: #6134

`floordiv` is already demoted and should not appear in codegen.
  • Loading branch information
strongoier authored Sep 22, 2022
1 parent 8147502 commit 79f2a6a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 92 deletions.
10 changes: 0 additions & 10 deletions taichi/codegen/cc/codegen_cc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,6 @@ class CCTransformer : public IRVisitor {
emit("{} = -({} {} {});", var, lhs_name, binop, rhs_name);
} else if (bin->op_type == BinaryOpType::truediv) {
emit("{} = ({}) {} / {};", var, dt_name, lhs_name, rhs_name);
} else if (bin->op_type == BinaryOpType::floordiv) {
auto lhs_dt_name = data_type_name(bin->lhs->element_type());
if (is_integral(bin->lhs->element_type()) &&
is_integral(bin->rhs->element_type())) {
emit("{} = Ti_floordiv_{}({}, {});", var, lhs_dt_name, lhs_name,
rhs_name);
} else {
emit("{} = Ti_floordiv_{}({}, {});", var, lhs_dt_name, lhs_name,
rhs_name);
}
} else {
emit("{} = {} {} {};", var, lhs_name, binop, rhs_name);
}
Expand Down
14 changes: 0 additions & 14 deletions taichi/codegen/cc/runtime/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@ static inline Ti_f32 Ti_fsgnf(Ti_f32 x) {
static inline Ti_f64 Ti_fsgn(Ti_f64 x) {
return x < 0 ? -1 : x != 0;
}
static inline Ti_i64 Ti_floordiv_i64(Ti_i64 x, Ti_i64 y) {
Ti_i64 r = x / y;
return r - ((x < 0) != (y < 0) && x && y * r != x);
}
static inline Ti_i32 Ti_floordiv_i32(Ti_i32 x, Ti_i32 y) {
Ti_i32 r = x / y;
return r - ((x < 0) != (y < 0) && x && y * r != x);
}
static inline Ti_f32 Ti_floordiv_f32(Ti_f32 x, Ti_f32 y) {
return floorf(x / y);
}
static inline Ti_f64 Ti_floordiv_f64(Ti_f64 x, Ti_f64 y) {
return floor(x / y);
}
static inline Ti_f32 Ti_rsqrtf(Ti_f32 x) {
return 1 / sqrt(x);
}
Expand Down
10 changes: 0 additions & 10 deletions taichi/codegen/llvm/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,6 @@ void TaskCodeGenLLVM::visit(BinaryOpStmt *stmt) {
llvm_val[stmt] =
builder->CreateMul(llvm_val[stmt->lhs], llvm_val[stmt->rhs]);
}
} else if (op == BinaryOpType::floordiv) {
if (is_integral(ret_type))
llvm_val[stmt] =
create_call(fmt::format("floordiv_{}", data_type_name(ret_type)),
{llvm_val[stmt->lhs], llvm_val[stmt->rhs]});
else {
auto div = builder->CreateFDiv(llvm_val[stmt->lhs], llvm_val[stmt->rhs]);
llvm_val[stmt] = builder->CreateIntrinsic(
llvm::Intrinsic::floor, {tlctx->get_data_type(ret_type)}, {div});
}
} else if (op == BinaryOpType::div) {
if (is_real(stmt->ret_type)) {
llvm_val[stmt] =
Expand Down
10 changes: 0 additions & 10 deletions taichi/codegen/metal/codegen_metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,6 @@ class KernelCodegenImpl : public IRVisitor {
const auto rhs_name = bin->rhs->raw_name();
const auto bin_name = bin->raw_name();
const auto op_type = bin->op_type;
if (op_type == BinaryOpType::floordiv) {
if (is_integral(bin->ret_type)) {
emit("const {} {} = ifloordiv({}, {});", dt_name, bin_name, lhs_name,
rhs_name);
} else {
emit("const {} {} = floor({} / {});", dt_name, bin_name, lhs_name,
rhs_name);
}
return;
}
const auto binop = metal_binary_op_type_symbol(op_type);
if (is_metal_binary_op_infix(op_type)) {
if (is_comparison(op_type)) {
Expand Down
9 changes: 0 additions & 9 deletions taichi/codegen/spirv/spirv_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,15 +916,6 @@ class TaskCodegen : public IRVisitor {
rhs_value = ir_->cast(dst_type, rhs_value);
bin_value = ir_->div(lhs_value, rhs_value);
}
else if (op_type == BinaryOpType::floordiv) {
uint32_t Floor_id = 8;
lhs_value =
ir_->cast(ir_->f32_type(), lhs_value); // TODO: Hard-coded f32
rhs_value = ir_->cast(ir_->f32_type(), rhs_value);
bin_value = ir_->div(lhs_value, rhs_value);
bin_value = ir_->call_glsl450(ir_->f32_type(), Floor_id, bin_value);
bin_value = ir_->cast(dst_type, bin_value);
}
else {TI_NOT_IMPLEMENTED} ir_->register_value(bin_name, bin_value);
}

Expand Down
32 changes: 0 additions & 32 deletions taichi/runtime/llvm/runtime_module/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,6 @@ void locked_task(void *lock, const T &func);
template <typename T, typename G>
void locked_task(void *lock, const T &func, const G &test);

template <typename T>
T ifloordiv(T a, T b) {
auto r = a / b;
// simply `a * b < 0` may leads to overflow (#969)
//
// Formal Anti-Regression Verification (FARV):
//
// old = a * b < 0
// new = (a < 0) != (b < 0) && a
//
// a b old new
// - - f = f (f&t)
// - + t = t (t&t)
// 0 - f = f (t&f)
// 0 + f = f (f&f)
// + - t = t (t&t)
// + + f = f (f&t)
//
// the situation of `b = 0` is ignored since we get FPE anyway.
//
r -= T((a < 0) != (b < 0) && a && b * r != a);
return r;
}

struct LLVMRuntime;
template <typename... Args>
void taichi_printf(LLVMRuntime *runtime, const char *format, Args &&...args);
Expand Down Expand Up @@ -213,14 +189,6 @@ i64 abs_i64(i64 a) {
return a >= 0 ? a : -a;
}

i32 floordiv_i32(i32 a, i32 b) {
return ifloordiv(a, b);
}

i64 floordiv_i64(i64 a, i64 b) {
return ifloordiv(a, b);
}

u16 min_u16(u16 a, u16 b) {
return a < b ? a : b;
}
Expand Down
7 changes: 0 additions & 7 deletions taichi/runtime/metal/shaders/helpers.metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ STR(
return *reinterpret_cast<thread const T *>(&g);
}

inline int ifloordiv(int lhs, int rhs) {
const int intm = (lhs / rhs);
return (((lhs < 0) != (rhs < 0) && lhs && (rhs * intm != lhs))
? (intm - 1)
: intm);
}

float fatomic_fetch_add(device float *dest, const float operand) {
// A huge hack! Metal does not support atomic floating point numbers
// natively.
Expand Down
3 changes: 3 additions & 0 deletions taichi/transforms/compile_to_offloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ void compile_function(IRNode *ir,
irpass::type_check(ir, config);
print("Typechecked");

irpass::demote_operations(ir, config);
print("Operations demoted");

irpass::full_simplify(
ir, config, {false, autodiff_mode != AutodiffMode::kNone, func->program});
print("Simplified");
Expand Down
17 changes: 17 additions & 0 deletions taichi/transforms/demote_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ class DemoteOperations : public BasicStmtVisitor {
// if (a < 0) != (b < 0) and a and b * r != a:
// r = r - 1
// return r
//
// simply `a * b < 0` may leads to overflow (#969)
//
// Formal Anti-Regression Verification (FARV):
//
// old = a * b < 0
// new = (a < 0) != (b < 0) && a
//
// a b old new
// - - f = f (f&t)
// - + t = t (t&t)
// 0 - f = f (t&f)
// 0 + f = f (f&f)
// + - t = t (t&t)
// + + f = f (f&t)
//
// the situation of `b = 0` is ignored since we get FPE anyway.
auto ret = Stmt::make<BinaryOpStmt>(BinaryOpType::div, lhs, rhs);
auto zero = Stmt::make<ConstStmt>(TypedConstant(0));
auto lhs_ltz =
Expand Down

0 comments on commit 79f2a6a

Please sign in to comment.