[Mlir-commits] [mlir] [mlir] Add FP software implementation lowering pass: `arith-to-apfloat` (PR #166618)
Matthias Springer
llvmlistbot at llvm.org
Mon Nov 10 03:13:21 PST 2025
================
@@ -0,0 +1,34 @@
+// Case 1: All floating-point arithmetics is lowered through APFloat.
+// RUN: mlir-opt %s --convert-arith-to-apfloat --convert-to-llvm | \
+// RUN: mlir-runner -e entry --entry-point-result=void \
+// RUN: --shared-libs=%mlir_c_runner_utils | FileCheck %s
+
+// Case 2: Only unsupported arithmetics (f8E4M3FN) is lowered through APFloat.
+// Arithmetics on f32 is lowered directly to LLVM.
+// RUN: mlir-opt %s --convert-to-llvm --convert-arith-to-apfloat \
+// RUN: --convert-to-llvm --reconcile-unrealized-casts | \
+// RUN: mlir-runner -e entry --entry-point-result=void \
+// RUN: --shared-libs=%mlir_c_runner_utils | FileCheck %s
+
+// Put rhs into separate function so that it won't be constant-folded.
+func.func @foo() -> (f8E4M3FN, f32) {
+ %cst1 = arith.constant 2.2 : f8E4M3FN
+ %cst2 = arith.constant 2.2 : f32
+ return %cst1, %cst2 : f8E4M3FN, f32
+}
+
+func.func @entry() {
+ %a1 = arith.constant 1.4 : f8E4M3FN
+ %a2 = arith.constant 1.4 : f32
+ %b1, %b2 = func.call @foo() : () -> (f8E4M3FN, f32)
+ %c1 = arith.addf %a1, %b1 : f8E4M3FN // not supported by LLVM
+ %c2 = arith.addf %a2, %b2 : f32 // supported by LLVM
+
+ // CHECK: 3.5
----------------
matthias-springer wrote:
In case of the `arith.constant`, the value is rounded as `APFloat::rmNearestTiesToEven` by the literal parser when parsing the `FloatAttr`. This is not new.
In case of the `arith.addf`, we also round as `llvm::RoundingMode::NearestTiesToEven`. I think is somewhat arbitrary. We chose the same rounding mode as the `arith.addf` folder, so at least it's consistent. There's a TODO in the op documentation:
```
TODO: In the distant future, this will accept optional attributes for fast math, contraction, rounding mode, and other controls.
```
https://github.com/llvm/llvm-project/pull/166618
More information about the Mlir-commits
mailing list