Skip to content

Commit

Permalink
[refactor] Implement CompiledKernelData::check() (taichi-dev#7743)
Browse files Browse the repository at this point in the history
Issue: taichi-dev#7002 

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 8d2c768</samp>

This pull request improves the error handling and debugging of the
kernel compilation and caching process using the LLVM backend. It adds a
`check` function to the `CompiledKernelData` class that verifies the
LLVM module and tasks, and uses it to assert and report the status of
the data.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 8d2c768</samp>

* Add a new error enum value and message for the case when the
CompiledKernelData is broken
([link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-5e2472488f9620231c8e3d6a2c0413742e2b42424691991f4a85af81832af3f4R95),
[link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-70ec30330946b7543dcf0b458091cfc6128a5cb5460041db64144b84722de4abR184-R185))
* Implement a check function for the CompiledKernelData class that
verifies the LLVM module and the tasks stored in the data using the LLVM
verifier
([link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-3986d4b2137cab0463bb950113c0ddc44cfdf3baff237da6286c42c478309c3bR3),
[link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-3986d4b2137cab0463bb950113c0ddc44cfdf3baff237da6286c42c478309c3bR30-R43),
[link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-e54a1ed2c2d35f9357e4dfbbbc1224e70a95280b43d4a59f0017dc2c6163dca0R55-R56))
* Add assertions and checks for the result of the check function after
compiling or loading a kernel using the LLVM backend, and modify the
debug message to include the cache filename
([link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-b7662dbf5bcf20f4b99048f4f2405316c0ba037c0722273522efaad72d256ef0R178),
[link](https://github.com/taichi-dev/taichi/pull/7743/files?diff=unified&w=0#diff-b7662dbf5bcf20f4b99048f4f2405316c0ba037c0722273522efaad72d256ef0L266-R275))
  • Loading branch information
PGZXB authored and quadpixels committed May 13, 2023
1 parent 9386ab3 commit 13a08c8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions taichi/codegen/compiled_kernel_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ std::string CompiledKernelData::get_err_msg(Err err) {
return "The taichi is not built with llvm";
case Err::kTiWithoutSpirv:
return "The taichi is not built with spirv";
case Err::kCompiledKernelDataBroken:
return "The CompiledKernelData is broken";
case Err::kUnknown:
return "Unkown error";
}
Expand Down
1 change: 1 addition & 0 deletions taichi/codegen/compiled_kernel_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class CompiledKernelData {
kOutOfMemory,
kTiWithoutLLVM,
kTiWithoutSpirv,
kCompiledKernelDataBroken,
kUnknown,
};

Expand Down
15 changes: 15 additions & 0 deletions taichi/codegen/llvm/compiled_kernel_data.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "taichi/codegen/llvm/compiled_kernel_data.h"

#include "llvm/IR/Verifier.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/Support/SourceMgr.h"

Expand All @@ -26,6 +27,20 @@ std::unique_ptr<lang::CompiledKernelData> CompiledKernelData::clone() const {
return std::make_unique<CompiledKernelData>(arch_, data_);
}

CompiledKernelData::Err CompiledKernelData::check() const {
const auto &compiled_data = data_.compiled_data;
const auto &tasks = compiled_data.tasks;
if (llvm::verifyModule(*compiled_data.module, &llvm::errs())) {
return Err::kCompiledKernelDataBroken;
}
for (const auto &t : tasks) {
if (compiled_data.module->getFunction(t.name) == nullptr) {
return Err::kCompiledKernelDataBroken;
}
}
return Err::kNoError;
}

CompiledKernelData::Err CompiledKernelData::load_impl(
const CompiledKernelDataFile &file) {
arch_ = file.arch();
Expand Down
2 changes: 2 additions & 0 deletions taichi/codegen/llvm/compiled_kernel_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CompiledKernelData : public lang::CompiledKernelData {
Arch arch() const override;
std::unique_ptr<lang::CompiledKernelData> clone() const override;

Err check() const override;

const InternalData &get_internal_data() const {
return data_;
}
Expand Down
8 changes: 7 additions & 1 deletion taichi/compilation_manager/kernel_compilation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ std::unique_ptr<CompiledKernelData> KernelCompilationManager::compile_kernel(
auto &compiler = *config_.kernel_compiler;
auto ir = compiler.compile(compile_config, kernel_def);
auto ckd = compiler.compile(compile_config, caps, kernel_def, *ir);
TI_ASSERT(ckd->check() == CompiledKernelData::Err::kNoError);
return ckd;
}

Expand Down Expand Up @@ -263,7 +264,12 @@ std::unique_ptr<CompiledKernelData> KernelCompilationManager::load_ckd(
CompiledKernelData::Err err;
auto ckd = CompiledKernelData::load(ifs, &err);
if (err != CompiledKernelData::Err::kNoError) {
TI_DEBUG("Load cached CompiledKernelData file failed: {}",
TI_DEBUG("Load cache file {} failed: {}", filename,
CompiledKernelData::get_err_msg(err));
return nullptr;
}
if (auto err = ckd->check(); err != CompiledKernelData::Err::kNoError) {
TI_DEBUG("Check CompiledKernelData loaded from {} failed: {}", filename,
CompiledKernelData::get_err_msg(err));
return nullptr;
}
Expand Down

0 comments on commit 13a08c8

Please sign in to comment.