[Mlir-commits] [mlir] Folding extract_strided_metadata input into reinterpret_cast on constant layout (PR #134845)

Mehdi Amini llvmlistbot at llvm.org
Tue Apr 8 08:03:03 PDT 2025


================
@@ -1948,6 +1948,27 @@ OpFoldResult ReinterpretCastOp::fold(FoldAdaptor /*operands*/) {
     if (auto prev = src.getDefiningOp<CastOp>())
       return prev.getSource();
 
+    // reinterpret_cast(extract_strided_metadata(x)) -> reinterpret_cast(x).
+    //
+    // We can always fold the input of a extract_strided_metadata operator
+    // to the input of a reinterpret_cast operator, because they point to
+    // the same memory. Note that the reinterpret_cast does not use the
+    // layout of its input memref, only its base memory pointer which is
+    // the same as the base pointer returned by the extract_strided_metadata
+    // operator and the base pointer of the extract_strided_metadata memref
+    // input. This folding is only profitable when the reinterpret_cast
----------------
joker-eph wrote:

I don't follow the explanation:

> In the case of constant sizes/sizes/offsets, the values are used directly as constants in the reinterpret_cast ops, and the sizes/offsets/strides coming from the extract_strided_metadata are not used and hence dead code elimination can get rid of the extract_strided_metadata operation.

This seems like a question whether the other components of the extract_strided_metadata are used independently somewhere in the IR? The "is constant" is just a proxy to this question isn't it?

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


More information about the Mlir-commits mailing list