Skip to content

Commit 7d11952

Browse files
committed
add condition that sizeof >= 1
1 parent cbcdcd3 commit 7d11952

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5118,7 +5118,7 @@ bool Type::isHLSLIntangibleType() const {
51185118

51195119
CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
51205120
assert(RD != nullptr &&
5121-
"all HLSL struct and classes should be CXXRecordDecl");
5121+
"all HLSL structs and classes should be CXXRecordDecl");
51225122
assert(RD->isCompleteDefinition() && "expecting complete type");
51235123
return RD->isHLSLIntangible();
51245124
}

clang/lib/Sema/HLSLExternalSemaSource.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,26 @@ static Expr *constructRawBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
891891
Context, IsIntangibleExpr, UO_Not, BoolTy, VK_LValue, OK_Ordinary,
892892
NameLoc, false, FPOptionsOverride());
893893

894-
return NotIntangibleExpr;
894+
// element types also may not be of 0 size
895+
UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
896+
UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc);
897+
898+
// Create a BinaryOperator that checks if the size of the type is not equal to
899+
// 1 Empty structs have a size of 1 in HLSL, so we need to check for that
900+
IntegerLiteral *rhs = IntegerLiteral::Create(
901+
Context, llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1, true),
902+
Context.getSizeType(), NameLoc);
903+
904+
BinaryOperator *SizeGEQOneExpr =
905+
BinaryOperator::Create(Context, SizeOfExpr, rhs, BO_GE, BoolTy, VK_LValue,
906+
OK_Ordinary, NameLoc, FPOptionsOverride());
907+
908+
// Combine the two constraints
909+
BinaryOperator *CombinedExpr = BinaryOperator::Create(
910+
Context, NotIntangibleExpr, SizeGEQOneExpr, BO_LAnd, BoolTy, VK_LValue,
911+
OK_Ordinary, NameLoc, FPOptionsOverride());
912+
913+
return CombinedExpr;
895914
}
896915

897916
static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, NamespaceDecl *NSD,

clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ typedef vector<float, 3> float3;
55
StructuredBuffer<float3> Buffer;
66

77
// expected-error@+2 {{class template 'StructuredBuffer' requires template arguments}}
8-
// expected-note@*:* {{template declaration from hidden source: template <typename element_type> class StructuredBuffer {}}}
8+
// expected-note@*:* {{template declaration from hidden source: template <typename element_type> requires __is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
99
StructuredBuffer BufferErr1;
1010

1111
// expected-error@+2 {{too few template arguments for class template 'StructuredBuffer'}}
12-
// expected-note@*:* {{template declaration from hidden source: template <typename element_type> class StructuredBuffer {}}}
12+
// expected-note@*:* {{template declaration from hidden source: template <typename element_type> requires __is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
1313
StructuredBuffer<> BufferErr2;
1414

15+
// test elements of 0 size
16+
// expected-error@+3{{constraints not satisfied for class template 'StructuredBuffer' [with element_type = int[0]]}}
17+
// expected-note@*:*{{because 'int[0]' does not satisfy '__is_raw_resource_element_compatible'}}
18+
// expected-note@*:*{{because 'sizeof(int[0]) >= 1UL' (0 >= 1) evaluated to false}}
19+
StructuredBuffer<int[0]> BufferErr3;
20+
21+
// In C++, empty structs do have a size of 1. So should HLSL.
22+
// The concept will accept empty structs as element types, despite it being unintuitive.
23+
struct Empty {};
24+
StructuredBuffer<Empty> BufferErr4;
25+
26+
1527
[numthreads(1,1,1)]
1628
void main() {
1729
(void)Buffer.__handle; // expected-error {{'__handle' is a private member of 'hlsl::StructuredBuffer<vector<float, 3>>'}}

0 commit comments

Comments
 (0)