[Mlir-commits] [mlir] [MLIR][LLVM] Add ftz and fuse FP ops related function attribute support (PR #97812)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Jul 6 07:08:53 PDT 2024


https://github.com/runseny updated https://github.com/llvm/llvm-project/pull/97812

>From c296c22b72f24c37acf37614b3f5001f1eb6475d Mon Sep 17 00:00:00 2001
From: runseny <runseny at nvidia.com>
Date: Thu, 4 Jul 2024 09:12:58 +0000
Subject: [PATCH] [MLIR][LLVM] Add ftz and fuse FP ops related function
 attribute support

---
 mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td   |  3 +
 mlir/lib/Target/LLVMIR/ModuleImport.cpp       | 15 +++
 mlir/lib/Target/LLVMIR/ModuleTranslation.cpp  | 12 +++
 mlir/test/Dialect/LLVMIR/func.mlir            | 19 ++++
 .../LLVMIR/Import/function-attributes.ll      | 66 +++++++++++++
 .../LLVMIR/fp-math-function-attributes.mlir   | 99 +++++++++++++++++++
 6 files changed, 214 insertions(+)

diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 3774bda05eb2b..54f38c93e5080 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1454,6 +1454,9 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
     OptionalAttr<BoolAttr>:$no_nans_fp_math,
     OptionalAttr<BoolAttr>:$approx_func_fp_math,
     OptionalAttr<BoolAttr>:$no_signed_zeros_fp_math,
+    OptionalAttr<StrAttr>:$denormal_fp_math,
+    OptionalAttr<StrAttr>:$denormal_fp_math_f32,
+    OptionalAttr<StrAttr>:$fp_contract,
     OptionalAttr<UnitAttr>:$no_inline,
     OptionalAttr<UnitAttr>:$always_inline,
     OptionalAttr<UnitAttr>:$optimize_none
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 9b917db5e7dfe..762221d98ec9e 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1676,6 +1676,9 @@ static constexpr std::array kExplicitAttributes{
     StringLiteral("alwaysinline"),
     StringLiteral("approx-func-fp-math"),
     StringLiteral("convergent"),
+    StringLiteral("denormal-fp-math"),
+    StringLiteral("denormal-fp-math-f32"),
+    StringLiteral("fp-contract"),
     StringLiteral("frame-pointer"),
     StringLiteral("no-infs-fp-math"),
     StringLiteral("no-nans-fp-math"),
@@ -1823,6 +1826,18 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
   if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
       attr.isStringAttribute())
     funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());
+
+  if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math");
+      attr.isStringAttribute())
+    funcOp.setDenormalFpMathAttr(StringAttr::get(context, attr.getValueAsString()));
+
+  if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math-f32");
+      attr.isStringAttribute())
+    funcOp.setDenormalFpMathF32Attr(StringAttr::get(context, attr.getValueAsString()));
+
+  if (llvm::Attribute attr = func->getFnAttribute("fp-contract");
+      attr.isStringAttribute())
+    funcOp.setFpContractAttr(StringAttr::get(context, attr.getValueAsString()));
 }
 
 DictionaryAttr
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 40196a5c760f9..f482c513290fd 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1351,6 +1351,18 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
     llvmFunc->addFnAttr("no-signed-zeros-fp-math",
                         llvm::toStringRef(*noSignedZerosFpMath));
 
+  if (auto denormalFpMath = func.getDenormalFpMath())
+    llvmFunc->addFnAttr("denormal-fp-math",
+                        *denormalFpMath);
+
+  if (auto denormalFpMathF32 = func.getDenormalFpMathF32())
+    llvmFunc->addFnAttr("denormal-fp-math-f32",
+                        *denormalFpMathF32);
+
+  if (auto fpContract = func.getFpContract())
+    llvmFunc->addFnAttr("fp-contract",
+                        *fpContract);
+
   // Add function attribute frame-pointer, if found.
   if (FramePointerKindAttr attr = func.getFramePointerAttr())
     llvmFunc->addFnAttr("frame-pointer",
diff --git a/mlir/test/Dialect/LLVMIR/func.mlir b/mlir/test/Dialect/LLVMIR/func.mlir
index d417942861940..e0810a23697f8 100644
--- a/mlir/test/Dialect/LLVMIR/func.mlir
+++ b/mlir/test/Dialect/LLVMIR/func.mlir
@@ -293,6 +293,25 @@ module {
     // CHECK-SAME: attributes {convergent}
     llvm.return
   }
+
+  llvm.func @denormal_fp_math_roundtrip() attributes {denormal_fp_math = "preserve-sign"} {
+    // CHECK: @denormal_fp_math_roundtrip
+    // CHECK-SAME: attributes {denormal_fp_math = "preserve-sign"}
+    llvm.return
+  }
+
+  llvm.func @denormal_fp_math_f32_roundtrip() attributes {denormal_fp_math_f32 = "preserve-sign"} {
+    // CHECK: @denormal_fp_math_f32_roundtrip
+    // CHECK-SAME: attributes {denormal_fp_math_f32 = "preserve-sign"}
+    llvm.return
+  }
+
+  llvm.func @fp_contract_roundtrip() attributes {fp_contract = "fast"} {
+    // CHECK: @fp_contract_roundtrip
+    // CHECK-SAME: attributes {fp_contract = "fast"}
+    llvm.return
+  }
+
 }
 
 // -----
diff --git a/mlir/test/Target/LLVMIR/Import/function-attributes.ll b/mlir/test/Target/LLVMIR/Import/function-attributes.ll
index 322ce6eadab4e..d87048950b3e4 100644
--- a/mlir/test/Target/LLVMIR/Import/function-attributes.ll
+++ b/mlir/test/Target/LLVMIR/Import/function-attributes.ll
@@ -344,6 +344,72 @@ declare void @func_attr_no_signed_zeros_fp_math_true() "no-signed-zeros-fp-math"
 ; CHECK-SAME: attributes {no_signed_zeros_fp_math = false}
 declare void @func_attr_no_signed_zeros_fp_math_false() "no-signed-zeros-fp-math"="false"
 
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_ieee
+; CHECK-SAME: attributes {denormal_fp_math = "ieee"}
+declare void @func_attr_denormal_fp_math_ieee() "denormal-fp-math"="ieee"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_preserve_sign
+; CHECK-SAME: attributes {denormal_fp_math = "preserve-sign"}
+declare void @func_attr_denormal_fp_math_preserve_sign() "denormal-fp-math"="preserve-sign"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_positive_zero
+; CHECK-SAME: attributes {denormal_fp_math = "positive-zero"}
+declare void @func_attr_denormal_fp_math_positive_zero() "denormal-fp-math"="positive-zero"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_dynamic
+; CHECK-SAME: attributes {denormal_fp_math = "dynamic"}
+declare void @func_attr_denormal_fp_math_dynamic() "denormal-fp-math"="dynamic"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_f32_ieee
+; CHECK-SAME: attributes {denormal_fp_math_f32 = "ieee"}
+declare void @func_attr_denormal_fp_math_f32_ieee() "denormal-fp-math-f32"="ieee"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_f32_preserve_sign
+; CHECK-SAME: attributes {denormal_fp_math_f32 = "preserve-sign"}
+declare void @func_attr_denormal_fp_math_f32_preserve_sign() "denormal-fp-math-f32"="preserve-sign"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_f32_positive_zero
+; CHECK-SAME: attributes {denormal_fp_math_f32 = "positive-zero"}
+declare void @func_attr_denormal_fp_math_f32_positive_zero() "denormal-fp-math-f32"="positive-zero"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_denormal_fp_math_f32_dynamic
+; CHECK-SAME: attributes {denormal_fp_math_f32 = "dynamic"}
+declare void @func_attr_denormal_fp_math_f32_dynamic() "denormal-fp-math-f32"="dynamic"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_fp_contract_fast
+; CHECK-SAME: attributes {fp_contract = "fast"}
+declare void @func_attr_fp_contract_fast() "fp-contract"="fast"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_fp_contract_on
+; CHECK-SAME: attributes {fp_contract = "on"}
+declare void @func_attr_fp_contract_on() "fp-contract"="on"
+
+; // -----
+
+; CHECK-LABEL: @func_attr_fp_contract_off
+; CHECK-SAME: attributes {fp_contract = "off"}
+declare void @func_attr_fp_contract_off() "fp-contract"="off"
+
 // -----
 
 ; CHECK-LABEL: @noinline_attribute
diff --git a/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir b/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir
index 4877c1137e3cd..cd14fdf86b0f5 100644
--- a/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir
+++ b/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir
@@ -87,3 +87,102 @@ llvm.func @no_signed_zeros_fp_math_func_false() attributes {no_signed_zeros_fp_m
   llvm.return
 }
 // CHECK: attributes #[[ATTRS]] = { "no-signed-zeros-fp-math"="false" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_func_ieee()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_func_ieee() attributes {denormal_fp_math = "ieee"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="ieee" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_func_preserve_sign()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_func_preserve_sign() attributes {denormal_fp_math = "preserve-sign"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="preserve-sign" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_func_positive_zero()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_func_positive_zero() attributes {denormal_fp_math = "positive-zero"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="positive-zero" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_func_dynamic()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_func_dynamic() attributes {denormal_fp_math = "dynamic"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="dynamic" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_f32_func_ieee()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_f32_func_ieee() attributes {denormal_fp_math_f32 = "ieee"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="ieee" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_f32_func_preserve_sign()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_f32_func_preserve_sign() attributes {denormal_fp_math_f32 = "preserve-sign"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="preserve-sign" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_f32_func_positive_zero()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_f32_func_positive_zero() attributes {denormal_fp_math_f32 = "positive-zero"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="positive-zero" }
+
+// -----
+
+// CHECK-LABEL: define void @denormal_fp_math_f32_func_dynamic()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @denormal_fp_math_f32_func_dynamic() attributes {denormal_fp_math_f32 = "dynamic"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="dynamic" }
+
+// -----
+
+// CHECK-LABEL: define void @fp_contract_func_fast()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @fp_contract_func_fast() attributes {fp_contract = "fast"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "fp-contract"="fast" }
+
+// -----
+
+// CHECK-LABEL: define void @fp_contract_func_on()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @fp_contract_func_on() attributes {fp_contract = "on"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "fp-contract"="on" }
+
+// -----
+
+// CHECK-LABEL: define void @fp_contract_func_off()
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @fp_contract_func_off() attributes {fp_contract = "off"}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { "fp-contract"="off" }



More information about the Mlir-commits mailing list