Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Pass/Pass.h"

#include "llvm/ADT/FloatingPointMode.h"

namespace mlir {
#define GEN_PASS_DEF_CONVERTMATHTOLLVMPASS
#include "mlir/Conversion/Passes.h.inc"
Expand Down Expand Up @@ -286,6 +288,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(), llvm::fcNan);
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(), llvm::fcFinite);
return success();
}
};

struct ConvertMathToLLVMPass
: public impl::ConvertMathToLLVMPassBase<ConvertMathToLLVMPass> {
using Base::Base;
Expand All @@ -309,6 +345,8 @@ void mlir::populateMathToLLVMConversionPatterns(
patterns.add<Log1pOpLowering>(converter, benefit);
// clang-format off
patterns.add<
IsNaNOpLowering,
IsFiniteOpLowering,
AbsFOpLowering,
AbsIOpLowering,
CeilOpLowering,
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading