[clang] [HLSL] Make HLSLAttributedResourceType canonical and add code paths to convert HLSL types to DirectX target types (PR #110327)

Helena Kotas via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 30 12:39:33 PDT 2024


================
@@ -29,19 +29,48 @@ class DirectXTargetCodeGenInfo : public TargetCodeGenInfo {
 
 llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM,
                                                   const Type *Ty) const {
-  auto *BuiltinTy = dyn_cast<BuiltinType>(Ty);
-  if (!BuiltinTy || BuiltinTy->getKind() != BuiltinType::HLSLResource)
+  auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
+  if (!ResType)
     return nullptr;
 
   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
-  // FIXME: translate __hlsl_resource_t to target("dx.TypedBuffer", <4 x float>,
-  // 1, 0, 0) only for now (RWBuffer<float4>); more work us needed to determine
-  // the target ext type and its parameters based on the handle type
-  // attributes (not yet implemented)
-  llvm::FixedVectorType *ElemType =
-      llvm::FixedVectorType::get(llvm::Type::getFloatTy(Ctx), 4);
-  unsigned Flags[] = {/*IsWriteable*/ 1, /*IsROV*/ 0, /*IsSigned*/ 0};
-  return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags);
+  const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
+  switch (ResAttrs.ResourceClass) {
+  case llvm::dxil::ResourceClass::UAV:
+  case llvm::dxil::ResourceClass::SRV: {
+    // convert element type
+    QualType ContainedTy = ResType->getContainedType();
+    llvm::Type *ElemType = nullptr;
+    if (!ContainedTy.isNull())
+      ElemType = CGM.getTypes().ConvertType(ContainedTy);
+
+    if (ResAttrs.RawBuffer) {
+      // RawBuffer needs element type
+      if (ContainedTy.isNull())
+        return nullptr;
+      return llvm::TargetExtType::get(Ctx, "dx.RawBuffer", {ElemType},
+                                      {/*IsWriteable*/ ResAttrs.ResourceClass ==
+                                           llvm::dxil::ResourceClass::UAV,
+                                       /*IsROV*/ ResAttrs.IsROV});
+    }
+
+    // TypedBuffer needs element type
+    if (ContainedTy.isNull())
+      return nullptr;
+    return llvm::TargetExtType::get(
+        Ctx, "dx.TypedBuffer", {ElemType},
+        {/*IsWriteable*/ ResAttrs.ResourceClass ==
+             llvm::dxil::ResourceClass::UAV,
+         /*IsROV*/ ResAttrs.IsROV,
+         /*IsSigned*/ ContainedTy->isSignedIntegerType()});
----------------
hekota wrote:

This code is incomplete but I get what you mean. I've rearranged the code based on your suggestion.

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


More information about the cfe-commits mailing list