Issue 179879
Summary [HLSL][Matrix][SPIRV] Support Cbuffers with Array of Vector memory layouts
Labels new issue
Assignees
Reporter farzonl
    This case will break after #179859 merges: https://hlsl.godbolt.org/z/9xj3bj5eT. We likely need a fix in the `spirv-cbuffer-access` pass.

Add support for this case
Repro:
> clang-dxc.exe -spirv -fspv-target-env=vulkan1.3 -fspv-extension=DXC  -T cs_6_0  -Xclang -emit-llvm source.hlsl > test.ir
```hlsl
RWStructuredBuffer<float2> Out0 : register(u0);
RWStructuredBuffer<float2> Out1 : register(u1);
RWStructuredBuffer<float2> Out2 : register(u2);
RWStructuredBuffer<float2> Out3 : register(u3);

cbuffer example : register(b4) {

    float4x2 M1;
};

[numthreads(1,1,1)]
void main() {
    Out0[0] = M1[0];
    Out1[0] = M1[1];
    Out2[0] = M1[2];
 Out3[0] = M1[3];
   
}

```
> llc test.ir
```llvm
; ModuleID = 'D:\repos\llvm-build\tools\OffloadTest\test\clang-vk\Feature\CBuffer\Matrix\SingleSubscript\Output\mat_cbuffer_multibuffer.f4x2.test.tmp/source.hlsl'
source_filename = "D:\\repos\\llvm-build\\tools\\OffloadTest\\test\\clang-vk\\Feature\\CBuffer\\Matrix\\SingleSubscript\\Output\\mat_cbuffer_multibuffer.f4x2.test.tmp/source.hlsl"
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G10"
target triple = "spirv1.6-unknown-vulkan1.3-compute"

%__cblayout_example = type <{ [4 x <2 x float>] }>

@.str = private unnamed_addr constant [5 x i8] c"Out0\00", align 1
@.str.2 = private unnamed_addr constant [5 x i8] c"Out1\00", align 1
@.str.4 = private unnamed_addr constant [5 x i8] c"Out2\00", align 1
@.str.6 = private unnamed_addr constant [5 x i8] c"Out3\00", align 1
@example.cb = local_unnamed_addr global target("spirv.VulkanBuffer", %__cblayout_example, 2, 0) poison
@M1 = external hidden local_unnamed_addr addrspace(12) global [4 x <2 x float>], align 4
@example.str = private unnamed_addr constant [8 x i8] c"example\00", align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none)
declare target("spirv.VulkanBuffer", %__cblayout_example, 2, 0) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_s___cblayout_examples_2_0t(i32, i32, i32, i32, ptr) #0

; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: none, target_mem0: none, target_mem1: none)
define void @main() local_unnamed_addr #1 {
entry:
  %0 = tail call target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v2f32_12_1t(i32 0, i32 0, i32 1, i32 0, ptr nonnull @.str)
  %1 = tail call target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v2f32_12_1t(i32 0, i32 1, i32 1, i32 0, ptr nonnull @.str.2)
  %2 = tail call target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v2f32_12_1t(i32 0, i32 2, i32 1, i32 0, ptr nonnull @.str.4)
  %3 = tail call target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v2f32_12_1t(i32 0, i32 3, i32 1, i32 0, ptr nonnull @.str.6)
  %example.cb_h.i.i = tail call target("spirv.VulkanBuffer", %__cblayout_example, 2, 0) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_s___cblayout_examples_2_0t(i32 0, i32 4, i32 1, i32 0, ptr nonnull @example.str)
  store target("spirv.VulkanBuffer", %__cblayout_example, 2, 0) %example.cb_h.i.i, ptr @example.cb, align 8
  %4 = load <5 x float>, ptr addrspace(12) @M1, align 4, !tbaa !8
  %matrix_row_ins2.i = shufflevector <5 x float> %4, <5 x float> poison, <2 x i32> <i32 0, i32 4>
  %5 = tail call noundef align 8 dereferenceable(8) ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v2f32_12_1t(target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) %0, i32 0)
  store <2 x float> %matrix_row_ins2.i, ptr addrspace(11) %5, align 8, !tbaa !8
  %6 = load <6 x float>, ptr addrspace(12) @M1, align 4, !tbaa !8
  %matrix_row_ins7.i = shufflevector <6 x float> %6, <6 x float> poison, <2 x i32> <i32 1, i32 5>
  %7 = tail call noundef align 8 dereferenceable(8) ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v2f32_12_1t(target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) %1, i32 0)
  store <2 x float> %matrix_row_ins7.i, ptr addrspace(11) %7, align 8, !tbaa !8
  %8 = load <7 x float>, ptr addrspace(12) @M1, align 4, !tbaa !8
  %matrix_row_ins12.i = shufflevector <7 x float> %8, <7 x float> poison, <2 x i32> <i32 2, i32 6>
  %9 = tail call noundef align 8 dereferenceable(8) ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v2f32_12_1t(target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) %2, i32 0)
  store <2 x float> %matrix_row_ins12.i, ptr addrspace(11) %9, align 8, !tbaa !8
  %10 = load <8 x float>, ptr addrspace(12) @M1, align 4, !tbaa !8
  %matrix_row_ins17.i = shufflevector <8 x float> %10, <8 x float> poison, <2 x i32> <i32 3, i32 7>
 %11 = tail call noundef align 8 dereferenceable(8) ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v2f32_12_1t(target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) %3, i32 0)
  store <2 x float> %matrix_row_ins17.i, ptr addrspace(11) %11, align 8, !tbaa !8
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none)
declare target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1) @llvm.spv.resource.handlefrombinding.tspirv.VulkanBuffer_a0v2f32_12_1t(i32, i32, i32, i32, ptr) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none)
declare ptr addrspace(11) @llvm.spv.resource.getpointer.p11.tspirv.VulkanBuffer_a0v2f32_12_1t(target("spirv.VulkanBuffer", [0 x <2 x float>], 12, 1), i32) #0

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { mustprogress nofree noinline norecurse nosync nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: none, target_mem0: none, target_mem1: none) "frame-pointer"="all" "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!hlsl.cbs = !{!0}
!llvm.module.flags = !{!1, !2}
!llvm.ident = !{!3}
!llvm.errno.tbaa = !{!4}

!0 = !{ptr @example.cb, ptr addrspace(12) @M1}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{!"clang version 23.0.0git ([email protected]:farzonl/llvm-project.git 3d686f857a14f86cdacde79c3e189eb60770e391)"}
!4 = !{!5, !5, i64 0}
!5 = !{!"int", !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!6, !6, i64 0}
```

## Crash in SPIRVLegalizePointerCast
```
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x000000019c20e388 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x000000019c247848 libsystem_pthread.dylib`pthread_kill + 296
    frame #2: 0x000000019c1509e4 libsystem_c.dylib`abort + 124
    frame #3: 0x000000010327e918 llc`llvm::llvm_unreachable_internal(msg="Unimplemented implicit down-cast from load.", file="/Users/farzonlotfi/Projects/llvm-project/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp", line=229) at ErrorHandling.cpp:244:3
    frame #4: 0x00000001002833cc llc`(anonymous namespace)::SPIRVLegalizePointerCast::transformLoad(this=0x000000013d017740, B=0x000000016fdfd170, LI=0x000000013d012b90, CastedOperand=0x000000013d020de0, OriginalOperand=0x000000013d01e350) at SPIRVLegalizePointerCast.cpp:229:7
    frame #5: 0x00000001002817d4 llc`(anonymous namespace)::SPIRVLegalizePointerCast::legalizePointerCast(this=0x000000013d017740, II=0x000000013d020de0) at SPIRVLegalizePointerCast.cpp:389:9
    frame #6: 0x0000000100280ff0 llc`(anonymous namespace)::SPIRVLegalizePointerCast::runOnFunction(this=0x000000013d017740, F=0x000000013d011f98) at SPIRVLegalizePointerCast.cpp:445:7
    frame #7: 0x000000010235e160 llc`llvm::FPPassManager::runOnFunction(this=0x000000013d017790, F=0x000000013d011f98) at LegacyPassManager.cpp:1398:27
    frame #8: 0x00000001023643f0 llc`llvm::FPPassManager::runOnModule(this=0x000000013d017790, M=0x000000013d80aa00) at LegacyPassManager.cpp:1444:16
    frame #9: 0x000000010235e9e4 llc`(anonymous namespace)::MPPassManager::runOnModule(this=0x000000013d005d50, M=0x000000013d80aa00) at LegacyPassManager.cpp:1513:27
    frame #10: 0x000000010235e568 llc`llvm::legacy::PassManagerImpl::run(this=0x000000013d811a00, M=0x000000013d80aa00) at LegacyPassManager.cpp:531:44
    frame #11: 0x00000001023647f8 llc`llvm::legacy::PassManager::run(this=0x000000016fdfdc68, M=0x000000013d80aa00) at LegacyPassManager.cpp:1640:14
    frame #12: 0x0000000100005db0 llc`compileModule(argv=0x000000016fdff050, PluginList=0x000000016fdfe8d8, Context=0x000000016fdfe820, OutputFilename="test.ir.s") at llc.cpp:868:8
    frame #13: 0x0000000100003b94 llc`main(argc=2, argv=0x000000016fdff050) at llc.cpp:458:22
    frame #14: 0x000000019bea6b98 dyld`start + 6076
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to