[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:04:53 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
+    // layout is constant, because the extract_strided_metadata gets
+    // eliminated by dead code elimination. For non-constant folding we don’t
+    // get the extract_strided_metadata node eliminated and one of the LLVM
+    // tests regress in performance because the folding gets in the way of
+    // another optimization. For this reason the folding is only done on
+    // constant layout.
+    if (auto prev = src.getDefiningOp<ExtractStridedMetadataOp>()) {
+      if (isLayoutConstant()) {
+        return prev.getSource();
+      }
----------------
joker-eph wrote:

```suggestion
      if (isLayoutConstant())
        return prev.getSource();
```

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


More information about the Mlir-commits mailing list