Skip to content

Commit 90b7fe4

Browse files
authored
[HLSL] Remove __builtin_hlsl_create_handle (llvm#109910)
The `__builtin_hlsl_create_handle` called from the constructor of resource buffer class was supposed to initialize the resource handle based on resource type and registry binding information. It is not possible to do though that because the registry binding information is not accessible from the constructor during codegen. Instead, the handle should be initialized to an empty or null handle with something like `__builtin_hlsl_create_null_handle`. This PR is removing `__builtin_hlsl_create_handle` first and the `__builtin_hlsl_create_null_handle` will be added to the constructor once the handle type changes to `__hlsl_resource_t` and HLSLAttributeResourceType is updated to be a canonical type, which will allow the initialization assignment. The actual handle initialization based on the registry binding will be implemented part 2/2 of llvm#105076 once the dependent tasks are completed. Part 1/2 of llvm#105076.
1 parent 0950078 commit 90b7fe4

File tree

11 files changed

+20
-54
lines changed

11 files changed

+20
-54
lines changed

clang/include/clang/Basic/Builtins.td

-6
Original file line numberDiff line numberDiff line change
@@ -4703,12 +4703,6 @@ def HLSLClamp : LangBuiltin<"HLSL_LANG"> {
47034703
let Prototype = "void(...)";
47044704
}
47054705

4706-
def HLSLCreateHandle : LangBuiltin<"HLSL_LANG"> {
4707-
let Spellings = ["__builtin_hlsl_create_handle"];
4708-
let Attributes = [NoThrow, Const];
4709-
let Prototype = "void*(unsigned char)";
4710-
}
4711-
47124706
def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
47134707
let Spellings = ["__builtin_hlsl_dot"];
47144708
let Attributes = [NoThrow, Const];

clang/lib/Sema/HLSLExternalSemaSource.cpp

+2-30
Original file line numberDiff line numberDiff line change
@@ -193,36 +193,8 @@ struct BuiltinTypeDeclBuilder {
193193
ExplicitSpecifier(), false, true, false,
194194
ConstexprSpecKind::Unspecified);
195195

196-
DeclRefExpr *Fn =
197-
lookupBuiltinFunction(AST, S, "__builtin_hlsl_create_handle");
198-
Expr *RCExpr = emitResourceClassExpr(AST, RC);
199-
Expr *Call = CallExpr::Create(AST, Fn, {RCExpr}, AST.VoidPtrTy, VK_PRValue,
200-
SourceLocation(), FPOptionsOverride());
201-
202-
CXXThisExpr *This = CXXThisExpr::Create(
203-
AST, SourceLocation(), Constructor->getFunctionObjectParameterType(),
204-
true);
205-
Expr *Handle = MemberExpr::CreateImplicit(AST, This, false, Fields["h"],
206-
Fields["h"]->getType(), VK_LValue,
207-
OK_Ordinary);
208-
209-
// If the handle isn't a void pointer, cast the builtin result to the
210-
// correct type.
211-
if (Handle->getType().getCanonicalType() != AST.VoidPtrTy) {
212-
Call = CXXStaticCastExpr::Create(
213-
AST, Handle->getType(), VK_PRValue, CK_Dependent, Call, nullptr,
214-
AST.getTrivialTypeSourceInfo(Handle->getType(), SourceLocation()),
215-
FPOptionsOverride(), SourceLocation(), SourceLocation(),
216-
SourceRange());
217-
}
218-
219-
BinaryOperator *Assign = BinaryOperator::Create(
220-
AST, Handle, Call, BO_Assign, Handle->getType(), VK_LValue, OK_Ordinary,
221-
SourceLocation(), FPOptionsOverride());
222-
223-
Constructor->setBody(
224-
CompoundStmt::Create(AST, {Assign}, FPOptionsOverride(),
225-
SourceLocation(), SourceLocation()));
196+
Constructor->setBody(CompoundStmt::Create(
197+
AST, {}, FPOptionsOverride(), SourceLocation(), SourceLocation()));
226198
Constructor->setAccess(AccessSpecifier::AS_public);
227199
Record->addDecl(Constructor);
228200
return *this;

clang/test/AST/HLSL/RWBuffer-AST.hlsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ RWBuffer<float> Buffer;
6666
// CHECK: TemplateArgument type 'float'
6767
// CHECK-NEXT: BuiltinType 0x{{[0-9A-Fa-f]+}} 'float'
6868
// CHECK-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
69-
// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced h 'float *
69+
// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'float *
7070
// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
7171
// CHECK-SAME{LITERAL}: [[hlsl::contained_type(float)]]
7272
// CHECK-SAME: ':'float *'

clang/test/AST/HLSL/StructuredBuffer-AST.hlsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ StructuredBuffer<float> Buffer;
7070
// CHECK: TemplateArgument type 'float'
7171
// CHECK-NEXT: BuiltinType 0x{{[0-9A-Fa-f]+}} 'float'
7272
// CHECK-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
73-
// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced h 'float *
73+
// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'float *
7474
// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
7575
// CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
7676
// CHECK-SAME{LITERAL}: [[hlsl::contained_type(float)]]

clang/test/CodeGenHLSL/builtins/RWBuffer-constructor.hlsl

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
22
// RUN: %clang_cc1 -triple spirv-vulkan-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
33

4+
// XFAIL: *
5+
// This expectedly fails because create.handle is no longer called
6+
// from RWBuffer constructor and the replacement has not been
7+
// implemented yet. This test should be updated to expect
8+
// dx.create.handleFromBinding as part of issue #105076.
9+
410
RWBuffer<float> Buf;
511

612
// CHECK: define linkonce_odr noundef ptr @"??0?$RWBuffer@M@hlsl@@QAA@XZ"
@@ -10,4 +16,4 @@ RWBuffer<float> Buf;
1016
// CHECK: store ptr %[[HandleRes]], ptr %h, align 4
1117

1218
// CHECK-SPIRV: %[[HandleRes:[0-9]+]] = call ptr @llvm.spv.create.handle(i8 1)
13-
// CHECK-SPIRV: store ptr %[[HandleRes]], ptr %h, align 8
19+
// CHECK-SPIRV: store ptr %[[HandleRes]], ptr %h, align 8

clang/test/CodeGenHLSL/builtins/StructuredBuffer-constructor.hlsl

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
12
// RUN: %clang_cc1 -triple spirv-vulkan-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
23

4+
// XFAIL: *
5+
// This expectedly fails because create.handle is no longer invoked
6+
// from StructuredBuffer constructor and the replacement has not been
7+
// implemented yet. This test should be updated to expect
8+
// dx.create.handleFromBinding as part of issue #105076.
9+
310
StructuredBuffer<float> Buf;
411

512
// CHECK: define linkonce_odr noundef ptr @"??0?$StructuredBuffer@M@hlsl@@QAA@XZ"

clang/test/CodeGenHLSL/builtins/create_handle.hlsl

-7
This file was deleted.

clang/test/ParserHLSL/hlsl_resource_handle_attrs.hlsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// CHECK: -ClassTemplateSpecializationDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> class RWBuffer definition implicit_instantiation
44
// CHECK: -TemplateArgument type 'float'
55
// CHECK: `-BuiltinType 0x{{[0-9a-f]+}} 'float'
6-
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced h 'float *
6+
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'float *
77
// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
88
// CHECK-SAME{LITERAL}: [[hlsl::contained_type(float)]]
99
// CHECK-SAME: ':'float *'
@@ -14,7 +14,7 @@ RWBuffer<float> Buffer1;
1414
// CHECK: -TemplateArgument type 'vector<float, 4>'
1515
// CHECK: `-ExtVectorType 0x{{[0-9a-f]+}} 'vector<float, 4>' 4
1616
// CHECK: `-BuiltinType 0x{{[0-9a-f]+}} 'float'
17-
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced h 'vector<float *, 4>
17+
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'vector<float *, 4>
1818
// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]
1919
// CHECK-SAME{LITERAL}: [[hlsl::is_rov]]
2020
// CHECK-SAME{LITERAL}: [[hlsl::contained_type(vector<float, 4>)]]

llvm/include/llvm/IR/IntrinsicsDirectX.td

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ def int_dx_group_id : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWi
1717
def int_dx_thread_id_in_group : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
1818
def int_dx_flattened_thread_id_in_group : Intrinsic<[llvm_i32_ty], [], [IntrNoMem, IntrWillReturn]>;
1919

20-
def int_dx_create_handle : ClangBuiltin<"__builtin_hlsl_create_handle">,
21-
Intrinsic<[ llvm_ptr_ty ], [llvm_i8_ty], [IntrWillReturn]>;
22-
2320
// Create resource handle given binding information. Returns a `target("dx.")`
2421
// type appropriate for the kind of resource given a register space ID, lower
2522
// bound and range size of the binding, as well as an index and an indicator

llvm/include/llvm/IR/IntrinsicsSPIRV.td

-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ let TargetPrefix = "spv" in {
5959

6060
// The following intrinsic(s) are mirrored from IntrinsicsDirectX.td for HLSL support.
6161
def int_spv_thread_id : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
62-
def int_spv_create_handle : ClangBuiltin<"__builtin_hlsl_create_handle">,
63-
Intrinsic<[ llvm_ptr_ty ], [llvm_i8_ty], [IntrWillReturn]>;
6462
def int_spv_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
6563
def int_spv_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
6664
def int_spv_frac : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty]>;

llvm/unittests/IR/IntrinsicsTest.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ TEST(IntrinsicNameLookup, ClangBuiltinLookup) {
9494
{"__builtin_amdgcn_workgroup_id_z", "amdgcn", amdgcn_workgroup_id_z},
9595
{"__builtin_arm_cdp", "arm", arm_cdp},
9696
{"__builtin_bpf_preserve_type_info", "bpf", bpf_preserve_type_info},
97-
{"__builtin_hlsl_create_handle", "dx", dx_create_handle},
9897
{"__builtin_HEXAGON_A2_tfr", "hexagon", hexagon_A2_tfr},
9998
{"__builtin_lasx_xbz_w", "loongarch", loongarch_lasx_xbz_w},
10099
{"__builtin_mips_bitrev", "mips", mips_bitrev},

0 commit comments

Comments
 (0)