Skip to content

Commit 5e2fb57

Browse files
committed
[mlir][EmitC] Add binary operators
This adds operations for binary operators.
1 parent 1530034 commit 5e2fb57

File tree

4 files changed

+193
-1
lines changed

4 files changed

+193
-1
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,118 @@ def EmitC_ApplyOp : EmitC_Op<"apply", []> {
9595
let hasVerifier = 1;
9696
}
9797

98+
def EmitC_BitwiseAndOp : EmitC_BinaryOp<"bitwise_and", []> {
99+
let summary = "Bitwise and operation";
100+
let description = [{
101+
With the `bitwise_and` operation the bitwise operator & (and) can
102+
be applied.
103+
104+
Example:
105+
106+
```mlirbool
107+
%0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
108+
```
109+
```c++
110+
// Code emitted for the operation above.
111+
int32_t v3 = v1 & v2;
112+
```
113+
}];
114+
}
115+
116+
def EmitC_BitwiseLeftShiftOp : EmitC_BinaryOp<"bitwise_left_shift", []> {
117+
let summary = "Bitwise left shift operation";
118+
let description = [{
119+
With the `bitwise_left_shift` operation the bitwise operator <<
120+
(left shift) can be applied.
121+
122+
Example:
123+
124+
```mlir
125+
%0 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
126+
```
127+
```c++
128+
// Code emitted for the operation above.
129+
int32_t v3 = v1 << v2;
130+
```
131+
}];
132+
}
133+
134+
def EmitC_BitwiseNotOp : EmitC_Op<"bitwise_not", []> {
135+
let summary = "Bitwise not operation";
136+
let description = [{
137+
With the `bitwise_not` operation the bitwise operator ~ (not) can
138+
be applied.
139+
140+
Example:
141+
142+
```mlir
143+
%0 = emitc.bitwise_not %arg0 : (i32) -> i32
144+
```
145+
```c++
146+
// Code emitted for the operation above.
147+
int32_t v2 = ~v1;
148+
```
149+
}];
150+
151+
let arguments = (ins AnyType);
152+
let results = (outs AnyType);
153+
let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)";
154+
}
155+
156+
def EmitC_BitwiseOrOp : EmitC_BinaryOp<"bitwise_or", []> {
157+
let summary = "Bitwise or operation";
158+
let description = [{
159+
With the `bitwise_or` operation the bitwise operator | (or)
160+
can be applied.
161+
162+
Example:
163+
164+
```mlir
165+
%0 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
166+
```
167+
```c++
168+
// Code emitted for the operation above.
169+
int32_t v3 = v1 | v2;
170+
```
171+
}];
172+
}
173+
174+
def EmitC_BitwiseRightShiftOp : EmitC_BinaryOp<"bitwise_right_shift", []> {
175+
let summary = "Bitwise right shift operation";
176+
let description = [{
177+
With the `bitwise_right_shift` operation the bitwise operator >>
178+
(right shift) can be applied.
179+
180+
Example:
181+
182+
```mlir
183+
%0 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
184+
```
185+
```c++
186+
// Code emitted for the operation above.
187+
int32_t v3 = v1 >> v2;
188+
```
189+
}];
190+
}
191+
192+
def EmitC_BitwiseXorOp : EmitC_BinaryOp<"bitwise_xor", []> {
193+
let summary = "Bitwise xor operation";
194+
let description = [{
195+
With the `bitwise_xor` operation the bitwise operator ^ (xor)
196+
can be applied.
197+
198+
Example:
199+
200+
```mlir
201+
%0 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
202+
```
203+
```c++
204+
// Code emitted for the operation above.
205+
int32_t v3 = v1 ^ v2;
206+
```
207+
}];
208+
}
209+
98210
def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", []> {
99211
let summary = "Opaque call operation";
100212
let description = [{

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,53 @@ static LogicalResult printOperation(CppEmitter &emitter,
588588
return success();
589589
}
590590

591+
static LogicalResult printOperation(CppEmitter &emitter,
592+
emitc::BitwiseAndOp bitwiseAndOp) {
593+
Operation *operation = bitwiseAndOp.getOperation();
594+
return printBinaryOperation(emitter, operation, "&");
595+
}
596+
597+
static LogicalResult
598+
printOperation(CppEmitter &emitter,
599+
emitc::BitwiseLeftShiftOp bitwiseLeftShiftOp) {
600+
Operation *operation = bitwiseLeftShiftOp.getOperation();
601+
return printBinaryOperation(emitter, operation, "<<");
602+
}
603+
604+
static LogicalResult printOperation(CppEmitter &emitter,
605+
emitc::BitwiseNotOp bitwiseNotOp) {
606+
raw_ostream &os = emitter.ostream();
607+
608+
if (failed(emitter.emitAssignPrefix(*bitwiseNotOp.getOperation())))
609+
return failure();
610+
611+
os << "~";
612+
613+
if (failed(emitter.emitOperand(bitwiseNotOp.getOperand())))
614+
return failure();
615+
616+
return success();
617+
}
618+
619+
static LogicalResult printOperation(CppEmitter &emitter,
620+
emitc::BitwiseOrOp bitwiseOrOp) {
621+
Operation *operation = bitwiseOrOp.getOperation();
622+
return printBinaryOperation(emitter, operation, "|");
623+
}
624+
625+
static LogicalResult
626+
printOperation(CppEmitter &emitter,
627+
emitc::BitwiseRightShiftOp bitwiseRightShiftOp) {
628+
Operation *operation = bitwiseRightShiftOp.getOperation();
629+
return printBinaryOperation(emitter, operation, ">>");
630+
}
631+
632+
static LogicalResult printOperation(CppEmitter &emitter,
633+
emitc::BitwiseXorOp bitwiseXorOp) {
634+
Operation *operation = bitwiseXorOp.getOperation();
635+
return printBinaryOperation(emitter, operation, "^");
636+
}
637+
591638
static LogicalResult printOperation(CppEmitter &emitter, emitc::CastOp castOp) {
592639
raw_ostream &os = emitter.ostream();
593640
Operation &op = *castOp.getOperation();
@@ -1307,7 +1354,10 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
13071354
.Case<cf::BranchOp, cf::CondBranchOp>(
13081355
[&](auto op) { return printOperation(*this, op); })
13091356
// EmitC ops.
1310-
.Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp, emitc::CallOp,
1357+
.Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
1358+
emitc::BitwiseAndOp, emitc::BitwiseLeftShiftOp,
1359+
emitc::BitwiseNotOp, emitc::BitwiseOrOp,
1360+
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
13111361
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
13121362
emitc::ConstantOp, emitc::DeclareFuncOp, emitc::DivOp,
13131363
emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp, emitc::IfOp,

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func.func @add_pointer(%arg0: !emitc.ptr<f32>, %arg1: i32, %arg2: !emitc.opaque<
6161
return
6262
}
6363

64+
func.func @bitwise(%arg0: i32, %arg1: i32) -> () {
65+
%0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
66+
%1 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
67+
%2 = emitc.bitwise_not %arg0 : (i32) -> i32
68+
%3 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
69+
%4 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
70+
%5 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
71+
return
72+
}
73+
6474
func.func @div_int(%arg0: i32, %arg1: i32) {
6575
%1 = "emitc.div" (%arg0, %arg1) : (i32, i32) -> i32
6676
return
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
2+
3+
func.func @bitwise(%arg0: i32, %arg1: i32) -> () {
4+
%0 = emitc.bitwise_and %arg0, %arg1 : (i32, i32) -> i32
5+
%1 = emitc.bitwise_left_shift %arg0, %arg1 : (i32, i32) -> i32
6+
%2 = emitc.bitwise_not %arg0 : (i32) -> i32
7+
%3 = emitc.bitwise_or %arg0, %arg1 : (i32, i32) -> i32
8+
%4 = emitc.bitwise_right_shift %arg0, %arg1 : (i32, i32) -> i32
9+
%5 = emitc.bitwise_xor %arg0, %arg1 : (i32, i32) -> i32
10+
11+
return
12+
}
13+
14+
// CHECK-LABEL: void bitwise
15+
// CHECK-NEXT: int32_t [[V2:[^ ]*]] = [[V0:[^ ]*]] & [[V1:[^ ]*]];
16+
// CHECK-NEXT: int32_t [[V3:[^ ]*]] = [[V0]] << [[V1]];
17+
// CHECK-NEXT: int32_t [[V4:[^ ]*]] = ~[[V0]];
18+
// CHECK-NEXT: int32_t [[V5:[^ ]*]] = [[V0]] | [[V1]];
19+
// CHECK-NEXT: int32_t [[V6:[^ ]*]] = [[V0]] >> [[V1]];
20+
// CHECK-NEXT: int32_t [[V7:[^ ]*]] = [[V0]] ^ [[V1]];

0 commit comments

Comments
 (0)