[clang] [HLSL] Disallow writing to readonly resources (PR #147806)

Justin Bogner via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 11 12:10:13 PDT 2025


================
@@ -697,7 +699,9 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addArraySubscriptOperators() {
       AST.DeclarationNames.getCXXOperatorName(OO_Subscript);
 
   addHandleAccessFunction(Subscript, /*IsConst=*/true, /*IsRef=*/true);
-  addHandleAccessFunction(Subscript, /*IsConst=*/false, /*IsRef=*/true);
+  if (ResClass == llvm::dxil::ResourceClass::UAV)
+    addHandleAccessFunction(Subscript, /*IsConst=*/false, /*IsRef=*/true);
----------------
bogner wrote:

I just realized that we can actually simplify this - while we don't store the resource information directly, we do have access to the `handle` member that does keep track of this information at this point via `getResourceHandleField()`. So we could do something like this:
```c++
HLSLAttributedResourceType::Attributes
BuiltinTypeDeclBuilder::getResourceAttrs() const {
  QualType HandleType = getResourceHandleField()->getType();
  return cast<HLSLAttributedResourceType>(HandleType)->getAttrs();
}
```

and then all we need to do is check `getResourceAttrs().ResourceClass` here.

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


More information about the cfe-commits mailing list