Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 16, 2023
1 parent 2620a53 commit ec7ac36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,4 @@ if (TI_WITH_AMDGPU)
file(GLOB AMDGPU_BC_FILES ${CMAKE_SOURCE_DIR}/external/amdgpu_libdevice/*.bc)
install(FILES ${AMDGPU_BC_FILES}
DESTINATION ${INSTALL_LIB_DIR}/runtime)
endif()
endif()
94 changes: 48 additions & 46 deletions taichi/runtime/llvm/llvm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,29 +487,29 @@ std::unique_ptr<llvm::Module> TaichiLLVMContext::module_from_file(
}

#ifdef TI_WITH_AMDGPU
auto patch_amdgpu_kernel_dim = [&](std::string name,
llvm::Value *lhs) {
std::string actual_name;
if (name == "block_dim")
actual_name = "__ockl_get_local_size";
else if (name == "grid_dim")
actual_name = "__ockl_get_num_groups";
else
TI_ERROR("Unknown patch function name");
auto func = module->getFunction(name);
auto actual_func = module->getFunction(actual_name);
if (!func || !actual_func) {
return;
}
func->deleteBody();
auto bb = llvm::BasicBlock::Create(*ctx, "entry", func);
IRBuilder<> builder(*ctx);
builder.SetInsertPoint(bb);
auto dim_ = builder.CreateCall(actual_func->getFunctionType(), actual_func, {lhs});
auto ret_ = builder.CreateTrunc(dim_, llvm::Type::getInt32Ty(*ctx));
builder.CreateRet(ret_);
TaichiLLVMContext::mark_inline(func);
};
auto patch_amdgpu_kernel_dim = [&](std::string name, llvm::Value *lhs) {
std::string actual_name;
if (name == "block_dim")
actual_name = "__ockl_get_local_size";
else if (name == "grid_dim")
actual_name = "__ockl_get_num_groups";
else
TI_ERROR("Unknown patch function name");
auto func = module->getFunction(name);
auto actual_func = module->getFunction(actual_name);
if (!func || !actual_func) {
return;
}
func->deleteBody();
auto bb = llvm::BasicBlock::Create(*ctx, "entry", func);
IRBuilder<> builder(*ctx);
builder.SetInsertPoint(bb);
auto dim_ = builder.CreateCall(actual_func->getFunctionType(),
actual_func, {lhs});
auto ret_ = builder.CreateTrunc(dim_, llvm::Type::getInt32Ty(*ctx));
builder.CreateRet(ret_);
TaichiLLVMContext::mark_inline(func);
};
#endif

if (arch_ == Arch::amdgpu) {
Expand All @@ -526,8 +526,10 @@ std::unique_ptr<llvm::Module> TaichiLLVMContext::module_from_file(
patch_intrinsic("block_idx", llvm::Intrinsic::amdgcn_workgroup_id_x);

link_module_with_amdgpu_libdevice(module);
patch_dim("block_dim", llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0));
patch_dim("grid_dim", llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0));
patch_dim("block_dim",
llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0));
patch_dim("grid_dim",
llvm::ConstantInt::get(llvm::Type::getInt32Ty(*ctx), 0));
#endif
}
}
Expand Down Expand Up @@ -567,27 +569,25 @@ void TaichiLLVMContext::link_module_with_cuda_libdevice(
}
}

void TaichiLLVMContext:::link_module_with_amdgpu_libdevice(
std::unique_ptr<llvm::Module> &module) {
void TaichiLLVMContext:: : link_module_with_amdgpu_libdevice(
std::unique_ptr<llvm::Module> &module) {
TI_ASSERT(arch_ == Arch::amdgpu);
auto isa_version = AMDGPUContext::get_instance().get_mcpu().substr(3,4);
std::string libdevice_files[] = {
"ocml.bc",
"oclc_wavefrontsize64_off.bc",
"ockl.bc",
"oclc_abi_version_400.bc",
"oclc_correctly_rounded_sqrt_off.bc",
"oclc_daz_opt_off.bc",
"oclc_finite_only_off.bc",
"oclc_isa_version_" + isa_version + ".bc",
"oclc_unsafe_math_off.bc",
"opencl.bc"
};
auto isa_version = AMDGPUContext::get_instance().get_mcpu().substr(3, 4);
std::string libdevice_files[] = {"ocml.bc",
"oclc_wavefrontsize64_off.bc",
"ockl.bc",
"oclc_abi_version_400.bc",
"oclc_correctly_rounded_sqrt_off.bc",
"oclc_daz_opt_off.bc",
"oclc_finite_only_off.bc",
"oclc_isa_version_" + isa_version + ".bc",
"oclc_unsafe_math_off.bc",
"opencl.bc"};

for (auto &libdevice : libdevice_files) {
std::string lib_dir = runtime_lib_dir() + "/";
auto libdevice_module = module_from_bitcode_file(lib_dir + libdevice,
get_this_thread_context());
get_this_thread_context());

if (libdevice == "ocml.bc")
module->setDataLayout(libdevice_module->getDataLayout());
Expand All @@ -602,18 +602,20 @@ void TaichiLLVMContext:::link_module_with_amdgpu_libdevice(
for (auto &f : libdevice_module->functions()) {
auto func_ = module->getFunction(f.getName());
if (!func_ && starts_with(f.getName().lower(), "__" + libdevice))
f.setLinkage(llvm::Function::CommonLinkage);`
f.setLinkage(llvm::Function::CommonLinkage);
`
}

bool failed = llvm::Linker::linkModules(*module, std::move(libdevice_module));
bool failed =
llvm::Linker::linkModules(*module, std::move(libdevice_module));
if (failed) {
TI_ERROR("AMDGPU libdevice linking failure.");
}

for (auto func_name : libdevice_function_names) {
auto func = module->getFunction(func_name);
if (func)
func->setLinkage(llvm::Function::InternalLinkage);
auto func = module->getFunction(func_name);
if (func)
func->setLinkage(llvm::Function::InternalLinkage);
}
}
}
Expand Down

0 comments on commit ec7ac36

Please sign in to comment.