Skip to content

Commit

Permalink
[refactor] Remove the difficult-to-implement CompiledKernelData::size…
Browse files Browse the repository at this point in the history
…() (taichi-dev#7540)

Issue: taichi-dev#7002 

### Brief Summary
It is challenging to implement the function
`CompiledKernelData::size()`, particularly for the
`llvm::CompiledKernelData` class, as it is not possible to obtain the
size of the `llvm::Module` without dumping it.
  • Loading branch information
PGZXB authored and quadpixels committed May 13, 2023
1 parent 88ff9f1 commit dbd3793
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 21 deletions.
1 change: 0 additions & 1 deletion taichi/codegen/compiled_kernel_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class CompiledKernelData {
virtual ~CompiledKernelData() = default;

virtual Arch arch() const = 0;
virtual std::size_t size() const = 0;

Err load(std::istream &is);
Err dump(std::ostream &os) const;
Expand Down
12 changes: 0 additions & 12 deletions taichi/codegen/spirv/compiled_kernel_data.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "taichi/codegen/spirv/compiled_kernel_data.h"

#include <numeric>

namespace taichi::lang {

static std::unique_ptr<CompiledKernelData> new_spirv_compiled_kernel_data() {
Expand All @@ -21,16 +19,6 @@ Arch CompiledKernelData::arch() const {
return arch_;
}

std::size_t CompiledKernelData::size() const {
return sizeof(std::uint32_t) *
std::accumulate(
data_.src.spirv_src.begin(), data_.src.spirv_src.end(),
(std::size_t)0,
[](std::size_t val, const std::vector<std::uint32_t> &c) {
return val + c.size();
});
}

std::unique_ptr<lang::CompiledKernelData> CompiledKernelData::clone() const {
return std::make_unique<CompiledKernelData>(arch_, data_);
}
Expand Down
1 change: 0 additions & 1 deletion taichi/codegen/spirv/compiled_kernel_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class CompiledKernelData : public lang::CompiledKernelData {
CompiledKernelData(Arch arch, InternalData data);

Arch arch() const override;
std::size_t size() const override;
std::unique_ptr<lang::CompiledKernelData> clone() const override;

const InternalData &get_internal_data() const {
Expand Down
8 changes: 5 additions & 3 deletions taichi/compilation_manager/kernel_compilation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ void KernelCompilationManager::dump() {
for (auto &[kernel_key, kernel] : caching_kernels_) {
if (kernel.cache_mode == CacheData::MemAndDiskCache) {
auto [iter, ok] = kernels.insert({kernel_key, std::move(kernel)});
data.size += ok ? iter->second.size : 0;
TI_ASSERT(!ok || iter->second.size == 0);
}
}
// Dump cached CompiledKernelData to disk
for (const auto &[_, k] : kernels) {
for (auto &[_, k] : kernels) {
if (k.compiled_kernel_data) {
const auto arch = k.compiled_kernel_data->arch();
auto cache_filename = make_filename(k.kernel_key, arch);
if (try_lock_with_file(cache_filename)) {
std::ofstream fs{cache_filename, std::ios::out | std::ios::binary};
TI_ASSERT(fs.is_open());
k.compiled_kernel_data->dump(fs);
k.size = fs.tellp();
data.size += k.size;
}
}
}
Expand Down Expand Up @@ -235,7 +237,7 @@ const CompiledKernelData &KernelCompilationManager::compile_and_cache_kernel(
k.kernel_key = kernel_key;
k.created_at = k.last_used_at = std::time(nullptr);
k.compiled_kernel_data = compile_kernel(compile_config, caps, kernel_def);
k.size = k.compiled_kernel_data->size();
k.size = 0; // Populate `size` within the KernelCompilationManager::dump()
k.cache_mode = cache_mode;
const auto &kernel_data = (caching_kernels_[kernel_key] = std::move(k));
return *kernel_data.compiled_kernel_data;
Expand Down
4 changes: 0 additions & 4 deletions tests/cpp/codegen/compiled_kernel_data_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class FakeCompiledKernelData : public CompiledKernelData {
return kFakeArch;
}

std::size_t size() const override {
return compiled_data_.so_bin.size();
}

std::unique_ptr<CompiledKernelData> clone() const override {
return std::make_unique<FakeCompiledKernelData>(*this);
}
Expand Down

0 comments on commit dbd3793

Please sign in to comment.