Issue 243614
Summary mlir][IndexToLLVM] Wrong results for `index.ceildivs` with `INT64_MIN` and positive divisors
Labels mlir
Assignees
Reporter lin334141-cell
    ## Summary

The lowering of `index.ceildivs` to LLVM produces incorrect results when the dividend is the minimum signed 64-bit integer and the divisor is positive.

For example, `ceildivs(INT64_MIN, 3)` should return `-3074457345618258602`, but the compiled program returns `+3074457345618258602`.

The program compiles and executes successfully, without an assertion or runtime error. The incorrect result was detected through differential testing of generated MLIR programs against an independent source-level reference.

## Minimal Reproducer

```mlir
module {
  func.func @ceildiv(%a: index, %b: index) -> index {
    %0 = index.ceildivs %a, %b
    return %0 : index
 }
}
```

Lower using:

```bash
mlir-opt repro.mlir \
 --convert-index-to-llvm="index-bitwidth=64" \
  --convert-func-to-llvm \
  --reconcile-unrealized-casts
```

Execute the lowered function with 64-bit index arguments using an LLVM runtime harness.

## Expected vs. Actual Results

| Dividend | Divisor | Expected | Actual |
|---|---:|---:|---:|
| `INT64_MIN` | 3 | -3074457345618258602 | +3074457345618258602 |
| `INT64_MIN` | 2 | -4611686018427387904 | +4611686018427387904 |
| `INT64_MIN` | -2 | +4611686018427387904 | +4611686018427387904 |
| -5 | 3 | -1 | -1 |

The failure is reproducible with multiple positive divisors, while ordinary inputs produce correct results.

## Root Cause Analysis

The problem appears in `ConvertIndexCeilDivS` in:

`mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp`

For operands of different signs, the lowering uses a negation-based calculation equivalent to:

```cpp
-((-n) / m)
```

When `n = INT64_MIN`, negating the dividend cannot be represented in signed 64-bit arithmetic.

The generated LLVM operations therefore do not preserve the mathematical ceiling-division result.

For `INT64_MIN / 3`, the intermediate negation produces the problematic minimum-integer bit pattern, and the final result becomes positive instead of negative.

The source operation itself is defined for these inputs. Neither division by zero nor the `INT64_MIN / -1` overflow case is involved.

## Independent Differential-Testing Evidence

This issue was observed in a generated MLIR program exercising `index.ceildivs`.

The test was evaluated using an independent source-level semantic reference and two native execution paths.

For the failing inputs:

- The independent reference returned the mathematically correct negative quotient.
- Both compiled execution paths returned the same incorrect positive quotient.
- The two native results matched each other, while both differed from the reference.
- Compilation and native execution completed successfully.

This demonstrates a silent wrong-result case that is not detected by comparing the two native executions alone.

## Tested Versions

The incorrect results were reproduced through native execution on:

- LLVM `67005b724c917e1bdca50c82ecaa1b21a69244ed`
- LLVM main-based build `c1856996` (built November 6, 2026)

Both configurations produced identical incorrect results.

The analogous `arith.ceildivsi` operation returned correct results for the same tested inputs, indicating that the problem is specific to the Index lowering path.

## Expected Behavior

`index.ceildivs` lowering should preserve the defined mathematical result for `INT64_MIN` divided by positive nonzero integers.

In particular, it should avoid intermediate negation of `INT64_MIN`, which can overflow even when the original ceiling-division operation is well-defined.

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

Reply via email to