[Mlir-commits] [mlir] [mlir][arith] Clarify the meaning of fastmath flags (PR #217087)

Hung-Kuan Tseng llvmlistbot at llvm.org
Mon Sep 14 10:53:58 PDT 2026


https://github.com/Tim096 commented:

Since @kuhar asked for a self-contained definition and this has been sitting for a while: arith already gives most of these flags a meaning in code (`nsz`/`nnan` in the `addf`/`subf`/`mulf` folders, `contract` in `extf(truncf)`, `ninf` in the integer range inference), and `convert-arith-to-llvm` maps all seven 1:1 to the LLVM flags, so the definitions only need to be written down in arith's own terms. The `addi` description already does this for `nsw`/`nuw` ("the result is poison"), and the dialect description defines poison propagation, so the same vocabulary works here. A starting point, if it helps:

```
Floating-point fast math flags. Each flag lets transformations assume
something about the operation or rewrite it; without flags the operation
follows IEEE 754.

* `nnan`: the operands and the result are assumed not to be NaN. If an
  operand is NaN, or the result would be NaN, the result is poison.
* `ninf`: the operands and the result are assumed not to be +/-Inf. If an
  operand is +/-Inf, or the result would be +/-Inf, the result is poison.
* `nsz`: the sign of a zero operand or result may be flipped
  non-deterministically. `-0.0` is not poison.
* `arcp`: a division may be replaced by a multiplication with the
  reciprocal, e.g. `a / b` by `a * (1.0 / b)`.
* `contract`: the operation may be fused with a neighbouring operation
  into one operation with a single rounding, e.g. a `mulf` feeding an
  `addf` into an `fma`.
* `afn`: the operation may be replaced by an approximation. This applies
  to the `math` ops that carry this attribute.
* `reassoc`: the operation may be replaced by an algebraically equivalent
  expression, e.g. reassociated with neighbouring operations, even though
  floating-point arithmetic is not associative.
* `fast`: all of the above.

`arcp`, `contract` and `reassoc` are rewrite-based: a rewrite that
involves several operations needs the flag on all of them, and the
operations it creates carry the intersection of the flags of the
operations they replace. The other flags are value-based and apply to
the operation on its own.

These match the LLVM IR fast-math flags of the same name, which is what
`convert-arith-to-llvm` lowers them to.
```

The text renders under the `Syntax` block of `FastMathFlagsAttr` in the generated dialect doc (checked with `mlir-tblgen -gen-dialect-doc`), so the enum is the right place for it.


https://github.com/llvm/llvm-project/pull/217087


More information about the Mlir-commits mailing list