[mlir][spirv] Support function argument decorations
We need to support function argument decorations in spirv.func to allow for some pointer types. For example, the spec requires the following:
If an OpFunctionParameter is a pointer (or array of pointers) in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of Aliased or Restrict.
If an OpFunctionParameter is a pointer (or array of pointers) and its pointee type is a pointer in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of AliasedPointer or RestrictPointer.
Note that this is already supported with the spirv.Variable op using the following syntax:
%3 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
@llvm/issue-subscribers-mlir-spirv
Author: Jakub Kuderski (kuhar)
> If an OpFunctionParameter is a pointer (or array of pointers) in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of Aliased or Restrict.
> If an OpFunctionParameter is a pointer (or array of pointers) and its pointee type is a pointer in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of AliasedPointer or RestrictPointer.
Note that this is already supported with the spirv.Variable op using the following syntax:
%3 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- In the comments of the issue, request for it to be assigned to you.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Jakub Kuderski (kuhar)
> If an OpFunctionParameter is a pointer (or array of pointers) in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of Aliased or Restrict.
> If an OpFunctionParameter is a pointer (or array of pointers) and its pointee type is a pointer in PhysicalStorageBuffer storage class, then the function parameter must be decorated with exactly one of AliasedPointer or RestrictPointer.
Note that this is already supported with the spirv.Variable op using the following syntax:
%3 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
Hi, I'm new to the SPIRV dialect, so I'd like to work on this issue.
Hi, I'm new to the SPIRV dialect, so I'd like to work on this issue.
Sure @sott0n, feel free to ping me if you need any pointers.
A good starting point would be llvm-project/mlir/test/Dialect/SPIRV/IR/target-and-abi.mlir -- we already support some argument attributes like:
func.func @interface_var(
%arg0 : f32 {spirv.interface_var_abi = #spirv.interface_var_abi<(0, 1), Uniform>}
) { return }
so what we need to do is to enable the same thing for decoration attributes and make sure we serialize them as OpDecorate over OpFunctionParameter. The following IR be accepted by mlir-opt
spirv.module PhysicalStorageBuffer64 GLSL450 requires #spirv.vce<v1.3,
[Shader, PhysicalStorageBufferAddresses], [SPV_KHR_physical_storage_buffer]> {
spirv.func @access(%arg0 : !spirv.ptr<!spirv.struct<(f32)>, PhysicalStorageBuffer> { spirv.decoration = #spirv.decoration<Aliased>}) -> f32 "None" {
%idx = spirv.Constant 0 : i32
%result = spirv.AccessChain %arg0[%idx] : !spirv.ptr<!spirv.struct<(f32)>, PhysicalStorageBuffer>, i32
%ptr = spirv.Load "PhysicalStorageBuffer" %result : f32
spirv.ReturnValue %ptr : f32
}
}
and pass validation with:
bin/mlir-translate --no-implicit-module --serialize-spirv test.mlir -o test.spv
spirv-val test.spv
I'm working on https://github.com/llvm/llvm-project/pull/76353
I have completed support for function argument decoration (I'm going to refactor it) and am now working on implementing serialization to pass SPIR-V's validation via spirv-val.
I've finished implementing the serializer and deserializer. However, spirv-val is causing an error with the OpLoad. For the physical buffer pointer, the SPIR-V dialect needs to add the Aligned with an OpLoad/OpStore (i'm going to try it after this PR).
SPIRV validation tool's error:
spirv-val test.spv
error: line 19: Memory accesses with PhysicalStorageBuffer must use Aligned.
%12 = OpLoad %float %11
This PR supported the decoration for the function's parameters (OpDecorate %6 Aliased), but not yet implemented the Aligned in the OpLoad when the operand is the physical buffer pointer.
spirv-dis test.spv
; SPIR-V
; Version: 1.3
; Generator: Khronos; 22
; Bound: 13
; Schema: 0
OpCapability Shader
OpCapability PhysicalStorageBufferAddresses
OpExtension "SPV_KHR_physical_storage_buffer"
OpMemoryModel PhysicalStorageBuffer64 GLSL450
OpName %access "access"
OpDecorate %_struct_4 Block
OpDecorate %6 Aliased
%float = OpTypeFloat 32
%_struct_4 = OpTypeStruct %float
%_ptr_PhysicalStorageBuffer__struct_4 = OpTypePointer PhysicalStorageBuffer %_struct_4
%1 = OpTypeFunction %float %_ptr_PhysicalStorageBuffer__struct_4
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%_ptr_PhysicalStorageBuffer_float = OpTypePointer PhysicalStorageBuffer %float
%access = OpFunction %float None %1
%6 = OpFunctionParameter %_ptr_PhysicalStorageBuffer__struct_4
%7 = OpLabel
%11 = OpAccessChain %_ptr_PhysicalStorageBuffer_float %6 %uint_0
%12 = OpLoad %float %11
OpReturnValue %12
This PR supported the decoration for the function's parameters (
OpDecorate %6 Aliased), but not yet implemented theAlignedin the OpLoad when the operand is the physical buffer pointer.
Good point @sott0n, I think it's just a bug in my example though, isn't it? The input mlir should have been:
%ptr = spirv.Load "PhysicalStorageBuffer" %result ["Aligned", 4] : f32
Yes, I think it's correct. I updated your example and this error is no longer there.