Skip to content

Commit f159ee7

Browse files
hekotacheezeburglar
authored andcommitted
[HLSL] Add HLSLResourceBindingAttr to default constant buffer numeric declarations ($Globals) (llvm#128981)
Translates `register(c#`) annotations on numeric constants in the global scope to `HLSLResourceBindingAttr`. Applies to scalar, vector and array constants. Fixes llvm#128964
1 parent 22c407e commit f159ee7

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,18 +1446,20 @@ static bool DiagnoseLocalRegisterBinding(Sema &S, SourceLocation &ArgLoc,
14461446
Ty = Ty->getArrayElementTypeNoTypeQual();
14471447

14481448
// Basic types
1449-
if (Ty->isArithmeticType()) {
1449+
if (Ty->isArithmeticType() || Ty->isVectorType()) {
14501450
bool DeclaredInCOrTBuffer = isa<HLSLBufferDecl>(D->getDeclContext());
14511451
if (SpecifiedSpace && !DeclaredInCOrTBuffer)
14521452
S.Diag(ArgLoc, diag::err_hlsl_space_on_global_constant);
14531453

1454-
if (!DeclaredInCOrTBuffer &&
1455-
(Ty->isIntegralType(S.getASTContext()) || Ty->isFloatingType())) {
1456-
// Default Globals
1454+
if (!DeclaredInCOrTBuffer && (Ty->isIntegralType(S.getASTContext()) ||
1455+
Ty->isFloatingType() || Ty->isVectorType())) {
1456+
// Register annotation on default constant buffer declaration ($Globals)
14571457
if (RegType == RegisterType::CBuffer)
14581458
S.Diag(ArgLoc, diag::warn_hlsl_deprecated_register_type_b);
14591459
else if (RegType != RegisterType::C)
14601460
S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << RegTypeNum;
1461+
else
1462+
return true;
14611463
} else {
14621464
if (RegType == RegisterType::C)
14631465
S.Diag(ArgLoc, diag::warn_hlsl_register_type_c_packoffset);
Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -ast-dump -o - %s | FileCheck %s
22

3-
// CHECK:HLSLBufferDecl 0x[[CB:[0-9a-f]+]] {{.*}} line:8:9 cbuffer CB
4-
// CHECK-NEXT:HLSLResourceClassAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit CBuffer
5-
// CHECK-NEXT:HLSLResourceAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit CBuffer
6-
// CHECK-NEXT:HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:14> "b3" "space2"
7-
// CHECK-NEXT:VarDecl 0x[[A:[0-9a-f]+]] {{.*}} col:9 used a 'hlsl_constant float'
3+
// CHECK: HLSLBufferDecl {{.*}} line:[[# @LINE + 5]]:9 cbuffer CB
4+
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer
5+
// CHECK-NEXT: HLSLResourceAttr {{.*}} Implicit CBuffer
6+
// CHECK-NEXT: HLSLResourceBindingAttr {{.*}} "b3" "space2"
7+
// CHECK-NEXT: VarDecl {{.*}} used a 'hlsl_constant float'
88
cbuffer CB : register(b3, space2) {
99
float a;
1010
}
1111

12-
// CHECK:HLSLBufferDecl 0x[[TB:[0-9a-f]+]] {{.*}} line:17:9 tbuffer TB
13-
// CHECK-NEXT:HLSLResourceClassAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit SRV
14-
// CHECK-NEXT:HLSLResourceAttr 0x[[CB:[0-9a-f]+]] {{.*}} Implicit TBuffer
15-
// CHECK-NEXT:HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:14> "t2" "space1"
16-
// CHECK-NEXT:VarDecl 0x[[B:[0-9a-f]+]] {{.*}} col:9 used b 'hlsl_constant float'
12+
// CHECK: HLSLBufferDecl {{.*}} line:[[# @LINE + 5]]:9 tbuffer TB
13+
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit SRV
14+
// CHECK-NEXT: HLSLResourceAttr {{.*}} Implicit TBuffer
15+
// CHECK-NEXT: HLSLResourceBindingAttr {{.*}} "t2" "space1"
16+
// CHECK-NEXT: VarDecl {{.*}} used b 'hlsl_constant float'
1717
tbuffer TB : register(t2, space1) {
1818
float b;
1919
}
2020

21-
float foo() {
22-
// CHECK: BinaryOperator 0x{{[0-9a-f]+}} <col:10, col:14> 'float' '+'
23-
// CHECK-NEXT: ImplicitCastExpr 0x{{[0-9a-f]+}} <col:10> 'float' <LValueToRValue>
24-
// CHECK-NEXT: DeclRefExpr 0x{{[0-9a-f]+}} <col:10> 'hlsl_constant float' lvalue Var 0x[[A]] 'a' 'hlsl_constant float'
25-
// CHECK-NEXT: ImplicitCastExpr 0x{{[0-9a-f]+}} <col:14> 'float' <LValueToRValue>
26-
// CHECK-NEXT: DeclRefExpr 0x{{[0-9a-f]+}} <col:14> 'hlsl_constant float' lvalue Var 0x[[B]] 'b' 'hlsl_constant float'
21+
export float foo() {
2722
return a + b;
2823
}
2924

30-
// CHECK: VarDecl 0x{{[0-9a-f]+}} <{{.*}}> col:17 UAV 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
31-
// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:17> 'RWBuffer<float>':'hlsl::RWBuffer<float>' 'void ()'
32-
// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:23> "u3" "space0"
25+
// CHECK: VarDecl {{.*}} UAV 'RWBuffer<float>':'hlsl::RWBuffer<float>'
26+
// CHECK: HLSLResourceBindingAttr {{.*}} "u3" "space0"
3327
RWBuffer<float> UAV : register(u3);
3428

35-
// CHECK: -VarDecl 0x{{[0-9a-f]+}} <{{.*}}> col:17 UAV1 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
36-
// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:17> 'RWBuffer<float>':'hlsl::RWBuffer<float>' 'void ()'
37-
// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:24> "u2" "space0"
38-
// CHECK-NEXT:-VarDecl 0x{{[0-9a-f]+}} <col:1, col:38> col:38 UAV2 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
39-
// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:38> 'RWBuffer<float>':'hlsl::RWBuffer<float>' 'void ()'
40-
// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:45> "u4" "space0"
29+
// CHECK: VarDecl {{.*}} UAV1 'RWBuffer<float>':'hlsl::RWBuffer<float>'
30+
// CHECK: HLSLResourceBindingAttr {{.*}} "u2" "space0"
31+
// CHECK: VarDecl {{.*}} UAV2 'RWBuffer<float>':'hlsl::RWBuffer<float>'
32+
// CHECK: HLSLResourceBindingAttr {{.*}} "u4" "space0"
4133
RWBuffer<float> UAV1 : register(u2), UAV2 : register(u4);
34+
35+
//
36+
// Default constants ($Globals) layout annotations
37+
38+
// CHECK: VarDecl {{.*}} f 'hlsl_constant float'
39+
// CHECK: HLSLResourceBindingAttr {{.*}} "c5" "space0"
40+
float f : register(c5);
41+
42+
// CHECK: VarDecl {{.*}} intv 'hlsl_constant int4':'vector<int hlsl_constant, 4>'
43+
// CHECK: HLSLResourceBindingAttr {{.*}} "c2" "space0"
44+
int4 intv : register(c2);
45+
46+
// CHECK: VarDecl {{.*}} dar 'hlsl_constant double[5]'
47+
// CHECK: HLSLResourceBindingAttr {{.*}} "c3" "space0"
48+
double dar[5] : register(c3);
49+
50+
struct S {
51+
int a;
52+
};
53+
54+
// CHECK: VarDecl {{.*}} s 'hlsl_constant S'
55+
// CHECK: HLSLResourceBindingAttr {{.*}} "c10" "space0
56+
S s : register(c10);

0 commit comments

Comments
 (0)