[Mlir-commits] [mlir] [MLIR][Affine] Fix isTilingValid missing anti-dependence when lb == ub < 0 (PR #192858)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Apr 29 08:28:14 PDT 2026
https://github.com/Mountagha updated https://github.com/llvm/llvm-project/pull/192858
>From 905d1eb41867117cf612938b781d3e0b650c4101 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/5] 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 20d6c8ece358e3ab692626bbf9f1730fb7a3df74 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/5] 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
+ }
+}
>From f38b1f224a7f3e5b82ba695189a39b6858ac5abe Mon Sep 17 00:00:00 2001
From: mountagha <muntaghaba at gmail.com>
Date: Sun, 19 Apr 2026 16:51:47 +0000
Subject: [PATCH 3/5] unrelated to issue change.
---
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 06a58b8c1fc7f..d420a7ebbf3b6 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 of a location in a file.
+/// Structure definition a location in a file.
struct Location {
std::shared_ptr<std::string> file; ///< filename.
int line; ///< line number.
>From 60faedaf719a779f4b5221d4e711d22d1bcc46aa Mon Sep 17 00:00:00 2001
From: mountagha <muntaghaba at gmail.com>
Date: Wed, 22 Apr 2026 14:45:03 +0000
Subject: [PATCH 4/5] moved test into existing test file.
---
.../Dialect/Affine/loop-tile-anti-dep.mlir | 23 -------------------
.../Dialect/Affine/loop-tiling-validity.mlir | 19 +++++++++++++++
2 files changed, 19 insertions(+), 23 deletions(-)
delete mode 100644 mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
diff --git a/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir b/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
deleted file mode 100644
index b285853f1011f..0000000000000
--- a/mlir/test/Dialect/Affine/loop-tile-anti-dep.mlir
+++ /dev/null
@@ -1,23 +0,0 @@
-// 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
- }
-}
diff --git a/mlir/test/Dialect/Affine/loop-tiling-validity.mlir b/mlir/test/Dialect/Affine/loop-tiling-validity.mlir
index cf2e12613ab92..23e4f3ab411b3 100644
--- a/mlir/test/Dialect/Affine/loop-tiling-validity.mlir
+++ b/mlir/test/Dialect/Affine/loop-tiling-validity.mlir
@@ -1,4 +1,5 @@
// RUN: mlir-opt %s -split-input-file -affine-loop-tile="tile-size=32" -verify-diagnostics | FileCheck %s
+// RUN: mlir-opt %s -split-input-file -affine-loop-tile="tile-sizes=16,16" | FileCheck %s --check-prefix=TILE16
// -----
@@ -47,3 +48,21 @@ func.func @illegal_loop_with_diag_dependence() {
return
}
+
+// -----
+
+// Verify that tiling is not applied when anti-dependences would be violated.
+
+// TILE16-LABEL: func @anti_dep
+// TILE16: affine.for %{{.*}} = 0 to 1023 {
+// TILE16-NEXT: affine.for %{{.*}} = 1 to 1024 {
+// TILE16-NOT: affine.for %{{.*}} = 0 to 1023 step 16
+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
+}
>From d1a4d4d4196920c37aadd1a42e52b51ba0094222 Mon Sep 17 00:00:00 2001
From: mountagha <muntaghaba at gmail.com>
Date: Thu, 23 Apr 2026 14:07:18 +0000
Subject: [PATCH 5/5] [Affine] use <= for inclusive dep bounds.
---
mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp b/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
index b31a8b7d9dbdb..a9993aab0d103 100644
--- a/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
+++ b/mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp
@@ -557,6 +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) {
LDBG() << "Dependence component lb = " << Twine(*depComp.lb)
<< " ub = " << Twine(*depComp.ub)
More information about the Mlir-commits
mailing list