[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:03 PDT 2026
https://github.com/Mountagha created https://github.com/llvm/llvm-project/pull/192858
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" />
>From b24559cf5644ccbb9dc03ebe675b5bc3ba27721a Mon Sep 17 00:00:00 2001
From: Mountagha <muntaghaba at gmail.com>
Date: Mon, 17 Nov 2025 23:58:40 -0500
Subject: [PATCH 1/2] playing with mlir toy.
---
mlir/examples/toy/Ch1/include/toy/Lexer.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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.
>From 44f8dae6f9039c54e2a16756b098e53d5521143a Mon Sep 17 00:00:00 2001
From: mountagha <muntaghaba at gmail.com>
Date: Sun, 19 Apr 2026 05:34:04 +0000
Subject: [PATCH 2/2] missing anti-dependance when lb == ub < 0
---
.../Dialect/Affine/Analysis/LoopAnalysis.cpp | 2 +-
.../Dialect/Affine/loop-tile-anti-dep.mlir | 23 +++++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
create mode 100644 mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
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
+ }
+}
More information about the Mlir-commits
mailing list