-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HLSL][SPIR-V] Add SV_DispatchThreadID semantic support (#82536)
Add SPIR-V backend support for the HLSL SV_DispatchThreadID semantic attribute, which is lowered to a @llvm.dx.thread.id intrinsic in LLVM IR. In the SPIR-V backend, this is now correctly translated to a `GlobalInvocationId` builtin variable. Fixes #82534
- Loading branch information
1 parent
aec6a04
commit 6325dd5
Showing
6 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,28 +1,25 @@ | ||
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | ||
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL | ||
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV | ||
|
||
// Make sure SV_DispatchThreadID translated into dx.thread.id. | ||
|
||
const RWBuffer<float> In; | ||
RWBuffer<float> Out; | ||
|
||
// CHECK: define void @foo() | ||
// CHECK: %[[ID:[0-9a-zA-Z]+]] = call i32 @llvm.dx.thread.id(i32 0) | ||
// CHECK: call void @"?foo@@YAXH@Z"(i32 %[[ID]]) | ||
// CHECK: define void @foo() | ||
// CHECK-DXIL: %[[#ID:]] = call i32 @llvm.dx.thread.id(i32 0) | ||
// CHECK-SPIRV: %[[#ID:]] = call i32 @llvm.spv.thread.id(i32 0) | ||
// CHECK: call void @{{.*}}foo{{.*}}(i32 %[[#ID]]) | ||
[shader("compute")] | ||
[numthreads(8,8,1)] | ||
void foo(uint Idx : SV_DispatchThreadID) { | ||
Out[Idx] = In[Idx]; | ||
} | ||
void foo(uint Idx : SV_DispatchThreadID) {} | ||
|
||
// CHECK: define void @bar() | ||
// CHECK: %[[ID_X:[0-9a-zA-Z]+]] = call i32 @llvm.dx.thread.id(i32 0) | ||
// CHECK: %[[ID_X_:[0-9a-zA-Z]+]] = insertelement <2 x i32> poison, i32 %[[ID_X]], i64 0 | ||
// CHECK: %[[ID_Y:[0-9a-zA-Z]+]] = call i32 @llvm.dx.thread.id(i32 1) | ||
// CHECK: %[[ID_XY:[0-9a-zA-Z]+]] = insertelement <2 x i32> %[[ID_X_]], i32 %[[ID_Y]], i64 1 | ||
// CHECK: call void @"?bar@@YAXT?$__vector@H$01@__clang@@@Z"(<2 x i32> %[[ID_XY]]) | ||
// CHECK: define void @bar() | ||
// CHECK-DXIL: %[[#ID_X:]] = call i32 @llvm.dx.thread.id(i32 0) | ||
// CHECK-SPIRV: %[[#ID_X:]] = call i32 @llvm.spv.thread.id(i32 0) | ||
// CHECK: %[[#ID_X_:]] = insertelement <2 x i32> poison, i32 %[[#ID_X]], i64 0 | ||
// CHECK-DXIL: %[[#ID_Y:]] = call i32 @llvm.dx.thread.id(i32 1) | ||
// CHECK-SPIRV: %[[#ID_Y:]] = call i32 @llvm.spv.thread.id(i32 1) | ||
// CHECK: %[[#ID_XY:]] = insertelement <2 x i32> %[[#ID_X_]], i32 %[[#ID_Y]], i64 1 | ||
// CHECK-DXIL: call void @{{.*}}bar{{.*}}(<2 x i32> %[[#ID_XY]]) | ||
[shader("compute")] | ||
[numthreads(8,8,1)] | ||
void bar(uint2 Idx : SV_DispatchThreadID) { | ||
Out[Idx.y] = In[Idx.x]; | ||
} | ||
void bar(uint2 Idx : SV_DispatchThreadID) {} | ||
|
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
76 changes: 76 additions & 0 deletions
76
llvm/test/CodeGen/SPIRV/hlsl-intrinsics/SV_DispatchThreadID.ll
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,76 @@ | ||
; RUN: llc -O0 -mtriple=spirv-vulkan-unknown %s -o - | FileCheck %s | ||
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %} | ||
|
||
; This file generated from the following command: | ||
; clang -cc1 -triple spirv-vulkan-library -x hlsl -emit-llvm -disable-llvm-passes -finclude-default-header - -o - <<EOF | ||
; [shader("compute")] | ||
; [numthreads(1,1,1)] | ||
; void main(uint3 ID : SV_DispatchThreadID) {} | ||
; EOF | ||
|
||
; CHECK-DAG: %[[#int:]] = OpTypeInt 32 0 | ||
; CHECK-DAG: %[[#v3int:]] = OpTypeVector %[[#int]] 3 | ||
; CHECK-DAG: %[[#ptr_Input_v3int:]] = OpTypePointer Input %[[#v3int]] | ||
; CHECK-DAG: %[[#tempvar:]] = OpUndef %[[#v3int]] | ||
; CHECK-DAG: %[[#GlobalInvocationId:]] = OpVariable %[[#ptr_Input_v3int]] Input | ||
|
||
; CHECK-DAG: OpEntryPoint GLCompute {{.*}} %[[#GlobalInvocationId]] | ||
; CHECK-DAG: OpName %[[#GlobalInvocationId]] "__spirv_BuiltInGlobalInvocationId" | ||
; CHECK-DAG: OpDecorate %[[#GlobalInvocationId]] LinkageAttributes "__spirv_BuiltInGlobalInvocationId" Import | ||
; CHECK-DAG: OpDecorate %[[#GlobalInvocationId]] BuiltIn GlobalInvocationId | ||
|
||
; ModuleID = '-' | ||
source_filename = "-" | ||
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" | ||
target triple = "spirv-unknown-vulkan-library" | ||
|
||
; Function Attrs: noinline norecurse nounwind optnone | ||
define internal spir_func void @main(<3 x i32> noundef %ID) #0 { | ||
entry: | ||
%ID.addr = alloca <3 x i32>, align 16 | ||
store <3 x i32> %ID, ptr %ID.addr, align 16 | ||
ret void | ||
} | ||
|
||
; Function Attrs: norecurse | ||
define void @main.1() #1 { | ||
entry: | ||
|
||
; CHECK: %[[#load:]] = OpLoad %[[#v3int]] %[[#GlobalInvocationId]] | ||
; CHECK: %[[#load0:]] = OpCompositeExtract %[[#int]] %[[#load]] 0 | ||
%0 = call i32 @llvm.spv.thread.id(i32 0) | ||
|
||
; CHECK: %[[#tempvar:]] = OpCompositeInsert %[[#v3int]] %[[#load0]] %[[#tempvar]] 0 | ||
%1 = insertelement <3 x i32> poison, i32 %0, i64 0 | ||
|
||
; CHECK: %[[#load:]] = OpLoad %[[#v3int]] %[[#GlobalInvocationId]] | ||
; CHECK: %[[#load1:]] = OpCompositeExtract %[[#int]] %[[#load]] 1 | ||
%2 = call i32 @llvm.spv.thread.id(i32 1) | ||
|
||
; CHECK: %[[#tempvar:]] = OpCompositeInsert %[[#v3int]] %[[#load1]] %[[#tempvar]] 1 | ||
%3 = insertelement <3 x i32> %1, i32 %2, i64 1 | ||
|
||
; CHECK: %[[#load:]] = OpLoad %[[#v3int]] %[[#GlobalInvocationId]] | ||
; CHECK: %[[#load2:]] = OpCompositeExtract %[[#int]] %[[#load]] 2 | ||
%4 = call i32 @llvm.spv.thread.id(i32 2) | ||
|
||
; CHECK: %[[#tempvar:]] = OpCompositeInsert %[[#v3int]] %[[#load2]] %[[#tempvar]] 2 | ||
%5 = insertelement <3 x i32> %3, i32 %4, i64 2 | ||
|
||
call void @main(<3 x i32> %5) | ||
ret void | ||
} | ||
|
||
; Function Attrs: nounwind willreturn memory(none) | ||
declare i32 @llvm.spv.thread.id(i32) #2 | ||
|
||
attributes #0 = { noinline norecurse nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" } | ||
attributes #1 = { norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } | ||
attributes #2 = { nounwind willreturn memory(none) } | ||
|
||
!llvm.module.flags = !{!0, !1} | ||
!llvm.ident = !{!2} | ||
|
||
!0 = !{i32 1, !"wchar_size", i32 4} | ||
!1 = !{i32 4, !"dx.disable_optimizations", i32 1} | ||
!2 = !{!"clang version 19.0.0git (git@github.com:llvm/llvm-project.git 91600507765679e92434ec7c5edb883bf01f847f)"} |