Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm] (Decomp of #5251 10/n) Make SNode tree compatible with parallel compilation #5390

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions taichi/runtime/llvm/llvm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,16 +515,30 @@ std::unique_ptr<llvm::Module> TaichiLLVMContext::clone_struct_module() {

void TaichiLLVMContext::set_struct_module(
const std::unique_ptr<llvm::Module> &module) {
auto data = get_this_thread_data();
TI_ASSERT(std::this_thread::get_id() == main_thread_id_);
auto this_thread_data = get_this_thread_data();
TI_ASSERT(module);
if (llvm::verifyModule(*module, &llvm::errs())) {
module->print(llvm::errs(), nullptr);
TI_ERROR("module broken");
}
// TODO: Move this after ``if (!arch_is_cpu(arch))``.
data->struct_module = llvm::CloneModule(*module);
this_thread_data->struct_module = llvm::CloneModule(*module);
for (auto &[id, data] : per_thread_data_) {
if (id == std::this_thread::get_id()) {
continue;
}
TI_ASSERT(!data->runtime_module);
data->struct_module.reset();
old_contexts_.push_back(std::move(data->thread_safe_llvm_context));
data->thread_safe_llvm_context =
std::make_unique<llvm::orc::ThreadSafeContext>(
std::make_unique<llvm::LLVMContext>());
data->llvm_context = data->thread_safe_llvm_context->getContext();
data->struct_module = clone_module_to_context(
this_thread_data->struct_module.get(), data->llvm_context);
}
}

template <typename T>
llvm::Value *TaichiLLVMContext::get_constant(DataType dt, T t) {
auto ctx = get_this_thread_context();
Expand Down Expand Up @@ -823,12 +837,23 @@ void TaichiLLVMContext::delete_functions_of_snode_tree(int id) {
func->eraseFromParent();
}
snode_tree_funcs_.erase(id);
set_struct_module(get_this_thread_data()->struct_module);
}

void TaichiLLVMContext::add_function_to_snode_tree(int id, std::string func) {
snode_tree_funcs_[id].push_back(func);
}

TaichiLLVMContext::ThreadLocalData::~ThreadLocalData() {
if (struct_module) {
TI_ASSERT(&struct_module->getContext() ==
thread_safe_llvm_context->getContext());
}
runtime_module.reset();
struct_module.reset();
thread_safe_llvm_context.reset();
}

TI_REGISTER_TASK(make_slim_libdevice);

} // namespace lang
Expand Down
4 changes: 4 additions & 0 deletions taichi/runtime/llvm/llvm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TaichiLLVMContext {
nullptr};
std::unique_ptr<llvm::Module> runtime_module{nullptr};
std::unique_ptr<llvm::Module> struct_module{nullptr};
~ThreadLocalData();
};

public:
Expand Down Expand Up @@ -162,6 +163,9 @@ class TaichiLLVMContext {
std::mutex thread_map_mut_;

std::unordered_map<int, std::vector<std::string>> snode_tree_funcs_;
std::vector<std::unique_ptr<llvm::orc::ThreadSafeContext>>
old_contexts_; // Contains old contexts that have modules stored inside
// the offline cache.
};

class LlvmModuleBitcodeLoader {
Expand Down