[clang] [HLSL][SPIRV] Add -fspv-use-unknown-image-format option (PR #155664)
Nathan Gauër via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 28 11:15:06 PDT 2025
================
@@ -517,14 +517,65 @@ llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
return nullptr;
}
+static unsigned
+getImageFormat(const LangOptions &LangOpts,
+ const HLSLAttributedResourceType::Attributes &attributes,
+ llvm::Type *SampledType, QualType Ty, unsigned NumChannels) {
+ // For images with `Sampled` operand equal to 2, there are restrictions on
+ // using the Unknown image format. To avoid these restrictions in common
+ // cases, we guess an image format for them based on the sampled type and the
+ // number of channels. This is intended to match the behaviour of DXC.
+ if (LangOpts.HLSLSpvUseUnknownImageFormat ||
+ attributes.ResourceClass != llvm::dxil::ResourceClass::UAV) {
+ return 0; // Unknown
+ }
+
+ if (SampledType->isIntegerTy(32)) {
+ if (Ty->isSignedIntegerType()) {
+ if (NumChannels == 1)
+ return 24; // R32i
+ if (NumChannels == 2)
+ return 25; // Rg32i
+ if (NumChannels == 4)
+ return 21; // Rgba32i
+ } else {
+ if (NumChannels == 1)
+ return 33; // R32ui
+ if (NumChannels == 2)
+ return 35; // Rg32ui
+ if (NumChannels == 4)
+ return 30; // Rgba32ui
+ }
+ } else if (SampledType->isIntegerTy(64)) {
+ if (NumChannels == 1) {
+ if (Ty->isSignedIntegerType()) {
+ return 41; // R64i
+ }
+ return 40; // R64ui
+ }
+ } else if (SampledType->isFloatTy()) {
+ if (NumChannels == 1)
+ return 3; // R32f
+ if (NumChannels == 2)
+ return 6; // Rg32f
+ if (NumChannels == 4)
+ return 1; // Rgba32f
+ }
+
+ return 0; // Unknown
+}
+
llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
- const HLSLAttributedResourceType::Attributes &attributes, QualType Ty,
- CodeGenModule &CGM) const {
+ const HLSLAttributedResourceType::Attributes &attributes,
+ QualType OriginalTy, CodeGenModule &CGM) const {
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
- Ty = Ty->getCanonicalTypeUnqualified();
- if (const VectorType *V = dyn_cast<VectorType>(Ty))
+ unsigned NumChannels = 1;
+ QualType Ty = OriginalTy->getCanonicalTypeUnqualified();
+ if (const VectorType *V = dyn_cast<VectorType>(Ty)) {
+ NumChannels = V->getNumElements();
----------------
Keenuts wrote:
The param name could remain Ty no?
https://github.com/llvm/llvm-project/pull/155664
More information about the cfe-commits
mailing list