Files
mandy/runtime/test/vulkan_loader.h
Hamza 5c9315d971 Test tools: turn MoltenVK fast-math off so NaN compares are exact (SHADER-234)
MoltenVK compiles MSL with Metal fast-math on by default, and Metal then folds
an f32 compare of a NaN with itself: v_cmpx_eq/le_f32 on (NaN, NaN) give true
(ps_vcmpx_forms fails on every Mac). The MSL is byte-identical with fast-math
on and off, so it's Metal's compile option, not the translation; "on demand"
(2) doesn't help, 0 does.

runtime/test/vulkan_loader.h, which every Vulkan test program loads MoltenVK
through, now sets MVK_CONFIG_FAST_MATH_ENABLED=0 before loading it unless the
variable is already set. docs/design.md gets a contract row: fast-math off
doesn't remove the optnone NoContraction helpers (the cost part, @Jolymmiles)
and doesn't fix SHADER-182 (same valu_fuzz f16 mismatch either way).

Apple A18 Pro, macOS 26.6.2, MoltenVK 1.4.2: ps_vcmpx_forms 4096/4096 exact;
run_gpu_tests.py 85 pass/52 skip/2 fail -> 86/52/1 native, 114/23/2 ->
115/23/1 with wave64 emulation (the remaining failure is ps_interp_f16_high,
fixed by #1407); ctest 25/25 either way; semantic suite 1220 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AI-assisted
2026-11-22 14:48:31 +03:00

58 lines
2.4 KiB
C++

// Loads Vulkan at run time. The macOS test tools default to MoltenVK.
#pragma once
#ifndef VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES
#endif
#include <vulkan/vulkan.h>
#include <cstdio>
#include <cstdlib>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
inline PFN_vkGetInstanceProcAddr load_vulkan() {
// Keep the Vulkan library loaded for the lifetime of the tool.
#ifdef _WIN32
constexpr auto library_name = "vulkan-1.dll";
static const auto library = LoadLibraryA(library_name);
const auto entry = library ? GetProcAddress(library, "vkGetInstanceProcAddr") : nullptr;
#elif defined(__APPLE__)
static const auto entry = []() -> void* {
// MoltenVK compiles MSL with fast-math on by default, which lets Metal fold an f32 compare of a NaN with
// itself to "ordered" (v_cmpx_eq/le_f32 on (NaN, NaN) give true; ps_vcmpx_forms). Translated shaders need
// IEEE results, so turn it off before MoltenVK reads its configuration, unless the user set the variable
// (docs/design.md, "MoltenVK fast-math"). The same variable reaches MoltenVK behind the Vulkan loader.
setenv("MVK_CONFIG_FAST_MATH_ENABLED", "0", 0);
// The default is direct MoltenVK: a portability driver would otherwise require instance opt-in.
const char* requested = std::getenv("LEONIDA_VULKAN_LOADER");
const bool overridden = requested && *requested;
const char* name = overridden ? requested : "libMoltenVK.dylib";
auto library = dlopen(name, RTLD_NOW | RTLD_LOCAL);
if (!library && !overridden) {
name = "/opt/homebrew/lib/libMoltenVK.dylib";
library = dlopen(name, RTLD_NOW | RTLD_LOCAL);
}
if (!library) {
std::fprintf(stderr, "could not load %s: %s\n", name, dlerror());
return nullptr;
}
const auto symbol = dlsym(library, "vkGetInstanceProcAddr");
if (!symbol) std::fprintf(stderr, "%s has no vkGetInstanceProcAddr\n", name);
return symbol;
}();
#else
constexpr auto library_name = "libvulkan.so.1";
static const auto library = dlopen(library_name, RTLD_NOW | RTLD_LOCAL);
const auto entry = library ? dlsym(library, "vkGetInstanceProcAddr") : nullptr;
#endif
#ifndef __APPLE__
if (!entry) std::fprintf(stderr, "could not load vkGetInstanceProcAddr from %s\n", library_name);
#endif
return reinterpret_cast<PFN_vkGetInstanceProcAddr>(entry);
}