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

[aot] Enable validation layers for C-API tests #6893

Merged
merged 12 commits into from
Dec 27, 2022
4 changes: 2 additions & 2 deletions .github/workflows/scripts/win_build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ if ($llvmVer -eq "10") {
$env:TAICHI_CMAKE_ARGS += " -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang"

if ($installVulkan) {
$env:VULKAN_SDK = "C:\VulkanSDK\1.2.189.0"
$env:VULKAN_SDK = "C:\VulkanSDK\1.3.236.0"
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
if (-not (Test-Path $env:VULKAN_SDK)) {
Info("Download and install Vulkan")
Invoke-WebRequest `
-Uri 'https://sdk.lunarg.com/sdk/download/1.2.189.0/windows/VulkanSDK-1.2.189.0-Installer.exe' `
-Uri 'https://sdk.lunarg.com/sdk/download/1.3.236.0/windows/VulkanSDK-1.3.236.0-Installer.exe' `
-MaximumRetryCount 10 -RetryIntervalSec 5 `
-OutFile VulkanSDK.exe
$installer = Start-Process -FilePath VulkanSDK.exe -Wait -PassThru -ArgumentList @("/S")
Expand Down
11 changes: 10 additions & 1 deletion c_api/src/taichi_core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "taichi/program/ndarray.h"
#include "taichi/program/texture.h"
#include "taichi/common/virtual_dir.h"
#include "taichi/common/utils.h"

bool is_vulkan_available() {
#ifdef TI_WITH_VULKAN
Expand Down Expand Up @@ -225,7 +226,15 @@ TiRuntime ti_create_runtime(TiArch arch) {
switch (arch) {
#ifdef TI_WITH_VULKAN
case TI_ARCH_VULKAN: {
out = (TiRuntime)(static_cast<Runtime *>(new VulkanRuntimeOwned));
VulkanRuntimeOwned *vulkan_runtime;
if (is_ci()) {
auto param = make_vulkan_runtime_creator_params();
param.enable_validation_layer = true;
vulkan_runtime = new VulkanRuntimeOwned(std::move(param));
} else {
vulkan_runtime = new VulkanRuntimeOwned;
}
out = (TiRuntime)(static_cast<Runtime *>(vulkan_runtime));
break;
}
#endif // TI_WITH_VULKAN
Expand Down
3 changes: 3 additions & 0 deletions c_api/src/taichi_vulkan_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ class VulkanRuntimeOwned : public VulkanRuntime {
virtual taichi::lang::gfx::GfxRuntime &get_gfx_runtime() override final;
};

taichi::lang::vulkan::VulkanDeviceCreator::Params
make_vulkan_runtime_creator_params();

#endif // TI_WITH_VULKAN
8 changes: 8 additions & 0 deletions c_api/tests/c_api_interface_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ TEST_F(CapiTest, FailMapDeviceOnlyMemory) {
ti::Memory mem = runtime.allocate_memory(100);
mem.map();

#ifdef __APPLE__
// Vulkan Validation aren't supported on MacOS platform
EXPECT_TAICHI_ERROR(TI_ERROR_INVALID_STATE, "Assertion failure",
/*reset_error=*/false);
EXPECT_TAICHI_ERROR(TI_ERROR_INVALID_STATE, "RHI map memory failed",
/*reset_error=*/true);
#else
EXPECT_TAICHI_ERROR(
TI_ERROR_INVALID_STATE,
"Mapping Memory without VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT set",
/*reset_error=*/true);
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions taichi/common/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cstdlib>

inline bool is_ci() {
char *res = std::getenv("TI_CI");
if (res == nullptr)
return false;
return std::stoi(res);
}
37 changes: 31 additions & 6 deletions taichi/rhi/vulkan/vulkan_device_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "taichi/rhi/vulkan/vulkan_common.h"
#include "taichi/rhi/vulkan/vulkan_loader.h"
#include "taichi/rhi/vulkan/vulkan_device.h"
#include "taichi/common/utils.h"

namespace taichi::lang {
namespace vulkan {
Expand Down Expand Up @@ -38,17 +39,26 @@ bool check_validation_layer_support() {
return true;
}

[[maybe_unused]] bool vk_ignore_validation_warning(
const std::string &msg_name) {
if (msg_name == "UNASSIGNED-DEBUG-PRINTF") {
// Ignore truncated Debug Printf message
return true;
}

if (msg_name == "VUID_Undefined") {
// FIXME: Remove this branch after upgrading Vulkan driver for built bots
return true;
}

return false;
}

VKAPI_ATTR VkBool32 VKAPI_CALL
vk_debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_type,
const VkDebugUtilsMessengerCallbackDataEXT *p_callback_data,
void *p_user_data) {
if (message_severity > VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
char msg_buf[512];
snprintf(msg_buf, sizeof(msg_buf), "Vulkan validation layer: %d, %s",
message_type, p_callback_data->pMessage);
RHI_LOG_ERROR(msg_buf);
}
if (message_type == VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT &&
message_severity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT &&
strstr(p_callback_data->pMessage, "DEBUG-PRINTF") != nullptr) {
Expand All @@ -57,6 +67,21 @@ vk_debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
auto const pos = msg.find_last_of("|");
std::cout << msg.substr(pos + 2);
}

if (message_severity > VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
char msg_buf[512];
snprintf(msg_buf, sizeof(msg_buf), "Vulkan validation layer: %d, %s",
message_type, p_callback_data->pMessage);

if (is_ci()) {
auto msg_name = std::string(p_callback_data->pMessageIdName);
if (!vk_ignore_validation_warning(msg_name))
TI_ERROR(msg_buf);
} else {
RHI_LOG_ERROR(msg_buf);
}
}

return VK_FALSE;
}

Expand Down