Skip to content

Fix: bail out when divisor is zero #133518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged

Conversation

hiraditya
Copy link
Collaborator

Fixes: #131279

@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-mlir-shape

Author: AdityaK (hiraditya)

Changes

Fixes: #131279


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

2 Files Affected:

  • (modified) mlir/lib/Dialect/Shape/IR/Shape.cpp (+1-1)
  • (added) mlir/test/Dialect/Shape/fold-div.mlir (+13)
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index 2200af0f67a86..10ba808cd26c2 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -1124,7 +1124,7 @@ OpFoldResult DivOp::fold(FoldAdaptor adaptor) {
   if (!lhs)
     return nullptr;
   auto rhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getRhs());
-  if (!rhs)
+  if (!rhs || rhs.getValue().isZero())
     return nullptr;
 
   // Division in APInt does not follow floor(lhs, rhs) when the result is
diff --git a/mlir/test/Dialect/Shape/fold-div.mlir b/mlir/test/Dialect/Shape/fold-div.mlir
new file mode 100644
index 0000000000000..9deac08bd85cc
--- /dev/null
+++ b/mlir/test/Dialect/Shape/fold-div.mlir
@@ -0,0 +1,13 @@
+// Bug: #131279
+// RUN: mlir-opt --test-scf-pipelining %s | FileCheck %s
+// CHECK: fold_div_index_neg_rhs
+// CHECK-NEXT: %c0 = arith.constant 0 : index
+// CHECK-NEXT: %0 = shape.div %c0, %c0 : index, index -> index
+// CHECK-NEXT: return %0 : index
+module {
+  func.func @fold_div_index_neg_rhs() -> index {
+    %c0 = arith.constant 0 : index
+    %0 = shape.div %c0, %c0 : index, index -> index
+    return %0 : index
+  }
+}

@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-mlir

Author: AdityaK (hiraditya)

Changes

Fixes: #131279


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

2 Files Affected:

  • (modified) mlir/lib/Dialect/Shape/IR/Shape.cpp (+1-1)
  • (added) mlir/test/Dialect/Shape/fold-div.mlir (+13)
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index 2200af0f67a86..10ba808cd26c2 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -1124,7 +1124,7 @@ OpFoldResult DivOp::fold(FoldAdaptor adaptor) {
   if (!lhs)
     return nullptr;
   auto rhs = llvm::dyn_cast_if_present<IntegerAttr>(adaptor.getRhs());
-  if (!rhs)
+  if (!rhs || rhs.getValue().isZero())
     return nullptr;
 
   // Division in APInt does not follow floor(lhs, rhs) when the result is
diff --git a/mlir/test/Dialect/Shape/fold-div.mlir b/mlir/test/Dialect/Shape/fold-div.mlir
new file mode 100644
index 0000000000000..9deac08bd85cc
--- /dev/null
+++ b/mlir/test/Dialect/Shape/fold-div.mlir
@@ -0,0 +1,13 @@
+// Bug: #131279
+// RUN: mlir-opt --test-scf-pipelining %s | FileCheck %s
+// CHECK: fold_div_index_neg_rhs
+// CHECK-NEXT: %c0 = arith.constant 0 : index
+// CHECK-NEXT: %0 = shape.div %c0, %c0 : index, index -> index
+// CHECK-NEXT: return %0 : index
+module {
+  func.func @fold_div_index_neg_rhs() -> index {
+    %c0 = arith.constant 0 : index
+    %0 = shape.div %c0, %c0 : index, index -> index
+    return %0 : index
+  }
+}

Copy link
Member

@jpienaar jpienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@hiraditya hiraditya merged commit 340f06a into llvm:main Apr 2, 2025
14 checks passed
@hiraditya hiraditya deleted the mlir_bug_131279 branch April 2, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MLIR] optimization LoopPipelining crashes
3 participants