-
Notifications
You must be signed in to change notification settings - Fork 25
[VULKAN] Add support for specialization constants #499
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
Open
s-perron
wants to merge
6
commits into
llvm:main
Choose a base branch
from
s-perron:spec_const
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
754ca1f
[VULKAN] Add support for specialization constants
s-perron daae43b
Add error for invalid strings
s-perron 1d12749
Add test for multiple spec constants in the HLSL.
s-perron ed1f8ed
Add error if the same spec id is defined twice.
s-perron 3809f85
Merge branch 'main' into spec_const
s-perron 880ae6a
XFAIL tests with short and double.
s-perron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| #include "API/Device.h" | ||
| #include "Support/Pipeline.h" | ||
| #include "llvm/ADT/DenseSet.h" | ||
| #include "llvm/Support/Error.h" | ||
|
|
||
| #include <memory> | ||
|
|
@@ -20,22 +21,34 @@ | |
|
|
||
| using namespace offloadtest; | ||
|
|
||
| #define VKFormats(FMT) \ | ||
| #define VKFormats(FMT, BITS) \ | ||
| if (Channels == 1) \ | ||
| return VK_FORMAT_R32_##FMT; \ | ||
| return VK_FORMAT_R##BITS##_##FMT; \ | ||
| if (Channels == 2) \ | ||
| return VK_FORMAT_R32G32_##FMT; \ | ||
| return VK_FORMAT_R##BITS##G##BITS##_##FMT; \ | ||
| if (Channels == 3) \ | ||
| return VK_FORMAT_R32G32B32_##FMT; \ | ||
| return VK_FORMAT_R##BITS##G##BITS##B##BITS##_##FMT; \ | ||
| if (Channels == 4) \ | ||
| return VK_FORMAT_R32G32B32A32_##FMT; | ||
| return VK_FORMAT_R##BITS##G##BITS##B##BITS##A##BITS##_##FMT; | ||
|
|
||
| static VkFormat getVKFormat(DataFormat Format, int Channels) { | ||
| switch (Format) { | ||
| case DataFormat::Int16: | ||
| VKFormats(SINT, 16) break; | ||
| case DataFormat::UInt16: | ||
| VKFormats(UINT, 16) break; | ||
| case DataFormat::Int32: | ||
| VKFormats(SINT) break; | ||
| VKFormats(SINT, 32) break; | ||
| case DataFormat::UInt32: | ||
| VKFormats(UINT, 32) break; | ||
| case DataFormat::Float32: | ||
| VKFormats(SFLOAT) break; | ||
| VKFormats(SFLOAT, 32) break; | ||
| case DataFormat::Int64: | ||
| VKFormats(SINT, 64) break; | ||
| case DataFormat::UInt64: | ||
| VKFormats(UINT, 64) break; | ||
| case DataFormat::Float64: | ||
| VKFormats(SFLOAT, 64) break; | ||
| default: | ||
| llvm_unreachable("Unsupported Resource format specified"); | ||
| } | ||
|
|
@@ -1273,6 +1286,105 @@ class VKDevice : public offloadtest::Device { | |
| return llvm::Error::success(); | ||
| } | ||
|
|
||
| static llvm::Error | ||
| parseSpecializationConstant(const SpecializationConstant &SpecConst, | ||
| VkSpecializationMapEntry &Entry, | ||
| llvm::SmallVector<char> &SpecData) { | ||
| Entry.constantID = SpecConst.ConstantID; | ||
| Entry.offset = SpecData.size(); | ||
| switch (SpecConst.Type) { | ||
| case DataFormat::Float32: { | ||
| float Value = 0.0f; | ||
| double Tmp = 0.0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsDouble(Tmp)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid float value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Value = static_cast<float>(Tmp); | ||
| Entry.size = sizeof(float); | ||
| SpecData.resize(SpecData.size() + sizeof(float)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(float)); | ||
| break; | ||
| } | ||
| case DataFormat::Float64: { | ||
| double Value = 0.0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsDouble(Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid double value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(double); | ||
| SpecData.resize(SpecData.size() + sizeof(double)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(double)); | ||
| break; | ||
| } | ||
| case DataFormat::Int16: { | ||
| int16_t Value = 0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid int16 value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(int16_t); | ||
| SpecData.resize(SpecData.size() + sizeof(int16_t)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(int16_t)); | ||
| break; | ||
| } | ||
| case DataFormat::UInt16: { | ||
| uint16_t Value = 0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid uint16 value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(uint16_t); | ||
| SpecData.resize(SpecData.size() + sizeof(uint16_t)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(uint16_t)); | ||
| break; | ||
| } | ||
| case DataFormat::Int32: { | ||
| int32_t Value = 0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid int32 value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(int32_t); | ||
| SpecData.resize(SpecData.size() + sizeof(int32_t)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(int32_t)); | ||
| break; | ||
| } | ||
| case DataFormat::UInt32: { | ||
| uint32_t Value = 0; | ||
| if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid uint32 value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(uint32_t); | ||
| SpecData.resize(SpecData.size() + sizeof(uint32_t)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(uint32_t)); | ||
| break; | ||
| } | ||
| case DataFormat::Bool: { | ||
| bool Value = false; | ||
| if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value)) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Invalid bool value for specialization constant '%s'", | ||
| SpecConst.Value.c_str()); | ||
| Entry.size = sizeof(bool); | ||
| SpecData.resize(SpecData.size() + sizeof(bool)); | ||
| memcpy(SpecData.data() + Entry.offset, &Value, sizeof(bool)); | ||
| break; | ||
| } | ||
| default: | ||
| llvm_unreachable("Unsupported specialization constant type"); | ||
| } | ||
| return llvm::Error::success(); | ||
| } | ||
|
|
||
| llvm::Error createPipeline(Pipeline &P, InvocationState &IS) { | ||
| VkPipelineCacheCreateInfo CacheCreateInfo = {}; | ||
| CacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; | ||
|
|
@@ -1282,15 +1394,42 @@ class VKDevice : public offloadtest::Device { | |
| "Failed to create pipeline cache."); | ||
|
|
||
| if (P.isCompute()) { | ||
| const CompiledShader &S = IS.Shaders[0]; | ||
| const offloadtest::Shader &Shader = P.Shaders[0]; | ||
| assert(IS.Shaders.size() == 1 && | ||
| "Currently only support one compute shader"); | ||
| const CompiledShader &S = IS.Shaders[0]; | ||
| VkPipelineShaderStageCreateInfo StageInfo = {}; | ||
| StageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; | ||
| StageInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; | ||
| StageInfo.module = S.Shader; | ||
| StageInfo.pName = S.Entry.c_str(); | ||
|
|
||
| llvm::SmallVector<VkSpecializationMapEntry> SpecEntries; | ||
| llvm::SmallVector<char> SpecData; | ||
| VkSpecializationInfo SpecInfo = {}; | ||
| if (!Shader.SpecializationConstants.empty()) { | ||
| llvm::DenseSet<uint32_t> SeenConstantIDs; | ||
| for (const auto &SpecConst : Shader.SpecializationConstants) { | ||
| if (!SeenConstantIDs.insert(SpecConst.ConstantID).second) | ||
| return llvm::createStringError( | ||
| std::errc::invalid_argument, | ||
| "Specialization constant ID %u is already defined", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: would change the error to something more explicit about the location of the issue: (Looking at the test, the error could be interpreted as "we disallow constant ID reuse in the shader itself") |
||
| SpecConst.ConstantID); | ||
|
|
||
| VkSpecializationMapEntry Entry; | ||
| if (auto Err = | ||
| parseSpecializationConstant(SpecConst, Entry, SpecData)) | ||
| return Err; | ||
| SpecEntries.push_back(Entry); | ||
| } | ||
|
|
||
| SpecInfo.mapEntryCount = SpecEntries.size(); | ||
| SpecInfo.pMapEntries = SpecEntries.data(); | ||
| SpecInfo.dataSize = SpecData.size(); | ||
| SpecInfo.pData = SpecData.data(); | ||
| StageInfo.pSpecializationInfo = &SpecInfo; | ||
| } | ||
|
|
||
| VkComputePipelineCreateInfo PipelineCreateInfo = {}; | ||
| PipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; | ||
| PipelineCreateInfo.stage = StageInfo; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/Feature/SpecializationConstant/duplicate_spec_id.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #--- duplicate_spec_id.hlsl | ||
| [[vk::constant_id(0)]] | ||
| const int spec_int_A = 0; | ||
| [[vk::constant_id(0)]] | ||
| const int spec_int_B = 0; | ||
|
|
||
| RWStructuredBuffer<int> Out : register(u0, space0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main(uint GI : SV_GroupIndex) { | ||
| Out[0] = spec_int_A; | ||
| Out[1] = spec_int_B; | ||
| } | ||
| #--- duplicate_spec_id.yaml | ||
| --- | ||
| Shaders: | ||
| - Stage: Compute | ||
| Entry: main | ||
| DispatchSize: [1, 1, 1] | ||
| SpecializationConstants: | ||
| - { ConstantID: 0, Value: 123, Type: Int32 } | ||
| Buffers: | ||
| - { Name: Out, Format: Int32, Stride: 4, FillSize: 8 } | ||
| DescriptorSets: | ||
| - Resources: | ||
| - Name: Out | ||
| Kind: RWStructuredBuffer | ||
| DirectXBinding: { Register: 0, Space: 0 } | ||
| VulkanBinding: { Binding: 0 } | ||
| ... | ||
| #--- end | ||
|
|
||
| # REQUIRES: Vulkan | ||
|
|
||
| # RUN: split-file %s %t | ||
| # RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/duplicate_spec_id.hlsl | ||
| # RUN: %offloader %t/duplicate_spec_id.yaml %t.o | FileCheck %s | ||
|
|
||
| # CHECK: Data: [ 123, 123 ] |
37 changes: 37 additions & 0 deletions
37
test/Feature/SpecializationConstant/duplicate_spec_id_in_config.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #--- duplicate_spec_id_in_config.hlsl | ||
| [[vk::constant_id(0)]] | ||
| const int spec_int = 0; | ||
|
|
||
| RWStructuredBuffer<int> Out : register(u0, space0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main(uint GI : SV_GroupIndex) { | ||
| Out[GI] = spec_int; | ||
| } | ||
| #--- duplicate_spec_id_in_config.yaml | ||
| --- | ||
| Shaders: | ||
| - Stage: Compute | ||
| Entry: main | ||
| DispatchSize: [1, 1, 1] | ||
| SpecializationConstants: | ||
| - { ConstantID: 0, Value: 123, Type: Int32 } | ||
| - { ConstantID: 0, Value: 456, Type: Int32 } | ||
| Buffers: | ||
| - { Name: Out, Format: Int32, Stride: 4, FillSize: 4 } | ||
| DescriptorSets: | ||
| - Resources: | ||
| - Name: Out | ||
| Kind: RWStructuredBuffer | ||
| DirectXBinding: { Register: 0, Space: 0 } | ||
| VulkanBinding: { Binding: 0 } | ||
| ... | ||
| #--- end | ||
|
|
||
| # REQUIRES: Vulkan | ||
|
|
||
| # RUN: split-file %s %t | ||
| # RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/duplicate_spec_id_in_config.hlsl | ||
| # RUN: not %offloader %t/duplicate_spec_id_in_config.yaml %t.o 2>&1 | FileCheck %s | ||
|
|
||
| # CHECK: gpu-exec: error: Specialization constant ID 0 is already defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #--- invalid_bool.hlsl | ||
| [[vk::constant_id(0)]] | ||
| const bool spec_bool = false; | ||
| RWStructuredBuffer<uint> OutBool : register(u0, space0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main(uint GI : SV_GroupIndex) { | ||
| OutBool[GI] = (uint)spec_bool; | ||
| } | ||
| #--- invalid_bool.yaml | ||
| --- | ||
| Shaders: | ||
| - Stage: Compute | ||
| Entry: main | ||
| DispatchSize: [1, 1, 1] | ||
| SpecializationConstants: | ||
| - { ConstantID: 0, Value: "not a number", Type: Bool } | ||
| Buffers: | ||
| - { Name: OutBool, Format: Int32, Stride: 4, FillSize: 4 } | ||
| DescriptorSets: | ||
| - Resources: | ||
| - Name: OutBool | ||
| Kind: RWStructuredBuffer | ||
| DirectXBinding: { Register: 0, Space: 0 } | ||
| VulkanBinding: { Binding: 0 } | ||
| ... | ||
| #--- end | ||
|
|
||
| # REQUIRES: Vulkan | ||
|
|
||
| # RUN: split-file %s %t | ||
| # RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_bool.hlsl | ||
| # RUN: not %offloader %t/invalid_bool.yaml %t.o 2>&1 | FileCheck %s | ||
|
|
||
| # CHECK: Invalid bool value for specialization constant 'not a number' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.