-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][LLVM] Add ftz and fuse FP ops related function attribute support #97812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: None (runseny) ChangesAdds
Full diff: https://github.com/llvm/llvm-project/pull/97812.diff 6 Files Affected:
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" }
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I added some mostly nit comments.
|
||
; 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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these are string attributes there is probably not much value in testing different string values for every attribute (i.e. ieee, preserve-sign)? Or is there specific reason for testing the different strings? Instead I would have a test for every attribute with possible different strings? So three tests here and three tests in the file below which exercises the lowering to LLVM IR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or is there specific reason for testing the different strings?
These different strings are the general cmd options in codegen such as denormalFpMath related and fp-contract. So i tested all these general cmd options for functional completeness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! I saw there is already prior art above.
I think the point I tried to make is that testing different values would make a lot of sense if the stored value would be an enum. Then we want to test that all enum values exist and are handled properly. In this case, we store a string so if one works all other strings should work as well (there are no separate code paths for different enum values). For maintenance reasons, I would thus keep the test set minimal if there is no extra functionality (in the sense of code coverage) that is tested.
@@ -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"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you sort the array alphabetically. It looks like it was sorted before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, i have sorted it.
@@ -1351,6 +1351,18 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { | |||
llvmFunc->addFnAttr("no-signed-zeros-fp-math", | |||
llvm::toStringRef(*noSignedZerosFpMath)); | |||
|
|||
if (auto DenormalFPMath = func.getDenormalFpMath()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (auto DenormalFPMath = func.getDenormalFpMath()) | |
if (auto denormalFPMath = func.getDenormalFpMath()) |
nit: the variable names should start lowercase (below as well).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, modified. The diff please see: diff, please take a look again :)
OptionalAttr<UnitAttr>:$optimize_none, | ||
OptionalAttr<StrAttr>:$denormal_fp_math, | ||
OptionalAttr<StrAttr>:$denormal_fp_math_f32, | ||
OptionalAttr<StrAttr>:$fp_contract |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Are these flags related to the other fp_math flags above? Would it make sense to group them together?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, these flags are related to fp_math, so i grouped these flags with other fastMath flags.
c296c22
to
6938bf8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for addressing the comments! I still think there is some redundancy in terms of test cases. Feel free to trim them down if you think my argument makes sense, or otherwise keep them :).
|
||
; 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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! I saw there is already prior art above.
I think the point I tried to make is that testing different values would make a lot of sense if the stored value would be an enum. Then we want to test that all enum values exist and are handled properly. In this case, we store a string so if one works all other strings should work as well (there are no separate code paths for different enum values). For maintenance reasons, I would thus keep the test set minimal if there is no extra functionality (in the sense of code coverage) that is tested.
Yeah, you are right, thanks for this suggestion. Now i only test one string for each attribute. @gysit |
@zero9178 Hi, i still need an extra approval, Could you help take a look? |
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super familiar with these fp functions, but given it mirrors LLVM, its their mess to figure and first and foremost 🙂
LGTM with clang-format addressed. Feel free to ping if no one merged this in a day once CI is green.
Given that some of these only have a finite set of limited values afaik, we could turn these into an enum attribute in the future. But this can be a future iteration.
@zero9178 Hi, i still need an extra approval, Could you help take a look?
FYI, there is no such general rule in LLVM that you need to have more than one approval, although you're of course free to get more eyes on the review 🙂! Sometimes the first reviewer will ask you for either more time before landing or another approval if they do not feel comfortable being the only approval, but that is not applicable here.
Thanks for your detail explanation. Now, the CI of this mr passed, could you help merge it? thanks. @zero9178 |
@runseny Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…rt (llvm#97812) 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`
Adds
denormal-fp-math-f32
,denormal-fp-math
,fp-contract
to llvmFuncOp attributes.denormal-fp-math-f32
anddenormal-fp-math
can enable the ftz, that is , flushing denormal to zero.fp-contract
can enable the fma fusion such asmul + add -> fma