Skip to content

[MLIR][MemRef] Add verifier check for index count vs memref rank in generic_atomic_rmw#189229

Closed
joker-eph wants to merge 1 commit into
llvm:mainfrom
joker-eph:fix/issue-178211
Closed

[MLIR][MemRef] Add verifier check for index count vs memref rank in generic_atomic_rmw#189229
joker-eph wants to merge 1 commit into
llvm:mainfrom
joker-eph:fix/issue-178211

Conversation

@joker-eph
Copy link
Copy Markdown
Contributor

memref.generic_atomic_rmw did not verify that the number of index operands matches the rank of the memref. This caused a crash during lowering to LLVM when an index count mismatch was present (e.g., a rank-0 memref accessed with one index).

Fixes #178211

Assisted-by: Claude Code

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Mar 29, 2026

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-memref

Author: Mehdi Amini (joker-eph)

Changes

memref.generic_atomic_rmw did not verify that the number of index operands matches the rank of the memref. This caused a crash during lowering to LLVM when an index count mismatch was present (e.g., a rank-0 memref accessed with one index).

Fixes #178211

Assisted-by: Claude Code


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

2 Files Affected:

  • (modified) mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp (+6)
  • (modified) mlir/test/Dialect/MemRef/invalid.mlir (+11)
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 404b2aacf1450..8728e66919baf 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -1593,6 +1593,12 @@ LogicalResult GenericAtomicRMWOp::verify() {
   if (getResult().getType() != body.getArgument(0).getType())
     return emitOpError("expected block argument of the same type result type");
 
+  auto memrefType = cast<MemRefType>(getMemref().getType());
+  if (getIndices().size() != static_cast<size_t>(memrefType.getRank()))
+    return emitOpError("index count (")
+           << getIndices().size() << ") does not match memref rank ("
+           << memrefType.getRank() << ")";
+
   bool hasSideEffects =
       body.walk([&](Operation *nestedOp) {
             if (isMemoryEffectFree(nestedOp))
diff --git a/mlir/test/Dialect/MemRef/invalid.mlir b/mlir/test/Dialect/MemRef/invalid.mlir
index af068d8ca8e95..eeb1a80278f1a 100644
--- a/mlir/test/Dialect/MemRef/invalid.mlir
+++ b/mlir/test/Dialect/MemRef/invalid.mlir
@@ -1125,6 +1125,17 @@ func.func @atomic_yield_type_mismatch(%I: memref<10xf32>, %i : index) {
 
 // -----
 
+func.func @generic_atomic_rmw_rank_mismatch(%arg0: memref<i32>, %idx: index) {
+  // expected-error@+1 {{index count (1) does not match memref rank (0)}}
+  %r = memref.generic_atomic_rmw %arg0[%idx] : memref<i32> {
+  ^bb0(%v: i32):
+    memref.atomic_yield %v : i32
+  }
+  return
+}
+
+// -----
+
 #map0 = affine_map<(d0) -> (d0 floordiv 8, d0 mod 8)>
 func.func @memref_realloc_layout(%src : memref<256xf32, #map0>) -> memref<?xf32>{
   // expected-error@+1 {{unsupported layout}}

@@ -1593,6 +1593,12 @@ LogicalResult GenericAtomicRMWOp::verify() {
if (getResult().getType() != body.getArgument(0).getType())
return emitOpError("expected block argument of the same type result type");

auto memrefType = cast<MemRefType>(getMemref().getType());
if (getIndices().size() != static_cast<size_t>(memrefType.getRank()))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Didn't double-check, let's ensure the op is already constrained to only accept ranked memrefs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is, and it reminded me I can write as:

  MemRefType memrefType = getMemref().getType();

Instead of

  auto memrefType = cast<MemRefType>(getMemref().getType());

Because getMemref() returns a TypedValue.

@joker-eph joker-eph enabled auto-merge (squash) April 1, 2026 10:25
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

🐧 Linux x64 Test Results

  • 7750 tests passed
  • 604 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

MLIR

MLIR.Dialect/MemRef/invalid.mlir
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/mlir-opt -allow-unregistered-dialect -split-input-file /home/gha/actions-runner/_work/llvm-project/llvm-project/mlir/test/Dialect/MemRef/invalid.mlir -verify-diagnostics
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/mlir-opt -allow-unregistered-dialect -split-input-file /home/gha/actions-runner/_work/llvm-project/llvm-project/mlir/test/Dialect/MemRef/invalid.mlir -verify-diagnostics
# .---command stdout------------
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# `-----------------------------
# .---command stderr------------
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/mlir/test/Dialect/MemRef/invalid.mlir:1130:8: error: unexpected error: 'memref.generic_atomic_rmw' op invalid number of indices for accessed memref, expected 0 but got 1
# |   %r = memref.generic_atomic_rmw %arg0[%idx] : memref<i32> {
# |        ^
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/mlir/test/Dialect/MemRef/invalid.mlir:1129:6: error: expected error "index count (1) does not match memref rank (0)" was not produced
# |   // expected-error@+1 {{index count (1) does not match memref rank (0)}}
# |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

🪟 Windows x64 Test Results

  • 3598 tests passed
  • 415 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

MLIR

MLIR.Dialect/MemRef/invalid.mlir
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
c:\_work\llvm-project\llvm-project\build\bin\mlir-opt.exe -allow-unregistered-dialect -split-input-file C:\_work\llvm-project\llvm-project\mlir\test\Dialect\MemRef\invalid.mlir -verify-diagnostics
# executed command: 'c:\_work\llvm-project\llvm-project\build\bin\mlir-opt.exe' -allow-unregistered-dialect -split-input-file 'C:\_work\llvm-project\llvm-project\mlir\test\Dialect\MemRef\invalid.mlir' -verify-diagnostics
# .---command stdout------------
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# | // -----
# `-----------------------------
# .---command stderr------------
# | C:\_work\llvm-project\llvm-project\mlir\test\Dialect\MemRef\invalid.mlir:1130:8: error: unexpected error: 'memref.generic_atomic_rmw' op invalid number of indices for accessed memref, expected 0 but got 1
# |   %r = memref.generic_atomic_rmw %arg0[%idx] : memref<i32> {
# |        ^
# | C:\_work\llvm-project\llvm-project\mlir\test\Dialect\MemRef\invalid.mlir:1129:6: error: expected error "index count (1) does not match memref rank (0)" was not produced
# |   // expected-error@+1 {{index count (1) does not match memref rank (0)}}
# |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

…eneric_atomic_rmw

`memref.generic_atomic_rmw` did not verify that the number of index
operands matches the rank of the memref. This caused a crash during
lowering to LLVM when an index count mismatch was present (e.g., a
rank-0 memref accessed with one index).

Add a verifier check to `GenericAtomicRMWOp::verify()` that emits a
proper error diagnostic instead of crashing in the lowering pattern.

Fixes llvm#178211

Assisted-by: Claude Code
@joker-eph
Copy link
Copy Markdown
Contributor Author

Can't reproduce the failure locally, rebased and pushed just to see...

@joker-eph
Copy link
Copy Markdown
Contributor Author

I didn't fetch before rebase, I reproduce the failure now.

This is because of b813b0b which changed this to use IndexedMemoryAccessOpInterface which already catch this case in the verifier now. So this change is obsolete and I can close the bug as fixed.

@joker-eph joker-eph closed this Apr 1, 2026
auto-merge was automatically disabled April 1, 2026 13:24

Pull request was closed

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-opt --convert-to-llvm crashes on memref.generic_atomic_rmw

3 participants