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

Krzysztof Drewniak llvmlistbot at llvm.org
Fri May 17 14:50:57 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:

I think the main thing with LLVM IR is hat `fptrunc` and `fpext` can't carry fastmath flags, last I checked.

Now, if they could, I'd absolutely permit
```mlir
func.func @f(%arg0: f32) -> f32 {
    %0 = arith.truncf contract %arg0 : f32 to f16
    %1 = arith.extf contract %0 : f16 to f32
    return %1 : f32
}
```
to fold to
```mlir
func.func @f(%arg0: f32) -> f32 {
    return %arg0 : f32
}
```

So, from an MLIR perspective, if we give extend and truncate the ability to fastmath (if they don't already have it) it'd make a lot of sense to
1. Add that folding and
2. Change the various unsupported float emulation/legalization passes to (optionally - or maybe by default) stick a `contract` on their truncates and extensions

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


More information about the Mlir-commits mailing list