-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specific handling for inline spirv pointer types (#6873)
Add specific handling for inline spirv pointer types We currently blindly create a new type for all vk::SpirvType* types. This can cause problems when the type is suppose to match another type. In this case, we want a spirv pointer type to match the pointer type of the variable. The OpTypePointer for the variable is implicitly created when declaring the variable, but, if we want to explicitly declare the pointer as a SpirvType, we get two different OpTypePointer instructions in the module. So technically the types do not match. To fix this, I add special handling in the SPIR-V backend to be able to merge the implicit pointer type created and the SpirvType when they match. This implements the SPIR-V Pointers in [HLSL spec proposal 0021](https://github.com/microsoft/hlsl-specs/blob/main/proposals/0021-vk-coop-matrix.md#spir-v-pointers) --------- Co-authored-by: Nathan Gauër <github@keenuts.net>
- Loading branch information
Showing
10 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
Submodule SPIRV-Headers
updated
13 files
Submodule SPIRV-Tools
updated
29 files
This file contains 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,34 @@ | ||
// Copyright (c) 2024 Google LLC | ||
// | ||
// This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef _HLSL_VK_SPIRV_H_ | ||
#define _HLSL_VK_SPIRV_H_ | ||
|
||
namespace vk { | ||
|
||
enum StorageClass { | ||
StorageClassWorkgroup = 4, | ||
}; | ||
|
||
// An opaque type to represent a Spir-V pointer to the workgroup storage class. | ||
// clang-format off | ||
template <typename PointeeType> | ||
using WorkgroupSpirvPointer = const vk::SpirvOpaqueType< | ||
/* OpTypePointer */ 32, | ||
vk::Literal<vk::integral_constant<uint, StorageClassWorkgroup> >, | ||
PointeeType>; | ||
// clang-format on | ||
|
||
// Returns an opaque Spir-V pointer to v. The memory object v's storage class | ||
// modifier must be groupshared. If the incorrect storage class is used, then | ||
// there will be a validation error, and it will not show the correct | ||
template <typename T> | ||
[[vk::ext_instruction(/* OpCopyObject */ 83)]] WorkgroupSpirvPointer<T> | ||
GetGroupSharedAddress([[vk::ext_reference]] T v); | ||
|
||
} // namespace vk | ||
|
||
#endif // _HLSL_VK_SPIRV_H_ |
This file contains 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 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 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 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,26 @@ | ||
// RUN: dxc -fspv-target-env=vulkan1.3 -T cs_6_0 -E main -spirv -HV 2021 -I %hlsl_headers %s 2>&1 | FileCheck %s | ||
|
||
#include "vk/spirv.h" | ||
|
||
// CHECK-NOT: OpCapability VariablePointers | ||
|
||
RWStructuredBuffer<int> data; | ||
|
||
groupshared int shared_data[64]; | ||
|
||
[[vk::ext_instruction(/* OpLoad */ 61)]] int | ||
Load(vk::WorkgroupSpirvPointer<int> p); | ||
|
||
int foo(vk::WorkgroupSpirvPointer<int> param) { | ||
return Load(param); | ||
} | ||
|
||
[numthreads(64, 1, 1)] void main() { | ||
// CHECK: [[ac:%[0-9]+]] = OpAccessChain %_ptr_Workgroup_int %shared_data %int_0 | ||
// CHECK: [[ld:%[0-9]+]] = OpLoad %int [[ac]] | ||
// CHECK: [[ac:%[0-9]+]] = OpAccessChain %_ptr_StorageBuffer_int %data %int_0 %uint_0 | ||
// CHECK: OpStore [[ac]] [[ld]] | ||
|
||
vk::WorkgroupSpirvPointer<int> p = vk::GetGroupSharedAddress(shared_data[0]); | ||
data[0] = foo(p); | ||
} |
23 changes: 23 additions & 0 deletions
23
tools/clang/test/CodeGenSPIRV/workgroupspirvpointer.varpointer.hlsl
This file contains 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,23 @@ | ||
// RUN: dxc -fspv-target-env=vulkan1.3 -T cs_6_0 -E main -spirv -HV 2021 -I %hlsl_headers %s 2>&1 | FileCheck %s | ||
|
||
#include "vk/spirv.h" | ||
|
||
// CHECK: OpCapability VariablePointers | ||
|
||
RWStructuredBuffer<int> data; | ||
|
||
groupshared int shared_data[64]; | ||
|
||
[[vk::ext_instruction(/* OpLoad */ 61)]] int | ||
Load(vk::WorkgroupSpirvPointer<int> p); | ||
|
||
[[noinline]] | ||
int foo(vk::WorkgroupSpirvPointer<int> param) { | ||
return Load(param); | ||
} | ||
|
||
[[vk::ext_capability(/* VariablePointersCapability */ 4442)]] | ||
[numthreads(64, 1, 1)] void main() { | ||
vk::WorkgroupSpirvPointer<int> p = vk::GetGroupSharedAddress(shared_data[0]); | ||
data[0] = foo(p); | ||
} |
This file contains 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 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