Skip to content

Commit 6e1751c

Browse files
TinaAMDAlexisPerry
authored andcommitted
[mlir][emitc] arith.negf to EmitC conversion (llvm#95372)
Lower arith.negf to the unary minus in EmitC.
1 parent f67b76c commit 6e1751c

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,34 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
288288
}
289289
};
290290

291+
class NegFOpConversion : public OpConversionPattern<arith::NegFOp> {
292+
public:
293+
using OpConversionPattern::OpConversionPattern;
294+
295+
LogicalResult
296+
matchAndRewrite(arith::NegFOp op, OpAdaptor adaptor,
297+
ConversionPatternRewriter &rewriter) const override {
298+
299+
auto adaptedOp = adaptor.getOperand();
300+
auto adaptedOpType = adaptedOp.getType();
301+
302+
if (isa<TensorType>(adaptedOpType) || isa<VectorType>(adaptedOpType)) {
303+
return rewriter.notifyMatchFailure(
304+
op.getLoc(),
305+
"negf currently only supports scalar types, not vectors or tensors");
306+
}
307+
308+
if (!emitc::isSupportedFloatType(adaptedOpType)) {
309+
return rewriter.notifyMatchFailure(
310+
op.getLoc(), "floating-point type is not supported by EmitC");
311+
}
312+
313+
rewriter.replaceOpWithNewOp<emitc::UnaryMinusOp>(op, adaptedOpType,
314+
adaptedOp);
315+
return success();
316+
}
317+
};
318+
291319
template <typename ArithOp, bool castToUnsigned>
292320
class CastConversion : public OpConversionPattern<ArithOp> {
293321
public:
@@ -621,6 +649,7 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
621649
BitwiseOpConversion<arith::XOrIOp, emitc::BitwiseXorOp>,
622650
CmpFOpConversion,
623651
CmpIOpConversion,
652+
NegFOpConversion,
624653
SelectOpConversion,
625654
// Truncation is guaranteed for unsigned types.
626655
UnsignedCastConversion<arith::TruncIOp>,

mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ func.func @arith_cmpf_tensor(%arg0: tensor<5xf32>, %arg1: tensor<5xf32>) -> tens
8181

8282
// -----
8383

84+
func.func @arith_negf_f80(%arg0: f80) -> f80 {
85+
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
86+
%n = arith.negf %arg0 : f80
87+
return %n: f80
88+
}
89+
90+
// -----
91+
92+
func.func @arith_negf_tensor(%arg0: tensor<5xf32>) -> tensor<5xf32> {
93+
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
94+
%n = arith.negf %arg0 : tensor<5xf32>
95+
return %n: tensor<5xf32>
96+
}
97+
98+
// -----
99+
100+
func.func @arith_negf_vector(%arg0: vector<5xf32>) -> vector<5xf32> {
101+
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
102+
%n = arith.negf %arg0 : vector<5xf32>
103+
return %n: vector<5xf32>
104+
}
105+
106+
// -----
107+
84108
func.func @arith_extsi_i1_to_i32(%arg0: i1) {
85109
// expected-error @+1 {{failed to legalize operation 'arith.extsi'}}
86110
%idx = arith.extsi %arg0 : i1 to i32

mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,17 @@ func.func @arith_cmpi_predicates(%arg0: i32, %arg1: i32) {
422422

423423
// -----
424424

425+
func.func @arith_negf(%arg0: f32) -> f32 {
426+
// CHECK-LABEL: arith_negf
427+
// CHECK-SAME: %[[Arg0:[^ ]*]]: f32
428+
// CHECK: %[[N:[^ ]*]] = emitc.unary_minus %[[Arg0]] : (f32) -> f32
429+
%n = arith.negf %arg0 : f32
430+
// CHECK: return %[[N]]
431+
return %n: f32
432+
}
433+
434+
// -----
435+
425436
func.func @arith_float_to_int_cast_ops(%arg0: f32, %arg1: f64) {
426437
// CHECK: emitc.cast %arg0 : f32 to i32
427438
%0 = arith.fptosi %arg0 : f32 to i32

0 commit comments

Comments
 (0)