[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
Fri Jul 5 03:32:02 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: None (runseny)
<details>
<summary>Changes</summary>
Adds `denormal-fp-math-f32`, `denormal-fp-math`, `fp-contract` to llvmFuncOp attributes.
`denormal-fp-math-f32` and `denormal-fp-math` can enable the ftz, that is , flushing denormal to zero.
`fp-contract` can enable the fma fusion such as `mul + add -> fma`
---
Full diff: https://github.com/llvm/llvm-project/pull/97812.diff
6 Files Affected:
- (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+4-1)
- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+15)
- (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+12)
- (modified) mlir/test/Dialect/LLVMIR/func.mlir (+19)
- (modified) mlir/test/Target/LLVMIR/Import/function-attributes.ll (+66)
- (modified) mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir (+99)
``````````diff
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 3774bda05eb2be..373847265a5d34 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1456,7 +1456,10 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
OptionalAttr<BoolAttr>:$no_signed_zeros_fp_math,
OptionalAttr<UnitAttr>:$no_inline,
OptionalAttr<UnitAttr>:$always_inline,
- OptionalAttr<UnitAttr>:$optimize_none
+ OptionalAttr<UnitAttr>:$optimize_none,
+ OptionalAttr<StrAttr>:$denormal_fp_math,
+ OptionalAttr<StrAttr>:$denormal_fp_math_f32,
+ OptionalAttr<StrAttr>:$fp_contract
);
let regions = (region AnyRegion:$body);
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 9b917db5e7dfe3..8c99452cf43b78 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1685,6 +1685,9 @@ static constexpr std::array kExplicitAttributes{
StringLiteral("target-features"),
StringLiteral("unsafe-fp-math"),
StringLiteral("vscale_range"),
+ StringLiteral("denormal-fp-math"),
+ StringLiteral("denormal-fp-math-f32"),
+ StringLiteral("fp-contract"),
};
static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp) {
@@ -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..457e03383a0c62 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..d87048950b3e4e 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 4877c1137e3cd7..cd14fdf86b0f53 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" }
``````````
</details>
https://github.com/llvm/llvm-project/pull/97812
More information about the Mlir-commits
mailing list