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

MVKShaderLibrary: Handle specializtion with macros (v2) #2441

Merged
merged 3 commits into from
Feb 18, 2025

Conversation

dboyan
Copy link
Contributor

@dboyan dboyan commented Feb 10, 2025

The converted MSL may use macro instead of function constants to realize spirv specialization constant for various reasons (e.g. when the constant is used as array size).

In this case, we should define the macros at shader compilation stage and generate different variants of the metal shader library depending on macro-value mapping to make specialization work properly when we cannot rely on metal's specialization.

There have been multiple use cases that require the proper handling we are missing, including llama.cpp and Vulkan CTS tests on VK_KHR_zero_initialize_workgroup_memory (we haven't supported it yet but I'm working on it).

This PR supersedes #2434, and is intended to be a version that can be mainlined. It is almost functionally equivalent to the previous PR (with some correctness fixes), but uses a new API from SPIRV-Cross to obtain the information of macros instead of unreliable heuristics to guess from the translated shader code. The new SPIRV-Cross API has been merged (KhronosGroup/SPIRV-Cross#2442).

There is one caveat though. Currently I assume the specialization constants (that are turned into macros) are 32-bit integers. It works on all uses I have seen because they can only be sizes of arrays, which are integers and usually represented with 32-bit values. I'm not very sure if my handling is enough or the most appropriate approach here.
The current version is taking types into account.

Fixes: #2423

@CLAassistant
Copy link

CLAassistant commented Feb 10, 2025

CLA assistant check
All committers have signed the CLA.

as a part of SPIRVToMSLConversionResultInfo, to be used later when processing
specialization constants as macro need special handling at compile time vs.
function constant.
The converted MSL may use macro instead of function constants to realize
spirv specialization constant for various reasons (e.g. when the constant
is used as array size).

In this case, we should define the macros at shader compilation stage and
generate different variants of the metal shader library depending on
macro-value mapping to make specializtion work properly when we cannot rely
on metal's specialization. We use the information from SPIRV-Cross to decide
the usage of macros and store variants of MTLLibrary according to macro values.
Copy link
Contributor

@billhollings billhollings left a comment

Choose a reason for hiding this comment

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

Thanks for all this work, both here and in SPIRV-Cross!

LGTM. Is this ready to be pulled in now?

@dboyan
Copy link
Contributor Author

dboyan commented Feb 17, 2025

Yes. I think so.

@billhollings billhollings merged commit 0b0e9a4 into KhronosGroup:main Feb 18, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MoltenVK bug people are discussing re: ollama, llama.cpp
3 participants