Skip to content

Commit

Permalink
[llvm] [refactor] Replace cast_int() with LLVM native integer cast (#…
Browse files Browse the repository at this point in the history
…5110)

* [llvm] [refactor] Use LLVM native integer cast

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
strongoier and pre-commit-ci[bot] authored Jun 8, 2022
1 parent 8791cda commit 9c4fa73
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
27 changes: 2 additions & 25 deletions taichi/codegen/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,6 @@ CodeGenLLVM::CodeGenLLVM(Kernel *kernel,
kernel_name = kernel->name + "_kernel";
}

llvm::Value *CodeGenLLVM::cast_int(llvm::Value *input_val,
Type *from,
Type *to) {
if (from == to)
return input_val;
auto from_size = 0;
if (from->is<CustomIntType>()) {
from_size = data_type_size(from->cast<CustomIntType>()->get_compute_type());
} else {
from_size = data_type_size(from);
}
if (from_size < data_type_size(to)) {
if (is_signed(from)) {
return builder->CreateSExt(input_val, tlctx->get_data_type(to));
} else {
return builder->CreateZExt(input_val, tlctx->get_data_type(to));
}
} else {
return builder->CreateTrunc(input_val, tlctx->get_data_type(to));
}
}

void CodeGenLLVM::visit(DecorationStmt *stmt) {
}

Expand Down Expand Up @@ -404,9 +382,8 @@ void CodeGenLLVM::visit(UnaryOpStmt *stmt) {
}
}
} else if (!is_real(from) && !is_real(to)) {
// TODO: implement casting into custom integer type
TI_ASSERT(!to->is<CustomIntType>());
llvm_val[stmt] = cast_int(llvm_val[stmt->operand], from, to);
llvm_val[stmt] = builder->CreateIntCast(llvm_val[stmt->operand],
llvm_type(to), is_signed(from));
}
} else if (stmt->op_type == UnaryOpType::cast_bits) {
TI_ASSERT(data_type_size(stmt->ret_type) ==
Expand Down
2 changes: 0 additions & 2 deletions taichi/codegen/codegen_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ class CodeGenLLVM : public IRVisitor, public LLVMModuleBuilder {

void visit(RandStmt *stmt) override;

llvm::Value *cast_int(llvm::Value *input_val, Type *from, Type *to);

virtual void emit_extra_unary(UnaryOpStmt *stmt);

void visit(DecorationStmt *stmt) override;
Expand Down
3 changes: 2 additions & 1 deletion taichi/codegen/codegen_llvm_quant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ llvm::Value *CodeGenLLVM::atomic_add_custom_int(AtomicOpStmt *stmt,
fmt::format("atomic_add_partial_bits_b{}", data_type_bits(physical_type)),
{builder->CreateBitCast(byte_ptr, llvm_ptr_type(physical_type)),
bit_offset, tlctx->get_constant(cit->get_num_bits()),
cast_int(llvm_val[stmt->val], stmt->val->ret_type, physical_type)});
builder->CreateIntCast(llvm_val[stmt->val], llvm_type(physical_type),
is_signed(stmt->val->ret_type))});
}

llvm::Value *CodeGenLLVM::atomic_add_custom_float(AtomicOpStmt *stmt,
Expand Down

0 comments on commit 9c4fa73

Please sign in to comment.