Skip to content

Commit 20b3873

Browse files
committed
[mlir][emitc] mark emitc.load with CExpression
Follow the `call` and `call_opaque` operations, as well as `apply`, which already are marked as `CExpression` even though they have side effects. Even though `emitc.load` can be included inside the `emitc.expression`, the inlining and `--form-expression` pass won't actually inline them inside other expression due to it having a side effect, thus unless the user manually writes the `emitc.load` inside the `emitc.expression` it won't appear there.
1 parent 2bf7018 commit 20b3873

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def EmitC_LogicalOrOp : EmitC_BinaryOp<"logical_or", [CExpression]> {
927927
let assemblyFormat = "operands attr-dict `:` type(operands)";
928928
}
929929

930-
def EmitC_LoadOp : EmitC_Op<"load", [
930+
def EmitC_LoadOp : EmitC_Op<"load", [CExpression,
931931
TypesMatchWith<"result type matches value type of 'operand'",
932932
"operand", "result",
933933
"::llvm::cast<LValueType>($_self).getValueType()">

mlir/lib/Target/Cpp/TranslateToCpp.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
100100
})
101101
.Case<emitc::ConditionalOp>([&](auto op) { return 2; })
102102
.Case<emitc::DivOp>([&](auto op) { return 13; })
103+
.Case<emitc::LoadOp>([&](auto op) { return 16; })
103104
.Case<emitc::LogicalAndOp>([&](auto op) { return 4; })
104105
.Case<emitc::LogicalNotOp>([&](auto op) { return 15; })
105106
.Case<emitc::LogicalOrOp>([&](auto op) { return 3; })

mlir/test/Dialect/EmitC/transforms.mlir

+23
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,26 @@ func.func @single_result_requirement() -> (i32, i32) {
129129
%0:2 = emitc.call_opaque "foo" () : () -> (i32, i32)
130130
return %0#0, %0#1 : i32, i32
131131
}
132+
133+
// CHECK-LABEL: func.func @expression_with_load(
134+
// CHECK-SAME: %[[VAL_0:.*]]: i32, %[[VAL_1:.*]]: i32) -> i1 {
135+
// CHECK: %[[VAL_2:.*]] = "emitc.variable"() <{value = #emitc.opaque<"42">}> : () -> !emitc.lvalue<i32>
136+
// CHECK: %[[VAL_3:.*]] = emitc.expression : i32 {
137+
// CHECK: %[[VAL_4:.*]] = load %[[VAL_2]] : <i32>
138+
// CHECK: yield %[[VAL_4]] : i32
139+
// CHECK: }
140+
// CHECK: %[[VAL_5:.*]] = emitc.expression : i1 {
141+
// CHECK: %[[VAL_6:.*]] = add %[[VAL_3]], %[[VAL_1]] : (i32, i32) -> i32
142+
// CHECK: %[[VAL_7:.*]] = cmp lt, %[[VAL_6]], %[[VAL_0]] : (i32, i32) -> i1
143+
// CHECK: yield %[[VAL_7]] : i1
144+
// CHECK: }
145+
// CHECK: return %[[VAL_5]] : i1
146+
// CHECK: }
147+
148+
func.func @expression_with_load(%arg0: i32, %arg1: i32) -> i1 {
149+
%0 = "emitc.variable"() <{value = #emitc.opaque<"42">}> : () -> !emitc.lvalue<i32>
150+
%a = emitc.load %0 : !emitc.lvalue<i32>
151+
%b = emitc.add %a, %arg1 : (i32, i32) -> i32
152+
%c = emitc.cmp lt, %b, %arg0 :(i32, i32) -> i1
153+
return %c : i1
154+
}

0 commit comments

Comments
 (0)