[clang] [HLSL] Split out resource class data from resource attribute (PR #98419)

Justin Bogner via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 10 22:04:44 PDT 2024


================
@@ -437,6 +437,49 @@ void SemaHLSL::handleShaderAttr(Decl *D, const ParsedAttr &AL) {
     D->addAttr(NewAttr);
 }
 
+llvm::dxil::ResourceClass
+getResourceClassFromStr(StringRef ResourceClassTypeStrRef) {
+  if (ResourceClassTypeStrRef == "SRV")
+    return llvm::dxil::ResourceClass::SRV;
+
+  if (ResourceClassTypeStrRef == "UAV")
+    return llvm::dxil::ResourceClass::UAV;
+
+  if (ResourceClassTypeStrRef == "CBuffer")
+    return llvm::dxil::ResourceClass::CBuffer;
+
+  if (ResourceClassTypeStrRef == "Sampler")
+    return llvm::dxil::ResourceClass::Sampler;
+
+  llvm_unreachable("Unsupported resource class type");
+}
+
+void SemaHLSL::handleResourceClassAttr(Decl *D, const ParsedAttr &AL) {
+  if (!AL.isArgIdent(0)) {
+    Diag(AL.getLoc(), diag::err_attribute_argument_type)
+        << AL << AANT_ArgumentIdentifier;
+    return;
+  }
+
+  IdentifierLoc *Loc = AL.getArgAsIdent(0);
+  StringRef ResourceClassTypeStrRef = Loc->Ident->getName();
+  SourceLocation ArgLoc = Loc->Loc;
+
+  // Validate.
+  if (ResourceClassTypeStrRef != "SRV" && ResourceClassTypeStrRef != "UAV" &&
+      ResourceClassTypeStrRef != "CBuffer" &&
+      ResourceClassTypeStrRef != "Sampler") {
+    Diag(ArgLoc, diag::err_hlsl_unsupported_resource_class)
+        << ResourceClassTypeStrRef;
+    return;
+  }
----------------
bogner wrote:

You should be able to use `HLSLResourceClassAttr::ConvertStrToResourceClass` here, rather than hand rolling this. It's also probably fine to just emit `diag::warn_attribute_type_not_supported` rather than create a more specific message just for this.

https://github.com/llvm/llvm-project/pull/98419


More information about the cfe-commits mailing list