[Mlir-commits] [mlir] [mlir][arith] Define the fastmath flags (PR #230085)

Hung-Kuan Tseng llvmlistbot at llvm.org
Sun Nov 1 05:27:54 PST 2026


https://github.com/Tim096 updated https://github.com/llvm/llvm-project/pull/230085

>From a2589de0a682dd6cab83c8927e05dc27078f6069 Mon Sep 17 00:00:00 2001
From: Hung-Kuan Tseng <tseng.tim096 at gmail.com>
Date: Thu, 8 Oct 2026 18:19:06 +0800
Subject: [PATCH 1/2] [mlir][arith] Define the fastmath flags

The `FastMathFlags` enum had no description. Define each flag in arith's
own terms, as #217087 started and as requested in its review. The
definitions follow the LLVM IR fast-math flags, which
`convert-arith-to-llvm` maps them to one-to-one.

Supersedes #217087.
---
 .../mlir/Dialect/Arith/IR/ArithBase.td        | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td b/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
index 0c05c4db79bed6..13a1622728ff46 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
@@ -139,6 +139,38 @@ def FastMathFlags : I32BitEnumAttr<
   let cppNamespace = "::mlir::arith";
   let genSpecializedAttr = 0;
   let printBitEnumPrimaryGroups = 1;
+  let description = [{
+    Floating-point fast math flags. Each flag lets transformations assume
+    something about the operation, or rewrite it into a different sequence of
+    operations.
+
+    * `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 may be flipped non-deterministically.
+      This does not make `-0.0` poison.
+    * `arcp`: a division may be treated as a multiplication by the reciprocal,
+      e.g. `a / b` as `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`. This does not allow reassociation to form a fusion.
+    * `afn`: the operation may be replaced by an approximation. This applies to
+      the `math` operations 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`, `afn` and `reassoc` are rewrite-based: a rewrite that
+    involves several operations needs the flag on all of them, and the
+    operations it creates generally carry the intersection of the flags of the
+    operations they replace. The other flags apply to each operation on its
+    own.
+
+    These are the LLVM IR fast-math flags of the same name, which is what
+    `convert-arith-to-llvm` lowers them to.
+  }];
 }
 
 def Arith_FastMathAttr :

>From ba747baf7e5ad9a5b2c1137c1ef74bfc5fef7691 Mon Sep 17 00:00:00 2001
From: Hung-Kuan Tseng <tseng.tim096 at gmail.com>
Date: Sun, 1 Nov 2026 20:00:59 +0800
Subject: [PATCH 2/2] Use e.g., and US spelling, and drop the sentence about
 the lowering

---
 mlir/include/mlir/Dialect/Arith/IR/ArithBase.td | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td b/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
index 13a1622728ff46..4fd8bdec84521a 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
@@ -151,14 +151,14 @@ def FastMathFlags : I32BitEnumAttr<
     * `nsz`: the sign of a zero operand may be flipped non-deterministically.
       This does not make `-0.0` poison.
     * `arcp`: a division may be treated as a multiplication by the reciprocal,
-      e.g. `a / b` as `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`
+      e.g., `a / b` as `a * (1.0 / b)`.
+    * `contract`: the operation may be fused with a neighboring operation into
+      one operation with a single rounding, e.g., a `mulf` feeding an `addf`
       into an `fma`. This does not allow reassociation to form a fusion.
     * `afn`: the operation may be replaced by an approximation. This applies to
       the `math` operations that carry this attribute.
     * `reassoc`: the operation may be replaced by an algebraically equivalent
-      expression, e.g. reassociated with neighbouring operations, even though
+      expression, e.g., reassociated with neighboring operations, even though
       floating-point arithmetic is not associative.
     * `fast`: all of the above.
 
@@ -167,9 +167,6 @@ def FastMathFlags : I32BitEnumAttr<
     operations it creates generally carry the intersection of the flags of the
     operations they replace. The other flags apply to each operation on its
     own.
-
-    These are the LLVM IR fast-math flags of the same name, which is what
-    `convert-arith-to-llvm` lowers them to.
   }];
 }
 



More information about the Mlir-commits mailing list