[Mlir-commits] [lldb] [llvm] [mlir] [mlir] Introduce replaceWithZeroTripCheck in LoopLikeOpInterface (PR #80331)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 1 15:48:13 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Jerry Wu (pzread)

<details>
<summary>Changes</summary>

Adds a new method `replaceWithZeroTripCheck` to `LoopLikeOpInterface`, to create zero-trip-check around the loop

The purpose is to let loop ops (e.g. `scf.while`, `scf.for`) implement their own transformations to add zero-trip-check. The zero-trip-check creates a guard (e.g. `scf.if`) around the loop and the condition will be true only if the loop body will run at least once.  An example usage is to hoist resource-intense loop invariants into this guard region so they will only run once but also not run when the loop body won't run at all.

The implementation of `scf.while` can be found in the follow-up change: #<!-- -->80349

---
Full diff: https://github.com/llvm/llvm-project/pull/80331.diff


1 Files Affected:

- (modified) mlir/include/mlir/Interfaces/LoopLikeInterface.td (+22) 


``````````diff
diff --git a/mlir/include/mlir/Interfaces/LoopLikeInterface.td b/mlir/include/mlir/Interfaces/LoopLikeInterface.td
index e2ac85a3f7725..77409cb3a8274 100644
--- a/mlir/include/mlir/Interfaces/LoopLikeInterface.td
+++ b/mlir/include/mlir/Interfaces/LoopLikeInterface.td
@@ -220,6 +220,28 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
       /*defaultImplementation=*/[{
         return ::mlir::failure();
       }]
+    >,
+    InterfaceMethod<[{
+        Add a zero-trip-check around the loop to check if the loop body is ever
+        run and return the new loop inside the check. The loop body is moved
+        over to the new loop. Returns "failure" if the loop doesn't support
+        this transformation.
+
+        After the transformation, the ops inserted to the parent region of the
+        loop are guaranteed to be run only if the loop body runs at least one
+        iteration.
+
+        Note: Ops in the loop body might be rearranged because of loop rotating
+        to maintain the semantic. Terminators might be removed/added during this
+        transformation.
+      }],
+      /*retTy=*/"::mlir::FailureOr<::mlir::LoopLikeOpInterface>",
+      /*methodName=*/"replaceWithZeroTripCheck",
+      /*args=*/(ins "::mlir::RewriterBase &":$rewriter),
+      /*methodBody=*/"",
+      /*defaultImplementation=*/[{
+        return ::mlir::failure();
+      }]
     >
   ];
 

``````````

</details>


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


More information about the Mlir-commits mailing list