[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:53:51 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;
+  }
+
+  // For HLSL types, the depth is always 2.
+  SmallVector<unsigned, 6> IntParams = {0, 2, 0, 0, 1, 0};
----------------
s-perron wrote:

Part of the first implementation was to initialize value. Then I decided to write each value separately I did not update the initialization. Thanks for catching.

The comment is a good idea.

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


More information about the cfe-commits mailing list