Skip to content

Commit dd2b2b8

Browse files
authored
[clang][HLSL] Add GroupMemoryBarrierWithGroupSync intrinsic (#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 * [[DXIL] Add GroupMemoryBarrierWithGroupSync intrinsic #111884](#111884) * [[SPIRV] Add GroupMemoryBarrierWithGroupSync intrinsic #111888](#111888)
1 parent 78e35e4 commit dd2b2b8

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4930,6 +4930,12 @@ def HLSLClip: LangBuiltin<"HLSL_LANG"> {
49304930
let Prototype = "void(...)";
49314931
}
49324932

4933+
def HLSLGroupMemoryBarrierWithGroupSync: LangBuiltin<"HLSL_LANG"> {
4934+
let Spellings = ["__builtin_hlsl_group_memory_barrier_with_group_sync"];
4935+
let Attributes = [NoThrow, Const];
4936+
let Prototype = "void()";
4937+
}
4938+
49334939
// Builtins for XRay.
49344940
def XRayCustomEvent : Builtin {
49354941
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19456,6 +19456,12 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1945619456
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
1945719457
"clip operands types mismatch");
1945819458
return handleHlslClip(E, this);
19459+
case Builtin::BI__builtin_hlsl_group_memory_barrier_with_group_sync: {
19460+
Intrinsic::ID ID =
19461+
CGM.getHLSLRuntime().getGroupMemoryBarrierWithGroupSyncIntrinsic();
19462+
return EmitRuntimeCall(
19463+
Intrinsic::getOrInsertDeclaration(&CGM.getModule(), ID));
19464+
}
1945919465
}
1946019466
return nullptr;
1946119467
}

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class CGHLSLRuntime {
103103

104104
GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding, handle_fromBinding)
105105
GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, bufferUpdateCounter)
106+
GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
107+
group_memory_barrier_with_group_sync)
106108

107109
//===----------------------------------------------------------------------===//
108110
// End of reserved area for HLSL intrinsic getters.

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,5 +2481,17 @@ float3 radians(float3);
24812481
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
24822482
float4 radians(float4);
24832483

2484+
//===----------------------------------------------------------------------===//
2485+
// GroupMemoryBarrierWithGroupSync builtins
2486+
//===----------------------------------------------------------------------===//
2487+
2488+
/// \fn void GroupMemoryBarrierWithGroupSync(void)
2489+
/// \brief Blocks execution of all threads in a group until all group shared
2490+
/// accesses have been completed and all threads in the group have reached this
2491+
/// call.
2492+
2493+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_group_memory_barrier_with_group_sync)
2494+
void GroupMemoryBarrierWithGroupSync(void);
2495+
24842496
} // namespace hlsl
24852497
#endif //_HLSL_HLSL_INTRINSICS_H_
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: -DTARGET=dx -DFNATTRS=noundef -check-prefixes=CHECK,CHECK-DXIL
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: spirv-unknown-vulkan-compute %s \
7+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
8+
// RUN: -DTARGET=spv -DFNATTRS="spir_func noundef" -check-prefixes=CHECK,CHECK-SPIRV
9+
10+
// CHECK-DXIL: define void @
11+
// CHECK-SPIRV: define spir_func void @
12+
void test_GroupMemoryBarrierWithGroupSync() {
13+
// CHECK-DXIL: call void @llvm.[[TARGET]].group.memory.barrier.with.group.sync()
14+
// CHECK-SPIRV: call spir_func void @llvm.[[TARGET]].group.memory.barrier.with.group.sync()
15+
GroupMemoryBarrierWithGroupSync();
16+
}
17+
18+
// CHECK: declare void @llvm.[[TARGET]].group.memory.barrier.with.group.sync() #[[ATTRS:[0-9]+]]
19+
// CHECK-NOT: attributes #[[ATTRS]] = {{.+}}memory(none){{.+}}
20+
// CHECK: attributes #[[ATTRS]] = {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify
2+
3+
void test_too_many_arg() {
4+
__builtin_hlsl_group_memory_barrier_with_group_sync(0);
5+
// expected-error@-1 {{too many arguments to function call, expected 0, have 1}}
6+
}

0 commit comments

Comments
 (0)