Skip to content

Commit

Permalink
[mlir][emitc] Implement arith.negf to EmitC
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaAMD committed Jun 5, 2024
1 parent 9db9dd7 commit 30ce428
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,29 @@ 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<FloatType>(adaptedOpType)) {
return rewriter.notifyMatchFailure(op.getLoc(),
"negf currently only supported on "
"floats, not tensors/vectors thereof");
}

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

template <typename ArithOp, bool castToUnsigned>
class CastConversion : public OpConversionPattern<ArithOp> {
public:
Expand Down Expand Up @@ -716,6 +739,7 @@ void mlir::populateArithToEmitCPatterns(RewritePatternSet &patterns,
UnsignedShiftOpConversion<arith::ShRUIOp, emitc::BitwiseRightShiftOp>,
CmpFOpConversion,
CmpIOpConversion,
NegFOpConversion,
SelectOpConversion,
// Truncation is guaranteed for unsigned types.
UnsignedCastConversion<arith::TruncIOp>,
Expand Down
16 changes: 16 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,22 @@ func.func @arith_cast_fptoui_i1(%arg0: f32) -> i1 {

// -----

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_shli_i1(%arg0: i1, %arg1: i1) {
// expected-error @+1 {{failed to legalize operation 'arith.shli'}}
%shli = arith.shli %arg0, %arg1 : i1
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ func.func @arith_cmpi_index(%arg0: i32, %arg1: i32) -> i1 {
return %slt: i1
}

// -----

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
}

// -----

Expand Down

0 comments on commit 30ce428

Please sign in to comment.