@@ -443,6 +443,46 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
443
443
}
444
444
};
445
445
446
+ template <typename ArithOp, typename EmitCOp, bool booleansLegal>
447
+ class BitwiseOpConversion final : public OpConversionPattern<ArithOp> {
448
+ public:
449
+ using OpConversionPattern<ArithOp>::OpConversionPattern;
450
+
451
+ LogicalResult
452
+ matchAndRewrite (ArithOp op, typename ArithOp::Adaptor adaptor,
453
+ ConversionPatternRewriter &rewriter) const override {
454
+
455
+ Type type = this ->getTypeConverter ()->convertType (op.getType ());
456
+ if (!isa_and_nonnull<IntegerType, emitc::SignedSizeTType, emitc::SizeTType>(
457
+ type)) {
458
+ return rewriter.notifyMatchFailure (
459
+ op, " expected integer or size_t/ssize_t type" );
460
+ }
461
+
462
+ if (type.isInteger (1 )) {
463
+ if (!booleansLegal)
464
+ return rewriter.notifyMatchFailure (op, " i1 type is not implemented" );
465
+
466
+ rewriter.replaceOpWithNewOp <EmitCOp>(op, type, adaptor.getLhs (),
467
+ adaptor.getRhs ());
468
+ return success ();
469
+ }
470
+
471
+ Type arithmeticType = adaptIntegralTypeSignedness (type, true );
472
+
473
+ Value lhs = adaptValueType (adaptor.getLhs (), rewriter, arithmeticType);
474
+ Value rhs = adaptValueType (adaptor.getRhs (), rewriter, arithmeticType);
475
+
476
+ Value arithmeticResult = rewriter.template create <EmitCOp>(
477
+ op.getLoc (), arithmeticType, lhs, rhs);
478
+
479
+ Value result = adaptValueType (arithmeticResult, rewriter, type);
480
+
481
+ rewriter.replaceOp (op, result);
482
+ return success ();
483
+ }
484
+ };
485
+
446
486
class SelectOpConversion : public OpConversionPattern <arith::SelectOp> {
447
487
public:
448
488
using OpConversionPattern<arith::SelectOp>::OpConversionPattern;
@@ -581,6 +621,11 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
581
621
IntegerOpConversion<arith::AddIOp, emitc::AddOp>,
582
622
IntegerOpConversion<arith::MulIOp, emitc::MulOp>,
583
623
IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
624
+ BitwiseOpConversion<arith::AndIOp, emitc::BitwiseAndOp, true >,
625
+ BitwiseOpConversion<arith::OrIOp, emitc::BitwiseOrOp, true >,
626
+ BitwiseOpConversion<arith::XOrIOp, emitc::BitwiseXorOp, true >,
627
+ BitwiseOpConversion<arith::ShLIOp, emitc::BitwiseLeftShiftOp, false >,
628
+ BitwiseOpConversion<arith::ShRSIOp, emitc::BitwiseRightShiftOp, false >,
584
629
CmpFOpConversion,
585
630
CmpIOpConversion,
586
631
SelectOpConversion,
0 commit comments