Skip to content

Commit

Permalink
[aot] [llvm] LLVM AOT Field #1: Adjust serialization/deserialization …
Browse files Browse the repository at this point in the history
…logics for FieldCacheData (#5111)

* [aot] [llvm] Implemented FieldCacheData and refactored initialize_llvm_runtime_snodes()

* Addressed compilation erros

* [aot] [llvm] LLVM AOT Field #1: Adjust serialization/deserialization logics for FieldCacheData
  • Loading branch information
jim19930609 authored Jun 10, 2022
1 parent 31ec9c3 commit 9c3da65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
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

0 comments on commit 9c3da65

Please sign in to comment.