Skip to content

Commit 4d23b0d

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 4d23b0d

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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+
}

mlir/test/Target/Cpp/expressions.mlir

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,60 @@ func.func @expression_with_subscript_user(%arg0: !emitc.ptr<!emitc.opaque<"void"
342342
%res_load = emitc.load %res : !emitc.lvalue<i32>
343343
return %res_load : i32
344344
}
345+
346+
// CPP-DEFAULT: bool expression_with_load(int32_t [[VAL_1:v.+]], int32_t [[VAL_2:v.+]], int32_t* [[VAL_3:v.+]]) {
347+
// CPP-DEFAULT-NEXT: int64_t [[VAL_4:v.+]] = 0;
348+
// CPP-DEFAULT-NEXT: int32_t [[VAL_5:v.+]] = 42;
349+
// CPP-DEFAULT-NEXT: bool [[VAL_6:v.+]] = [[VAL_5]] + [[VAL_2]] < [[VAL_3]][[[VAL_4]]] + [[VAL_1]];
350+
// CPP-DEFAULT-NEXT: return [[VAL_6]];
351+
352+
// CPP-DECLTOP: bool expression_with_load(int32_t [[VAL_1:v.+]], int32_t [[VAL_2:v.+]], int32_t* [[VAL_3:v.+]]) {
353+
// CPP-DECLTOP-NEXT: int64_t [[VAL_4:v.+]];
354+
// CPP-DECLTOP-NEXT: int32_t [[VAL_5:v.+]];
355+
// CPP-DECLTOP-NEXT: bool [[VAL_6:v.+]];
356+
// CPP-DECLTOP-NEXT: [[VAL_4]] = 0;
357+
// CPP-DECLTOP-NEXT: [[VAL_5]] = 42;
358+
// CPP-DECLTOP-NEXT: [[VAL_6]] = [[VAL_5]] + [[VAL_2]] < [[VAL_3]][[[VAL_4]]] + [[VAL_1]];
359+
// CPP-DECLTOP-NEXT: return [[VAL_6]];
360+
361+
func.func @expression_with_load(%arg0: i32, %arg1: i32, %arg2: !emitc.ptr<i32>) -> i1 {
362+
%c0 = "emitc.constant"() {value = 0 : i64} : () -> i64
363+
%0 = "emitc.variable"() <{value = #emitc.opaque<"42">}> : () -> !emitc.lvalue<i32>
364+
%ptr = emitc.subscript %arg2[%c0] : (!emitc.ptr<i32>, i64) -> !emitc.lvalue<i32>
365+
%result = emitc.expression : i1 {
366+
%a = emitc.load %0 : !emitc.lvalue<i32>
367+
%b = emitc.add %a, %arg1 : (i32, i32) -> i32
368+
%c = emitc.load %ptr : !emitc.lvalue<i32>
369+
%d = emitc.add %c, %arg0 : (i32, i32) -> i32
370+
%e = emitc.cmp lt, %b, %d :(i32, i32) -> i1
371+
yield %e : i1
372+
}
373+
return %result : i1
374+
}
375+
376+
// CPP-DEFAULT: bool expression_with_load_and_call(int32_t* [[VAL_1:v.+]]) {
377+
// CPP-DEFAULT-NEXT: int64_t [[VAL_2:v.+]] = 0;
378+
// CPP-DEFAULT-NEXT: bool [[VAL_3:v.+]] = [[VAL_1]][[[VAL_2]]] + bar([[VAL_1]][[[VAL_2]]]) < [[VAL_1]][[[VAL_2]]];
379+
// CPP-DEFAULT-NEXT: return [[VAL_3]];
380+
381+
// CPP-DECLTOP: bool expression_with_load_and_call(int32_t* [[VAL_1:v.+]]) {
382+
// CPP-DECLTOP-NEXT: int64_t [[VAL_2:v.+]];
383+
// CPP-DECLTOP-NEXT: bool [[VAL_3:v.+]];
384+
// CPP-DECLTOP-NEXT: [[VAL_2]] = 0;
385+
// CPP-DECLTOP-NEXT: [[VAL_3]] = [[VAL_1]][[[VAL_2]]] + bar([[VAL_1]][[[VAL_2]]]) < [[VAL_1]][[[VAL_2]]];
386+
// CPP-DECLTOP-NEXT: return [[VAL_3]];
387+
388+
func.func @expression_with_load_and_call(%arg0: !emitc.ptr<i32>) -> i1 {
389+
%c0 = "emitc.constant"() {value = 0 : i64} : () -> i64
390+
%ptr = emitc.subscript %arg0[%c0] : (!emitc.ptr<i32>, i64) -> !emitc.lvalue<i32>
391+
%result = emitc.expression : i1 {
392+
%a = emitc.load %ptr : !emitc.lvalue<i32>
393+
%b = emitc.load %ptr : !emitc.lvalue<i32>
394+
%c = emitc.load %ptr : !emitc.lvalue<i32>
395+
%d = emitc.call_opaque "bar" (%a) : (i32) -> (i32)
396+
%e = add %c, %d : (i32, i32) -> i32
397+
%f = emitc.cmp lt, %e, %b :(i32, i32) -> i1
398+
yield %f : i1
399+
}
400+
return %result : i1
401+
}

0 commit comments

Comments
 (0)