[Mlir-commits] [mlir] [mlir][vector] Teach `TransferOptimization` to forward masked stores (PR #87794)

Andrzej WarzyƄski llvmlistbot at llvm.org
Wed May 15 06:55:54 PDT 2024


================
@@ -170,12 +170,43 @@ AffineMap mlir::vector::getTransferMinorIdentityMap(ShapedType shapedType,
       shapedType.getContext());
 }
 
+/// Returns true if the value written by `defWrite` could be the same as the
+/// value read by `read`. Note: True is 'could be' not 'definitely' (as this
+/// simply looks at the masks and the value written). For a definite answer use
+/// `checkSameValueRAW()` -- which calls this function.
+static bool couldBeSameValueWithMasking(vector::TransferWriteOp defWrite,
+                                        vector::TransferReadOp read) {
+  if (!defWrite.getMask() && !read.getMask()) {
+    // Success: No masks (values could be the same).
+    return true;
+  }
+  // Check for constant splats. These will be the same value if the read is
+  // masked (and padded with the splat value), and the write is unmasked or has
+  // the same mask.
+  bool couldBeSameSplatValue =
+      read.getMask() &&
+      (!defWrite.getMask() || defWrite.getMask() == read.getMask());
----------------
banach-space wrote:

Thanks! I guess this  boils down to the fact that "read" takes a padding value that could "salvage" this opimization? For example, this would be rejected, right?
```
Memory = [1, 1, 1, 1]
Write [0, 0, 0, 0]
Memory = [0, 0, 0, 0]
Read  mask = [true, true, false, false] pad = 1  
Gives [0, 0, 1, 1]
```

This was rather not obvious to me - could you document? Your example was very helpful. Also, could you add a test for this case  - I couldn't find one (write with mask -> read without mask).

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


More information about the Mlir-commits mailing list