[Mlir-commits] [mlir] Fix/mlir affine loop tile anti dependance (PR #192858)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Apr 19 09:50:47 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Mountagha (Mountagha)

<details>
<summary>Changes</summary>

Issue: #<!-- -->191147 

[mlir][affine] Fix isTilingValid missing anti-dependence when lb == ub < 0

The legality check in `isTilingValid` used the condition:
  `*depComp.lb < *depComp.ub && *depComp.ub < 0`

This incorrectly skipped dependence components where `lb == ub == -1`
(e.g. a distance of exactly -1), because `lb < ub` evaluates to false
when they are equal. As a result, loops with such anti-dependences were
illegally tiled as shown below when executed with the code provided in the issue


<img width="1315" height="321" alt="image" src="https://github.com/user-attachments/assets/22f33083-40b4-4d82-a16d-2bc5849822da" />



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


3 Files Affected:

- (modified) mlir/examples/toy/Ch1/include/toy/Lexer.h (+1-1) 
- (modified) mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp (+1-1) 
- (added) mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir (+23) 


``````````diff
diff --git a/mlir/examples/toy/Ch1/include/toy/Lexer.h b/mlir/examples/toy/Ch1/include/toy/Lexer.h
index d420a7ebbf3b6..06a58b8c1fc7f 100644
--- a/mlir/examples/toy/Ch1/include/toy/Lexer.h
+++ b/mlir/examples/toy/Ch1/include/toy/Lexer.h
@@ -21,7 +21,7 @@
 
 namespace toy {
 
-/// Structure definition a location in a file.
+/// Structure definition of a location in a file.
 struct Location {
   std::shared_ptr<std::string> file; ///< filename.
   int line;                          ///< line number.
diff --git a/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp b/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
index 166d39e88d41e..b31a8b7d9dbdb 100644
--- a/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
+++ b/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
@@ -557,7 +557,7 @@ bool mlir::affine::isTilingValid(ArrayRef<AffineForOp> loops) {
                               OpPrintingFlags().skipRegions());
         for (const DependenceComponent &depComp : depComps) {
           if (depComp.lb.has_value() && depComp.ub.has_value() &&
-              *depComp.lb < *depComp.ub && *depComp.ub < 0) {
+              *depComp.ub < 0) {
             LDBG() << "Dependence component lb = " << Twine(*depComp.lb)
                    << " ub = " << Twine(*depComp.ub)
                    << " is negative  at depth: " << Twine(d)
diff --git a/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir b/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
new file mode 100644
index 0000000000000..b285853f1011f
--- /dev/null
+++ b/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
@@ -0,0 +1,23 @@
+// RUN: mlir-opt --affine-loop-tile="tile-sizes=16,16" %s | FileCheck %s
+
+// Verify that tiling is NOT applied when there is an anti-dependence
+// (lb == ub == -1 in the dependence component) that would be violated.
+// Before the fix, the condition `*depComp.lb < *depComp.ub && *depComp.ub < 0`
+// incorrectly skipped the lb==ub==-1 case, allowing illegal tiling.
+
+// CHECK-LABEL: func.func @anti_dep
+// CHECK:         affine.for %{{.*}} = 0 to 1023 {
+// CHECK-NEXT:      affine.for %{{.*}} = 1 to 1024 {
+// CHECK-NOT:       affine.for %{{.*}} = 0 to 1023 step 16
+
+module {
+  func.func @anti_dep(%arr: memref<1024x1024xi32>) {
+    affine.for %i = 0 to 1023 {
+      affine.for %j = 1 to 1024 {
+        %val = affine.load %arr[%i + 1, %j - 1] : memref<1024x1024xi32>
+        affine.store %val, %arr[%i, %j] : memref<1024x1024xi32>
+      }
+    }
+    return
+  }
+}

``````````

</details>


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


More information about the Mlir-commits mailing list