| Issue |
233861
|
| Summary |
[mlir][arith] `Canonicalize`: valid int2fp2int round trips are not folded
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
dashygo097
|
### Summary
`canonicalize` retains an integer–float–integer round trip even when the intermediate floating-point format can represent every source integer exactly.
### Reproducer
```mlir
func.func @signed_exact(%x: i32) -> i32 {
%f = arith.sitofp %x : i32 to f64
%r = arith.fptosi %f : f64 to i32
return %r : i32
}
```
```sh
mlir-opt input.mlir --canonicalize
```
### Actual behavior
Without the proposed fold, both casts remain in the output.
### Expected behavior
```mlir
func.func @signed_exact(%x: i32) -> i32 {
return %x : i32
}
```
All signed i32 values are exactly representable in f64, so this transformation does not require fast-math flags. The corresponding `uitofp`/`fptoui` round trip can also be removed when the floating-point format represents every unsigned source value exactly.
### Analysis
For a matching signed or unsigned `iN -> F -> iN` round trip, let `P` be the floating-point significand precision, including the implicit leading bit, and `Emax` its maximum binary exponent. These are properties of the format, not its storage bit width.
For the conventional binary formats below, the following conditions establish exact representation of every source integer:
| Conversion pair | Source integer range | Precision requirement | Exponent requirement |
| --- | --- | --- | --- |
| `sitofp` followed by `fptosi` | `[-2^(N-1), 2^(N-1) - 1]` | `N - 1 <= P` | `N - 1 <= Emax` |
| `uitofp` followed by `fptoui` | `[0, 2^N - 1]` | `N <= P` | `N - 1 <= Emax` |
And here are some examples:
| Intermediate format | `P` | `Emax` | Largest signed source width | Largest unsigned source width |
| --- | --- | --- | --- | --- |
| `f16` | 11 | 15 | `i12` | `i11` |
| `bf16` | 8 | 127 | `i9` | `i8` |
| `f32` | 24 | 127 | `i25` | `i24` |
| `f64` | 53 | 1023 | `i54` | `i53` |
| `f6E2M3FN` | 4 | 2 | `i3` | `i3` |
The format must also represent zero and, for signed inputs, negative values. For example, `f8E8M0FNU` cannot represent zero, so it does not satisfy this proof. An unsigned-only format such as `f8E5M3FNU` may qualify for unsigned inputs but not the complete signed source domain.
The implementation belongs in `FPToSIOp::fold()` and `FPToUIOp::fold()` in `mlir/lib/Dialect/Arith/IR/ArithOps.cpp`, with regression tests in `mlir/test/Dialect/Arith/canonicalize.mlir`.
LLVM InstCombine already implements related transformations in [`foldItoFPtoI()`](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp). Handling the exact round trip in Arith would simplify IR before later MLIR transformations and also benefit pipelines that do not use the LLVM backend.
### Potential Solution
PR
### LLVM Version
LLVM version: 24.0.0git
LLVM commit: 9683a9b87f67dea3365d76cfea84aa041f202713
Optimized build with assertions.
### Environment
Operating system: MacOS 27.2 Beta(26B5112a)
Machine: Apple M4 Air
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs