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

[aot] [llvm] LLVM AOT Field #1: Adjust serialization/deserialization logics for FieldCacheData #5111

Merged
merged 4 commits into from
Jun 10, 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
14 changes: 14 additions & 0 deletions taichi/llvm/llvm_offline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ LlvmOfflineCacheFileReader::LlvmOfflineCacheFileReader(
: path_(path), data_(std::move(data)), format_(format) {
}

bool LlvmOfflineCacheFileReader::get_field_cache(
LlvmOfflineCache::FieldCacheData &res,
int snode_tree_id) {
auto itr = data_.fields.find(snode_tree_id);
if (itr == data_.fields.end()) {
TI_DEBUG("Cannot find field with snode_tree_id={}", snode_tree_id);
return false;
}

const auto &loaded_field_cache = itr->second;
res = loaded_field_cache; // copy assign
return true;
}

bool LlvmOfflineCacheFileReader::get_kernel_cache(
LlvmOfflineCache::KernelCacheData &res,
const std::string &key,
Expand Down
32 changes: 28 additions & 4 deletions taichi/llvm/llvm_offline_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,31 @@ struct LlvmOfflineCache {

TI_IO_DEF(tree_id, root_id, root_size, snode_metas);

// TODO(zhanlue)
// Serialize/Deserialize the llvm::Module from StructCompiler
// At runtime, make sure loaded Field-Modules and Kernel-Modules are linked
// altogether.
// TODO(zhanlue): refactor llvm::Modules
//
// struct_module will eventually get cloned into each kernel_module,
// so there's no need to serialize it here.
//
// We have three different types of llvm::Module
// 1. runtime_module: contains runtime functions.
// 2. struct_module: contains compiled SNodeTree in llvm::Type.
// 3. kernel_modules: contains compiled kernel codes.
//
// The way those modules work rely on a recursive clone mechanism:
// runtime_module = load("runtime.bc")
// struct_module = clone(runtime_module) + compiled-SNodeTree
// kernel_module = clone(struct_module) + compiled-Kernel
//
// As a result, every kernel_module contains a copy of struct_module +
// runtime_module.
//
// This recursive clone mechanism is super fragile,
// which potentially causes inconsistency between modules if not handled
// properly.
//
// Let's turn to use llvm::link to bind the modules,
// and make runtime_module, struct_module, kernel_module independent of each
// other
};

// TODO(zhanlue): we need a better identifier for each FieldCacheData
Expand All @@ -83,6 +104,9 @@ class LlvmOfflineCacheFileReader {
const std::string &key,
llvm::LLVMContext &llvm_ctx);

bool get_field_cache(LlvmOfflineCache::FieldCacheData &res,
int snode_tree_id);

static std::unique_ptr<LlvmOfflineCacheFileReader> make(
const std::string &path,
LlvmOfflineCache::Format format = LlvmOfflineCache::Format::LL);
Expand Down