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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Apr 8 06:26:49 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
----------------
ivangarcia44 wrote:

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 is the main motivation/use-case for this change.

When the sizes/offsets/strides are non-constant DCE does not get rid of it. I have confirmed this in various LLVM tests.

In addition, one of the tests regress when the folding is done on non-constant sizes/strides/offsets because the folding gets in the way of another transform. For this reason I added the isLayoutConstant guard. Since for the non-constant case DCE does not kick in, I thought of leaving this as future work since it is a low or zero gain task that could involve significant work. 

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


More information about the Mlir-commits mailing list