[Mlir-commits] [mlir] [mlir][LLVM] Add fastmath flags support to fpext/fptrunc ops. (PR #192185)
Ming Yan
llvmlistbot at llvm.org
Thu Apr 16 00:33:57 PDT 2026
https://github.com/NexMing updated https://github.com/llvm/llvm-project/pull/192185
>From 81219614160e13e4e6e62de0b7e18d38351d1f4d Mon Sep 17 00:00:00 2001
From: yanming <ming.yan at terapines.com>
Date: Wed, 15 Apr 2026 13:27:54 +0800
Subject: [PATCH 1/3] Add tests.
---
mlir/test/Target/LLVMIR/Import/fastmath.ll | 11 +++++++++++
mlir/test/Target/LLVMIR/llvmir.mlir | 5 +++++
2 files changed, 16 insertions(+)
diff --git a/mlir/test/Target/LLVMIR/Import/fastmath.ll b/mlir/test/Target/LLVMIR/Import/fastmath.ll
index 0c6a74cda9283..fff574ad436b6 100644
--- a/mlir/test/Target/LLVMIR/Import/fastmath.ll
+++ b/mlir/test/Target/LLVMIR/Import/fastmath.ll
@@ -19,6 +19,17 @@ define void @fastmath_inst(float %arg1, float %arg2, i1 %arg3) {
; // -----
+; CHECK-LABEL: @fastmath_cast
+define void @fastmath_cast(float %arg1) {
+ ; CHECK: llvm.fpext %{{.*}} : f32 to f64
+ %1 = fpext nnan float %arg1 to double
+ ; CHECK: llvm.fptrunc %{{.*}} : f32 to f16
+ %2 = fptrunc fast float %arg1 to half
+ ret void
+}
+
+; // -----
+
; CHECK-LABEL: @fastmath_fcmp
define void @fastmath_fcmp(float %arg1, float %arg2) {
; CHECK: llvm.fcmp "oge" %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath<nsz>} : f32
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 8adf305805b0e..878ba092ef3c2 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -2155,6 +2155,11 @@ llvm.func @fastmathFlags(%arg0: f32, %arg1 : vector<2xf32>) {
%25 = llvm.mlir.constant(true) : i1
// CHECK: select contract i1
%26 = llvm.select %25, %arg0, %20 {fastmathFlags = #llvm.fastmath<contract>} : i1, f32
+
+// CHECK: {{.*}} = fpext float {{.*}} to double
+// CHECK: {{.*}} = fptrunc float {{.*}} to half
+ %27 = llvm.fpext %arg0 {fastmathFlags = #llvm.fastmath<nnan>} : f32 to f64
+ %28 = llvm.fptrunc %arg0 {fastmathFlags = #llvm.fastmath<fast>} : f32 to f16
llvm.return
}
>From 8507f17acb688149e12fbd05ba50efa8ce3dc4dd Mon Sep 17 00:00:00 2001
From: yanming <ming.yan at terapines.com>
Date: Wed, 15 Apr 2026 13:30:07 +0800
Subject: [PATCH 2/3] [mlir][LLVM] Add fastmath flags support to fpext/fptrunc
ops.
---
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td | 23 +++++++++++++++++++--
mlir/test/Target/LLVMIR/Import/fastmath.ll | 4 ++--
mlir/test/Target/LLVMIR/llvmir.mlir | 4 ++--
3 files changed, 25 insertions(+), 6 deletions(-)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index f6f1c90b481c5..a841df7cc3ba3 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -588,6 +588,25 @@ class LLVM_CastOpWithOverflowFlag<string mnemonic, string instName, Type type,
}];
}
+class LLVM_CastOpWithFastMathFlag<string mnemonic, string instName, Type type,
+ Type resultType, list<Trait> traits = []> :
+ LLVM_Op<mnemonic, !listconcat([Pure], [DeclareOpInterfaceMethods<FastmathFlagsInterface>], traits)>,
+ LLVM_Builder<"$res = builder.Create" # instName # "($arg, $_resultType);"> {
+ let arguments = (
+ ins type:$arg,
+ DefaultValuedAttr<LLVM_FastmathFlagsAttr, "{}">:$fastmathFlags);
+ let results = (outs resultType:$res);
+ let builders = [LLVM_OneResultOpBuilder];
+ let assemblyFormat = "$arg attr-dict `:` type($arg) `to` type($res)";
+ string llvmInstName = instName;
+ string mlirBuilder = [{
+ auto op = $_qualCppClassName::create($_builder,
+ $_location, $_resultType, $arg);
+ moduleImport.setFastmathFlagsAttr(inst, op);
+ $res = op;
+ }];
+}
+
class LLVM_DereferenceableCastOp<string mnemonic, string instName, Type type,
Type resultType, list<Trait> traits = []> :
LLVM_Op<mnemonic, !listconcat([Pure], [DeclareOpInterfaceMethods<DereferenceableOpInterface>], traits)> {
@@ -655,10 +674,10 @@ def LLVM_FPToSIOp : LLVM_CastOp<"fptosi", "FPToSI",
def LLVM_FPToUIOp : LLVM_CastOp<"fptoui", "FPToUI",
LLVM_ScalarOrVectorOf<LLVM_AnyFloat>,
LLVM_ScalarOrVectorOf<AnySignlessInteger>>;
-def LLVM_FPExtOp : LLVM_CastOp<"fpext", "FPExt",
+def LLVM_FPExtOp : LLVM_CastOpWithFastMathFlag<"fpext", "FPExt",
LLVM_ScalarOrVectorOf<LLVM_AnyFloat>,
LLVM_ScalarOrVectorOf<LLVM_AnyFloat>>;
-def LLVM_FPTruncOp : LLVM_CastOp<"fptrunc", "FPTrunc",
+def LLVM_FPTruncOp : LLVM_CastOpWithFastMathFlag<"fptrunc", "FPTrunc",
LLVM_ScalarOrVectorOf<LLVM_AnyFloat>,
LLVM_ScalarOrVectorOf<LLVM_AnyFloat>>;
diff --git a/mlir/test/Target/LLVMIR/Import/fastmath.ll b/mlir/test/Target/LLVMIR/Import/fastmath.ll
index fff574ad436b6..f5da1cb92cf7b 100644
--- a/mlir/test/Target/LLVMIR/Import/fastmath.ll
+++ b/mlir/test/Target/LLVMIR/Import/fastmath.ll
@@ -21,9 +21,9 @@ define void @fastmath_inst(float %arg1, float %arg2, i1 %arg3) {
; CHECK-LABEL: @fastmath_cast
define void @fastmath_cast(float %arg1) {
- ; CHECK: llvm.fpext %{{.*}} : f32 to f64
+ ; CHECK: llvm.fpext %{{.*}} {fastmathFlags = #llvm.fastmath<nnan>} : f32 to f64
%1 = fpext nnan float %arg1 to double
- ; CHECK: llvm.fptrunc %{{.*}} : f32 to f16
+ ; CHECK: llvm.fptrunc %{{.*}} {fastmathFlags = #llvm.fastmath<fast>} : f32 to f16
%2 = fptrunc fast float %arg1 to half
ret void
}
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 878ba092ef3c2..d740622060b8b 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -2156,8 +2156,8 @@ llvm.func @fastmathFlags(%arg0: f32, %arg1 : vector<2xf32>) {
// CHECK: select contract i1
%26 = llvm.select %25, %arg0, %20 {fastmathFlags = #llvm.fastmath<contract>} : i1, f32
-// CHECK: {{.*}} = fpext float {{.*}} to double
-// CHECK: {{.*}} = fptrunc float {{.*}} to half
+// CHECK: {{.*}} = fpext nnan float {{.*}} to double
+// CHECK: {{.*}} = fptrunc fast float {{.*}} to half
%27 = llvm.fpext %arg0 {fastmathFlags = #llvm.fastmath<nnan>} : f32 to f64
%28 = llvm.fptrunc %arg0 {fastmathFlags = #llvm.fastmath<fast>} : f32 to f16
llvm.return
>From 1091edd8c7888b8aae343b99a772959dfb0c8efd Mon Sep 17 00:00:00 2001
From: yanming <ming.yan at terapines.com>
Date: Thu, 16 Apr 2026 15:30:34 +0800
Subject: [PATCH 3/3] Simplify fast-math assembly format.
---
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td | 3 ++-
mlir/test/Target/LLVMIR/Import/fastmath.ll | 4 ++--
mlir/test/Target/LLVMIR/llvmir.mlir | 4 ++--
3 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index a841df7cc3ba3..ed39cd0828249 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -597,7 +597,8 @@ class LLVM_CastOpWithFastMathFlag<string mnemonic, string instName, Type type,
DefaultValuedAttr<LLVM_FastmathFlagsAttr, "{}">:$fastmathFlags);
let results = (outs resultType:$res);
let builders = [LLVM_OneResultOpBuilder];
- let assemblyFormat = "$arg attr-dict `:` type($arg) `to` type($res)";
+ let assemblyFormat = "$arg (`fastmath` `` $fastmathFlags^)? "
+ "attr-dict `:` type($arg) `to` type($res)";
string llvmInstName = instName;
string mlirBuilder = [{
auto op = $_qualCppClassName::create($_builder,
diff --git a/mlir/test/Target/LLVMIR/Import/fastmath.ll b/mlir/test/Target/LLVMIR/Import/fastmath.ll
index f5da1cb92cf7b..a6f8f10a5f8f9 100644
--- a/mlir/test/Target/LLVMIR/Import/fastmath.ll
+++ b/mlir/test/Target/LLVMIR/Import/fastmath.ll
@@ -21,9 +21,9 @@ define void @fastmath_inst(float %arg1, float %arg2, i1 %arg3) {
; CHECK-LABEL: @fastmath_cast
define void @fastmath_cast(float %arg1) {
- ; CHECK: llvm.fpext %{{.*}} {fastmathFlags = #llvm.fastmath<nnan>} : f32 to f64
+ ; CHECK: llvm.fpext %{{.*}} fastmath<nnan> : f32 to f64
%1 = fpext nnan float %arg1 to double
- ; CHECK: llvm.fptrunc %{{.*}} {fastmathFlags = #llvm.fastmath<fast>} : f32 to f16
+ ; CHECK: llvm.fptrunc %{{.*}} fastmath<fast> : f32 to f16
%2 = fptrunc fast float %arg1 to half
ret void
}
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index d740622060b8b..d197b9ca3d201 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -2158,8 +2158,8 @@ llvm.func @fastmathFlags(%arg0: f32, %arg1 : vector<2xf32>) {
// CHECK: {{.*}} = fpext nnan float {{.*}} to double
// CHECK: {{.*}} = fptrunc fast float {{.*}} to half
- %27 = llvm.fpext %arg0 {fastmathFlags = #llvm.fastmath<nnan>} : f32 to f64
- %28 = llvm.fptrunc %arg0 {fastmathFlags = #llvm.fastmath<fast>} : f32 to f16
+ %27 = llvm.fpext %arg0 fastmath<nnan> : f32 to f64
+ %28 = llvm.fptrunc %arg0 fastmath<fast> : f32 to f16
llvm.return
}
More information about the Mlir-commits
mailing list