[Mlir-commits] [mlir] 7ac03e8 - [mlir][EmitC] Add bitwise operators (#83387)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Mar 1 04:21:14 PST 2024
Author: Marius Brehler
Date: 2024-03-01T13:21:11+01:00
New Revision: 7ac03e8a369d8ac74a2c4d97a9e2a41221428abd
URL: https://github.com/llvm/llvm-project/commit/7ac03e8a369d8ac74a2c4d97a9e2a41221428abd
DIFF: https://github.com/llvm/llvm-project/commit/7ac03e8a369d8ac74a2c4d97a9e2a41221428abd.diff
LOG: [mlir][EmitC] Add bitwise operators (#83387)
This adds operations for bitwise operators. Furthermore, an UnaryOp
class and a helper to print unary operations are introduced.
Added:
mlir/test/Target/Cpp/bitwise_operators.mlir
Modified:
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
mlir/lib/Target/Cpp/TranslateToCpp.cpp
mlir/test/Dialect/EmitC/ops.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 7b9fbb494e895a..5679742bfa16ec 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -31,6 +31,14 @@ include "mlir/IR/RegionKindInterface.td"
class EmitC_Op<string mnemonic, list<Trait> traits = []>
: Op<EmitC_Dialect, mnemonic, traits>;
+// Base class for unary operations.
+class EmitC_UnaryOp<string mnemonic, list<Trait> traits = []> :
+ EmitC_Op<mnemonic, traits> {
+ let arguments = (ins AnyType);
+ let results = (outs AnyType);
+ let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)";
+}
+
// Base class for binary operations.
class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
EmitC_Op<mnemonic, traits> {
@@ -95,6 +103,114 @@ def EmitC_ApplyOp : EmitC_Op<"apply", []> {
let hasVerifier = 1;
}
+def EmitC_BitwiseAndOp : EmitC_BinaryOp<"bitwise_and", []> {
+ let summary = "Bitwise and operation";
+ let description = [{
+ With the `bitwise_and` operation the bitwise operator & (and) can
+ be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v3 = v1 & v2;
+ ```
+ }];
+}
+
+def EmitC_BitwiseLeftShiftOp : EmitC_BinaryOp<"bitwise_left_shift", []> {
+ let summary = "Bitwise left shift operation";
+ let description = [{
+ With the `bitwise_left_shift` operation the bitwise operator <<
+ (left shift) can be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v3 = v1 << v2;
+ ```
+ }];
+}
+
+def EmitC_BitwiseNotOp : EmitC_UnaryOp<"bitwise_not", []> {
+ let summary = "Bitwise not operation";
+ let description = [{
+ With the `bitwise_not` operation the bitwise operator ~ (not) can
+ be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_not %arg0 : (i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v2 = ~v1;
+ ```
+ }];
+}
+
+def EmitC_BitwiseOrOp : EmitC_BinaryOp<"bitwise_or", []> {
+ let summary = "Bitwise or operation";
+ let description = [{
+ With the `bitwise_or` operation the bitwise operator | (or)
+ can be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v3 = v1 | v2;
+ ```
+ }];
+}
+
+def EmitC_BitwiseRightShiftOp : EmitC_BinaryOp<"bitwise_right_shift", []> {
+ let summary = "Bitwise right shift operation";
+ let description = [{
+ With the `bitwise_right_shift` operation the bitwise operator >>
+ (right shift) can be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v3 = v1 >> v2;
+ ```
+ }];
+}
+
+def EmitC_BitwiseXorOp : EmitC_BinaryOp<"bitwise_xor", []> {
+ let summary = "Bitwise xor operation";
+ let description = [{
+ With the `bitwise_xor` operation the bitwise operator ^ (xor)
+ can be applied.
+
+ Example:
+
+ ```mlir
+ %0 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
+ ```
+ ```c++
+ // Code emitted for the operation above.
+ int32_t v3 = v1 ^ v2;
+ ```
+ }];
+}
+
def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", []> {
let summary = "Opaque call operation";
let description = [{
@@ -679,7 +795,7 @@ def EmitC_LogicalAndOp : EmitC_BinaryOp<"logical_and", []> {
let assemblyFormat = "operands attr-dict `:` type(operands)";
}
-def EmitC_LogicalNotOp : EmitC_Op<"logical_not", []> {
+def EmitC_LogicalNotOp : EmitC_UnaryOp<"logical_not", []> {
let summary = "Logical not operation";
let description = [{
With the `logical_not` operation the logical operator ! (negation) can
@@ -696,7 +812,6 @@ def EmitC_LogicalNotOp : EmitC_Op<"logical_not", []> {
```
}];
- let arguments = (ins AnyType);
let results = (outs I1);
let assemblyFormat = "operands attr-dict `:` type(operands)";
}
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 16aa136c5a4e28..4bc707c43ad92f 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -361,6 +361,22 @@ static LogicalResult printBinaryOperation(CppEmitter &emitter,
return success();
}
+static LogicalResult printUnaryOperation(CppEmitter &emitter,
+ Operation *operation,
+ StringRef unaryOperator) {
+ raw_ostream &os = emitter.ostream();
+
+ if (failed(emitter.emitAssignPrefix(*operation)))
+ return failure();
+
+ os << unaryOperator;
+
+ if (failed(emitter.emitOperand(operation->getOperand(0))))
+ return failure();
+
+ return success();
+}
+
static LogicalResult printOperation(CppEmitter &emitter, emitc::AddOp addOp) {
Operation *operation = addOp.getOperation();
@@ -588,6 +604,44 @@ static LogicalResult printOperation(CppEmitter &emitter,
return success();
}
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::BitwiseAndOp bitwiseAndOp) {
+ Operation *operation = bitwiseAndOp.getOperation();
+ return printBinaryOperation(emitter, operation, "&");
+}
+
+static LogicalResult
+printOperation(CppEmitter &emitter,
+ emitc::BitwiseLeftShiftOp bitwiseLeftShiftOp) {
+ Operation *operation = bitwiseLeftShiftOp.getOperation();
+ return printBinaryOperation(emitter, operation, "<<");
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::BitwiseNotOp bitwiseNotOp) {
+ Operation *operation = bitwiseNotOp.getOperation();
+ return printUnaryOperation(emitter, operation, "~");
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::BitwiseOrOp bitwiseOrOp) {
+ Operation *operation = bitwiseOrOp.getOperation();
+ return printBinaryOperation(emitter, operation, "|");
+}
+
+static LogicalResult
+printOperation(CppEmitter &emitter,
+ emitc::BitwiseRightShiftOp bitwiseRightShiftOp) {
+ Operation *operation = bitwiseRightShiftOp.getOperation();
+ return printBinaryOperation(emitter, operation, ">>");
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::BitwiseXorOp bitwiseXorOp) {
+ Operation *operation = bitwiseXorOp.getOperation();
+ return printBinaryOperation(emitter, operation, "^");
+}
+
static LogicalResult printOperation(CppEmitter &emitter, emitc::CastOp castOp) {
raw_ostream &os = emitter.ostream();
Operation &op = *castOp.getOperation();
@@ -635,17 +689,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
static LogicalResult printOperation(CppEmitter &emitter,
emitc::LogicalNotOp logicalNotOp) {
- raw_ostream &os = emitter.ostream();
-
- if (failed(emitter.emitAssignPrefix(*logicalNotOp.getOperation())))
- return failure();
-
- os << "!";
-
- if (failed(emitter.emitOperand(logicalNotOp.getOperand())))
- return failure();
-
- return success();
+ Operation *operation = logicalNotOp.getOperation();
+ return printUnaryOperation(emitter, operation, "!");
}
static LogicalResult printOperation(CppEmitter &emitter,
@@ -1307,7 +1352,10 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
.Case<cf::BranchOp, cf::CondBranchOp>(
[&](auto op) { return printOperation(*this, op); })
// EmitC ops.
- .Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp, emitc::CallOp,
+ .Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
+ emitc::BitwiseAndOp, emitc::BitwiseLeftShiftOp,
+ emitc::BitwiseNotOp, emitc::BitwiseOrOp,
+ emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
emitc::ConstantOp, emitc::DeclareFuncOp, emitc::DivOp,
emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp, emitc::IfOp,
diff --git a/mlir/test/Dialect/EmitC/ops.mlir b/mlir/test/Dialect/EmitC/ops.mlir
index 045fb24cb67f8d..f852390f03e298 100644
--- a/mlir/test/Dialect/EmitC/ops.mlir
+++ b/mlir/test/Dialect/EmitC/ops.mlir
@@ -61,6 +61,16 @@ func.func @add_pointer(%arg0: !emitc.ptr<f32>, %arg1: i32, %arg2: !emitc.opaque<
return
}
+func.func @bitwise(%arg0: i32, %arg1: i32) -> () {
+ %0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
+ %1 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
+ %2 = emitc.bitwise_not %arg0 : (i32) -> i32
+ %3 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
+ %4 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
+ %5 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
+ return
+}
+
func.func @div_int(%arg0: i32, %arg1: i32) {
%1 = "emitc.div" (%arg0, %arg1) : (i32, i32) -> i32
return
diff --git a/mlir/test/Target/Cpp/bitwise_operators.mlir b/mlir/test/Target/Cpp/bitwise_operators.mlir
new file mode 100644
index 00000000000000..e666359fc82c91
--- /dev/null
+++ b/mlir/test/Target/Cpp/bitwise_operators.mlir
@@ -0,0 +1,20 @@
+// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
+
+func.func @bitwise(%arg0: i32, %arg1: i32) -> () {
+ %0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
+ %1 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
+ %2 = emitc.bitwise_not %arg0 : (i32) -> i32
+ %3 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
+ %4 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
+ %5 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
+
+ return
+}
+
+// CHECK-LABEL: void bitwise
+// CHECK-NEXT: int32_t [[V2:[^ ]*]] = [[V0:[^ ]*]] & [[V1:[^ ]*]];
+// CHECK-NEXT: int32_t [[V3:[^ ]*]] = [[V0]] << [[V1]];
+// CHECK-NEXT: int32_t [[V4:[^ ]*]] = ~[[V0]];
+// CHECK-NEXT: int32_t [[V5:[^ ]*]] = [[V0]] | [[V1]];
+// CHECK-NEXT: int32_t [[V6:[^ ]*]] = [[V0]] >> [[V1]];
+// CHECK-NEXT: int32_t [[V7:[^ ]*]] = [[V0]] ^ [[V1]];
More information about the Mlir-commits
mailing list