Skip to content

[mlir][emitc] Add EmitC lowering for arith.cmpi #88700

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

Merged
merged 1 commit into from
Apr 18, 2024
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
74 changes: 74 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,79 @@ class ArithConstantOpConversionPattern
}
};

class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
public:
using OpConversionPattern::OpConversionPattern;

bool needsUnsignedCmp(arith::CmpIPredicate pred) const {
switch (pred) {
case arith::CmpIPredicate::eq:
case arith::CmpIPredicate::ne:
case arith::CmpIPredicate::slt:
case arith::CmpIPredicate::sle:
case arith::CmpIPredicate::sgt:
case arith::CmpIPredicate::sge:
return false;
case arith::CmpIPredicate::ult:
case arith::CmpIPredicate::ule:
case arith::CmpIPredicate::ugt:
case arith::CmpIPredicate::uge:
return true;
}
llvm_unreachable("unknown cmpi predicate kind");
}

emitc::CmpPredicate toEmitCPred(arith::CmpIPredicate pred) const {
switch (pred) {
case arith::CmpIPredicate::eq:
return emitc::CmpPredicate::eq;
case arith::CmpIPredicate::ne:
return emitc::CmpPredicate::ne;
case arith::CmpIPredicate::slt:
case arith::CmpIPredicate::ult:
return emitc::CmpPredicate::lt;
case arith::CmpIPredicate::sle:
case arith::CmpIPredicate::ule:
return emitc::CmpPredicate::le;
case arith::CmpIPredicate::sgt:
case arith::CmpIPredicate::ugt:
return emitc::CmpPredicate::gt;
case arith::CmpIPredicate::sge:
case arith::CmpIPredicate::uge:
return emitc::CmpPredicate::ge;
}
llvm_unreachable("unknown cmpi predicate kind");
}

LogicalResult
matchAndRewrite(arith::CmpIOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {

Type type = adaptor.getLhs().getType();
if (!isa_and_nonnull<IntegerType, IndexType>(type)) {
return rewriter.notifyMatchFailure(op, "expected integer or index type");
}

bool needsUnsigned = needsUnsignedCmp(op.getPredicate());
emitc::CmpPredicate pred = toEmitCPred(op.getPredicate());
Type arithmeticType = type;
if (type.isUnsignedInteger() != needsUnsigned) {
arithmeticType = rewriter.getIntegerType(type.getIntOrFloatBitWidth(),
/*isSigned=*/!needsUnsigned);
}
Value lhs = adaptor.getLhs();
Value rhs = adaptor.getRhs();
if (arithmeticType != type) {
lhs = rewriter.template create<emitc::CastOp>(op.getLoc(), arithmeticType,
lhs);
rhs = rewriter.template create<emitc::CastOp>(op.getLoc(), arithmeticType,
rhs);
}
rewriter.replaceOpWithNewOp<emitc::CmpOp>(op, op.getType(), pred, lhs, rhs);
return success();
}
};

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

// -----

func.func @arith_cmpi_eq(%arg0: i32, %arg1: i32) -> i1 {
// CHECK-LABEL: arith_cmpi_eq
// CHECK-SAME: ([[Arg0:[^ ]*]]: i32, [[Arg1:[^ ]*]]: i32)
// CHECK-DAG: [[EQ:[^ ]*]] = emitc.cmp eq, [[Arg0]], [[Arg1]] : (i32, i32) -> i1
%eq = arith.cmpi eq, %arg0, %arg1 : i32
// CHECK: return [[EQ]]
return %eq: i1
}

func.func @arith_cmpi_ult(%arg0: i32, %arg1: i32) -> i1 {
// CHECK-LABEL: arith_cmpi_ult
// CHECK-SAME: ([[Arg0:[^ ]*]]: i32, [[Arg1:[^ ]*]]: i32)
// CHECK-DAG: [[CastArg0:[^ ]*]] = emitc.cast [[Arg0]] : i32 to ui32
// CHECK-DAG: [[CastArg1:[^ ]*]] = emitc.cast [[Arg1]] : i32 to ui32
// CHECK-DAG: [[ULT:[^ ]*]] = emitc.cmp lt, [[CastArg0]], [[CastArg1]] : (ui32, ui32) -> i1
%ult = arith.cmpi ult, %arg0, %arg1 : i32

// CHECK: return [[ULT]]
return %ult: i1
}

func.func @arith_cmpi_predicates(%arg0: i32, %arg1: i32) {
// CHECK: emitc.cmp lt, {{.*}} : (ui32, ui32) -> i1
%ult = arith.cmpi ult, %arg0, %arg1 : i32
// CHECK: emitc.cmp lt, {{.*}} : (i32, i32) -> i1
%slt = arith.cmpi slt, %arg0, %arg1 : i32
// CHECK: emitc.cmp le, {{.*}} : (ui32, ui32) -> i1
%ule = arith.cmpi ule, %arg0, %arg1 : i32
// CHECK: emitc.cmp le, {{.*}} : (i32, i32) -> i1
%sle = arith.cmpi sle, %arg0, %arg1 : i32
// CHECK: emitc.cmp gt, {{.*}} : (ui32, ui32) -> i1
%ugt = arith.cmpi ugt, %arg0, %arg1 : i32
// CHECK: emitc.cmp gt, {{.*}} : (i32, i32) -> i1
%sgt = arith.cmpi sgt, %arg0, %arg1 : i32
// CHECK: emitc.cmp ge, {{.*}} : (ui32, ui32) -> i1
%uge = arith.cmpi uge, %arg0, %arg1 : i32
// CHECK: emitc.cmp ge, {{.*}} : (i32, i32) -> i1
%sge = arith.cmpi sge, %arg0, %arg1 : i32
// CHECK: emitc.cmp eq, {{.*}} : (i32, i32) -> i1
%eq = arith.cmpi eq, %arg0, %arg1 : i32
// CHECK: emitc.cmp ne, {{.*}} : (i32, i32) -> i1
%ne = arith.cmpi ne, %arg0, %arg1 : i32

return
}