Skip to content

Commit 657eda3

Browse files
[MLIR][EmitC] Don't translate expressions inline if user is emitc.subscript (#91087)
This change updates the logic that determines whether an `emitc.expression` result is translated into a dedicated variable assignment. Due to how the translation of `emitc.subscript` currently works, a previously inline-able `emitc.expression` would produce incorrect C++ if its single user was a `emitc.subscript` operation.
1 parent 363ec6f commit 657eda3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,16 @@ static bool shouldBeInlined(ExpressionOp expressionOp) {
293293
if (!result.hasOneUse())
294294
return false;
295295

296+
Operation *user = *result.getUsers().begin();
297+
298+
// Do not inline expressions used by subscript operations, since the
299+
// way the subscript operation translation is implemented requires that
300+
// variables be materialized.
301+
if (isa<emitc::SubscriptOp>(user))
302+
return false;
303+
296304
// Do not inline expressions used by other expressions, as any desired
297305
// expression folding was taken care of by transformations.
298-
Operation *user = *result.getUsers().begin();
299306
return !user->getParentOfType<ExpressionOp>();
300307
}
301308

mlir/test/Target/Cpp/expressions.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,18 @@ func.func @expression_with_address_taken(%arg0: i32, %arg1: i32, %arg2: !emitc.p
210210
}
211211
return %c : i1
212212
}
213+
214+
// CPP-DEFAULT: int32_t expression_with_subscript_user(void* [[VAL_1:v.+]])
215+
// CPP-DEFAULT-NEXT: int64_t [[VAL_2:v.+]] = 0;
216+
// CPP-DEFAULT-NEXT: int32_t* [[VAL_3:v.+]] = (int32_t*) [[VAL_1]];
217+
// CPP-DEFAULT-NEXT: return [[VAL_3]][[[VAL_2]]];
218+
219+
func.func @expression_with_subscript_user(%arg0: !emitc.ptr<!emitc.opaque<"void">>) -> i32 {
220+
%c0 = "emitc.constant"() {value = 0 : i64} : () -> i64
221+
%0 = emitc.expression : !emitc.ptr<i32> {
222+
%0 = emitc.cast %arg0 : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
223+
emitc.yield %0 : !emitc.ptr<i32>
224+
}
225+
%1 = emitc.subscript %0[%c0] : (!emitc.ptr<i32>, i64) -> i32
226+
return %1 : i32
227+
}

0 commit comments

Comments
 (0)