Skip to content

Commit 68b071d

Browse files
author
Simon Camphausen
authored
[mlir][emitc] Fix corner case in translation of literal ops (#71375)
Fix a corner case missed in #71296 when operands generated by literals are mixed with the args attribute of a call op. Additionally remove a range check that is already handled by the CallOp verifier.
1 parent 4a8b0ea commit 68b071d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,13 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::CallOp callOp) {
425425
// Index attributes are treated specially as operand index.
426426
if (t.getType().isIndex()) {
427427
int64_t idx = t.getInt();
428-
if ((idx < 0) || (idx >= op.getNumOperands()))
429-
return op.emitOpError("invalid operand index");
430-
if (!emitter.hasValueInScope(op.getOperand(idx)))
428+
Value operand = op.getOperand(idx);
429+
auto literalDef =
430+
dyn_cast_if_present<LiteralOp>(operand.getDefiningOp());
431+
if (!literalDef && !emitter.hasValueInScope(operand))
431432
return op.emitOpError("operand ")
432433
<< idx << "'s value not defined in scope";
433-
os << emitter.getOrCreateName(op.getOperand(idx));
434+
os << emitter.getOrCreateName(operand);
434435
return success();
435436
}
436437
}

mlir/test/Target/Cpp/literal_call_operand.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ func.func @emitc_call_operand() {
1212
// CPP-DECLTOP: void emitc_call_operand() {
1313
// CPP-DECLTOP-NEXT: float v1;
1414
// CPP-DECLTOP-NEXT: v1 = foo(M_PI);
15+
16+
func.func @emitc_call_operand_arg() {
17+
%p0 = emitc.literal "M_PI" : f32
18+
%1 = emitc.call "bar"(%p0) {args = [42 : i32, 0 : index]} : (f32) -> f32
19+
return
20+
}
21+
// CPP-DEFAULT: void emitc_call_operand_arg() {
22+
// CPP-DEFAULT-NEXT: float v1 = bar(42, M_PI);
23+
24+
// CPP-DECLTOP: void emitc_call_operand_arg() {
25+
// CPP-DECLTOP-NEXT: float v1;
26+
// CPP-DECLTOP-NEXT: v1 = bar(42, M_PI);

0 commit comments

Comments
 (0)