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

Hugo Trachino llvmlistbot at llvm.org
Mon May 20 03:53:44 PDT 2024


================
@@ -157,7 +157,10 @@ struct MaskableOpRewritePattern : OpRewritePattern<SourceOp> {
     if (failed(newOp))
       return failure();
 
-    rewriter.replaceOp(rootOp, *newOp);
+    if (rootOp->getNumResults() == 0 || *newOp == Value())
+      rewriter.eraseOp(rootOp);
+    else
+      rewriter.replaceOp(rootOp, *newOp);
----------------
nujaa wrote:

Sorry for late answer, I have been thinking about it while implementing and did not come up with a solution I liked. With the weekend fresh mind, Here is my point returning `Value()`  means it did NOT fail. aka code updates happened but no value to give e.g. memref case.
if we split cases : 
```
if *newOp == Value() 
|  if NumResult == 0 // simple case
|  |  rewriter.eraseOp(rootOp); 
|  else 
|  |  // We have to replace something with a value with Value() so there might be uses of rootOp in the rest 
|  |  // of the program if we try to erase it. So I suggest to raise an error.
|  |  raise Error(); 
if pattern returns a value:
|  if NumResult == 1 // simple case
|  | rewriter.replaceOp(rootOp, *newOp);
|  else // We created ops with a value which should replace something without a value. We can't use it in the program. It will most likely be DCE-ed.
|  |  rewriter.eraseOp(rootOp); 
```
Which can then be reduced to 
```
if (failed(newOp))
  return failure();
if NumResult == 0
  rewriter.eraseOp(rootOp); 
else 
  assert(*newOp != Value() && "Can't replace an op use with Value()");
  rewriter.replaceOp(rootOp, *newOp);
return success()
```
As an additionnal point, technically, `matchAndRewriteMaskableOp` could return a ValueRange as replaceOp takes a ValueRange as input. replaceOp will assert `rootOp->getNumResults() != newOp.size()`. And will allow to handle cases where ops have multiple results. But I suggest as part of a separate patch.

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


More information about the Mlir-commits mailing list