Skip to content

ggml: improve SPIR-V headers detection with __has_include while preserving original _WIN32 logic - #21918

Merged
0cc4m merged 4 commits into
ggml-org:masterfrom
EmilAskerov:ggml-vulkan-spirv-robust-include
Apr 28, 2026
Merged

0cc4m merged 4 commits into
ggml-org:masterfrom
EmilAskerov:ggml-vulkan-spirv-robust-include

Conversation

@EmilAskerov

@EmilAskerov EmilAskerov commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Fix SPIR-V header include detection in ggml-vulkan.cpp to 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.cpp requires C++17, this implementation relies entirely on standard __has_include and 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:

  • custom Qt / CMake projects
  • MinGW / llvm-mingw with official LunarG Vulkan SDK
  • different Linux package layouts

Example build error:

fatal error: 'spirv/unified1/spirv.hpp' file not found

Solution

Use __has_include to 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:

  • toolchain-independent
  • robust across SDK layouts
  • compatible with custom build systems
  • strictly C++17 compliant

Testing

Tested on:

  • Windows 11 (64-bit)
    • Qt Creator 19.0.0
    • Qt 6.11.0 (llvm-mingw 64-bit)
    • Vulkan SDK 1.4.341.1 (official LunarG)

Impact

  • Fixes build failures outside official CMake builds
  • No functional changes for standard builds
  • Fully backward compatible

Requirements

I have read and agree with the contributing guidelines

AI usage disclosure:

The code changes, testing, PR description were performed by the author.

@EmilAskerov
EmilAskerov requested a review from a team as a code owner April 14, 2026 19:37
@github-actions github-actions Bot added Vulkan Issues specific to the Vulkan backend ggml changes relating to the ggml tensor library for machine learning labels Apr 14, 2026
@ilevad

ilevad commented Apr 14, 2026

Copy link
Copy Markdown

Supporting this — '__has_include' approach is more robust

@0cc4m

0cc4m commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Please restore the PR description template and fill it out.

@EmilAskerov

Copy link
Copy Markdown
Contributor Author

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.

@gboddin

gboddin commented Apr 21, 2026

Copy link
Copy Markdown

For who it may concern, you might want to throw a :

#elif __has_include(<spirv_cross/spirv.hpp>)
#    include <spirv_cross/spirv.hpp>

for freebsd users!

https://www.freshports.org/graphics/spirv-cross/

Comment thread ggml/src/ggml-vulkan/ggml-vulkan.cpp Outdated
#elif __has_include(<spirv.hpp>)
# include <spirv.hpp>
#elif defined(_WIN32) && !defined(__MINGW32__)
# include <spirv-headers/spirv.hpp>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this fallback or the one below ever be reached, assuming __has_include works as intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@EmilAskerov
EmilAskerov force-pushed the ggml-vulkan-spirv-robust-include branch from 413951b to b7e5c41 Compare April 21, 2026 09:54
@EmilAskerov

Copy link
Copy Markdown
Contributor Author

For who it may concern, you might want to throw a :

#elif __has_include(<spirv_cross/spirv.hpp>)
#    include <spirv_cross/spirv.hpp>

for freebsd users!

https://www.freshports.org/graphics/spirv-cross/

Thanks for the tip! I've added <spirv_cross/spirv.hpp> for FreeBSD users in the latest commit.

@EmilAskerov

Copy link
Copy Markdown
Contributor Author

Supporting this — '__has_include' approach is more robust

Thanks for the support!

@0cc4m

0cc4m commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

For who it may concern, you might want to throw a :

#elif __has_include(<spirv_cross/spirv.hpp>)
#    include <spirv_cross/spirv.hpp>

for freebsd users!

https://www.freshports.org/graphics/spirv-cross/

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.

@EmilAskerov

Copy link
Copy Markdown
Contributor Author

For who it may concern, you might want to throw a :

#elif __has_include(<spirv_cross/spirv.hpp>)
#    include <spirv_cross/spirv.hpp>

for freebsd users!
https://www.freshports.org/graphics/spirv-cross/

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!

@jeffbolznv

Copy link
Copy Markdown
Contributor

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.

@Peter0x44

Copy link
Copy Markdown
Contributor

w64devkit's llama.mak is also broken without this change, because I use mingw + windows vulkan SDK.
https://github.com/skeeto/w64devkit/blob/master/contrib/llama.mak

You can test it by placing llama.mak in the root of the repo, and running:
$ make -f llama.mak -j32 VULKAN_SDK="C:/VulkanSDK/1.4.328.1"

So it gets a +1 from me.

Comment thread ggml/src/ggml-vulkan/ggml-vulkan.cpp Outdated
# endif
#else
#include <spirv/unified1/spirv.hpp>
// Fallback logic for older compilers that do not support __has_include

@Peter0x44 Peter0x44 Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peter0x44 @0cc4m Removed the redundant C++17 fallback block. Thanks for pointing it out!

@EmilAskerov

Copy link
Copy Markdown
Contributor Author

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.

@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.

@EmilAskerov
EmilAskerov requested review from 0cc4m and Peter0x44 April 27, 2026 17:41
@EmilAskerov

Copy link
Copy Markdown
Contributor Author

@0cc4m @Peter0x44 It looks like two CI jobs failed due to unrelated infrastructure/flaky issues:

ggml-ci-win-intel-vulkan was cancelled after ~3 hours (timeout/runner issue).

ggml-ci-x64-cpu-low-perf is a CPU test and doesn't even compile ggml-vulkan.cpp.

All primary Vulkan builds passed successfully. Could you please re-trigger the failed jobs when you have a moment?

@Peter0x44 Peter0x44 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have zero power to commit anything but LGTM.

@0cc4m

0cc4m commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

@ggml-org/maintainers Another approval needed.

@0cc4m

0cc4m commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Thank you for the contribution.

@0cc4m
0cc4m merged commit 698d19b into ggml-org:master Apr 28, 2026
45 of 47 checks passed
IntelNav pushed a commit to IntelNav/llama.cpp that referenced this pull request Apr 29, 2026
…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
@jeffbolznv

Copy link
Copy Markdown
Contributor

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.

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.

@EmilAskerov

Copy link
Copy Markdown
Contributor Author

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.

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.

baramofme pushed a commit to baramofme/llama-cpp-turboquant that referenced this pull request May 23, 2026
…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
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* 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
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
…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
@EmilAskerov
EmilAskerov deleted the ggml-vulkan-spirv-robust-include branch October 19, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Vulkan Issues specific to the Vulkan backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants