From 9c4fa7372417cb14c783edaf239436d149602e96 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 8 Jun 2022 16:39:24 +0800 Subject: [PATCH] [llvm] [refactor] Replace cast_int() with LLVM native integer cast (#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> --- taichi/codegen/codegen_llvm.cpp | 27 ++------------------------- taichi/codegen/codegen_llvm.h | 2 -- taichi/codegen/codegen_llvm_quant.cpp | 3 ++- 3 files changed, 4 insertions(+), 28 deletions(-) diff --git a/taichi/codegen/codegen_llvm.cpp b/taichi/codegen/codegen_llvm.cpp index 9b9f312cd4f2b..5e0489d5c9a39 100644 --- a/taichi/codegen/codegen_llvm.cpp +++ b/taichi/codegen/codegen_llvm.cpp @@ -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()) { - from_size = data_type_size(from->cast()->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) { } @@ -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()); - 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) == diff --git a/taichi/codegen/codegen_llvm.h b/taichi/codegen/codegen_llvm.h index 5cbe35cd4ea9e..d0d857275be0c 100644 --- a/taichi/codegen/codegen_llvm.h +++ b/taichi/codegen/codegen_llvm.h @@ -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; diff --git a/taichi/codegen/codegen_llvm_quant.cpp b/taichi/codegen/codegen_llvm_quant.cpp index f136f2fb8e970..d0eee7694141d 100644 --- a/taichi/codegen/codegen_llvm_quant.cpp +++ b/taichi/codegen/codegen_llvm_quant.cpp @@ -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,