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

Adam Yang via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 10 11:22:06 PDT 2024


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

partially fixes #70103 

### Changes
* Implemented `GroupMemoryBarrierWithGroupSync` clang builtin
* Linked `GroupMemoryBarrierWithGroupSync` clang builtin with `hlsl_intrinsics.h`
* Added sema checks for `GroupMemoryBarrierWithGroupSync` to `CheckHLSLBuiltinFunctionCall` in
`SemaChecking.cpp`
* Add codegen for `GroupMemoryBarrierWithGroupSync` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
* Add codegen tests to `clang/test/CodeGenHLSL/builtins/GroupMemoryBarrierWithGroupSync.hlsl`
* Add sema tests to `clang/test/SemaHLSL/BuiltIns/GroupMemoryBarrierWithGroupSync-errors.hlsl`

### Related PRs

>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/2] 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/2] 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]+]]



More information about the cfe-commits mailing list