Skip to content

Commit 5344a37

Browse files
authored
[mlir][emitc] Fix form-expressions inside expression (#86081)
Make form-expressions not create `emitc.expression`s for operations inside the `emitc.expression`s, since they are invalid.
1 parent 276283d commit 5344a37

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mlir/lib/Dialect/EmitC/Transforms/FormExpressions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ struct FormExpressionsPass
3636
// Wrap each C operator op with an expression op.
3737
OpBuilder builder(context);
3838
auto matchFun = [&](Operation *op) {
39-
if (op->hasTrait<OpTrait::emitc::CExpression>())
39+
if (op->hasTrait<OpTrait::emitc::CExpression>() &&
40+
!op->getParentOfType<emitc::ExpressionOp>())
4041
createExpression(op, builder);
4142
};
4243
rootOp->walk(matchFun);

mlir/test/Dialect/EmitC/transforms.mlir

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,20 @@ func.func @expression_with_address_taken(%arg0: i32, %arg1: i32, %arg2: !emitc.p
107107
%d = emitc.cmp lt, %c, %arg2 :(!emitc.ptr<i32>, !emitc.ptr<i32>) -> i1
108108
return %d : i1
109109
}
110+
111+
// CHECK-LABEL: func.func @no_nested_expression(
112+
// CHECK-SAME: %[[VAL_0:.*]]: i32, %[[VAL_1:.*]]: i32) -> i1 {
113+
// CHECK: %[[VAL_2:.*]] = emitc.expression : i1 {
114+
// CHECK: %[[VAL_3:.*]] = emitc.cmp lt, %[[VAL_0]], %[[VAL_1]] : (i32, i32) -> i1
115+
// CHECK: emitc.yield %[[VAL_3]] : i1
116+
// CHECK: }
117+
// CHECK: return %[[VAL_2]] : i1
118+
// CHECK: }
119+
120+
func.func @no_nested_expression(%arg0: i32, %arg1: i32) -> i1 {
121+
%a = emitc.expression : i1 {
122+
%b = emitc.cmp lt, %arg0, %arg1 :(i32, i32) -> i1
123+
emitc.yield %b : i1
124+
}
125+
return %a : i1
126+
}

0 commit comments

Comments
 (0)