python3kgae created this revision.
python3kgae added reviewers: beanz, pow2clk, bogner.
Herald added a reviewer: aaron.ballman.
Herald added a subscriber: Anastasia.
Herald added a project: All.
python3kgae requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Allow register binding attribute on variables.

Report warning when register binding attribute applies to local variable or 
static variable.
It will be ignored in this case.

Type check for register binding is tracked with 
https://github.com/llvm/llvm-project/issues/57886.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134617

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Parse/Parser.h
  clang/lib/Parse/ParseDecl.cpp
  clang/test/AST/HLSL/resource_binding_attr.hlsl
  clang/test/SemaHLSL/resource_binding_attr_error.hlsl

Index: clang/test/SemaHLSL/resource_binding_attr_error.hlsl
===================================================================
--- clang/test/SemaHLSL/resource_binding_attr_error.hlsl
+++ clang/test/SemaHLSL/resource_binding_attr_error.hlsl
@@ -1,10 +1,6 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - -fsyntax-only %s -verify
 
-// expected-error@+5 {{expected ';' after top level declarator}}
-// expected-error@+4 {{expected ')'}}
-// expected-note@+3 {{to match this '('}}
-// expected-error@+2 {{a type specifier is required for all declarations}}
-// expected-error@+1 {{illegal storage class on file-scoped variable}}
+// expected-error@+1 {{invalid resource class specifier 'c' used; expected 'b', 's', 't', or 'u'}}
 float a : register(c0, space1);
 
 // expected-error@+1 {{invalid resource class specifier 'i' used; expected 'b', 's', 't', or 'u'}}
@@ -36,3 +32,26 @@
 // expected-error@+2 {{wrong argument format for hlsl attribute, use b2 instead}}
 // expected-error@+1 {{wrong argument format for hlsl attribute, use space3 instead}}
 cbuffer D : register(b 2, space 3) {}
+
+// expected-warning@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}}
+static RWBuffer<float> U : register(u5);
+
+void foo() {
+  // expected-warning@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}}
+  RWBuffer<float> U : register(u3);
+}
+
+// FIXEME: expect-error once fix https://github.com/llvm/llvm-project/issues/57886.
+float b : register(u0, space1);
+
+// expected-warning@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}}
+void bar(RWBuffer<float> U : register(u3)) {
+
+}
+
+struct S {
+  // FIXME: generate better error when support semantic on struct field.
+  // See https://github.com/llvm/llvm-project/issues/57889.
+  // expected-error@+1 {{expected expression}}
+  RWBuffer<float> U : register(u3);
+};
Index: clang/test/AST/HLSL/resource_binding_attr.hlsl
===================================================================
--- clang/test/AST/HLSL/resource_binding_attr.hlsl
+++ clang/test/AST/HLSL/resource_binding_attr.hlsl
@@ -22,3 +22,16 @@
 // CHECK-NEXT: DeclRefExpr 0x{{[0-9a-f]+}} <col:14> 'float' lvalue Var 0x[[B]] 'b' 'float'
   return a + b;
 }
+
+// CHECK: VarDecl 0x{{[0-9a-f]+}} <{{.*}}> col:17 UAV 'RWBuffer<float>':'hlsl::RWBuffer<>' callinit
+// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:17> 'RWBuffer<float>':'hlsl::RWBuffer<>' 'void ()'
+// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:23> "u3" "space0"
+RWBuffer<float> UAV : register(u3);
+
+// CHECK: -VarDecl 0x{{[0-9a-f]+}} <{{.*}}> col:17 UAV1 'RWBuffer<float>':'hlsl::RWBuffer<>' callinit
+// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:17> 'RWBuffer<float>':'hlsl::RWBuffer<>' 'void ()'
+// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:24> "u2" "space0"
+// CHECK-NEXT:-VarDecl 0x{{[0-9a-f]+}} <col:1, col:38> col:38 UAV2 'RWBuffer<float>':'hlsl::RWBuffer<>' callinit
+// CHECK-NEXT:-CXXConstructExpr 0x{{[0-9a-f]+}} <col:38> 'RWBuffer<float>':'hlsl::RWBuffer<>' 'void ()'
+// CHECK-NEXT:-HLSLResourceBindingAttr 0x{{[0-9a-f]+}} <col:45> "u4" "space0"
+RWBuffer<float> UAV1 : register(u2), UAV2 : register(u4);
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -2054,6 +2054,9 @@
     return nullptr;
   }
 
+  if (getLangOpts().HLSL)
+    MaybeParseHLSLSemantics(D);
+
   if (Tok.is(tok::kw_requires))
     ParseTrailingRequiresClause(D);
 
@@ -2223,6 +2226,10 @@
       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
 
     ParseDeclarator(D);
+
+    if (getLangOpts().HLSL)
+      MaybeParseHLSLSemantics(D);
+
     if (!D.isInvalidType()) {
       // C++2a [dcl.decl]p1
       //    init-declarator:
Index: clang/include/clang/Parse/Parser.h
===================================================================
--- clang/include/clang/Parse/Parser.h
+++ clang/include/clang/Parse/Parser.h
@@ -2814,6 +2814,16 @@
       Sema::AttributeCompletion Completion = Sema::AttributeCompletion::None,
       const IdentifierInfo *EnclosingScope = nullptr);
 
+
+  void MaybeParseHLSLSemantics(Declarator &D,
+                               SourceLocation *EndLoc = nullptr) {
+    if (Tok.is(tok::colon)) {
+      ParsedAttributes Attrs(AttrFactory);
+      ParseHLSLSemantics(Attrs, EndLoc);
+      D.takeAttributes(Attrs);
+    }
+  }
+
   void MaybeParseHLSLSemantics(ParsedAttributes &Attrs,
                                SourceLocation *EndLoc = nullptr) {
     if (getLangOpts().HLSL && Tok.is(tok::colon))
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -133,6 +133,10 @@
 def GlobalVar : SubsetSubject<Var,
                              [{S->hasGlobalStorage()}], "global variables">;
 
+def ExternalGlobalVar : SubsetSubject<Var,
+                             [{S->hasGlobalStorage() && S->getStorageClass()!=StorageClass::SC_Static}],
+                             "external global variables">;
+
 def InlineFunction : SubsetSubject<Function,
                              [{S->isInlineSpecified()}], "inline functions">;
 
@@ -4046,7 +4050,7 @@
 
 def HLSLResourceBinding: InheritableAttr {
   let Spellings = [HLSLSemantic<"register">];
-  let Subjects = SubjectList<[HLSLBufferObj, GlobalVar]>;
+  let Subjects = SubjectList<[HLSLBufferObj, ExternalGlobalVar]>;
   let LangOpts = [HLSL];
   let Args = [StringArgument<"Slot">, StringArgument<"Space", 1>];
   let Documentation = [HLSLResourceBindingDocs];
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to