[Mlir-commits] [mlir] e485a5d - [MLIR][SCFToOpenMP] Fix crash when scf.parallel uses non-LLVM-compatible reduction type (#188559)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 26 09:20:30 PDT 2026


Author: Mehdi Amini
Date: 2026-03-26T17:20:25+01:00
New Revision: e485a5d36e00e69e1b721b81a41be300662729c5

URL: https://github.com/llvm/llvm-project/commit/e485a5d36e00e69e1b721b81a41be300662729c5
DIFF: https://github.com/llvm/llvm-project/commit/e485a5d36e00e69e1b721b81a41be300662729c5.diff

LOG: [MLIR][SCFToOpenMP] Fix crash when scf.parallel uses non-LLVM-compatible reduction type (#188559)

Replace the assert in ParallelOpLowering::matchAndRewrite that fires
when an scf.parallel reduction init value has a type incompatible with
LLVM (e.g. index) with an early notifyMatchFailure call, so the pass
fails gracefully instead of crashing.

Add a regression test that verifies the pass emits an error instead of
asserting.

Fixes #61342

Assisted-by: Claude Code

Added: 
    mlir/test/Conversion/SCFToOpenMP/non-llvm-reduction-type.mlir

Modified: 
    mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp b/mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp
index 48845734e9547..00945b957e188 100644
--- a/mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp
+++ b/mlir/lib/Conversion/SCFToOpenMP/SCFToOpenMP.cpp
@@ -412,6 +412,16 @@ struct ParallelOpLowering : public OpRewritePattern<scf::ParallelOp> {
 
   LogicalResult matchAndRewrite(scf::ParallelOp parallelOp,
                                 PatternRewriter &rewriter) const override {
+    // Bail out early if any reduction init value has a type that is not
+    // compatible with LLVM (e.g. index), since we cannot allocate a reduction
+    // variable for such types.
+    for (Value init : parallelOp.getInitVals()) {
+      if (!LLVM::isCompatibleType(init.getType()) &&
+          !isa<LLVM::PointerElementTypeInterface>(init.getType()))
+        return rewriter.notifyMatchFailure(
+            parallelOp, "reduction init type is not an LLVM-compatible type");
+    }
+
     // Declare reductions.
     // TODO: consider checking it here is already a compatible reduction
     // declaration and use it instead of redeclaring.
@@ -437,10 +447,6 @@ struct ParallelOpLowering : public OpRewritePattern<scf::ParallelOp> {
     reductionVariables.reserve(parallelOp.getNumReductions());
     auto ptrType = LLVM::LLVMPointerType::get(parallelOp.getContext());
     for (Value init : parallelOp.getInitVals()) {
-      assert((LLVM::isCompatibleType(init.getType()) ||
-              isa<LLVM::PointerElementTypeInterface>(init.getType())) &&
-             "cannot create a reduction variable if the type is not an LLVM "
-             "pointer element");
       Value storage = LLVM::AllocaOp::create(rewriter, loc, ptrType,
                                              init.getType(), one, 0);
       LLVM::StoreOp::create(rewriter, loc, init, storage);

diff  --git a/mlir/test/Conversion/SCFToOpenMP/non-llvm-reduction-type.mlir b/mlir/test/Conversion/SCFToOpenMP/non-llvm-reduction-type.mlir
new file mode 100644
index 0000000000000..46417c46af46d
--- /dev/null
+++ b/mlir/test/Conversion/SCFToOpenMP/non-llvm-reduction-type.mlir
@@ -0,0 +1,28 @@
+// RUN: not mlir-opt -convert-scf-to-openmp %s 2>&1 | FileCheck %s
+
+// Regression test for https://github.com/llvm/llvm-project/issues/61342
+// Verify that -convert-scf-to-openmp does not crash when an scf.parallel
+// reduction uses a type that is not an LLVM-compatible type (e.g. index).
+// Instead of asserting, the pass must fail gracefully.
+
+// CHECK: unconverted operation found
+
+func.func @no_crash_index_reduction(%A: index, %B: index) -> (index, index) {
+  %c1 = arith.constant 1 : index
+  %c2 = arith.constant 2 : index
+  %c3 = arith.constant 3 : index
+  %c6 = arith.constant 6 : index
+  %0:2 = scf.parallel (%i0, %i1) = (%c1, %c3) to (%c2, %c6) step (%c1, %c3)
+      init (%A, %B) -> (index, index) {
+    scf.reduce(%i0, %i1 : index, index) {
+    ^bb0(%lhs0: index, %rhs0: index):
+      %r0 = arith.addi %lhs0, %rhs0 : index
+      scf.reduce.return %r0 : index
+    }, {
+    ^bb0(%lhs1: index, %rhs1: index):
+      %r1 = arith.muli %lhs1, %rhs1 : index
+      scf.reduce.return %r1 : index
+    }
+  }
+  return %0#0, %0#1 : index, index
+}


        


More information about the Mlir-commits mailing list