-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[mlir][emitc] Fix bug in ApplyOp translation #155171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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; } ```
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-emitc Author: Gil Rapaport (aniragil) ChangesThe translator emits 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: int32_t foo(int32_t v1, int32_t* v2) {
int32_t v3 = *v4;
return v3;
} instead of: int32_t foo(int32_t v1, int32_t* v2) {
int32_t v3 = *(v2 - v1);
return v3;
} Full diff: https://github.com/llvm/llvm-project/pull/155171.diff 2 Files Affected:
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>) -> i32 {
+ %c = emitc.expression : i32 {
+ %e = emitc.sub %arg2, %arg1 : (!emitc.ptr<i32>, i32) -> !emitc.ptr<i32>
+ %d = emitc.apply "*"(%e) : (!emitc.ptr<i32>) -> 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]];
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good to me!
Thanks @simon-camp ! |
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.translates to:
instead of: