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

Precompiled SPIR-V import support #5048

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

cheneym2
Copy link
Collaborator

Adds appropriate linkage and function declaration syntax for SPIR-V functions that are declared, to be imported from another SPIR-V module.

Unlike DXIL, stripping the Slang IR for a function down to a declaration requires retaining a block of parameters, as the function declaration must be emitted to SPIR-V with the same parameters as a definition. Because that thwarts the logic in Slang to tell the difference between a declaration and definition, and explicit decoration is introduced to explicitly mark functions which need to be treated as declarations during emit phase.

Fixes #4992

Adds appropriate linkage and function declaration syntax
for SPIR-V functions that are declared, to be imported
from another SPIR-V module.

Unlike DXIL, stripping the Slang IR for a function down
to a declaration requires retaining a block of parameters,
as the function declaration must be emitted to SPIR-V
with the same parameters as a definition. Because that
thwarts the logic in Slang to tell the difference between
a declaration and definition, and explicit decoration is
introduced to explicitly mark functions which need to be
treated as declarations during emit phase.

Fixes shader-slang#4992
@cheneym2 cheneym2 added the pr: non-breaking PRs without breaking changes label Sep 10, 2024
source/slang/slang-ir.cpp Outdated Show resolved Hide resolved
source/slang/slang-ir-redundancy-removal.cpp Outdated Show resolved Hide resolved
source/slang/slang-ir-redundancy-removal.cpp Outdated Show resolved Hide resolved
source/slang/slang-ir-redundancy-removal.cpp Outdated Show resolved Hide resolved
Unify spirv and dxil paths more by using function type instead
of parameter ops when emitting spirv function declarations.
This allows for SPIR-V function declarations to be emitted from
Slang IR function declarations. The previous implementation required
that Slang IR function declarations retain blocks with OpParams
in them, though that both complicated function declaration emission
and created asymmetry between DXIL and SPIR-V paths.

To further ensure function parameters are correctly emitted for
SPIR-V function declarations, add a second parameter to a test
case using a different type, as spirv-val will complain when the
parameter list is incorrect.
for (UInt pp = 0; pp < paramCount; ++pp)
{
auto paramType = funcType->getParamType(pp);
emitOpFunctionParameter(spvFunc, nullptr, paramType);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to make sure to emit necessary decorations for pointer-typed parameters as well. Need to call maybeEmitPointerDecoration here.

Copy link
Collaborator Author

@cheneym2 cheneym2 Sep 17, 2024

Choose a reason for hiding this comment

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

Looking at the implementation, it's for PhysicalStorageBuffer pointers and I'm unclear how to write a test case for that. What function parameter would I define in Slang that corresponds to that in spir-v?
I'm currently trying to find a test case that hits this path for function definitions to model after.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nm, I believe I have a test case now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Exporting this shows an "Alias" decoration applied from the function definition side.

public int normalFunc(int a, const float *b)
{
    return a - floor(*b);
}

Trying to replicate that on the import side function declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SPIR-V library support for import decorations on function declarations
2 participants