[clang] [HLSL][SPIRV] Add HLSL type translation for spirv. (PR #114273)

Steven Perron via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 06:48:28 PDT 2024


================
@@ -323,6 +327,83 @@ llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
   return nullptr;
 }
 
+llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM,
+                                                     const Type *Ty) const {
+  auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
+  if (!ResType)
+    return nullptr;
+
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+  const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
+  switch (ResAttrs.ResourceClass) {
+  case llvm::dxil::ResourceClass::UAV:
+  case llvm::dxil::ResourceClass::SRV: {
+    // TypedBuffer and RawBuffer both need element type
+    QualType ContainedTy = ResType->getContainedType();
+    if (ContainedTy.isNull())
+      return nullptr;
+
+    assert(!ResAttrs.RawBuffer &&
+           "Raw buffers handles are not implemented for SPIR-V yet");
+    assert(!ResAttrs.IsROV &&
+           "Rasterizer order views not implemented for SPIR-V yet");
+
+    // convert element type
+    llvm::Type *ElemType = CGM.getTypes().ConvertType(ContainedTy);
+    return getSPIRVImageTypeFromHLSLResource(ResAttrs, ElemType, Ctx);
+  }
+  case llvm::dxil::ResourceClass::CBuffer:
+    llvm_unreachable("CBuffer handles are not implemented for SPIR-V yet");
+    break;
+  case llvm::dxil::ResourceClass::Sampler:
+    return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
+  }
+  return nullptr;
+}
+
+llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
+    const HLSLAttributedResourceType::Attributes &attributes,
+    llvm::Type *ElementType, llvm::LLVMContext &Ctx) const {
+
+  if (ElementType->isVectorTy()) {
+    ElementType = ElementType->getScalarType();
+  }
+
+  if (!ElementType->isIntegerTy() && !ElementType->isFloatingPointTy()) {
+    // TODO: Should there be an error message?
+    ElementType->dump();
+    assert(false && "Bad element type");
+    return nullptr;
+  }
----------------
s-perron wrote:

I should have fixed this up. I had extra code like the dump for debugging. I'll change it to an assert with a clearer message. I don't want to put change sema at this time because the HLSL spec is still unclear to me. I don't want to put a restriction on HLSL.

I want an assert to let us know when we are trying to generate something invalid. We may need to do more messaging of the type as we do for vectors. For example, if the type is bool, we can change it to an int, but we will have to do a conversion when reading and writing. 

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


More information about the cfe-commits mailing list