[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
Mon Jul 8 01:27:30 PDT 2024


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

>From 8af308feef2237d7a8183a4192a0b84d0b8914bf 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      | 18 +++++++++++++
 .../LLVMIR/fp-math-function-attributes.mlir   | 27 +++++++++++++++++++
 6 files changed, 94 insertions(+)

diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 3774bda05eb2be..54f38c93e50808 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 9b917db5e7dfe3..762221d98ec9eb 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 40196a5c760f96..f482c513290fdd 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 d417942861940b..e0810a23697f8a 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 322ce6eadab4e4..9ca6f62fd0e2d6 100644
--- a/mlir/test/Target/LLVMIR/Import/function-attributes.ll
+++ b/mlir/test/Target/LLVMIR/Import/function-attributes.ll
@@ -344,6 +344,24 @@ 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_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_fp_contract_fast
+; CHECK-SAME: attributes {fp_contract = "fast"}
+declare void @func_attr_fp_contract_fast() "fp-contract"="fast"
+
 // -----
 
 ; 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 4877c1137e3cd7..673cbd8b87828d 100644
--- a/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir
+++ b/mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir
@@ -87,3 +87,30 @@ 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_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 @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" }



More information about the Mlir-commits mailing list