[Mlir-commits] [mlir] [mlir][arith] Preserve fast-math flags when lowering floating casts (PR #211173)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jul 21 23:23:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: 1sgtpepper

<details>
<summary>Changes</summary>

Fixes #<!-- -->156692.

Convert Arith fast-math flags to LLVM fast-math flags when lowering
`arith.extf` and unconstrained `arith.truncf`. Previously these two patterns
passed the source-dialect attribute through unchanged, producing an Arith
attribute on an LLVM operation.

The constrained `arith.truncf` path remains unchanged.

Tests cover fast-math flag conversion for both floating-point casts.

Checks:
- Fork CI: `mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir`
  (https://github.com/1sgtpepper/llvm-project/actions/runs/29861025327)


---
Full diff: https://github.com/llvm/llvm-project/pull/211173.diff


2 Files Affected:

- (modified) mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp (+5-4) 
- (modified) mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir (+10-7) 


``````````diff
diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index 6bf0fe85bb62f..619cb0b741812 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -113,9 +113,10 @@ using DivSIOpLowering =
     VectorConvertToLLVMPattern<arith::DivSIOp, LLVM::SDivOp>;
 using DivUIOpLowering =
     VectorConvertToLLVMPattern<arith::DivUIOp, LLVM::UDivOp>;
-using ExtFOpLowering = VectorConvertToLLVMPattern<arith::ExtFOp, LLVM::FPExtOp,
-                                                  AttrConvertPassThrough,
-                                                  /*FailOnUnsupportedFP=*/true>;
+using ExtFOpLowering =
+    VectorConvertToLLVMPattern<arith::ExtFOp, LLVM::FPExtOp,
+                               arith::AttrConvertFastMathToLLVM,
+                               /*FailOnUnsupportedFP=*/true>;
 using ExtSIOpLowering =
     VectorConvertToLLVMPattern<arith::ExtSIOp, LLVM::SExtOp>;
 using ExtUIOpLowering =
@@ -202,7 +203,7 @@ using SubIOpLowering =
 using TruncFOpLowering =
     ConstrainedVectorConvertToLLVMPattern<arith::TruncFOp, LLVM::FPTruncOp,
                                           /*HasRoundingMode=*/false,
-                                          AttrConvertPassThrough,
+                                          arith::AttrConvertFastMathToLLVM,
                                           /*FailOnUnsupportedFP=*/true>;
 using ConstrainedTruncFOpLowering = ConstrainedVectorConvertToLLVMPattern<
     arith::TruncFOp, LLVM::ConstrainedFPTruncIntr, /*HasRoundingMode=*/true,
diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index 43f21561e6544..d8f7cfcaded17 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -866,18 +866,22 @@ func.func @ops_supporting_fastmath(%arg0: f32, %arg1: f32, %arg2: i32) {
   %0 = arith.addf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fdiv %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
   %1 = arith.divf %arg0, %arg1 fastmath<fast> : f32
+// CHECK: llvm.fpext %arg0 fastmath<fast> : f32 to f64
+  %2 = arith.extf %arg0 fastmath<fast> : f32 to f64
 // CHECK: llvm.intr.maximum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
-  %2 = arith.maximumf %arg0, %arg1 fastmath<fast> : f32
+  %3 = arith.maximumf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.intr.minimum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
-  %3 = arith.minimumf %arg0, %arg1 fastmath<fast> : f32
+  %4 = arith.minimumf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fmul %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
-  %4 = arith.mulf %arg0, %arg1 fastmath<fast> : f32
+  %5 = arith.mulf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fneg %arg0  {fastmathFlags = #llvm.fastmath<fast>} : f32
-  %5 = arith.negf %arg0 fastmath<fast> : f32
+  %6 = arith.negf %arg0 fastmath<fast> : f32
 // CHECK: llvm.frem %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
-  %6 = arith.remf %arg0, %arg1 fastmath<fast> : f32
+  %7 = arith.remf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fsub %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
-  %7 = arith.subf %arg0, %arg1 fastmath<fast> : f32
+  %8 = arith.subf %arg0, %arg1 fastmath<fast> : f32
+// CHECK: llvm.fptrunc %arg0 fastmath<fast> : f32 to f16
+  %9 = arith.truncf %arg0 fastmath<fast> : f32 to f16
   return
 }
 
@@ -981,4 +985,3 @@ func.func @supported_fp_type(%arg0: f32, %arg1: vector<4xf32>, %arg2: vector<4x8
   %3 = arith.cmpf oeq, %arg0, %arg3 : f32
   return
 }
-

``````````

</details>


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


More information about the Mlir-commits mailing list