From d4429f785505aced433d92d64e18a3b21ba1f014 Mon Sep 17 00:00:00 2001 From: Cassandra Beckley Date: Mon, 10 Mar 2025 14:13:56 -0700 Subject: [PATCH 1/3] [HLSL] Add support for SV_GroupIndex in SPIR-V Lower the `SV_GroupIndex` semantic as the `llvm.spv.flattened.thread.id.in.group` intrinsic. --- clang/lib/CodeGen/CGHLSLRuntime.cpp | 6 ++--- clang/lib/CodeGen/CGHLSLRuntime.h | 2 ++ .../semantics/GroupIndex-codegen.hlsl | 22 ++++++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index dc34653e8f497..ad06f89b5975f 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -437,9 +437,9 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B, llvm::Type *Ty) { assert(D.hasAttrs() && "Entry parameter missing annotation attribute!"); if (D.hasAttr()) { - llvm::Function *DxGroupIndex = - CGM.getIntrinsic(Intrinsic::dx_flattened_thread_id_in_group); - return B.CreateCall(FunctionCallee(DxGroupIndex)); + llvm::Function *GroupIndex = + CGM.getIntrinsic(getFlattenedThreadIdInGroupIntrinsic()); + return B.CreateCall(FunctionCallee(GroupIndex)); } if (D.hasAttr()) { llvm::Function *ThreadIDIntrinsic = diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index a9da42324a038..45ab9f8325489 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -86,6 +86,8 @@ class CGHLSLRuntime { GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross) GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees) GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac) + GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup, + flattened_thread_id_in_group) GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp) GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize) GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt) diff --git a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl index ea358c411997d..e78583b1fb0f0 100644 --- a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl +++ b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl @@ -1,7 +1,8 @@ -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL -DTARGET=dx +// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV -DTARGET=spv -[numthreads(1,1,1)] -void main(unsigned GI : SV_GroupIndex) { +[numthreads(1, 1, 1)] void main(unsigned GI : SV_GroupIndex) +{ main(GI - 1); } @@ -10,13 +11,14 @@ void main(unsigned GI : SV_GroupIndex) { // semantic parameters and provides the expected void(void) signature that // drivers expect for entry points. -//CHECK: define void @main() #[[#ENTRY_ATTR:]] { -//CHECK-NEXT: entry: -//CHECK-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group() -//CHECK-NEXT: call void @_Z4mainj(i32 %0) -//CHECK-NEXT: ret void -//CHECK-NEXT: } +// CHECK: define void @main() #[[#ENTRY_ATTR:]] { +// CHECK: entry: +// CHECK: %[[#ID_X:]] = call i32 @llvm.[[TARGET]].flattened.thread.id.in.group() +// CHECK-DXIL: call void @_Z4mainj(i32 %[[#ID_X]]) +// CHECK-SPIRV: call spir_func void @_Z4mainj(i32 %[[#ID_X]]) +// CHECK: ret void +// CHECK: } // Verify that the entry had the expected dx.shader attribute -//CHECK: attributes #[[#ENTRY_ATTR]] = { {{.*}}"hlsl.shader"="compute"{{.*}} } +// CHECK: attributes #[[#ENTRY_ATTR]] = { {{.*}}"hlsl.shader"="compute"{{.*}} } From 30bab181bd2170314a97333ac519d1269ca8c7e3 Mon Sep 17 00:00:00 2001 From: Cassandra Beckley Date: Mon, 17 Mar 2025 14:52:25 -0700 Subject: [PATCH 2/3] Formatting --- clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl index e78583b1fb0f0..a8dc6ff59771e 100644 --- a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl +++ b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl @@ -1,8 +1,8 @@ // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL -DTARGET=dx // RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV -DTARGET=spv -[numthreads(1, 1, 1)] void main(unsigned GI : SV_GroupIndex) -{ +[numthreads(1, 1, 1)] +void main(unsigned GI : SV_GroupIndex) { main(GI - 1); } From d3321a27e72a1c18796d298e1269bdece18eb36a Mon Sep 17 00:00:00 2001 From: Cassandra Beckley Date: Mon, 17 Mar 2025 14:54:13 -0700 Subject: [PATCH 3/3] Undo unnecessary formatting change --- clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl index a8dc6ff59771e..be9a96cc3c9d3 100644 --- a/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl +++ b/clang/test/CodeGenHLSL/semantics/GroupIndex-codegen.hlsl @@ -1,7 +1,7 @@ // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL -DTARGET=dx // RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV -DTARGET=spv -[numthreads(1, 1, 1)] +[numthreads(1,1,1)] void main(unsigned GI : SV_GroupIndex) { main(GI - 1); }