Skip to content

Commit

Permalink
[llvm] Drop code for llvm 15. (taichi-dev#5313)
Browse files Browse the repository at this point in the history
* Drop code for llvm 15

Still use clang10 for COMPILE_LLVM_RUNTIME. Most changes are related to the opaque ptr change in llvm 15 which requires type when create Load and GEP. It would be nice to generate the type from taichi type.

Known issue.
Still some getPointerElementType use which is Deprecated for llvm 15.
Also some cases use hack to get llvm type for Load/GEP.

Only tested python tests/run_tests.py -v -t3 -a cpu -s on windows.
2 crashes
crashes:
  fp16 crash when hw not support fp16 for vulkan.
  element_wise crashed in llvm pass, need more time to debug.
  • Loading branch information
python3kgae authored Jul 8, 2022
1 parent 2c23a28 commit 0d7168f
Show file tree
Hide file tree
Showing 15 changed files with 498 additions and 79 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ if (WIN32)
#
# FIXME: (penguinliong) This is fixed in later releases of LLVM so maybe
# someday we can distribute `Debug` libraries, if it's ever needed.
SET(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
if (NOT TI_LLVM_15)
SET(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
endif()
message("CMAKE_MSVC_RUNTIME_LIBRARY: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
endif()

# No support of Python for Android build; or in any case taichi is integrated
Expand Down
5 changes: 5 additions & 0 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
option(USE_STDCPP "Use -stdlib=libc++" OFF)
option(TI_WITH_LLVM "Build with LLVM backends" ON)
option(TI_LLVM_15 "Switch to LLVM 15" OFF)
option(TI_WITH_METAL "Build with the Metal backend" ON)
option(TI_WITH_CUDA "Build with the CUDA backend" ON)
option(TI_WITH_CUDA_TOOLKIT "Build with the CUDA toolkit" OFF)
Expand Down Expand Up @@ -142,6 +143,10 @@ else()
list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_LLVM_SOURCE})
endif()

if (TI_LLVM_15)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_LLVM_15")
endif()

if (TI_WITH_CUDA)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_CUDA")
file(GLOB TAICHI_CUDA_RUNTIME_SOURCE "taichi/runtime/cuda/runtime.cpp")
Expand Down
20 changes: 16 additions & 4 deletions taichi/codegen/cpu/codegen_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,14 @@ class CodeGenLLVMCPU : public CodeGenLLVM {

{
builder->SetInsertPoint(loop_test_bb);
#ifdef TI_LLVM_15
auto *loop_index_load =
builder->CreateLoad(builder->getInt32Ty(), loop_index);
#else
auto *loop_index_load = builder->CreateLoad(loop_index);
#endif
auto cond = builder->CreateICmp(
llvm::CmpInst::Predicate::ICMP_SLT, builder->CreateLoad(loop_index),
llvm::CmpInst::Predicate::ICMP_SLT, loop_index_load,
llvm_val[stmt->owned_num_local.find(stmt->major_from_type)
->second]);
builder->CreateCondBr(cond, loop_body_bb, func_exit);
Expand All @@ -121,9 +127,15 @@ class CodeGenLLVMCPU : public CodeGenLLVM {
auto &s = stmt->body->statements[i];
s->accept(this);
}
builder->CreateStore(builder->CreateAdd(builder->CreateLoad(loop_index),
tlctx->get_constant(1)),
loop_index);
#ifdef TI_LLVM_15
auto *loop_index_load =
builder->CreateLoad(builder->getInt32Ty(), loop_index);
#else
auto *loop_index_load = builder->CreateLoad(loop_index);
#endif
builder->CreateStore(
builder->CreateAdd(loop_index_load, tlctx->get_constant(1)),
loop_index);
builder->CreateBr(loop_test_bb);
builder->SetInsertPoint(func_exit);
}
Expand Down
55 changes: 42 additions & 13 deletions taichi/codegen/cuda/codegen_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {
auto value_arr = builder->CreateAlloca(stype);
for (int i = 0; i < values.size(); i++) {
auto value_ptr = builder->CreateGEP(
#ifdef TI_LLVM_15
stype,
#endif
value_arr, {tlctx->get_constant(0), tlctx->get_constant(i)});
builder->CreateStore(values[i], value_ptr);
}
Expand Down Expand Up @@ -324,8 +327,11 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {

// Use the value from the memory that atomicCAS operates on to initialize
// cas_old_output.
llvm::Value *cas_old_output =
builder->CreateLoad(atomic_memory_address, "cas_old_output");
llvm::Value *cas_old_output = builder->CreateLoad(
#ifdef TI_LLVM_15
atomic_type,
#endif
atomic_memory_address, "cas_old_output");
builder->CreateStore(cas_old_output, cas_old_output_address);

llvm::BasicBlock *loop_body_bb =
Expand All @@ -338,21 +344,35 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {
// loop body for one atomicCAS
{
// Use cas_old_output to initialize cas_new_output.
cas_old_output =
builder->CreateLoad(cas_old_output_address, "cas_old_output");
cas_old_output = builder->CreateLoad(
#ifdef TI_LLVM_15
atomic_type,
#endif
cas_old_output_address, "cas_old_output");
builder->CreateStore(cas_old_output, cas_new_output_address);

auto binop_output = op(builder->CreateLoad(binop_output_address), val);
auto binop_output = op(builder->CreateLoad(
#ifdef TI_LLVM_15
atomic_type,
#endif
binop_output_address),
val);
builder->CreateStore(binop_output, binop_output_address);

llvm::Value *cas_new_output =
builder->CreateLoad(cas_new_output_address, "cas_new_output");
llvm::Value *cas_new_output = builder->CreateLoad(
#ifdef TI_LLVM_15
atomic_type,
#endif
cas_new_output_address, "cas_new_output");

// Emit code to perform the atomicCAS operation
// (cas_old_output, success) = atomicCAS(memory_address, cas_old_output,
// cas_new_output);
llvm::Value *ret_value = builder->CreateAtomicCmpXchg(
atomic_memory_address, cas_old_output, cas_new_output,
#ifdef TI_LLVM_15
llvm::MaybeAlign(0),
#endif
llvm::AtomicOrdering::SequentiallyConsistent,
llvm::AtomicOrdering::SequentiallyConsistent);

Expand Down Expand Up @@ -427,8 +447,8 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {
llvm::BasicBlock::Create(*llvm_context, "loop_body", func);
auto func_exit =
llvm::BasicBlock::Create(*llvm_context, "func_exit", func);
auto loop_index =
create_entry_block_alloca(llvm::Type::getInt32Ty(*llvm_context));
auto i32_ty = llvm::Type::getInt32Ty(*llvm_context);
auto loop_index = create_entry_block_alloca(i32_ty);
llvm::Value *thread_idx =
builder->CreateIntrinsic(Intrinsic::nvvm_read_ptx_sreg_tid_x, {}, {});
llvm::Value *block_dim = builder->CreateIntrinsic(
Expand All @@ -439,7 +459,12 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {
{
builder->SetInsertPoint(loop_test_bb);
auto cond = builder->CreateICmp(
llvm::CmpInst::Predicate::ICMP_SLT, builder->CreateLoad(loop_index),
llvm::CmpInst::Predicate::ICMP_SLT,
builder->CreateLoad(
#ifdef TI_LLVM_15
i32_ty,
#endif
loop_index),
llvm_val[stmt->owned_num_local.find(stmt->major_from_type)
->second]);
builder->CreateCondBr(cond, loop_body_bb, func_exit);
Expand All @@ -452,9 +477,13 @@ class CodeGenLLVMCUDA : public CodeGenLLVM {
auto &s = stmt->body->statements[i];
s->accept(this);
}
builder->CreateStore(
builder->CreateAdd(builder->CreateLoad(loop_index), block_dim),
loop_index);
builder->CreateStore(builder->CreateAdd(builder->CreateLoad(
#ifdef TI_LLVM_15
i32_ty,
#endif
loop_index),
block_dim),
loop_index);
builder->CreateBr(loop_test_bb);
builder->SetInsertPoint(func_exit);
}
Expand Down
Loading

0 comments on commit 0d7168f

Please sign in to comment.