From 874d208016d192858db0b9b4c04dc40bd7e35ae7 Mon Sep 17 00:00:00 2001 From: Gil Rapaport Date: Sun, 24 Aug 2025 11:48:55 +0300 Subject: [PATCH] [mlir][emitc] Fix bug in ApplyOp translation 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 { %c = emitc.expression : i32 { %e = emitc.sub %p, %a : (!emitc.ptr, i32) -> !emitc.ptr %d = emitc.apply "*"(%e) : (!emitc.ptr) -> 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; } ``` --- mlir/lib/Target/Cpp/TranslateToCpp.cpp | 4 +--- mlir/test/Target/Cpp/expressions.mlir | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index a5ee64c7fe38b..704a8c6edf8aa 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -782,9 +782,7 @@ static LogicalResult printOperation(CppEmitter &emitter, if (failed(emitter.emitAssignPrefix(op))) return failure(); os << applyOp.getApplicableOperator(); - os << emitter.getOrCreateName(applyOp.getOperand()); - - return success(); + return emitter.emitOperand(applyOp.getOperand()); } static LogicalResult printOperation(CppEmitter &emitter, diff --git a/mlir/test/Target/Cpp/expressions.mlir b/mlir/test/Target/Cpp/expressions.mlir index 9316d7b77619b..857b162f0bd8b 100644 --- a/mlir/test/Target/Cpp/expressions.mlir +++ b/mlir/test/Target/Cpp/expressions.mlir @@ -295,6 +295,25 @@ func.func @different_expressions(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32) return %v_load : i32 } +// CPP-DEFAULT: int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) { +// CPP-DEFAULT-NEXT: int32_t [[VAL_3:v[0-9]+]] = *([[VAL_2]] - [[VAL_1]]); +// CPP-DEFAULT-NEXT: return [[VAL_3]]; +// CPP-DEFAULT-NEXT: } + +// CPP-DECLTOP: int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) { +// CPP-DECLTOP-NEXT: int32_t [[VAL_3:v[0-9]+]]; +// CPP-DECLTOP-NEXT: [[VAL_3]] = *([[VAL_2]] - [[VAL_1]]); +// CPP-DECLTOP-NEXT: return [[VAL_3]]; +// CPP-DECLTOP-NEXT: } +func.func @expression_with_dereference(%arg1: i32, %arg2: !emitc.ptr) -> i32 { + %c = emitc.expression : i32 { + %e = emitc.sub %arg2, %arg1 : (!emitc.ptr, i32) -> !emitc.ptr + %d = emitc.apply "*"(%e) : (!emitc.ptr) -> i32 + emitc.yield %d : i32 + } + return %c : i32 +} + // 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]]) { // CPP-DEFAULT-NEXT: int32_t [[VAL_4:v[0-9]+]] = 42; // CPP-DEFAULT-NEXT: return &[[VAL_4]] - [[VAL_2]] < [[VAL_3]];