-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
[MLIR][Math] Add lowering for isnan and isfinite #128125
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir Author: William Moses (wsmoses) ChangesFull diff: https://github.com/llvm/llvm-project/pull/128125.diff 2 Files Affected:
diff --git a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
index 85ec288268aeb..fe0d2a6ae9747 100644
--- a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
+++ b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
@@ -286,6 +286,40 @@ struct RsqrtOpLowering : public ConvertOpToLLVMPattern<math::RsqrtOp> {
}
};
+struct IsNaNOpLowering : public ConvertOpToLLVMPattern<math::IsNaNOp> {
+ using ConvertOpToLLVMPattern<math::IsNaNOp>::ConvertOpToLLVMPattern;
+
+ LogicalResult
+ matchAndRewrite(math::IsNaNOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ auto operandType = adaptor.getOperand().getType();
+
+ if (!operandType || !LLVM::isCompatibleType(operandType))
+ return failure();
+
+ rewriter.replaceOpWithNewOp<LLVM::IsFPClass>(op, op.getType(),
+ adaptor.getOperand(), 3);
+ return success();
+ }
+};
+
+struct IsFiniteOpLowering : public ConvertOpToLLVMPattern<math::IsFiniteOp> {
+ using ConvertOpToLLVMPattern<math::IsFiniteOp>::ConvertOpToLLVMPattern;
+
+ LogicalResult
+ matchAndRewrite(math::IsFiniteOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ auto operandType = adaptor.getOperand().getType();
+
+ if (!operandType || !LLVM::isCompatibleType(operandType))
+ return failure();
+
+ rewriter.replaceOpWithNewOp<LLVM::IsFPClass>(op, op.getType(),
+ adaptor.getOperand(), 504);
+ return success();
+ }
+};
+
struct ConvertMathToLLVMPass
: public impl::ConvertMathToLLVMPassBase<ConvertMathToLLVMPass> {
using Base::Base;
@@ -307,6 +341,8 @@ void mlir::populateMathToLLVMConversionPatterns(
bool approximateLog1p, PatternBenefit benefit) {
if (approximateLog1p)
patterns.add<Log1pOpLowering>(converter, benefit);
+ patterns.add<IsNaNOpLowering, IsFiniteOpLowering>(converter);
+
// clang-format off
patterns.add<
AbsFOpLowering,
diff --git a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
index 45a37af293890..974743a55932b 100644
--- a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
+++ b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
@@ -263,6 +263,26 @@ func.func @ctpop_scalable_vector(%arg0 : vector<[4]xi32>) -> vector<[4]xi32> {
// -----
+// CHECK-LABEL: func @isnan_double(
+// CHECK-SAME: f64
+func.func @isnan_double(%arg0 : f64) {
+ // CHECK: "llvm.intr.is.fpclass"(%arg0) <{bit = 3 : i32}> : (f64) -> i1
+ %0 = math.isnan %arg0 : f64
+ func.return
+}
+
+// -----
+
+// CHECK-LABEL: func @isfinite_double(
+// CHECK-SAME: f64
+func.func @isfinite_double(%arg0 : f64) {
+ // CHECK: "llvm.intr.is.fpclass"(%arg0) <{bit = 504 : i32}> : (f64) -> i1
+ %0 = math.isfinite %arg0 : f64
+ func.return
+}
+
+// -----
+
// CHECK-LABEL: func @rsqrt_double(
// CHECK-SAME: f64
func.func @rsqrt_double(%arg0 : f64) {
|
ivanradanov
reviewed
Feb 21, 2025
Co-authored-by: Ivan R. Ivanov <[email protected]>
9bd7837
to
37acb20
Compare
ivanradanov
approved these changes
Feb 21, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.