Skip to content

Commit 99831c1

Browse files
committed
[HLSL] Add __hlsl_resource_t to types recognized by clang TableGen for built-in functions
1 parent f9910a2 commit 99831c1

File tree

9 files changed

+70
-84
lines changed

9 files changed

+70
-84
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4930,25 +4930,25 @@ def HLSLResourceGetPointer : LangBuiltin<"HLSL_LANG"> {
49304930
def HLSLResourceUninitializedHandle : LangBuiltin<"HLSL_LANG"> {
49314931
let Spellings = ["__builtin_hlsl_resource_uninitializedhandle"];
49324932
let Attributes = [NoThrow];
4933-
let Prototype = "void(...)";
4933+
let Prototype = "__hlsl_resource_t(__hlsl_resource_t)";
49344934
}
49354935

49364936
def HLSLResourceHandleFromBinding : LangBuiltin<"HLSL_LANG"> {
49374937
let Spellings = ["__builtin_hlsl_resource_handlefrombinding"];
49384938
let Attributes = [NoThrow];
4939-
let Prototype = "void(...)";
4939+
let Prototype = "__hlsl_resource_t(__hlsl_resource_t, uint32_t, uint32_t, int32_t, uint32_t, char const*)";
49404940
}
49414941

49424942
def HLSLResourceHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
49434943
let Spellings = ["__builtin_hlsl_resource_handlefromimplicitbinding"];
49444944
let Attributes = [NoThrow];
4945-
let Prototype = "void(...)";
4945+
let Prototype = "__hlsl_resource_t(__hlsl_resource_t, uint32_t, uint32_t, int32_t, uint32_t, char const*)";
49464946
}
49474947

49484948
def HLSLResourceCounterHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
49494949
let Spellings = ["__builtin_hlsl_resource_counterhandlefromimplicitbinding"];
4950-
let Attributes = [NoThrow, CustomTypeChecking];
4951-
let Prototype = "void(...)";
4950+
let Attributes = [NoThrow];
4951+
let Prototype = "__hlsl_resource_t(__hlsl_resource_t, uint32_t, uint32_t)";
49524952
}
49534953

49544954
def HLSLResourceNonUniformIndex : LangBuiltin<"HLSL_LANG"> {
@@ -5176,7 +5176,7 @@ def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
51765176
def HLSLBufferUpdateCounter : LangBuiltin<"HLSL_LANG"> {
51775177
let Spellings = ["__builtin_hlsl_buffer_update_counter"];
51785178
let Attributes = [NoThrow];
5179-
let Prototype = "uint32_t(...)";
5179+
let Prototype = "uint32_t(__hlsl_resource_t, int)";
51805180
}
51815181

51825182
def HLSLSplitDouble: LangBuiltin<"HLSL_LANG"> {

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12603,6 +12603,9 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
1260312603
case 'm':
1260412604
Type = Context.MFloat8Ty;
1260512605
break;
12606+
case 'r':
12607+
Type = Context.HLSLResourceTy;
12608+
break;
1260612609
}
1260712610

1260812611
// If there are modifiers and if we're allowed to parse them, go for it.

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,54 +2947,29 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
29472947
break;
29482948
}
29492949
case Builtin::BI__builtin_hlsl_resource_uninitializedhandle: {
2950-
if (SemaRef.checkArgCount(TheCall, 1) ||
2951-
CheckResourceHandle(&SemaRef, TheCall, 0))
2952-
return true;
2950+
assert(TheCall->getNumArgs() == 1 && "expected 1 arg");
29532951
// use the type of the handle (arg0) as a return type
29542952
QualType ResourceTy = TheCall->getArg(0)->getType();
29552953
TheCall->setType(ResourceTy);
29562954
break;
29572955
}
29582956
case Builtin::BI__builtin_hlsl_resource_handlefrombinding: {
2959-
ASTContext &AST = SemaRef.getASTContext();
2960-
if (SemaRef.checkArgCount(TheCall, 6) ||
2961-
CheckResourceHandle(&SemaRef, TheCall, 0) ||
2962-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(1), AST.UnsignedIntTy) ||
2963-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(2), AST.UnsignedIntTy) ||
2964-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(3), AST.IntTy) ||
2965-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(4), AST.UnsignedIntTy) ||
2966-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(5),
2967-
AST.getPointerType(AST.CharTy.withConst())))
2968-
return true;
2957+
assert(TheCall->getNumArgs() == 6 && "expected 6 args");
29692958
// use the type of the handle (arg0) as a return type
29702959
QualType ResourceTy = TheCall->getArg(0)->getType();
29712960
TheCall->setType(ResourceTy);
29722961
break;
29732962
}
29742963
case Builtin::BI__builtin_hlsl_resource_handlefromimplicitbinding: {
2975-
ASTContext &AST = SemaRef.getASTContext();
2976-
if (SemaRef.checkArgCount(TheCall, 6) ||
2977-
CheckResourceHandle(&SemaRef, TheCall, 0) ||
2978-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(1), AST.UnsignedIntTy) ||
2979-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(2), AST.UnsignedIntTy) ||
2980-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(3), AST.IntTy) ||
2981-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(4), AST.UnsignedIntTy) ||
2982-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(5),
2983-
AST.getPointerType(AST.CharTy.withConst())))
2984-
return true;
2964+
assert(TheCall->getNumArgs() == 6 && "expected 6 args");
29852965
// use the type of the handle (arg0) as a return type
29862966
QualType ResourceTy = TheCall->getArg(0)->getType();
29872967
TheCall->setType(ResourceTy);
29882968
break;
29892969
}
29902970
case Builtin::BI__builtin_hlsl_resource_counterhandlefromimplicitbinding: {
2971+
assert(TheCall->getNumArgs() == 3 && "expected 3 args");
29912972
ASTContext &AST = SemaRef.getASTContext();
2992-
if (SemaRef.checkArgCount(TheCall, 3) ||
2993-
CheckResourceHandle(&SemaRef, TheCall, 0) ||
2994-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(1), AST.UnsignedIntTy) ||
2995-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(2), AST.UnsignedIntTy))
2996-
return true;
2997-
29982973
QualType MainHandleTy = TheCall->getArg(0)->getType();
29992974
auto *MainResType = MainHandleTy->getAs<HLSLAttributedResourceType>();
30002975
auto MainAttrs = MainResType->getAttrs();
@@ -3302,14 +3277,12 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
33023277
break;
33033278
}
33043279
case Builtin::BI__builtin_hlsl_buffer_update_counter: {
3280+
assert(TheCall->getNumArgs() == 2 && "expected 2 args");
33053281
auto checkResTy = [](const HLSLAttributedResourceType *ResTy) -> bool {
33063282
return !(ResTy->getAttrs().ResourceClass == ResourceClass::UAV &&
33073283
ResTy->getAttrs().RawBuffer && ResTy->hasContainedType());
33083284
};
3309-
if (SemaRef.checkArgCount(TheCall, 2) ||
3310-
CheckResourceHandle(&SemaRef, TheCall, 0, checkResTy) ||
3311-
CheckArgTypeMatches(&SemaRef, TheCall->getArg(1),
3312-
SemaRef.getASTContext().IntTy))
3285+
if (CheckResourceHandle(&SemaRef, TheCall, 0, checkResTy))
33133286
return true;
33143287
Expr *OffsetExpr = TheCall->getArg(1);
33153288
std::optional<llvm::APSInt> Offset =

clang/lib/Sema/SemaOverload.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,15 +1825,24 @@ TryImplicitConversion(Sema &S, Expr *From, QualType ToType,
18251825
return ICS;
18261826
}
18271827

1828-
if (S.getLangOpts().HLSL && ToType->isHLSLAttributedResourceType() &&
1829-
FromType->isHLSLAttributedResourceType()) {
1830-
auto *ToResType = cast<HLSLAttributedResourceType>(ToType);
1831-
auto *FromResType = cast<HLSLAttributedResourceType>(FromType);
1832-
if (S.Context.hasSameUnqualifiedType(ToResType->getWrappedType(),
1833-
FromResType->getWrappedType()) &&
1834-
S.Context.hasSameUnqualifiedType(ToResType->getContainedType(),
1835-
FromResType->getContainedType()) &&
1836-
ToResType->getAttrs() == FromResType->getAttrs()) {
1828+
const Type *FromTy = FromType->getUnqualifiedDesugaredType();
1829+
if (S.getLangOpts().HLSL && FromTy->isHLSLAttributedResourceType()) {
1830+
bool CanConvert = false;
1831+
const Type *ToTy = ToType->getUnqualifiedDesugaredType();
1832+
if (ToTy->isHLSLAttributedResourceType()) {
1833+
auto *ToResType = cast<HLSLAttributedResourceType>(ToTy);
1834+
auto *FromResType = cast<HLSLAttributedResourceType>(FromTy);
1835+
if (S.Context.hasSameUnqualifiedType(ToResType->getWrappedType(),
1836+
FromResType->getWrappedType()) &&
1837+
S.Context.hasSameUnqualifiedType(ToResType->getContainedType(),
1838+
FromResType->getContainedType()) &&
1839+
ToResType->getAttrs() == FromResType->getAttrs())
1840+
CanConvert = true;
1841+
}
1842+
else if (ToTy->isHLSLResourceType()) {
1843+
CanConvert = true;
1844+
}
1845+
if (CanConvert) {
18371846
ICS.setStandard();
18381847
ICS.Standard.setAsIdentityConversion();
18391848
ICS.Standard.setFromType(FromType);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ RESOURCE Buffer;
5050
// CHECK-NEXT: MemberExpr {{.*}} lvalue .__handle
5151
// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue implicit this
5252
// CHECK-NEXT: CallExpr {{.*}} '__hlsl_resource_t
53-
// CHECK-NEXT: ImplicitCastExpr {{.*}} <BuiltinFnToFnPtr>
54-
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_uninitializedhandle'
53+
// CHECK-NEXT: ImplicitCastExpr {{.*}} '__hlsl_resource_t (*)(__hlsl_resource_t) noexcept' <BuiltinFnToFnPtr>
54+
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_uninitializedhandle' '__hlsl_resource_t (__hlsl_resource_t) noexcept'
5555
// CHECK-NEXT: MemberExpr {{.*}} lvalue .__handle
5656
// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue implicit this
5757
// CHECK-NEXT: AlwaysInlineAttr
@@ -97,8 +97,8 @@ RESOURCE Buffer;
9797
// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t {{.*}}' lvalue .__handle
9898
// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue Var {{.*}} 'tmp' 'hlsl::[[RESOURCE]]'
9999
// CHECK-NEXT: CallExpr {{.*}} '__hlsl_resource_t {{.*}}'
100-
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(...) noexcept' <BuiltinFnToFnPtr>
101-
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_handlefrombinding' 'void (...) noexcept'
100+
// CHECK-NEXT: ImplicitCastExpr {{.*}} '__hlsl_resource_t (*)(__hlsl_resource_t, unsigned int, unsigned int, int, unsigned int, const char *) noexcept' <BuiltinFnToFnPtr>
101+
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_handlefrombinding' '__hlsl_resource_t (__hlsl_resource_t, unsigned int, unsigned int, int, unsigned int, const char *) noexcept'
102102
// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t {{.*}}' lvalue .__handle
103103
// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue Var {{.*}} 'tmp' 'hlsl::[[RESOURCE]]'
104104
// CHECK-NEXT: DeclRefExpr {{.*}} 'unsigned int' ParmVar {{.*}} 'registerNo' 'unsigned int'
@@ -127,8 +127,8 @@ RESOURCE Buffer;
127127
// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t {{.*}}' lvalue .__handle
128128
// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue Var {{.*}} 'tmp' 'hlsl::[[RESOURCE]]'
129129
// CHECK-NEXT: CallExpr {{.*}} '__hlsl_resource_t {{.*}}'
130-
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(...) noexcept' <BuiltinFnToFnPtr>
131-
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_handlefromimplicitbinding' 'void (...) noexcept'
130+
// CHECK-NEXT: ImplicitCastExpr {{.*}} '__hlsl_resource_t (*)(__hlsl_resource_t, unsigned int, unsigned int, int, unsigned int, const char *) noexcept' <BuiltinFnToFnPtr>
131+
// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_handlefromimplicitbinding' '__hlsl_resource_t (__hlsl_resource_t, unsigned int, unsigned int, int, unsigned int, const char *) noexcept'
132132
// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t {{.*}}' lvalue .__handle
133133
// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::[[RESOURCE]]' lvalue Var {{.*}} 'tmp' 'hlsl::[[RESOURCE]]'
134134
// CHECK-NEXT: DeclRefExpr {{.*}} 'unsigned int' ParmVar {{.*}} 'orderId' 'unsigned int'

0 commit comments

Comments
 (0)