[Mlir-commits] [mlir] [MLIR][Math] add canonicalize-f32-promotion pass (PR #92482)

Krzysztof Drewniak llvmlistbot at llvm.org
Fri May 17 10:44:25 PDT 2024


================
@@ -36,4 +36,51 @@ def MathLegalizeToF32 : Pass<"math-legalize-to-f32"> {
   let dependentDialects = ["math::MathDialect", "arith::ArithDialect"];
 }
 
+def MathCanonicalizeF32Promotion : Pass<"math-canonicalize-f32-promotion"> {
+  let summary = "Eliminate redundant truncf/extf pairs";
+  let description = [{
+    `legalize-to-f32` pass does f32 promotion for every op belonging to the
+    illegal op list. Once there are some consecutive illegal ops, `legalize-to-f32`
+    will insert redundant `arith.truncf` and `arith.extf` pairs between the illegal
+    ops.
+    
+    This pass is to eliminate the redundant truncf/extf pairs to improve
+    performance.
+
+    However, this pass may introduce numerical difference as the `f32->bf16` rounding
----------------
krzysz00 wrote:

No, I did mean to send a link to the commit - the discussion moved there.

What I mean to say is that
```mlir
%y = arith.truncf %arg0 : float to half
%z =arith.extf %y : half to float
return %z : float
```

can't be simplified, even under fastmath, because that is an explicit, user-specified desire to lose precision.

However, rewrites (like `arith-emulate-unsupported-floats`, which probably should also get this intermediate preservation treatment) which would introduce such a colliding truncf/extf pair are allowed to not do that and keep a higher intermediate precision. This is allowed without fastmath.

These are, to my current understanding, the C, and thus the LLVM, semantics.

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


More information about the Mlir-commits mailing list