Skip to content

Commit 94bde8c

Browse files
authored
[HLSL] Add Increment/DecrementCounter methods to structured buffers (#114148)
Introduces `__builtin_hlsl_buffer_update_counter` clang buildin that is used to implement the `IncrementCounter` and `DecrementCounter` methods on `RWStructuredBuffer` and `RasterizerOrderedStructuredBuffer` (see Note). The builtin is translated to LLVM intrisic `llvm.dx.bufferUpdateCounter` or `llvm.spv.bufferUpdateCounter`. Introduces `BuiltinTypeMethodBuilder` helper in `HLSLExternalSemaSource` that enables adding methods to builtin types using builder pattern like this: ``` BuiltinTypeMethodBuilder(Sema, RecordBuilder, "MethodName", ReturnType) .addParam("param_name", Type, InOutModifier) .callBuiltin("buildin_name", { BuiltinParams }) .finalizeMethod(); ``` Fixes #113513
1 parent 1944d19 commit 94bde8c

File tree

14 files changed

+534
-96
lines changed

14 files changed

+534
-96
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4882,7 +4882,6 @@ def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
48824882
let Prototype = "void(...)";
48834883
}
48844884

4885-
48864885
def HLSLSelect : LangBuiltin<"HLSL_LANG"> {
48874886
let Spellings = ["__builtin_hlsl_select"];
48884887
let Attributes = [NoThrow, Const];
@@ -4907,6 +4906,12 @@ def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
49074906
let Prototype = "void(...)";
49084907
}
49094908

4909+
def HLSLBufferUpdateCounter : LangBuiltin<"HLSL_LANG"> {
4910+
let Spellings = ["__builtin_hlsl_buffer_update_counter"];
4911+
let Attributes = [NoThrow];
4912+
let Prototype = "uint32_t(...)";
4913+
}
4914+
49104915
def HLSLSplitDouble: LangBuiltin<"HLSL_LANG"> {
49114916
let Spellings = ["__builtin_hlsl_elementwise_splitdouble"];
49124917
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7287,6 +7287,8 @@ def err_typecheck_illegal_increment_decrement : Error<
72877287
"cannot %select{decrement|increment}1 value of type %0">;
72887288
def err_typecheck_expect_int : Error<
72897289
"used type %0 where integer is required">;
7290+
def err_typecheck_expect_hlsl_resource : Error<
7291+
"used type %0 where __hlsl_resource_t is required">;
72907292
def err_typecheck_arithmetic_incomplete_or_sizeless_type : Error<
72917293
"arithmetic on a pointer to %select{an incomplete|sizeless}0 type %1">;
72927294
def err_typecheck_pointer_arith_function_type : Error<
@@ -12528,6 +12530,10 @@ def warn_attr_min_eq_max: Warning<
1252812530

1252912531
def err_hlsl_attribute_number_arguments_insufficient_shader_model: Error<
1253012532
"attribute %0 with %1 arguments requires shader model %2 or greater">;
12533+
def err_hlsl_expect_arg_const_int_one_or_neg_one: Error<
12534+
"argument %0 must be constant integer 1 or -1">;
12535+
def err_invalid_hlsl_resource_type: Error<
12536+
"invalid __hlsl_resource_t type attributes">;
1253112537

1253212538
// Layout randomization diagnostics.
1253312539
def err_non_designated_init_used : Error<

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19409,6 +19409,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1940919409
CGM.getHLSLRuntime().getRadiansIntrinsic(), ArrayRef<Value *>{Op0},
1941019410
nullptr, "hlsl.radians");
1941119411
}
19412+
case Builtin::BI__builtin_hlsl_buffer_update_counter: {
19413+
Value *ResHandle = EmitScalarExpr(E->getArg(0));
19414+
Value *Offset = EmitScalarExpr(E->getArg(1));
19415+
Value *OffsetI8 = Builder.CreateIntCast(Offset, Int8Ty, true);
19416+
return Builder.CreateIntrinsic(
19417+
/*ReturnType=*/Offset->getType(),
19418+
CGM.getHLSLRuntime().getBufferUpdateCounterIntrinsic(),
19419+
ArrayRef<Value *>{ResHandle, OffsetI8}, nullptr);
19420+
}
1941219421
case Builtin::BI__builtin_hlsl_elementwise_splitdouble: {
1941319422

1941419423
assert((E->getArg(0)->getType()->hasFloatingRepresentation() &&

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class CGHLSLRuntime {
102102
GENERATE_HLSL_INTRINSIC_FUNCTION(UClamp, uclamp)
103103

104104
GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding, handle_fromBinding)
105+
GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, bufferUpdateCounter)
105106

106107
//===----------------------------------------------------------------------===//
107108
// End of reserved area for HLSL intrinsic getters.

0 commit comments

Comments
 (0)