Repository navigation
ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic - #21918
Conversation
…rving original _WIN32 logic
|
Supporting this — '__has_include' approach is more robust |
|
Please restore the PR description template and fill it out. |
Done, restored the template and filled it out. Please let me know if anything else should be adjusted. |
|
For who it may concern, you might want to throw a : for freebsd users! |
| #elif __has_include(<spirv.hpp>) | ||
| # include <spirv.hpp> | ||
| #elif defined(_WIN32) && !defined(__MINGW32__) | ||
| # include <spirv-headers/spirv.hpp> |
There was a problem hiding this comment.
How would this fallback or the one below ever be reached, assuming __has_include works as intended?
There was a problem hiding this comment.
Good catch, thank you! You are completely right. If __has_include fails to find anything, my previous fallback would just cause a missing header error anyway. I have wrapped the new logic in #if defined(__has_include) and strictly separated the original logic into the #else block to serve as a true fallback for older compilers.
413951b to
b7e5c41
Compare
Thanks for the tip! I've added <spirv_cross/spirv.hpp> for FreeBSD users in the latest commit. |
Thanks for the support! |
Why spirv-cross and not a general spirv-header-like package? spirv-cross is for compiling spirv to other languages. It can contain the header, but it should also be available elsewhere. I can't check on that webpage cause it just tells me the server is busy. |
@0cc4m You are completely right. I looked into it, and spirv-cross is indeed a translation library that just happens to vendor spirv.hpp under its own namespace. The actual correct FreeBSD package is graphics/spirv-headers, which installs the headers into the standard Khronos layout: <spirv/unified1/spirv.hpp>. Since <spirv/unified1/spirv.hpp> is already our first __has_include check, standard FreeBSD setups are already fully covered without needing this workaround. @gboddin As noted above, the correct way on FreeBSD is to install graphics/spirv-headers rather than relying on spirv-cross. I have just pushed a commit to remove the spirv_cross fallback to keep the include paths standard and clean. Thanks for pointing this out! |
|
There's a bug open to make the windows SDK use the spirv/unified1/spirv.hpp location, so hopefully we can use that unconditionally in the not too distant future. If we need this more complex workaround until then, I guess that's fine. |
|
w64devkit's You can test it by placing llama.mak in the root of the repo, and running: So it gets a +1 from me. |
| # endif | ||
| #else | ||
| #include <spirv/unified1/spirv.hpp> | ||
| // Fallback logic for older compilers that do not support __has_include |
There was a problem hiding this comment.
FYI __has_include is part of C++17, and I think llama.cpp already requires it. So I don't think a real compiler will ever hit this.
Just a minor nit...
There was a problem hiding this comment.
@Peter0x44 @0cc4m Removed the redundant C++17 fallback block. Thanks for pointing it out!
@jeffbolznv Thanks for the context! Yeah, once the Windows SDK adopts the standard Khronos layout and it becomes widely used, we can definitely simplify this and just use <spirv/unified1/spirv.hpp> unconditionally. Until then, hopefully, this keeps everything building smoothly. |
|
@0cc4m @Peter0x44 It looks like two CI jobs failed due to unrelated infrastructure/flaky issues: All primary Vulkan builds passed successfully. Could you please re-trigger the failed jobs when you have a moment? |
Peter0x44
left a comment
There was a problem hiding this comment.
I have zero power to commit anything but LGTM.
|
@ggml-org/maintainers Another approval needed. |
|
Thank you for the contribution. |
…1918) * ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic * Address review comments: fix fallback logic and add FreeBSD support * Remove spirv_cross fallback as per review * Remove redundant __has_include check
This is fixed in the 1.4.350 SDK that was released today. There's not a great way in cmake to require a specific version of the SDK (you can potentially look at Vulkan_VERSION, but that could be misleading on other platforms). I don't think we're in any rush to remove the __has_include, but at some point in the future we probably should. |
Thanks for the update and the context. Glad the change was useful in the meantime. |
…1918) * ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic * Address review comments: fix fallback logic and add FreeBSD support * Remove spirv_cross fallback as per review * Remove redundant __has_include check
* ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic * Address review comments: fix fallback logic and add FreeBSD support * Remove spirv_cross fallback as per review * Remove redundant __has_include check
…1918) * ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic * Address review comments: fix fallback logic and add FreeBSD support * Remove spirv_cross fallback as per review * Remove redundant __has_include check
Overview
Fix SPIR-V header include detection in
ggml-vulkan.cppto support a wider range of real-world build environments.The previous platform-specific check:
_WIN32 && !__MINGW32__fails in several valid configurations and can lead to missing header errors.
This PR introduces a
__has_include-based detection mechanism that probes available include paths at compile time and selects the correct one automatically.Since
llama.cpprequires C++17, this implementation relies entirely on standard__has_includeand removes outdated platform-specific fallback logic, resulting in cleaner code.This primarily improves downstream integration of llama.cpp as a library.
Additional information
Problem
The existing logic breaks in multiple real-world scenarios:
Example build error:
Solution
Use
__has_includeto detect SPIR-V headers in a portable way:<spirv/unified1/spirv.hpp><spirv-headers/spirv.hpp><spirv.hpp>If none are found, it defaults to the standard Khronos layout to let the compiler throw a proper "file not found" error.
This approach is:
Testing
Tested on:
Impact
Requirements
I have read and agree with the contributing guidelines
AI usage disclosure:
The code changes, testing, PR description were performed by the author.