Skip to content

Commit f355cd6

Browse files
authored
[mlir][EmitC] Allow further ops within expressions (#84284)
This adds the `CExpression` trait to additional ops to allow to use these ops within the expression operation. Furthermore, the operator precedence is defined for those ops.
1 parent e4d4cfa commit f355cd6

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def EmitC_ApplyOp : EmitC_Op<"apply", [CExpression]> {
106106
let hasVerifier = 1;
107107
}
108108

109-
def EmitC_BitwiseAndOp : EmitC_BinaryOp<"bitwise_and", []> {
109+
def EmitC_BitwiseAndOp : EmitC_BinaryOp<"bitwise_and", [CExpression]> {
110110
let summary = "Bitwise and operation";
111111
let description = [{
112112
With the `bitwise_and` operation the bitwise operator & (and) can
@@ -124,7 +124,8 @@ def EmitC_BitwiseAndOp : EmitC_BinaryOp<"bitwise_and", []> {
124124
}];
125125
}
126126

127-
def EmitC_BitwiseLeftShiftOp : EmitC_BinaryOp<"bitwise_left_shift", []> {
127+
def EmitC_BitwiseLeftShiftOp : EmitC_BinaryOp<"bitwise_left_shift",
128+
[CExpression]> {
128129
let summary = "Bitwise left shift operation";
129130
let description = [{
130131
With the `bitwise_left_shift` operation the bitwise operator <<
@@ -142,7 +143,7 @@ def EmitC_BitwiseLeftShiftOp : EmitC_BinaryOp<"bitwise_left_shift", []> {
142143
}];
143144
}
144145

145-
def EmitC_BitwiseNotOp : EmitC_UnaryOp<"bitwise_not", []> {
146+
def EmitC_BitwiseNotOp : EmitC_UnaryOp<"bitwise_not", [CExpression]> {
146147
let summary = "Bitwise not operation";
147148
let description = [{
148149
With the `bitwise_not` operation the bitwise operator ~ (not) can
@@ -160,7 +161,7 @@ def EmitC_BitwiseNotOp : EmitC_UnaryOp<"bitwise_not", []> {
160161
}];
161162
}
162163

163-
def EmitC_BitwiseOrOp : EmitC_BinaryOp<"bitwise_or", []> {
164+
def EmitC_BitwiseOrOp : EmitC_BinaryOp<"bitwise_or", [CExpression]> {
164165
let summary = "Bitwise or operation";
165166
let description = [{
166167
With the `bitwise_or` operation the bitwise operator | (or)
@@ -178,7 +179,8 @@ def EmitC_BitwiseOrOp : EmitC_BinaryOp<"bitwise_or", []> {
178179
}];
179180
}
180181

181-
def EmitC_BitwiseRightShiftOp : EmitC_BinaryOp<"bitwise_right_shift", []> {
182+
def EmitC_BitwiseRightShiftOp : EmitC_BinaryOp<"bitwise_right_shift",
183+
[CExpression]> {
182184
let summary = "Bitwise right shift operation";
183185
let description = [{
184186
With the `bitwise_right_shift` operation the bitwise operator >>
@@ -196,7 +198,7 @@ def EmitC_BitwiseRightShiftOp : EmitC_BinaryOp<"bitwise_right_shift", []> {
196198
}];
197199
}
198200

199-
def EmitC_BitwiseXorOp : EmitC_BinaryOp<"bitwise_xor", []> {
201+
def EmitC_BitwiseXorOp : EmitC_BinaryOp<"bitwise_xor", [CExpression]> {
200202
let summary = "Bitwise xor operation";
201203
let description = [{
202204
With the `bitwise_xor` operation the bitwise operator ^ (xor)
@@ -515,7 +517,7 @@ def EmitC_ForOp : EmitC_Op<"for",
515517
}
516518

517519
def EmitC_CallOp : EmitC_Op<"call",
518-
[CallOpInterface,
520+
[CallOpInterface, CExpression,
519521
DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
520522
let summary = "call operation";
521523
let description = [{
@@ -771,7 +773,7 @@ def EmitC_LiteralOp : EmitC_Op<"literal", [Pure]> {
771773
let assemblyFormat = "$value attr-dict `:` type($result)";
772774
}
773775

774-
def EmitC_LogicalAndOp : EmitC_BinaryOp<"logical_and", []> {
776+
def EmitC_LogicalAndOp : EmitC_BinaryOp<"logical_and", [CExpression]> {
775777
let summary = "Logical and operation";
776778
let description = [{
777779
With the `logical_and` operation the logical operator && (and) can
@@ -792,7 +794,7 @@ def EmitC_LogicalAndOp : EmitC_BinaryOp<"logical_and", []> {
792794
let assemblyFormat = "operands attr-dict `:` type(operands)";
793795
}
794796

795-
def EmitC_LogicalNotOp : EmitC_UnaryOp<"logical_not", []> {
797+
def EmitC_LogicalNotOp : EmitC_UnaryOp<"logical_not", [CExpression]> {
796798
let summary = "Logical not operation";
797799
let description = [{
798800
With the `logical_not` operation the logical operator ! (negation) can
@@ -813,7 +815,7 @@ def EmitC_LogicalNotOp : EmitC_UnaryOp<"logical_not", []> {
813815
let assemblyFormat = "operands attr-dict `:` type(operands)";
814816
}
815817

816-
def EmitC_LogicalOrOp : EmitC_BinaryOp<"logical_or", []> {
818+
def EmitC_LogicalOrOp : EmitC_BinaryOp<"logical_or", [CExpression]> {
817819
let summary = "Logical or operation";
818820
let description = [{
819821
With the `logical_or` operation the logical operator || (inclusive or)

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ inline LogicalResult interleaveCommaWithError(const Container &c,
7171
/// imply higher precedence.
7272
static FailureOr<int> getOperatorPrecedence(Operation *operation) {
7373
return llvm::TypeSwitch<Operation *, FailureOr<int>>(operation)
74-
.Case<emitc::AddOp>([&](auto op) { return 11; })
75-
.Case<emitc::ApplyOp>([&](auto op) { return 13; })
76-
.Case<emitc::CastOp>([&](auto op) { return 13; })
74+
.Case<emitc::AddOp>([&](auto op) { return 12; })
75+
.Case<emitc::ApplyOp>([&](auto op) { return 15; })
76+
.Case<emitc::BitwiseAndOp>([&](auto op) { return 7; })
77+
.Case<emitc::BitwiseLeftShiftOp>([&](auto op) { return 11; })
78+
.Case<emitc::BitwiseNotOp>([&](auto op) { return 15; })
79+
.Case<emitc::BitwiseOrOp>([&](auto op) { return 5; })
80+
.Case<emitc::BitwiseRightShiftOp>([&](auto op) { return 11; })
81+
.Case<emitc::BitwiseXorOp>([&](auto op) { return 6; })
82+
.Case<emitc::CallOp>([&](auto op) { return 16; })
83+
.Case<emitc::CallOpaqueOp>([&](auto op) { return 16; })
84+
.Case<emitc::CastOp>([&](auto op) { return 15; })
7785
.Case<emitc::CmpOp>([&](auto op) -> FailureOr<int> {
7886
switch (op.getPredicate()) {
7987
case emitc::CmpPredicate::eq:
@@ -89,11 +97,13 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
8997
}
9098
return op->emitError("unsupported cmp predicate");
9199
})
92-
.Case<emitc::DivOp>([&](auto op) { return 12; })
93-
.Case<emitc::MulOp>([&](auto op) { return 12; })
94-
.Case<emitc::RemOp>([&](auto op) { return 12; })
95-
.Case<emitc::SubOp>([&](auto op) { return 11; })
96-
.Case<emitc::CallOpaqueOp>([&](auto op) { return 14; })
100+
.Case<emitc::DivOp>([&](auto op) { return 13; })
101+
.Case<emitc::LogicalAndOp>([&](auto op) { return 4; })
102+
.Case<emitc::LogicalNotOp>([&](auto op) { return 15; })
103+
.Case<emitc::LogicalOrOp>([&](auto op) { return 3; })
104+
.Case<emitc::MulOp>([&](auto op) { return 13; })
105+
.Case<emitc::RemOp>([&](auto op) { return 13; })
106+
.Case<emitc::SubOp>([&](auto op) { return 12; })
97107
.Default([](auto op) { return op->emitError("unsupported operation"); });
98108
}
99109

0 commit comments

Comments
 (0)