Issue 243663
Summary [hlsl][spirv] Suboptimal codegen for byte address buffers and int8 capability requirement
Labels new issue
Assignees
Reporter rj123-nv
    For accesses of Byte address buffer, the address is guaranteed to be aligned to the largest contained scalar type.

E.g. for uint or float4 the address must be a multiple of 4B.
The align operand in generated llvm reflects this:

```
  %1 = tail call target("spirv.VulkanBuffer", [0 x i8], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0i8_12_1t(i32 0, i32 0, i32 1, i32 0, ptr nonnull @.str)
  %2 = tail call i32 @llvm.spv.thread.id.i32(i32 0)
  %mul.i = shl i32 %2, 2
  %3 = call ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0i8_12_1t.i32(target("spirv.VulkanBuffer", [0 x i8], 12, 1) %1, i32 %mul.i) [ "convergencectrl"(token 
  %0) ]
  %4 = load i32, ptr addrspace(11) %3, align 4, !tbaa !6
```

After https://github.com/llvm/llvm-project/pull/212999, accesses of a wider type derived from the base (unbounded array of i8) are legalized to byte accesses, for example in the following hlsl, 4 x 1-byte loads and 4 x 1-byte stores are produced:

```
  RWByteAddressBuffer buf : register(u0);

 [numthreads(1, 1, 1)]
  void main(uint3 tid : SV_DispatchThreadID) {
 uint offset = tid.x * 4;
    uint value = buf.Load<uint>(offset);
 buf.Store<uint>(offset, value + 1);
  }
```

just the load part, also it requires int8 capability:

```
  OpCapability Shader
  OpCapability Int8
...
  %31 = OpCopyObject %spirv_VulkanBuffer %buf
  %32 = OpLoad %v3uint %__spirv_BuiltInGlobalInvocationId
  %33 = OpCompositeExtract %uint %32 0
  %34 = OpShiftLeftLogical %uint %33 %uint_2
  %35 = OpAccessChain %_ptr_StorageBuffer_uchar %31 %uint_0 %34
  %36 = OpLoad %uchar %35
  %37 = OpUConvert %uint %36
  %38 = OpIAdd %uint %34 %uint_1
  %39 = OpAccessChain %_ptr_StorageBuffer_uchar %31 %uint_0 %38
  %40 = OpLoad %uchar %39
  %41 = OpUConvert %uint %40
  %42 = OpShiftLeftLogical %uint %41 %uint_8
  %43 = OpBitwiseOr %uint %37 %42
  %44 = OpIAdd %uint %34 %uint_2
  %45 = OpAccessChain %_ptr_StorageBuffer_uchar %31 %uint_0 %44
  %46 = OpLoad %uchar %45
  %47 = OpUConvert %uint %46
  %48 = OpShiftLeftLogical %uint %47 %uint_16
  %49 = OpBitwiseOr %uint %43 %48
  %50 = OpIAdd %uint %34 %uint_3
  %51 = OpAccessChain %_ptr_StorageBuffer_uchar %31 %uint_0 %50
 %52 = OpLoad %uchar %51
  %53 = OpUConvert %uint %52
  %54 = OpShiftLeftLogical %uint %53 %uint_24
  %55 = OpBitwiseOr %uint %49 %54
```

One simple solution to this is to emit SPIRV with untyped access chain, use load/stores with the loaded type in this example, and passthrough the align operand as a SPIRV Aligned memory operand. This works for vector types too, e.g. float4 would have 4B alignment as well and we can simply emit vectorized loads with align 4.
https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_untyped_pointers.html

proposed spirv using untyped pointers:

```
     OpCapability UntypedPointersKHR 
 OpCapability Int8 
     OpExtension "SPV_KHR_untyped_pointers" 
...
  %uint = OpTypeInt 32 0
  %uchar = OpTypeInt 8 0
 %_runtimearr_uchar = OpTypeRuntimeArray %uchar
  %_struct_8 = OpTypeStruct %_runtimearr_uchar
  %spirv_VulkanBuffer = OpTypePointer StorageBuffer %_struct_8
  %_ptr_StorageBuffer = OpTypeUntypedPointerKHR StorageBuffer
 %buf = OpVariable %spirv_VulkanBuffer StorageBuffer 
...

  %main = OpFunction %void DontInline %3
  %entry = OpLabel
  %_str = OpVariable %_ptr_Function__arr_uchar_uint_4 Function %22
  %27 = OpCopyObject %spirv_VulkanBuffer %buf
  %28 = OpLoad %v3uint %__spirv_BuiltInGlobalInvocationId
  %29 = OpCompositeExtract %uint %28 0
 %30 = OpShiftLeftLogical %uint %29 %uint_2
  %31 = OpUntypedAccessChainKHR %_ptr_StorageBuffer %_runtimearr_uchar %27 %30
  %32 = OpLoad %uint %31 Aligned 4
  %33 = OpIAdd %uint %32 %uint_1
        OpStore %31 %33 Aligned 4
        OpReturn
        OpFunctionEnd
```

I'm assuming this would let us skip most legalization too (SPIRVLegalizePointerCast), in this case for StorageBuffer pointers, this could also be extended to shared memory (Workgroup) pointers.

If we need a fallback too when untyped pointers/i8 arent supported, we should ideally emit the SPIRV buffer as an array of uint32's and index by the address >> 2, so on for other widths and vector types, also aliasing multiple buffer id's if needed.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to