Skip to content

[mlir][emitc] arith.negf to EmitC conversion #95372

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 5 commits into from
Jun 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
29 changes: 29 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,34 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
}
};

class NegFOpConversion : public OpConversionPattern<arith::NegFOp> {
public:
using OpConversionPattern::OpConversionPattern;

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

auto adaptedOp = adaptor.getOperand();
auto adaptedOpType = adaptedOp.getType();

if (isa<TensorType>(adaptedOpType) || isa<VectorType>(adaptedOpType)) {
return rewriter.notifyMatchFailure(
op.getLoc(),
"negf currently only supports scalar types, not vectors or tensors");
}

if (!emitc::isSupportedFloatType(adaptedOpType)) {
return rewriter.notifyMatchFailure(
op.getLoc(), "floating-point type is not supported by EmitC");
}

rewriter.replaceOpWithNewOp<emitc::UnaryMinusOp>(op, adaptedOpType,
adaptedOp);
return success();
}
};

template <typename ArithOp, bool castToUnsigned>
class CastConversion : public OpConversionPattern<ArithOp> {
public:
Expand Down Expand Up @@ -621,6 +649,7 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
BitwiseOpConversion<arith::XOrIOp, emitc::BitwiseXorOp>,
CmpFOpConversion,
CmpIOpConversion,
NegFOpConversion,
SelectOpConversion,
// Truncation is guaranteed for unsigned types.
UnsignedCastConversion<arith::TruncIOp>,
Expand Down
24 changes: 24 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ func.func @arith_cmpf_tensor(%arg0: tensor<5xf32>, %arg1: tensor<5xf32>) -> tens

// -----

func.func @arith_negf_f80(%arg0: f80) -> f80 {
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
%n = arith.negf %arg0 : f80
return %n: f80
}

// -----

func.func @arith_negf_tensor(%arg0: tensor<5xf32>) -> tensor<5xf32> {
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
%n = arith.negf %arg0 : tensor<5xf32>
return %n: tensor<5xf32>
}

// -----

func.func @arith_negf_vector(%arg0: vector<5xf32>) -> vector<5xf32> {
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
%n = arith.negf %arg0 : vector<5xf32>
return %n: vector<5xf32>
}

// -----

func.func @arith_extsi_i1_to_i32(%arg0: i1) {
// expected-error @+1 {{failed to legalize operation 'arith.extsi'}}
%idx = arith.extsi %arg0 : i1 to i32
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,17 @@ func.func @arith_cmpi_predicates(%arg0: i32, %arg1: i32) {

// -----

func.func @arith_negf(%arg0: f32) -> f32 {
// CHECK-LABEL: arith_negf
// CHECK-SAME: %[[Arg0:[^ ]*]]: f32
// CHECK: %[[N:[^ ]*]] = emitc.unary_minus %[[Arg0]] : (f32) -> f32
%n = arith.negf %arg0 : f32
// CHECK: return %[[N]]
return %n: f32
}

// -----

func.func @arith_float_to_int_cast_ops(%arg0: f32, %arg1: f64) {
// CHECK: emitc.cast %arg0 : f32 to i32
%0 = arith.fptosi %arg0 : f32 to i32
Expand Down
Loading