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

[llvm] [aot] Add LLVM to CAPI part 9: Added AOT field tests for LLVM backend in C-API #5461

Merged
merged 11 commits into from
Jul 21, 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
13 changes: 13 additions & 0 deletions c_api/src/c_api_test_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "c_api_test_utils.h"
#include "taichi_llvm_impl.h"
#include "taichi/platform/cuda/detect_cuda.h"

#ifdef TI_WITH_VULKAN
Expand All @@ -20,5 +21,17 @@ bool is_cuda_available() {
return taichi::is_cuda_api_available();
}

void check_runtime_error(TiRuntime runtime) {
#ifdef TI_WITH_LLVM
auto *llvm_runtime = dynamic_cast<capi::LlvmRuntime *>((Runtime *)runtime);
if (!llvm_runtime) {
TI_NOT_IMPLEMENTED;
}
llvm_runtime->check_runtime_error();
#else
TI_NOT_IMPLEMENTED;
#endif
}

} // namespace utils
} // namespace capi
2 changes: 2 additions & 0 deletions c_api/src/c_api_test_utils.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once
#include <taichi/taichi_platform.h>
#include "taichi/taichi_core.h"

namespace capi {
namespace utils {

TI_DLL_EXPORT bool TI_API_CALL is_vulkan_available();
TI_DLL_EXPORT bool TI_API_CALL is_cuda_available();
TI_DLL_EXPORT void TI_API_CALL check_runtime_error(TiRuntime runtime);
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved

} // namespace utils
} // namespace capi
4 changes: 4 additions & 0 deletions c_api/src/taichi_llvm_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ LlvmRuntime::LlvmRuntime(taichi::Arch arch) : Runtime(arch) {
&result_buffer);
}

void LlvmRuntime::check_runtime_error() {
executor_->check_runtime_error(this->result_buffer);
}

taichi::lang::Device &LlvmRuntime::get() {
taichi::lang::Device *device = executor_->get_compute_device();
return *device;
Expand Down
2 changes: 2 additions & 0 deletions c_api/src/taichi_llvm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class LlvmRuntime : public Runtime {
public:
LlvmRuntime(taichi::Arch arch);

void check_runtime_error();

private:
/* Internally used interfaces */
taichi::lang::Device &get() override;
Expand Down
65 changes: 65 additions & 0 deletions c_api/tests/c_api_aot_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,71 @@ void kernel_aot_test(TiArch arch) {
ti_destroy_runtime(runtime);
}

void field_aot_test(TiArch arch) {
int base_val = 10;

const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH");

std::stringstream aot_mod_ss;
aot_mod_ss << folder_dir;

TiRuntime runtime = ti_create_runtime(arch);

// Load Aot and Kernel
TiAotModule aot_mod = ti_load_aot_module(runtime, aot_mod_ss.str().c_str());

TiKernel k_init_fields = ti_get_aot_module_kernel(aot_mod, "init_fields");
TiKernel k_check_init_x = ti_get_aot_module_kernel(aot_mod, "check_init_x");
TiKernel k_check_init_y = ti_get_aot_module_kernel(aot_mod, "check_init_y");
TiKernel k_deactivate_pointer_fields =
ti_get_aot_module_kernel(aot_mod, "deactivate_pointer_fields");
TiKernel k_activate_pointer_fields =
ti_get_aot_module_kernel(aot_mod, "activate_pointer_fields");
TiKernel k_check_deactivate_pointer_fields =
ti_get_aot_module_kernel(aot_mod, "check_deactivate_pointer_fields");
TiKernel k_check_activate_pointer_fields =
ti_get_aot_module_kernel(aot_mod, "check_activate_pointer_fields");

// Prepare Arguments
TiArgument base_arg = {.type = TiArgumentType::TI_ARGUMENT_TYPE_I32,
.value = {.i32 = base_val}};

constexpr uint32_t arg_count = 1;
TiArgument args[arg_count] = {std::move(base_arg)};

// Kernel Execution
ti_launch_kernel(runtime, k_init_fields, arg_count, &args[0]);
ti_launch_kernel(runtime, k_check_init_x, arg_count, &args[0]);
ti_launch_kernel(runtime, k_check_init_y, 0 /*arg_count*/, &args[0]);

ti_launch_kernel(runtime, k_deactivate_pointer_fields, 0 /*arg_count*/,
&args[0]);
ti_launch_kernel(runtime, k_check_deactivate_pointer_fields, 0 /*arg_count*/,
&args[0]);
ti_launch_kernel(runtime, k_activate_pointer_fields, 0 /*arg_count*/,
&args[0]);
ti_launch_kernel(runtime, k_check_activate_pointer_fields, 0 /*arg_count*/,
&args[0]);

// Check Results
capi::utils::check_runtime_error(runtime);

ti_destroy_aot_module(aot_mod);
ti_destroy_runtime(runtime);
}

TEST(CapiAotTest, CpuField) {
TiArch arch = TiArch::TI_ARCH_X64;
field_aot_test(arch);
}

TEST(CapiAotTest, CudaField) {
if (capi::utils::is_cuda_available()) {
TiArch arch = TiArch::TI_ARCH_CUDA;
field_aot_test(arch);
}
}

TEST(CapiAotTest, CpuKernel) {
TiArch arch = TiArch::TI_ARCH_X64;
kernel_aot_test(arch);
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
}

__capi_aot_test_cases = {
"CapiAotTest.CpuField":
[os.path.join('cpp', 'aot', 'llvm', 'field_aot_test.py'), "--arch=cpu"],
"CapiAotTest.CudaField":
[os.path.join('cpp', 'aot', 'llvm', 'field_aot_test.py'), "--arch=cuda"],
"CapiGraphTest.CpuGraph":
[os.path.join('cpp', 'aot', 'llvm', 'graph_aot_test.py'), "--arch=cpu"],
"CapiGraphTest.CudaGraph":
Expand Down