[Mlir-commits] [mlir] [mlir][memref][spirv] Add SPIR-V Image Lowering (PR #150978)
Jack Frankland
llvmlistbot at llvm.org
Tue Jul 29 06:33:06 PDT 2025
================
@@ -661,6 +682,79 @@ LoadOpPattern::matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
return success();
}
+LogicalResult
+ImageLoadOpPattern::matchAndRewrite(memref::LoadOp loadOp, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const {
+ auto memrefType = cast<MemRefType>(loadOp.getMemref().getType());
+ if (memrefType.getMemorySpace() !=
+ spirv::StorageClassAttr::get(rewriter.getContext(),
+ spirv::StorageClass::Image))
+ return failure();
+
+ auto loadPtr = adaptor.getMemref();
+ auto memoryRequirements = calculateMemoryRequirements(loadPtr, loadOp);
+ if (failed(memoryRequirements))
+ return rewriter.notifyMatchFailure(
+ loadOp, "failed to determine memory requirements");
+
+ const auto [memoryAccess, alignment] = *memoryRequirements;
+
+ if (!loadOp.getMemRefType().hasRank())
+ return rewriter.notifyMatchFailure(
+ loadOp, "cannot lower unranked memrefs to SPIR-V images");
+
+ // We currently only support lowering of scalar memref elements to texels in
+ // the R[16|32][f|i|ui] formats. Future work will enable lowering of vector
+ // elements to texels in richer formats.
+ if (!loadOp.getMemRefType().getElementType().isIntOrFloat())
+ return rewriter.notifyMatchFailure(
+ loadOp, "cannot lower memrefs who's element type is not int or float "
+ "to SPIR-V images");
+
+ // We currently only support sampled images since OpImageFetch does not work
+ // for plain images and the OpImageRead instruction needs to be materialized
+ // instead or texels need to be accessed via atomics through a texel pointer.
+ // Future work will generalize support to plain images.
+ if (auto convertedPointeeType = cast<spirv::PointerType>(
----------------
FranklandJack wrote:
Yeah I agree, I was just trying to be clever here and reduce the scope of the `convertedPointeeType` variable to the `if` block, but I agree it is at the cost of reduced readability so happy to hoist it.
https://github.com/llvm/llvm-project/pull/150978
More information about the Mlir-commits
mailing list