From 06ef7138c97632ff8e4a02dbf70c7fb57ebb88f5 Mon Sep 17 00:00:00 2001 From: pytorchbot Date: Mon, 27 Jan 2025 15:46:27 -0600 Subject: [PATCH] [ET-VK][mac] Allow vulkan binaries to work with Vulkan SDK + fixes to enable debugPrintf extension ## Context Add a buck config option to prevent linking against MoltenVK when building for MacOS, instead opting to use volk. This is useful for seeing debug print statments from GLSL code. Also implement some fixes to make things work when using system-installed Vulkan SDK, and some fixes to enable using debug Printf extension on Mac. Specifically, the fixes are as follows: * On newer versions of the Vulkan SDK, a portability instance extension needs to be explicitly enabled, otherwise creating the Vulkan instance will complain about the driver being incompatible. [source](https://stackoverflow.com/questions/58732459/vk-error-incompatible-driver-with-mac-os-and-vulkan-moltenvk) * VkConfig on Mac doesn't show options to enable debugprintf, therefore it has to be enabled programmatically. Differential Revision: [D68650936](https://our.internmc.facebook.com/intern/diff/D68650936/) ghstack-source-id: 263006772 Pull Request resolved: https://github.com/pytorch/executorch/pull/7957 Co-authored-by: Stephen Jia --- backends/vulkan/runtime/vk_api/Runtime.cpp | 54 ++++++++++++++++------ backends/vulkan/targets.bzl | 20 ++++++-- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/backends/vulkan/runtime/vk_api/Runtime.cpp b/backends/vulkan/runtime/vk_api/Runtime.cpp index da03c4e86d..9b30aaacda 100644 --- a/backends/vulkan/runtime/vk_api/Runtime.cpp +++ b/backends/vulkan/runtime/vk_api/Runtime.cpp @@ -92,28 +92,52 @@ VkInstance create_instance(const RuntimeConfig& config) { std::vector enabled_layers; std::vector enabled_extensions; + std::vector requested_layers; + std::vector requested_extensions; + if (config.enable_validation_messages) { - std::vector requested_layers{ - // "VK_LAYER_LUNARG_api_dump", - "VK_LAYER_KHRONOS_validation", - }; - std::vector requested_extensions{ + requested_layers.emplace_back("VK_LAYER_KHRONOS_validation"); #ifdef VK_EXT_debug_report - VK_EXT_DEBUG_REPORT_EXTENSION_NAME, + requested_extensions.emplace_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); #endif /* VK_EXT_debug_report */ - }; - - find_requested_layers_and_extensions( - enabled_layers, - enabled_extensions, - requested_layers, - requested_extensions); } + VkInstanceCreateFlags instance_flags = 0; +#ifdef __APPLE__ + instance_flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + requested_extensions.emplace_back( + VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); +#endif + + find_requested_layers_and_extensions( + enabled_layers, + enabled_extensions, + requested_layers, + requested_extensions); + + const void* instance_create_next = nullptr; + // VkConfig on Mac platforms does not expose debugPrintf settings for whatever + // reason so it has to be enabled manually. +#if defined(__APPLE__) && defined(VULKAN_DEBUG) + std::vector enabled_validation_features{ + VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT, + }; + VkValidationFeaturesEXT validation_features = { + VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, // sType + nullptr, // pNext + static_cast( + enabled_validation_features.size()), // enabledValidationFeatureCount + enabled_validation_features.data(), // pEnabledValidationFeatures + 0, + nullptr, // pDisabledValidationFeatures + }; + instance_create_next = &validation_features; +#endif /* __APPLE__ && VULKAN_DEBUG */ + const VkInstanceCreateInfo instance_create_info{ VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType - nullptr, // pNext - 0u, // flags + instance_create_next, // pNext + instance_flags, // flags &application_info, // pApplicationInfo static_cast(enabled_layers.size()), // enabledLayerCount enabled_layers.data(), // ppEnabledLayerNames diff --git a/backends/vulkan/targets.bzl b/backends/vulkan/targets.bzl index b2f3d2d201..85b962784e 100644 --- a/backends/vulkan/targets.bzl +++ b/backends/vulkan/targets.bzl @@ -142,19 +142,31 @@ def define_common_targets(is_fbcode = False): "fbsource//third-party/swiftshader/lib/linux-x64:libvk_swiftshader_so", ] else: + link_moltenvk = read_config("etvk", "link_moltenvk", "1") == "1" + mac_deps = default_deps + if link_moltenvk: + mac_deps = [ + "//third-party/khronos:moltenVK_static" + ] + mac_flags = default_flags + if link_moltenvk: + mac_flags = [] + VK_API_DEPS += select({ "DEFAULT": default_deps, "ovr_config//os:android": android_deps, - "ovr_config//os:macos": [ - "//third-party/khronos:moltenVK_static" - ], + "ovr_config//os:macos": mac_deps, }) VK_API_PREPROCESSOR_FLAGS += select({ "DEFAULT": default_flags, "ovr_config//os:android": android_flags, - "ovr_config//os:macos": [] + "ovr_config//os:macos": mac_flags, }) + debug_mode = read_config("etvk", "debug", "0") == "1" + if debug_mode: + VK_API_PREPROCESSOR_FLAGS += ["-DVULKAN_DEBUG"] + runtime.cxx_library( name = "vulkan_compute_api{}".format(suffix), compiler_flags = get_vulkan_compiler_flags(),