From 613a29affe272772dfe0c463d866fb0b8c1d42ee Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Tue, 29 Oct 2024 16:42:18 -0400 Subject: Precompiled SPIR-V import support (#5048) * Precompiled SPIR-V import support 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 Co-authored-by: Yong He --- tests/library/export-library-generics.slang | 4 +-- tests/library/module-library-pointer-param.slang | 10 +++++++ tests/library/precompiled-dxil-generics.slang | 2 +- tests/library/precompiled-spirv-generics.slang | 2 +- .../library/precompiled-spirv-pointer-param.slang | 31 ++++++++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/library/module-library-pointer-param.slang create mode 100644 tests/library/precompiled-spirv-pointer-param.slang (limited to 'tests/library') diff --git a/tests/library/export-library-generics.slang b/tests/library/export-library-generics.slang index f88541da3..3f17e0664 100644 --- a/tests/library/export-library-generics.slang +++ b/tests/library/export-library-generics.slang @@ -33,7 +33,7 @@ public int normalFuncUsesGeneric(int a) return genericFunc(obj); } -public int normalFunc(int a) +public int normalFunc(int a, float b) { - return a - 2; + return a - floor(b); } diff --git a/tests/library/module-library-pointer-param.slang b/tests/library/module-library-pointer-param.slang new file mode 100644 index 000000000..fff4f67c6 --- /dev/null +++ b/tests/library/module-library-pointer-param.slang @@ -0,0 +1,10 @@ +//TEST_IGNORE_FILE: + +// module-library-pointer-param.slang + +module "module-library-pointer-param"; + +public int ptrFunc(int* a) +{ + return *a; +} diff --git a/tests/library/precompiled-dxil-generics.slang b/tests/library/precompiled-dxil-generics.slang index a0b371519..a57f31ac0 100644 --- a/tests/library/precompiled-dxil-generics.slang +++ b/tests/library/precompiled-dxil-generics.slang @@ -24,5 +24,5 @@ struct Attributes [shader("anyhit")] void anyhit(inout Payload payload, Attributes attrib) { - payload.val = normalFunc(x * y) + normalFuncUsesGeneric(y); + payload.val = normalFunc(floor(x * y), x) + normalFuncUsesGeneric(y); } diff --git a/tests/library/precompiled-spirv-generics.slang b/tests/library/precompiled-spirv-generics.slang index a55f58513..64fd8a64c 100644 --- a/tests/library/precompiled-spirv-generics.slang +++ b/tests/library/precompiled-spirv-generics.slang @@ -24,5 +24,5 @@ struct Attributes [shader("anyhit")] void anyhit(inout Payload payload, Attributes attrib) { - payload.val = normalFunc(x * y) + normalFuncUsesGeneric(y); + payload.val = normalFunc(floor(x * y), x) + normalFuncUsesGeneric(y); } diff --git a/tests/library/precompiled-spirv-pointer-param.slang b/tests/library/precompiled-spirv-pointer-param.slang new file mode 100644 index 000000000..10cd7124b --- /dev/null +++ b/tests/library/precompiled-spirv-pointer-param.slang @@ -0,0 +1,31 @@ +// precompiled-spirv-pointer-param.slang + +// A test that uses slang-modules with embedded precompiled SPIRV and a library containing +// a function with a pointer parameter. +// The test compiles a library slang (module-library-pointer-param.slang) with -embed-downstream-ir then links the +// library to entrypoint slang (this file). +// The test passes if there is no errror thrown. +// TODO: Check if final linkage used only the precompiled spirv. + +//TEST:COMPILE: tests/library/module-library-pointer-param.slang -o tests/library/module-library-pointer-param.slang-module -target spirv -embed-downstream-ir -incomplete-library +//TEST:COMPILE: tests/library/precompiled-spirv-pointer-param.slang -target spirv -stage anyhit -entry anyhit -o tests/library/linked.spirv + +import "module-library-pointer-param"; + +struct Payload +{ + int val; +} + +struct Attributes +{ + float2 bary; +} + +[vk::push_constant] int* g_int; + +[shader("anyhit")] +void anyhit(inout Payload payload, Attributes attrib) +{ + payload.val = ptrFunc(g_int); +} -- cgit v1.2.3