[clang] [clang][HLSL] Add GroupMemoryBarrierWithGroupSync intrinsic (PR #111883)

Adam Yang via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 10 12:31:16 PDT 2024


https://github.com/adam-yang updated https://github.com/llvm/llvm-project/pull/111883

>From 0f97b92617dcadd78362f24f3ef0719c19f6f3f6 Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Thu, 10 Oct 2024 10:20:37 -0700
Subject: [PATCH 1/3] Added the intrinsic to clang

---
 clang/include/clang/Basic/Builtins.td         |  6 +++++
 clang/lib/CodeGen/CGBuiltin.cpp               |  4 ++++
 clang/lib/CodeGen/CGHLSLRuntime.h             |  1 +
 clang/lib/Headers/hlsl/hlsl_intrinsics.h      | 12 ++++++++++
 clang/lib/Sema/SemaHLSL.cpp                   |  5 +++++
 ...roupMemoryBarrierWithGroupSync-errors.hlsl |  6 +++++
 .../GroupMemoryBarrierWithGroupSync.hlsl      | 22 +++++++++++++++++++
 7 files changed, 56 insertions(+)
 create mode 100644 clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
 create mode 100644 clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl

diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 9ebee81fcb0d3d..96fea17badfe9e 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4830,6 +4830,12 @@ def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLGroupMemoryBarrierWithGroupSync: LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_group_memory_barrier_with_group_sync"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 57705f2d2d0423..45890019ee2a64 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18901,6 +18901,10 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
         CGM.getHLSLRuntime().getRadiansIntrinsic(), ArrayRef<Value *>{Op0},
         nullptr, "hlsl.radians");
   }
+  case Builtin::BI__builtin_group_memory_barrier_with_group_sync: {
+    Intrinsic::ID ID = CGM.getHLSLRuntime().getGroupMemoryBarrierWithGroupSyncIntrinsic();
+    return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
+  }
   }
   return nullptr;
 }
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 05ff325216f55b..1e095cedf58731 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -89,6 +89,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
   GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
   GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync, groupMemoryBarrierWithGroupSync)
 
   //===----------------------------------------------------------------------===//
   // End of reserved area for HLSL intrinsic getters.
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index f39a68ba847e98..637df8924e2468 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -2168,5 +2168,17 @@ float3 radians(float3);
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
 float4 radians(float4);
 
+//===----------------------------------------------------------------------===//
+// GroupMemoryBarrierWithGroupSync builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn void GroupMemoryBarrierWithGroupSync(void)
+/// \brief Blocks execution of all threads in a group until all group shared
+/// accesses have been completed and all threads in the group have reached this
+/// call.
+
+_HLSL_BUILTIN_ALIAS(__builtin_group_memory_barrier_with_group_sync)
+void GroupMemoryBarrierWithGroupSync(void);
+
 } // namespace hlsl
 #endif //_HLSL_HLSL_INTRINSICS_H_
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index d2d2df829e7b11..30480f8f57c12d 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2018,6 +2018,11 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
       return true;
     break;
   }
+  case Builtin::BI__builtin_group_memory_barrier_with_group_sync: {
+    if (SemaRef.checkArgCountAtMost(TheCall, 0))
+      return true;
+    break;
+  }
   }
   return false;
 }
diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
new file mode 100644
index 00000000000000..9ffc1ebce1c909
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
+
+void test_too_many_arg() {
+  __builtin_group_memory_barrier_with_group_sync(0);
+  // expected-error at -1 {{too many arguments to function call, expected at most 0, have 1}}
+}
diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
new file mode 100644
index 00000000000000..f93c1966bf64bc
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-compute %s \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
+// RUN:   -DTARGET=dx -DFNATTRS=noundef
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   spirv-unknown-vulkan-compute %s \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
+// RUN:   -DTARGET=spv -DFNATTRS="spir_func noundef"
+
+// CHECK: define [[FNATTRS]] i32 @
+[numthreads(1, 1, 1)]
+void main() {
+  while (true) {
+// CHECK: call void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync()
+  GroupMemoryBarrierWithGroupSync();
+  break;
+  }
+}
+
+// CHECK: declare void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync() #[[ATTRS:[0-9]+]]
+// CHECK-NOT: attributes #[[ATTRS]] = {{.+}}memory(none){{.+}}
+// CHECK: attributes #[[ATTRS]] = {

>From 628e7d8991f3e8114bb1afbb28bf3eebba622be8 Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Thu, 10 Oct 2024 11:17:36 -0700
Subject: [PATCH 2/3] Tests fixed

---
 .../GroupMemoryBarrierWithGroupSync-errors.hlsl   |  2 +-
 .../builtins/GroupMemoryBarrierWithGroupSync.hlsl | 15 ++++++---------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
index 9ffc1ebce1c909..58d265c7c56f38 100644
--- a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
 
 void test_too_many_arg() {
   __builtin_group_memory_barrier_with_group_sync(0);
diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
index f93c1966bf64bc..1287de6119384a 100644
--- a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
@@ -1,20 +1,17 @@
 // RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
-// RUN:   dxil-pc-shadermodel6.3-compute %s \
+// RUN:   dxil-pc-shadermodel6.0-library %s \
 // RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
-// RUN:   -DTARGET=dx -DFNATTRS=noundef
+// RUN:   -DTARGET=dx -DFNATTRS=noundef -check-prefixes=CHECK,CHECK-DXIL
 // RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
 // RUN:   spirv-unknown-vulkan-compute %s \
 // RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
-// RUN:   -DTARGET=spv -DFNATTRS="spir_func noundef"
+// RUN:   -DTARGET=spv -DFNATTRS="spir_func noundef" -check-prefixes=CHECK,CHECK-SPIRV
 
-// CHECK: define [[FNATTRS]] i32 @
-[numthreads(1, 1, 1)]
-void main() {
-  while (true) {
+// CHECK-DXIL: define void @
+// CHECK-SPIRV: define spir_func void @
+void test_GroupMemoryBarrierWithGroupSync() {
 // CHECK: call void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync()
   GroupMemoryBarrierWithGroupSync();
-  break;
-  }
 }
 
 // CHECK: declare void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync() #[[ATTRS:[0-9]+]]

>From f6f7ab500a79c212ea521d490435a306ccffe213 Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Thu, 10 Oct 2024 12:30:58 -0700
Subject: [PATCH 3/3] Fixed formatting, missing hlsl prefix in the builtin, and
 reflected the naming changes in the intrinsics

---
 clang/include/clang/Basic/Builtins.td                        | 2 +-
 clang/lib/CodeGen/CGBuiltin.cpp                              | 5 +++--
 clang/lib/CodeGen/CGHLSLRuntime.h                            | 3 ++-
 clang/lib/Headers/hlsl/hlsl_intrinsics.h                     | 2 +-
 clang/lib/Sema/SemaHLSL.cpp                                  | 2 +-
 .../builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl     | 2 +-
 .../builtins/GroupMemoryBarrierWithGroupSync.hlsl            | 4 ++--
 7 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 96fea17badfe9e..05775d4f1fcd20 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4831,7 +4831,7 @@ def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
 }
 
 def HLSLGroupMemoryBarrierWithGroupSync: LangBuiltin<"HLSL_LANG"> {
-  let Spellings = ["__builtin_group_memory_barrier_with_group_sync"];
+  let Spellings = ["__builtin_hlsl_group_memory_barrier_with_group_sync"];
   let Attributes = [NoThrow, Const];
   let Prototype = "void(...)";
 }
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 45890019ee2a64..61bc4335fa738a 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18901,8 +18901,9 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
         CGM.getHLSLRuntime().getRadiansIntrinsic(), ArrayRef<Value *>{Op0},
         nullptr, "hlsl.radians");
   }
-  case Builtin::BI__builtin_group_memory_barrier_with_group_sync: {
-    Intrinsic::ID ID = CGM.getHLSLRuntime().getGroupMemoryBarrierWithGroupSyncIntrinsic();
+  case Builtin::BI__builtin_hlsl_group_memory_barrier_with_group_sync: {
+    Intrinsic::ID ID =
+        CGM.getHLSLRuntime().getGroupMemoryBarrierWithGroupSyncIntrinsic();
     return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
   }
   }
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 1e095cedf58731..fb29d39ef6982e 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -89,7 +89,8 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
   GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
   GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
-  GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync, groupMemoryBarrierWithGroupSync)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
+                                   group_memory_barrier_with_group_sync)
 
   //===----------------------------------------------------------------------===//
   // End of reserved area for HLSL intrinsic getters.
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 637df8924e2468..9f72af0f99007a 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -2177,7 +2177,7 @@ float4 radians(float4);
 /// accesses have been completed and all threads in the group have reached this
 /// call.
 
-_HLSL_BUILTIN_ALIAS(__builtin_group_memory_barrier_with_group_sync)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_group_memory_barrier_with_group_sync)
 void GroupMemoryBarrierWithGroupSync(void);
 
 } // namespace hlsl
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 30480f8f57c12d..57220ce320079e 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2018,7 +2018,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
       return true;
     break;
   }
-  case Builtin::BI__builtin_group_memory_barrier_with_group_sync: {
+  case Builtin::BI__builtin_hlsl_group_memory_barrier_with_group_sync: {
     if (SemaRef.checkArgCountAtMost(TheCall, 0))
       return true;
     break;
diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
index 58d265c7c56f38..82f36758d8f9ec 100644
--- a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync-errors.hlsl
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
 
 void test_too_many_arg() {
-  __builtin_group_memory_barrier_with_group_sync(0);
+  __builtin_hlsl_group_memory_barrier_with_group_sync(0);
   // expected-error at -1 {{too many arguments to function call, expected at most 0, have 1}}
 }
diff --git a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
index 1287de6119384a..145cb21f775b08 100644
--- a/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl
@@ -10,10 +10,10 @@
 // CHECK-DXIL: define void @
 // CHECK-SPIRV: define spir_func void @
 void test_GroupMemoryBarrierWithGroupSync() {
-// CHECK: call void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync()
+// CHECK: call void @llvm.[[TARGET]].group.memory.barrier.with.group.sync()
   GroupMemoryBarrierWithGroupSync();
 }
 
-// CHECK: declare void @llvm.[[TARGET]].groupMemoryBarrierWithGroupSync() #[[ATTRS:[0-9]+]]
+// CHECK: declare void @llvm.[[TARGET]].group.memory.barrier.with.group.sync() #[[ATTRS:[0-9]+]]
 // CHECK-NOT: attributes #[[ATTRS]] = {{.+}}memory(none){{.+}}
 // CHECK: attributes #[[ATTRS]] = {



More information about the cfe-commits mailing list