[Mlir-commits] [mlir] [mlir][emitc] Lower arith.andi, arith.ori, arith.xori to EmitC (PR #93666)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed May 29 03:56:55 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-emitc
Author: Corentin Ferry (cferry-AMD)
<details>
<summary>Changes</summary>
These arith ops have a pretty straightforward lowering to EmitC. This PR also brings in `adaptValueType` and `adaptIntegralTypeSignedness` that other ArithToEmitC functions can benefit from.
---
Full diff: https://github.com/llvm/llvm-project/pull/93666.diff
2 Files Affected:
- (modified) mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp (+66)
- (modified) mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir (+38)
``````````diff
diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
index 388794ec122d2..b40ff00390882 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
@@ -40,6 +40,29 @@ class ArithConstantOpConversionPattern
}
};
+/// Check if the signedness of type \p ty matches the expected
+/// signedness, and issue a type with the correct signedness if
+/// necessary.
+Type adaptIntegralTypeSignedness(Type ty, bool needsUnsigned) {
+ if (isa<IntegerType>(ty)) {
+ // Turns signless integers into signed integers.
+ if (ty.isUnsignedInteger() != needsUnsigned) {
+ auto signedness = needsUnsigned
+ ? IntegerType::SignednessSemantics::Unsigned
+ : IntegerType::SignednessSemantics::Signed;
+ return IntegerType::get(ty.getContext(), ty.getIntOrFloatBitWidth(),
+ signedness);
+ }
+ }
+ return ty;
+}
+
+/// Insert a cast operation to type \p ty if \p val
+/// does not have this type.
+Value adaptValueType(Value val, ConversionPatternRewriter &rewriter, Type ty) {
+ return rewriter.createOrFold<emitc::CastOp>(val.getLoc(), ty, val);
+}
+
class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
public:
using OpConversionPattern::OpConversionPattern;
@@ -265,6 +288,46 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
}
};
+template <typename ArithOp, typename EmitCOp>
+class BitwiseOpConversion : public OpConversionPattern<ArithOp> {
+public:
+ using OpConversionPattern<ArithOp>::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+
+ Type type = this->getTypeConverter()->convertType(op.getType());
+ if (!isa_and_nonnull<IntegerType>(type)) {
+ return rewriter.notifyMatchFailure(
+ op,
+ "expected integer type, vector/tensor support not yet implemented");
+ }
+
+ // Bitwise ops can be performed directly on booleans
+ if (type.isInteger(1)) {
+ rewriter.replaceOpWithNewOp<EmitCOp>(op, type, adaptor.getLhs(),
+ adaptor.getRhs());
+ return success();
+ }
+
+ // Bitwise ops are defined by the C standard on unsigned operands.
+ Type arithmeticType =
+ adaptIntegralTypeSignedness(type, /*needsUnsigned=*/true);
+
+ Value lhs = adaptValueType(adaptor.getLhs(), rewriter, arithmeticType);
+ Value rhs = adaptValueType(adaptor.getRhs(), rewriter, arithmeticType);
+
+ Value arithmeticResult = rewriter.template create<EmitCOp>(
+ op.getLoc(), arithmeticType, lhs, rhs);
+
+ Value result = adaptValueType(arithmeticResult, rewriter, type);
+
+ rewriter.replaceOp(op, result);
+ return success();
+ }
+};
+
class SelectOpConversion : public OpConversionPattern<arith::SelectOp> {
public:
using OpConversionPattern<arith::SelectOp>::OpConversionPattern;
@@ -401,6 +464,9 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
IntegerOpConversion<arith::AddIOp, emitc::AddOp>,
IntegerOpConversion<arith::MulIOp, emitc::MulOp>,
IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
+ BitwiseOpConversion<arith::AndIOp, emitc::BitwiseAndOp>,
+ BitwiseOpConversion<arith::OrIOp, emitc::BitwiseOrOp>,
+ BitwiseOpConversion<arith::XOrIOp, emitc::BitwiseXorOp>,
CmpIOpConversion,
SelectOpConversion,
// Truncation is guaranteed for unsigned types.
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
index dac3fd99b607c..e34d93c20bb70 100644
--- a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
@@ -88,6 +88,44 @@ func.func @arith_index(%arg0: index, %arg1: index) {
// -----
+// CHECK-LABEL: arith_bitwise
+// CHECK-SAME: %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
+func.func @arith_bitwise(%arg0: i32, %arg1: i32) {
+ // CHECK: %[[C1:[^ ]*]] = emitc.cast %[[ARG0]] : i32 to ui32
+ // CHECK: %[[C2:[^ ]*]] = emitc.cast %[[ARG1]] : i32 to ui32
+ // CHECK: %[[AND:[^ ]*]] = emitc.bitwise_and %[[C1]], %[[C2]] : (ui32, ui32) -> ui32
+ // CHECK: %[[C3:[^ ]*]] = emitc.cast %[[AND]] : ui32 to i32
+ %5 = arith.andi %arg0, %arg1 : i32
+ // CHECK: %[[C1:[^ ]*]] = emitc.cast %[[ARG0]] : i32 to ui32
+ // CHECK: %[[C2:[^ ]*]] = emitc.cast %[[ARG1]] : i32 to ui32
+ // CHECK: %[[OR:[^ ]*]] = emitc.bitwise_or %[[C1]], %[[C2]] : (ui32, ui32) -> ui32
+ // CHECK: %[[C3:[^ ]*]] = emitc.cast %[[OR]] : ui32 to i32
+ %6 = arith.ori %arg0, %arg1 : i32
+ // CHECK: %[[C1:[^ ]*]] = emitc.cast %[[ARG0]] : i32 to ui32
+ // CHECK: %[[C2:[^ ]*]] = emitc.cast %[[ARG1]] : i32 to ui32
+ // CHECK: %[[XOR:[^ ]*]] = emitc.bitwise_xor %[[C1]], %[[C2]] : (ui32, ui32) -> ui32
+ // CHECK: %[[C3:[^ ]*]] = emitc.cast %[[XOR]] : ui32 to i32
+ %7 = arith.xori %arg0, %arg1 : i32
+
+ return
+}
+
+// -----
+
+// CHECK-LABEL: arith_bitwise_bool
+func.func @arith_bitwise_bool(%arg0: i1, %arg1: i1) {
+ // CHECK: %[[AND:[^ ]*]] = emitc.bitwise_and %arg0, %arg1 : (i1, i1) -> i1
+ %5 = arith.andi %arg0, %arg1 : i1
+ // CHECK: %[[OR:[^ ]*]] = emitc.bitwise_or %arg0, %arg1 : (i1, i1) -> i1
+ %6 = arith.ori %arg0, %arg1 : i1
+ // CHECK: %[[xor:[^ ]*]] = emitc.bitwise_xor %arg0, %arg1 : (i1, i1) -> i1
+ %7 = arith.xori %arg0, %arg1 : i1
+
+ return
+}
+
+// -----
+
// CHECK-LABEL: arith_signed_integer_div_rem
func.func @arith_signed_integer_div_rem(%arg0: i32, %arg1: i32) {
// CHECK: emitc.div %arg0, %arg1 : (i32, i32) -> i32
``````````
</details>
https://github.com/llvm/llvm-project/pull/93666
More information about the Mlir-commits
mailing list