[Mlir-commits] [mlir] [mlir][linalg] Add check for 'affine.for' map bound results (PR #127105)

Ayokunle Amodu llvmlistbot at llvm.org
Thu Feb 13 11:12:13 PST 2025


https://github.com/ayokunle321 updated https://github.com/llvm/llvm-project/pull/127105

>From d320635b846d1929ff67fd4f3ec846953bfaa69a Mon Sep 17 00:00:00 2001
From: Ayokunle Amodu <121697771+ayokunle321 at users.noreply.github.com>
Date: Thu, 13 Feb 2025 11:23:53 -0700
Subject: [PATCH 1/5] added check for affine.for bounds

---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp |  6 ++++++
 mlir/test/Dialect/Affine/invalid.mlir    | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 147f5dd7a24b6..06e26e887b050 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -1903,6 +1903,12 @@ LogicalResult AffineForOp::verifyRegions() {
                                              getUpperBoundMap().getNumDims())))
       return failure();
 
+  if (getLowerBoundMap().getNumResults() < 1)
+    return emitOpError("expected lower bound map to have at least one result");
+
+  if (getUpperBoundMap().getNumResults() < 1)
+    return emitOpError("expected upper bound map to have at least one result");
+    
   unsigned opNumResults = getNumResults();
   if (opNumResults == 0)
     return success();
diff --git a/mlir/test/Dialect/Affine/invalid.mlir b/mlir/test/Dialect/Affine/invalid.mlir
index 44e484b9ba598..5a3243cb074c1 100644
--- a/mlir/test/Dialect/Affine/invalid.mlir
+++ b/mlir/test/Dialect/Affine/invalid.mlir
@@ -523,3 +523,25 @@ func.func @dynamic_dimension_index() {
   }) : () -> ()
   return
 }
+
+// -----
+
+#map = affine_map<() -> ()>
+#map1 = affine_map<() -> (1)>
+func.func @no_lower_bound() {
+  // expected-error at +1 {{'affine.for' op expected lower bound map to have at least one result}}
+  affine.for %i = max #map() to min #map1() {
+  }
+  return
+}
+
+// -----
+
+#map = affine_map<() -> ()>
+#map1 = affine_map<() -> (1)>
+func.func @no_upper_bound() {
+  // expected-error at +1 {{'affine.for' op expected upper bound map to have at least one result}}
+  affine.for %i = max #map1() to min #map() {
+  }
+  return
+}

>From fdf4bfa67c29298c16299afb11e0e840e399b049 Mon Sep 17 00:00:00 2001
From: Ayokunle Amodu <121697771+ayokunle321 at users.noreply.github.com>
Date: Thu, 13 Feb 2025 11:52:48 -0700
Subject: [PATCH 2/5] added comments

---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 06e26e887b050..f6161b3547f21 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -1902,13 +1902,14 @@ LogicalResult AffineForOp::verifyRegions() {
     if (failed(verifyDimAndSymbolIdentifiers(*this, getUpperBoundOperands(),
                                              getUpperBoundMap().getNumDims())))
       return failure();
-
+  // Verify that the bound maps produce at least one result.
+  /// Lower bound.
   if (getLowerBoundMap().getNumResults() < 1)
     return emitOpError("expected lower bound map to have at least one result");
-
+  /// Upper bound.
   if (getUpperBoundMap().getNumResults() < 1)
     return emitOpError("expected upper bound map to have at least one result");
-    
+
   unsigned opNumResults = getNumResults();
   if (opNumResults == 0)
     return success();

>From 440a0c31c43409a96d11f4d7e3bb80fabb0f1227 Mon Sep 17 00:00:00 2001
From: Ayokunle Amodu <121697771+ayokunle321 at users.noreply.github.com>
Date: Thu, 13 Feb 2025 11:59:08 -0700
Subject: [PATCH 3/5] fixed code style

---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index f6161b3547f21..b664e19e6c279 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -1902,6 +1902,7 @@ LogicalResult AffineForOp::verifyRegions() {
     if (failed(verifyDimAndSymbolIdentifiers(*this, getUpperBoundOperands(),
                                              getUpperBoundMap().getNumDims())))
       return failure();
+      
   // Verify that the bound maps produce at least one result.
   /// Lower bound.
   if (getLowerBoundMap().getNumResults() < 1)

>From 9db6d896b9ff3ce76a0de1732376a6d2a59e314f Mon Sep 17 00:00:00 2001
From: Ayokunle Amodu <121697771+ayokunle321 at users.noreply.github.com>
Date: Thu, 13 Feb 2025 12:03:34 -0700
Subject: [PATCH 4/5] fix style

---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index b664e19e6c279..64e593f720973 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -1901,8 +1901,7 @@ LogicalResult AffineForOp::verifyRegions() {
   if (getUpperBoundMap().getNumInputs() > 0)
     if (failed(verifyDimAndSymbolIdentifiers(*this, getUpperBoundOperands(),
                                              getUpperBoundMap().getNumDims())))
-      return failure();
-      
+      return failure();    
   // Verify that the bound maps produce at least one result.
   /// Lower bound.
   if (getLowerBoundMap().getNumResults() < 1)

>From 83d3b14306ac1f64ca1251a3d8b91fc6d7a3fd60 Mon Sep 17 00:00:00 2001
From: Ayokunle Amodu <121697771+ayokunle321 at users.noreply.github.com>
Date: Thu, 13 Feb 2025 12:11:59 -0700
Subject: [PATCH 5/5] format code properly

---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 64e593f720973..f6161b3547f21 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -1901,7 +1901,7 @@ LogicalResult AffineForOp::verifyRegions() {
   if (getUpperBoundMap().getNumInputs() > 0)
     if (failed(verifyDimAndSymbolIdentifiers(*this, getUpperBoundOperands(),
                                              getUpperBoundMap().getNumDims())))
-      return failure();    
+      return failure();
   // Verify that the bound maps produce at least one result.
   /// Lower bound.
   if (getLowerBoundMap().getNumResults() < 1)



More information about the Mlir-commits mailing list