Skip to content

Commit 8baae27

Browse files
committed
add resource attr to hlslbufferdecl, drive diags from attr. Also disallow builtin types as resource types
1 parent 9bdcea2 commit 8baae27

File tree

9 files changed

+51
-22
lines changed

9 files changed

+51
-22
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12154,9 +12154,10 @@ def err_hlsl_missing_semantic_annotation : Error<
1215412154
def err_hlsl_init_priority_unsupported : Error<
1215512155
"initializer priorities are not supported in HLSL">;
1215612156

12157-
def err_hlsl_mismatching_register_resource_type_and_name: Error<"invalid register name prefix '%0' for register resource type '%1' (expected %select{'t'|'u'|'b'|'t'|'s'}2)">;
12157+
def err_hlsl_mismatching_register_resource_type_and_name: Error<"invalid register name prefix '%0' for register resource type '%1' (expected %select{'t'|'u'|'b'|'s'}2)">;
1215812158
def err_hlsl_mismatching_register_builtin_type_and_name: Error<"invalid register name prefix '%0' for '%1' (expected %2)">;
12159-
def err_hlsl_unsupported_register_type : Error<"invalid resource class specifier '%0' used; expected 'b', 's', 't', or 'u'">;
12159+
def err_hlsl_unsupported_register_prefix : Error<"invalid resource class specifier '%0' used; expected 'b', 's', 't', or 'u'">;
12160+
def err_hlsl_unsupported_register_resource_type : Error<"invalid resource '%0' used">;
1216012161
def err_hlsl_unsupported_register_number : Error<"register number should be an integer">;
1216112162
def err_hlsl_expected_space : Error<"invalid space specifier '%0' used; expected 'space' followed by an integer, like space1">;
1216212163
def err_hlsl_pointers_unsupported : Error<

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace clang {
2929
class SemaHLSL : public SemaBase {
3030
public:
3131
SemaHLSL(Sema &S);
32-
32+
HLSLResourceAttr *mergeHLSLResourceAttr(bool CBuffer);
3333
Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
3434
IdentifierInfo *Ident, SourceLocation IdentLoc,
3535
SourceLocation LBrace);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7355,10 +7355,8 @@ static void DiagnoseHLSLResourceRegType(Sema &S, SourceLocation &ArgLoc,
73557355
PrintingPolicy PP = S.getPrintingPolicy();
73567356
std::string typestr = QualType::getAsString(QT.split(), PP);
73577357

7358-
if (Slot[0] != 't')
7359-
S.Diag(ArgLoc,
7360-
diag::err_hlsl_mismatching_register_builtin_type_and_name)
7361-
<< Slot.substr(0, 1) << typestr << "'t'";
7358+
S.Diag(ArgLoc, diag::err_hlsl_unsupported_register_resource_type)
7359+
<< typestr;
73627360
return;
73637361
}
73647362

@@ -7377,7 +7375,13 @@ static void DiagnoseHLSLResourceRegType(Sema &S, SourceLocation &ArgLoc,
73777375
DeclResourceClass = Attr->getResourceClass();
73787376
VarTy = TheRecordDecl->getName();
73797377
} else {
7380-
DeclResourceClass = llvm::hlsl::ResourceClass::CBuffer;
7378+
HLSLResourceAttr *Attr = CBufferOrTBuffer->getAttr<HLSLResourceAttr>();
7379+
DeclResourceClass = Attr->getResourceClass();
7380+
if (CBufferOrTBuffer->isCBuffer()) {
7381+
VarTy = "cbuffer";
7382+
} else {
7383+
VarTy = "tbuffer";
7384+
}
73817385
}
73827386
switch (DeclResourceClass) {
73837387
case llvm::hlsl::ResourceClass::SRV: {
@@ -7392,15 +7396,10 @@ static void DiagnoseHLSLResourceRegType(Sema &S, SourceLocation &ArgLoc,
73927396
}
73937397
case llvm::hlsl::ResourceClass::CBuffer: {
73947398
// could be CBuffer or TBuffer
7395-
if (CBufferOrTBuffer->isCBuffer()) {
7396-
VarTy = "cbuffer";
7399+
if (VarTy == "cbuffer") {
73977400
if (Slot[0] == 'b')
73987401
return;
7399-
} else {
7400-
VarTy = "tbuffer";
7401-
// This isn't an SRV, but we need the diagnostic to emit
7402-
// the same binding prefix that would be expected on an SRV.
7403-
DeclResourceClass = llvm::hlsl::ResourceClass::SRV;
7402+
} else if (VarTy == "tbuffer") {
74047403
if (Slot[0] == 't')
74057404
return;
74067405
}
@@ -7460,7 +7459,7 @@ static void handleHLSLResourceBindingAttr(Sema &S, Decl *D,
74607459
case 't':
74617460
break;
74627461
default:
7463-
S.Diag(ArgLoc, diag::err_hlsl_unsupported_register_type)
7462+
S.Diag(ArgLoc, diag::err_hlsl_unsupported_register_prefix)
74647463
<< Slot.substr(0, 1);
74657464
return;
74667465
}

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ using namespace clang;
2424

2525
SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S) {}
2626

27+
HLSLResourceAttr *SemaHLSL::mergeHLSLResourceAttr(bool CBuffer) {
28+
// cbuffer case
29+
if (CBuffer) {
30+
HLSLResourceAttr *attr = HLSLResourceAttr::CreateImplicit(
31+
getASTContext(), llvm::hlsl::ResourceClass::CBuffer,
32+
llvm::hlsl::ResourceKind::CBuffer,
33+
/*IsROV=*/false);
34+
return attr;
35+
}
36+
// tbuffer case
37+
else {
38+
HLSLResourceAttr *attr = HLSLResourceAttr::CreateImplicit(
39+
getASTContext(), llvm::hlsl::ResourceClass::SRV,
40+
llvm::hlsl::ResourceKind::TBuffer,
41+
/*IsROV=*/false);
42+
return attr;
43+
}
44+
}
45+
2746
Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
2847
SourceLocation KwLoc, IdentifierInfo *Ident,
2948
SourceLocation IdentLoc,
@@ -32,7 +51,9 @@ Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
3251
DeclContext *LexicalParent = SemaRef.getCurLexicalContext();
3352
HLSLBufferDecl *Result = HLSLBufferDecl::Create(
3453
getASTContext(), LexicalParent, CBuffer, KwLoc, Ident, IdentLoc, LBrace);
35-
54+
HLSLResourceAttr *NewAttr = mergeHLSLResourceAttr(CBuffer);
55+
if (NewAttr)
56+
Result->addAttr(NewAttr);
3657
SemaRef.PushOnScopeChains(Result, BufferScope);
3758
SemaRef.PushDeclContext(BufferScope, Result);
3859

clang/test/AST/HLSL/ast-dump-comment-cbuffe-tbufferr.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ tbuffer B {
3838
}
3939

4040
// AST:HLSLBufferDecl {{.*}}:11:1, line:20:1> line:11:9 cbuffer A
41+
// AST-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit CBuffer CBuffer
4142
// AST-NEXT:FullComment {{.*}}<line:10:4, col:17>
4243
// AST-NEXT:`-ParagraphComment {{.*}}<col:4, col:17>
4344
// AST-NEXT:`-TextComment {{.*}}<col:4, col:17> Text=" CBuffer decl."
4445
// AST-NEXT:-VarDecl {{.*}}<line:15:5, col:11> col:11 a 'float'
4546
// AST-NEXT:`-VarDecl {{.*}}<line:19:5, col:9> col:9 b 'int'
4647
// AST-NEXT:HLSLBufferDecl {{.*}}<line:29:1, line:38:1> line:29:9 tbuffer B
48+
// AST-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit SRV TBuffer
4749
// AST-NEXT:-FullComment {{.*}}<line:28:4, col:17>
4850
// AST-NEXT: `-ParagraphComment {{.*}}<col:4, col:17>
4951
// AST-NEXT: `-TextComment {{.*}}<col:4, col:17> Text=" TBuffer decl."

clang/test/AST/HLSL/cbuffer_tbuffer.hlsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -ast-dump -o - %s | FileCheck %s
22

3-
// CHECK:HLSLBufferDecl 0x[[CB:[0-9a-f]+]] {{.*}} line:5:9 cbuffer CB
3+
// CHECK:HLSLBufferDecl 0x[[CB:[0-9a-f]+]] {{.*}} line:6:9 cbuffer CB
4+
// CHECK-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit CBuffer CBuffer
45
// CHECK-NEXT:VarDecl 0x[[A:[0-9a-f]+]] {{.*}} col:9 used a 'float'
56
cbuffer CB {
67
float a;
78
}
89

9-
// CHECK:HLSLBufferDecl 0x[[TB:[0-9a-f]+]] {{.*}} line:11:9 tbuffer TB
10+
// CHECK:HLSLBufferDecl 0x[[TB:[0-9a-f]+]] {{.*}} line:13:9 tbuffer TB
11+
// CHECK-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit SRV TBuffer
1012
// CHECK-NEXT:VarDecl 0x[[B:[0-9a-f]+]] {{.*}} col:9 used b 'float'
1113
tbuffer TB {
1214
float b;

clang/test/AST/HLSL/pch_hlsl_buffer.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ float foo() {
1717
}
1818
// Make sure cbuffer/tbuffer works for PCH.
1919
// CHECK:HLSLBufferDecl 0x{{[0-9a-f]+}} <{{.*}}:7:1, line:9:1> line:7:9 imported <undeserialized declarations> cbuffer A
20+
// CHECK-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit CBuffer CBuffer
2021
// CHECK-NEXT:`-VarDecl 0x[[A:[0-9a-f]+]] <line:8:3, col:9> col:9 imported used a 'float'
2122
// CHECK-NEXT:HLSLBufferDecl 0x{{[0-9a-f]+}} <line:11:1, line:13:1> line:11:9 imported <undeserialized declarations> tbuffer B
23+
// CHECK-NEXT:HLSLResourceAttr {{.*}} <<invalid sloc>> Implicit SRV TBuffer
2224
// CHECK-NEXT:`-VarDecl 0x[[B:[0-9a-f]+]] <line:12:3, col:9> col:9 imported used b 'float'
2325
// CHECK-NEXT:FunctionDecl 0x{{[0-9a-f]+}} <line:15:1, line:17:1> line:15:7 imported foo 'float ()'
2426
// CHECK-NEXT:CompoundStmt 0x{{[0-9a-f]+}} <col:13, line:17:1>

clang/test/AST/HLSL/resource_binding_attr.hlsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -ast-dump -o - %s | FileCheck %s
22

3-
// CHECK:HLSLBufferDecl 0x[[CB:[0-9a-f]+]] {{.*}} line:6:9 cbuffer CB
3+
// CHECK:HLSLBufferDecl 0x[[CB:[0-9a-f]+]] {{.*}} line:7:9 cbuffer CB
4+
// CHECK-NEXT:HLSLResourceAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit CBuffer CBuffer
45
// CHECK-NEXT:HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:14> "b3" "space2"
56
// CHECK-NEXT:VarDecl 0x[[A:[0-9a-f]+]] {{.*}} col:9 used a 'float'
67
cbuffer CB : register(b3, space2) {
78
float a;
89
}
910

10-
// CHECK:HLSLBufferDecl 0x[[TB:[0-9a-f]+]] {{.*}} line:13:9 tbuffer TB
11+
// CHECK:HLSLBufferDecl 0x[[TB:[0-9a-f]+]] {{.*}} line:15:9 tbuffer TB
12+
// CHECK-NEXT:HLSLResourceAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit SRV TBuffer
1113
// CHECK-NEXT:HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:14> "t2" "space1"
1214
// CHECK-NEXT:VarDecl 0x[[B:[0-9a-f]+]] {{.*}} col:9 used b 'float'
1315
tbuffer TB : register(t2, space1) {

clang/test/SemaHLSL/resource_binding_attr_error.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void foo2() {
4444
// expected-warning@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}}
4545
extern RWBuffer<float> U2 : register(u5);
4646
}
47-
// expected-error@+1 {{invalid register name prefix 'u' for 'float' (expected 't')}}
47+
// expected-error@+1 {{invalid resource 'float' used}}
4848
float b : register(u0, space1);
4949

5050
// expected-warning@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}}

0 commit comments

Comments
 (0)