[Mlir-commits] [mlir] [MLIR][Vector] Implement transferXXPermutationLowering as MaskableOpRewritePattern (PR #91987)

Hugo Trachino llvmlistbot at llvm.org
Fri May 17 02:41:16 PDT 2024


================
@@ -285,10 +303,14 @@ struct TransferWriteNonPermutationLowering
       newInBoundsValues.push_back(op.isDimInBounds(i));
     }
     ArrayAttr newInBoundsAttr = rewriter.getBoolArrayAttr(newInBoundsValues);
-    rewriter.replaceOpWithNewOp<vector::TransferWriteOp>(
-        op, newVec, op.getSource(), op.getIndices(), AffineMapAttr::get(newMap),
-        newMask, newInBoundsAttr);
-    return success();
+    auto newWrite = rewriter.create<vector::TransferWriteOp>(
+        op.getLoc(), newVec, op.getSource(), op.getIndices(),
+        AffineMapAttr::get(newMap), newMask, newInBoundsAttr);
+    if (newWrite.hasPureTensorSemantics())
+      return newWrite.getResult();
+    // In memref case, MaskableOpRewritePattern cannot replaceOp with result.
----------------
nujaa wrote:

`permutation_with_mask_xfer_write_fixed_width` indeed does not have `PureTensorSemantics` hence this check.
The `MaskableOpRewritePattern` interface replaces uses of the original op with a value with `RewriterBase::replaceOp(oldOp, newValue)`. Which requires the same amount of newValues than return values in oldOp. `newValue` is normally the return value of the newly generated op.
 In this case, both `oldOp` and the new Op do not have a return value. Hence, there is no `newValue` to replace the oldOp with.
In the case the op does not have a result, the solution might be to have `matchAndRewriteMaskableOp` return `Value()` to differentiate it from `failure()` and update MaskableOpRewritePattern::matchAndRewrite so it handles ops with no result.

e.g. : 
`mlir/include/mlir/Dialect/Vector/Utils/VectorUtils.h:155`
```
FailureOr<Value> newOp =
    matchAndRewriteMaskableOp(sourceOp, maskOp, rewriter);
if (failed(newOp))
  return failure();
////////// To add
// We could also check newOp == Value() but not sure it is necessary.
if(rootOp->getNumResults() == 0)
  eraseOp(rootOp)
else
///////////////////////
rewriter.replaceOp(rootOp, *newOp);
return success();
```

I can create such MR if you think it is a good addition to your feature. 😉 



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


More information about the Mlir-commits mailing list