[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 22:29:11 PDT 2026
https://github.com/1sgtpepper created https://github.com/llvm/llvm-project/pull/211173
# [mlir][arith] Preserve fast-math flags when lowering floating casts
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)
>From f23adcfb3a26bd0b1a1eb898587bb5954603efc5 Mon Sep 17 00:00:00 2001
From: 1sgtpepper <165563006+1sgtpepper at users.noreply.github.com>
Date: Wed, 22 Jul 2026 00:05:30 +0800
Subject: [PATCH] [mlir][arith] Preserve fast-math flags when lowering floating
casts
---
mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp | 4 ++--
.../Conversion/ArithToLLVM/arith-to-llvm.mlir | 17 ++++++++++-------
2 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index 6bf0fe85bb62f..254a8b9a4d296 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -114,7 +114,7 @@ using DivSIOpLowering =
using DivUIOpLowering =
VectorConvertToLLVMPattern<arith::DivUIOp, LLVM::UDivOp>;
using ExtFOpLowering = VectorConvertToLLVMPattern<arith::ExtFOp, LLVM::FPExtOp,
- AttrConvertPassThrough,
+ arith::AttrConvertFastMathToLLVM,
/*FailOnUnsupportedFP=*/true>;
using ExtSIOpLowering =
VectorConvertToLLVMPattern<arith::ExtSIOp, LLVM::SExtOp>;
@@ -202,7 +202,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
}
-
More information about the Mlir-commits
mailing list