Skip to content
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

arith-to-emitc: cmpf #142

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@ class ArithConstantOpConversionPattern
}
};

/// Return an operation that returns true (in i1) when operand is NaN.
emitc::CmpOp isNan(ConversionPatternRewriter &rewriter, Location loc,
Value operand) {
// A value is NaN exactly when it compares unequal to itself.
return rewriter.create<emitc::CmpOp>(
loc, rewriter.getI1Type(), emitc::CmpPredicate::ne, operand, operand);
}

class CmpFOpConversion : public OpConversionPattern<arith::CmpFOp> {
public:
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(arith::CmpFOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
emitc::CmpPredicate predicate;
switch (op.getPredicate()) {
case arith::CmpFPredicate::UGT:
// unordered or greater than
predicate = emitc::CmpPredicate::gt;
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved
break;
case arith::CmpFPredicate::ULT:
// unordered or less than
predicate = emitc::CmpPredicate::lt;
break;
case arith::CmpFPredicate::UNO: {
// unordered, i.e. either operand is nan
auto lhsIsNan = isNan(rewriter, op.getLoc(), adaptor.getLhs());
if (adaptor.getLhs() == adaptor.getRhs()) {
rewriter.replaceOp(op, lhsIsNan);
return success();
}
auto rhsIsNan = isNan(rewriter, op.getLoc(), adaptor.getRhs());
rewriter.replaceOpWithNewOp<arith::OrIOp>(op, op.getType(), lhsIsNan,
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved
rhsIsNan);
return success();
}
default:
return rewriter.notifyMatchFailure(op.getLoc(),
"cannot match predicate ");
}

rewriter.replaceOpWithNewOp<emitc::CmpOp>(
op, op.getType(), predicate, adaptor.getLhs(), adaptor.getRhs());
return success();
}
};

template <typename ArithOp, typename EmitCOp>
class ArithOpConversion final : public OpConversionPattern<ArithOp> {
public:
Expand Down Expand Up @@ -99,6 +147,7 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
ArithOpConversion<arith::AddIOp, emitc::AddOp>,
ArithOpConversion<arith::MulIOp, emitc::MulOp>,
ArithOpConversion<arith::SubIOp, emitc::SubOp>,
CmpFOpConversion,
SelectOpConversion
>(typeConverter, ctx);
// clang-format on
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ func.func @arith_select(%arg0: i1, %arg1: tensor<8xi32>, %arg2: tensor<8xi32>) -
%0 = arith.select %arg0, %arg1, %arg2 : i1, tensor<8xi32>
return
}

// -----

func.func @cmpf(%arg0: f32, %arg1: f32) {
// CHECK: emitc.cmp gt, %arg0, %arg1 : (f32, f32) -> i1
%0 = arith.cmpf ugt, %arg0, %arg1 : f32
// CHECK: emitc.cmp lt, %arg0, %arg1 : (f32, f32) -> i1
%1 = arith.cmpf ult, %arg0, %arg1 : f32
// CHECK: emitc.cmp ne, %arg0, %arg0 : (f32, f32) -> i1
%2 = arith.cmpf uno, %arg0, %arg0 : f32

return
}
Loading