Issue 243226
Summary [mlir][bufferization] `ValueComparator` asserts on block arguments from distinct blocks in the same region
Labels mlir
Assignees
Reporter quiet-maple-47
    ## Description

`--ownership-based-buffer-deallocation` hits an assertion failure in `mlir::bufferization::ValueComparator::operator()` when sorting two live `BlockArgument` memrefs that have the same argument index (`getArgNumber()`) and belong to different blocks of the same region.

`ValueComparator` checks `getArgNumber()` and assumes that equal parent regions imply identical `BlockArgument`s, without comparing `getOwner()` (the parent `Block *`).

## Reproducer

`input.mlir`:

```mlir
func.func @f() -> (memref<i32>, memref<i32>) {
  %a = memref.alloc() : memref<i32>
  %b = memref.alloc() : memref<i32>
  cf.br ^bb1(%a : memref<i32>)
^bb1(%arg0: memref<i32>):
  cf.br ^bb2(%b : memref<i32>)
^bb2(%arg1: memref<i32>):
  cf.br ^bb3
^bb3:
  return %arg0, %arg1 : memref<i32>, memref<i32>
}
```

```bash
mlir-opt --ownership-based-buffer-deallocation input.mlir
```

[View on Compiler Explorer (LLVM trunk, non-assertions)](https://godbolt.org/z/zn7Y716aG)

## Actual Behavior

On assertions-enabled builds (`llvm-project` `b3df0665`), `mlir-opt` aborts when sorting `retainedByLiveness` in `DeallocationState::getMemrefsToRetain`:

```text
mlir-opt: mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp:273: bool mlir::bufferization::ValueComparator::operator()(const mlir::Value &, const mlir::Value &) const: Assertion `lhsRegion != rhsRegion && "lhsRegion == rhsRegion implies lhs == rhs"' failed.
```

On non-assertions builds, `ValueComparator` evaluates `lhsRegion == rhsRegion` at `func.func`, returns `false` for both `(lhs, rhs)` and `(rhs, lhs)` (violating strict weak ordering for distinct values), and leaves the `retain(...)` operand order dependent on hash-set iteration order.

## Expected Behavior

`ValueComparator` should establish a deterministic total order across distinct `BlockArgument`s in different blocks of the same region (e.g., comparing `lhsBBArg.getOwner()->computeBlockNumber() < rhsBBArg.getOwner()->computeBlockNumber()`), matching how it orders `OpResult`s in the same region.

## Root Cause

In [`ValueComparator::operator()`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp#L264-L273), when `lhs` and `rhs` are `BlockArgument`s with equal `getArgNumber()` and `lhsRegion == rhsRegion`, the code asserts `lhsRegion != rhsRegion` instead of comparing `lhsBBArg.getOwner()` and `rhsBBArg.getOwner()` via `computeBlockNumber()` (just as the `OpResult` branch does at lines 280–287).

## Version

- `llvm-project` `b3df0665` (Linux x86_64, assertions enabled)
- Relevant implementation verified unchanged on `main` at `e0c9350a3f88` (2026-11-16)
- Compiler Explorer link above shows LLVM trunk (non-assertions build)

## Claim

This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

**Assisted-by:** Gemini / Antigravity

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to