Skip to content

Commit f179e98

Browse files
authored
[mlir][emitc] Fix bug in ApplyOp translation (#155171)
The translator emits `emitc.apply` incorrectly when the op is part of an expression, as it prints the name of the operand instead of calling emitOperand() which takes into account the expression being emitted, leaving out the part of the expression feeding this op, e.g. ```mlir func.func @foo(%a: i32, %p: !emitc.ptr<i32>) -> i32 { %c = emitc.expression : i32 { %e = emitc.sub %p, %a : (!emitc.ptr<i32>, i32) -> !emitc.ptr<i32> %d = emitc.apply "*"(%e) : (!emitc.ptr<i32>) -> i32 emitc.yield %d : i32 } return %c : i32 } ``` translates to: ```C int32_t foo(int32_t v1, int32_t* v2) { int32_t v3 = *v4; return v3; } ``` instead of: ```C int32_t foo(int32_t v1, int32_t* v2) { int32_t v3 = *(v2 - v1); return v3; } ```
1 parent 4780bd9 commit f179e98

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
782782
if (failed(emitter.emitAssignPrefix(op)))
783783
return failure();
784784
os << applyOp.getApplicableOperator();
785-
os << emitter.getOrCreateName(applyOp.getOperand());
786-
787-
return success();
785+
return emitter.emitOperand(applyOp.getOperand());
788786
}
789787

790788
static LogicalResult printOperation(CppEmitter &emitter,

mlir/test/Target/Cpp/expressions.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,25 @@ func.func @different_expressions(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32)
295295
return %v_load : i32
296296
}
297297

298+
// CPP-DEFAULT: int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) {
299+
// CPP-DEFAULT-NEXT: int32_t [[VAL_3:v[0-9]+]] = *([[VAL_2]] - [[VAL_1]]);
300+
// CPP-DEFAULT-NEXT: return [[VAL_3]];
301+
// CPP-DEFAULT-NEXT: }
302+
303+
// CPP-DECLTOP: int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) {
304+
// CPP-DECLTOP-NEXT: int32_t [[VAL_3:v[0-9]+]];
305+
// CPP-DECLTOP-NEXT: [[VAL_3]] = *([[VAL_2]] - [[VAL_1]]);
306+
// CPP-DECLTOP-NEXT: return [[VAL_3]];
307+
// CPP-DECLTOP-NEXT: }
308+
func.func @expression_with_dereference(%arg1: i32, %arg2: !emitc.ptr<i32>) -> i32 {
309+
%c = emitc.expression : i32 {
310+
%e = emitc.sub %arg2, %arg1 : (!emitc.ptr<i32>, i32) -> !emitc.ptr<i32>
311+
%d = emitc.apply "*"(%e) : (!emitc.ptr<i32>) -> i32
312+
emitc.yield %d : i32
313+
}
314+
return %c : i32
315+
}
316+
298317
// CPP-DEFAULT: bool expression_with_address_taken(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t* [[VAL_3]]) {
299318
// CPP-DEFAULT-NEXT: int32_t [[VAL_4:v[0-9]+]] = 42;
300319
// CPP-DEFAULT-NEXT: return &[[VAL_4]] - [[VAL_2]] < [[VAL_3]];

0 commit comments

Comments
 (0)