[Mlir-commits] [mlir] [mlir] Implement memory-space cast operand fusion into consumers (PR #159454)
Nicolas Vasilache
llvmlistbot at llvm.org
Fri Sep 19 03:54:37 PDT 2025
================
@@ -1645,6 +1722,33 @@ OpFoldResult MemorySpaceCastOp::fold(FoldAdaptor adaptor) {
return Value{};
}
+TypedValue<PtrLikeTypeInterface> MemorySpaceCastOp::getSourcePtr() {
+ return cast<TypedValue<PtrLikeTypeInterface>>(getSource());
+}
+
+TypedValue<PtrLikeTypeInterface> MemorySpaceCastOp::getTargetPtr() {
+ return cast<TypedValue<PtrLikeTypeInterface>>(getDest());
+}
+
+bool MemorySpaceCastOp::isValidMemorySpaceCast(PtrLikeTypeInterface tgt,
+ PtrLikeTypeInterface src) {
+ return isa<MemRefType>(tgt) &&
+ tgt.clonePtrWith(src.getMemorySpace(), std::nullopt) == src;
+}
+
+MemorySpaceCastOpInterface
+MemorySpaceCastOp::cloneMemorySpaceCastOp(OpBuilder &b, Type tgt, Value src) {
+ assert(isValidMemorySpaceCast(cast<PtrLikeTypeInterface>(tgt),
+ cast<PtrLikeTypeInterface>(src.getType())) &&
+ "invalid arguments");
+ return MemorySpaceCastOp::create(b, getLoc(), tgt, src);
+}
+
+bool MemorySpaceCastOp::isFusableMemorySpaceCast() {
+ // Only allow fusion when this is discarding information.
+ return getDest().getType().getMemorySpace() == nullptr;
----------------
nicolasvasilache wrote:
thanks, important check indeed given the LLVM LangRef definition.
Could we better doc (in the decl and around the failing test) that refers to the LLVM langref and the fact that a memrefcast may be a costly operation; the fuse pass does not take the responsibility of shifting the cost.
https://github.com/llvm/llvm-project/pull/159454
More information about the Mlir-commits
mailing list