python3kgae created this revision.
python3kgae added reviewers: pow2clk, beanz, bogner.
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.

unbounded resource array is allowed in HLSL.
Here is an example.

RWBuffer<float> B[];
 float main(uint ID : BufID) : SV_Target {

    return B[ID][0];
  }

Type::isHLSLResourceType is added to check HLSLResource.
HLSLResourceAttr is added on the forward decl so it is ready when check 
incomplete array type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136482

Files:
  clang/include/clang/AST/Type.h
  clang/lib/AST/Type.cpp
  clang/lib/Sema/HLSLExternalSemaSource.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/test/AST/HLSL/unbounded_global_resource_array.hlsl
  clang/test/SemaHLSL/unbounded_global_resource_array.hlsl

Index: clang/test/SemaHLSL/unbounded_global_resource_array.hlsl
===================================================================
--- /dev/null
+++ clang/test/SemaHLSL/unbounded_global_resource_array.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil--shadermodel6.7-library  %s  -verify
+
+// expected-error@+1 {{definition of variable with array type needs an explicit size or an initializer}}
+static RWBuffer<float> A[];
+// expected-error@+1 {{definition of variable with array type needs an explicit size or an initializer}}
+float F[];
+
+// Make sure extern global resource unbounded array compiles.
+RWBuffer<float> B[];
Index: clang/test/AST/HLSL/unbounded_global_resource_array.hlsl
===================================================================
--- /dev/null
+++ clang/test/AST/HLSL/unbounded_global_resource_array.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -fsyntax-only -ast-dump %s | FileCheck %s
+
+
+// CHECK:VarDecl 0x{{[0-9a-f]+}} <{{.*}}:7:1, col:19> col:17 B 'RWBuffer<float>[]'
+// Make sure no CXXConstructExpr created for unbounded resource array.
+// CHECK-NOT:CXXConstructExpr
+RWBuffer<float> B[];
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -13504,7 +13504,9 @@
 
     // Provide a specific diagnostic for uninitialized variable
     // definitions with incomplete array type.
-    if (Type->isIncompleteArrayType()) {
+    if (Type->isIncompleteArrayType() &&
+        !(getLangOpts().HLSL && Var->hasExternalFormalLinkage() &&
+          Type->getArrayElementTypeNoTypeQual()->isHLSLResourceType())) {
       if (Var->isConstexpr())
         Diag(Var->getLocation(), diag::err_constexpr_var_requires_const_init)
             << Var;
@@ -13576,6 +13578,12 @@
     if (getLangOpts().OpenCL &&
         Var->getType().getAddressSpace() == LangAS::opencl_local)
       return;
+
+    // In HLSL, global resource array is allowed to be incomplete.
+    // Skip initializing incomplate array here.
+    if (getLangOpts().HLSL && Var->getType()->isIncompleteArrayType())
+      return;
+
     // C++03 [dcl.init]p9:
     //   If no initializer is specified for an object, and the
     //   object is of (possibly cv-qualified) non-POD class type (or
Index: clang/lib/Sema/HLSLExternalSemaSource.cpp
===================================================================
--- clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -474,6 +474,8 @@
              .addTemplateArgumentList()
              .addTypeParameter("element_type", SemaPtr->getASTContext().FloatTy)
              .finalizeTemplateArgs()
+             .annotateResourceClass(HLSLResourceAttr::UAV,
+                                    HLSLResourceAttr::TypedBuffer)
              .Record;
   if (!Decl->isCompleteDefinition())
     Completions.insert(
@@ -503,7 +505,5 @@
       .addHandleMember()
       .addDefaultHandleConstructor(*SemaPtr, ResourceClass::UAV)
       .addArraySubscriptOperators()
-      .annotateResourceClass(HLSLResourceAttr::UAV,
-                             HLSLResourceAttr::TypedBuffer)
       .completeDefinition();
 }
Index: clang/lib/AST/Type.cpp
===================================================================
--- clang/lib/AST/Type.cpp
+++ clang/lib/AST/Type.cpp
@@ -572,6 +572,19 @@
   return false;
 }
 
+bool Type::isHLSLResourceType() const {
+  if (const auto *RT = getAs<RecordType>()) {
+    if (const auto *D =
+            llvm::dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
+      if (D->isCompleteDefinition())
+        return D->hasAttr<HLSLResourceAttr>();
+      ClassTemplateDecl *CTD = D->getSpecializedTemplate();
+      return CTD->getTemplatedDecl()->hasAttr<HLSLResourceAttr>();
+    }
+  }
+  return false;
+}
+
 bool Type::isInterfaceType() const {
   if (const auto *RT = getAs<RecordType>())
     return RT->getDecl()->isInterface();
Index: clang/include/clang/AST/Type.h
===================================================================
--- clang/include/clang/AST/Type.h
+++ clang/include/clang/AST/Type.h
@@ -2220,6 +2220,9 @@
   /// object for a subclass of NSFoo".
   bool isObjCClassOrClassKindOfType() const;
 
+  ///  Whether the type is HLSL SRV/UAV/ConstantBuffer/Sampler type.
+  bool isHLSLResourceType() const;
+
   bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const;
   bool isObjCSelType() const;                 // Class
   bool isObjCBuiltinType() const;               // 'id' or 'Class'
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to