diff --git a/taichi/runtime/llvm/llvm_aot_module_builder.cpp b/taichi/runtime/llvm/llvm_aot_module_builder.cpp index 002fbd1f2d66b..76de10854efeb 100644 --- a/taichi/runtime/llvm/llvm_aot_module_builder.cpp +++ b/taichi/runtime/llvm/llvm_aot_module_builder.cpp @@ -26,6 +26,8 @@ void LlvmAotModuleBuilder::add_per_backend(const std::string &identifier, kcache.owned_module = std::move(compiled.module); kcache.args = infer_launch_args(kernel); kcache.offloaded_task_list = std::move(compiled.tasks); + kcache.last_used_at = std::time(nullptr); + kcache.created_at = std::time(nullptr); cache_.kernels[identifier] = std::move(kcache); } @@ -64,7 +66,10 @@ void LlvmAotModuleBuilder::add_field_per_backend(const std::string &identifier, void LlvmAotModuleBuilder::add_compiled_kernel(aot::Kernel *kernel) { auto *kernel_impl = dynamic_cast(kernel); TI_ASSERT(kernel_impl); - + if (!kernel_impl->kernel_data_.created_at) { + kernel_impl->kernel_data_.last_used_at = std::time(nullptr); + kernel_impl->kernel_data_.created_at = std::time(nullptr); + } const std::string &kernel_name = kernel_impl->kernel_name_; if (cache_.kernels.find(kernel_name) == cache_.kernels.end()) { cache_.kernels[kernel_name] = std::move(kernel_impl->kernel_data_); diff --git a/taichi/runtime/llvm/llvm_offline_cache.cpp b/taichi/runtime/llvm/llvm_offline_cache.cpp index 024f7a6188c4d..8f4a017b0c62f 100644 --- a/taichi/runtime/llvm/llvm_offline_cache.cpp +++ b/taichi/runtime/llvm/llvm_offline_cache.cpp @@ -115,6 +115,10 @@ bool LlvmOfflineCacheFileReader::get_kernel_cache( kernel_data.module = kernel_data.owned_module.get(); } + kernel_data.last_used_at = std::time(nullptr); + + res.created_at = kernel_data.created_at; + res.last_used_at = kernel_data.last_used_at; res.kernel_key = key; res.args = kernel_data.args; res.offloaded_task_list = kernel_data.offloaded_task_list; @@ -146,7 +150,6 @@ void LlvmOfflineCacheFileWriter::dump(const std::string &path, LlvmOfflineCache::Format format, bool merge_with_old) { taichi::create_directories(path); - std::time_t now = std::time(nullptr); std::size_t new_kernels_size = 0; // bytes for (auto &[k, v] : data_.kernels) { @@ -185,8 +188,8 @@ void LlvmOfflineCacheFileWriter::dump(const std::string &path, } // Set meta info - v.created_at = now; - v.last_used_at = now; + TI_ASSERT(v.created_at); + TI_ASSERT(v.last_used_at); v.size = size; TI_ASSERT(v.size > 0); new_kernels_size += v.size; diff --git a/taichi/runtime/program_impls/llvm/llvm_program.cpp b/taichi/runtime/program_impls/llvm/llvm_program.cpp index d05e10b1bc521..603ad87ed9cc7 100644 --- a/taichi/runtime/program_impls/llvm/llvm_program.cpp +++ b/taichi/runtime/program_impls/llvm/llvm_program.cpp @@ -126,6 +126,8 @@ void LlvmProgramImpl::cache_kernel( return; } auto &kernel_cache = cache_data_->kernels[kernel_key]; + kernel_cache.created_at = std::time(nullptr); + kernel_cache.last_used_at = std::time(nullptr); kernel_cache.kernel_key = kernel_key; kernel_cache.owned_module = llvm::CloneModule(*module); kernel_cache.args = std::move(args); diff --git a/tests/cpp/llvm/llvm_offline_cache_test.cpp b/tests/cpp/llvm/llvm_offline_cache_test.cpp index 7ddc3ece1e0b8..0e2e69937962d 100644 --- a/tests/cpp/llvm/llvm_offline_cache_test.cpp +++ b/tests/cpp/llvm/llvm_offline_cache_test.cpp @@ -96,6 +96,8 @@ TEST_P(LlvmOfflineCacheTest, ReadWrite) { LlvmOfflineCacheFileWriter writer; LlvmOfflineCache::KernelCacheData kcache; + kcache.created_at = 1; + kcache.last_used_at = 1; kcache.kernel_key = kKernelName; kcache.owned_module = make_module(*llvm_ctx); kcache.module = kcache.owned_module.get();