[Mlir-commits] [mlir] 5954e9c - [MLIR][Target/Cpp] Fix variable naming conflict for function declarations (#147927)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Jul 10 07:09:54 PDT 2025
Author: Niklas Degener
Date: 2025-07-10T16:09:49+02:00
New Revision: 5954e9c1a56fc313394b9dcc21f20c20c058f83d
URL: https://github.com/llvm/llvm-project/commit/5954e9c1a56fc313394b9dcc21f20c20c058f83d
DIFF: https://github.com/llvm/llvm-project/commit/5954e9c1a56fc313394b9dcc21f20c20c058f83d.diff
LOG: [MLIR][Target/Cpp] Fix variable naming conflict for function declarations (#147927)
This is a fix for https://github.com/llvm/llvm-project/pull/136102. It
missed scoping for `DeclareFuncOps`.
In scenarios with multiple function declarations, the `valueMapper`
wasn't updated and later uses of values in other functions still used
the assigned names in prior functions.
This is visible in the reproducer here
https://github.com/iree-org/iree/issues/21303: Although the counter for
variable enumeration was reset, as it is visible for the local vars, the
function arguments were mapped to old names. Due to this mapping, the
counter was never increased, and the local variables conflicted with the
arguments.
This fix adds proper scoping for declarations and a test-case to cover
the scenario with multiple `DeclareFuncOps`.
Added:
Modified:
mlir/lib/Target/Cpp/TranslateToCpp.cpp
mlir/test/Target/Cpp/declare_func.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 06977e07fda5b..a393d88ba4a08 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -1258,6 +1258,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
DeclareFuncOp declareFuncOp) {
raw_indented_ostream &os = emitter.ostream();
+ CppEmitter::FunctionScope scope(emitter);
auto functionOp = SymbolTable::lookupNearestSymbolFrom<emitc::FuncOp>(
declareFuncOp, declareFuncOp.getSymNameAttr());
diff --git a/mlir/test/Target/Cpp/declare_func.mlir b/mlir/test/Target/Cpp/declare_func.mlir
index 6901e135df389..0208b92b30860 100644
--- a/mlir/test/Target/Cpp/declare_func.mlir
+++ b/mlir/test/Target/Cpp/declare_func.mlir
@@ -24,3 +24,20 @@ emitc.declare_func @array_arg
emitc.func @array_arg(%arg0: !emitc.array<3xi32>) {
emitc.return
}
+
+// CHECK: int32_t foo1(int32_t [[V1:[^ ]*]]);
+emitc.declare_func @foo1
+// CHECK: int32_t foo2(int32_t [[V1]]);
+emitc.declare_func @foo2
+// CHECK: int32_t foo1(int32_t [[V1]]) {
+emitc.func @foo1(%arg0: i32) -> i32 {
+ // CHECK-NOT: int32_t [[V1]] = 0;
+ %0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
+ // CHECK: return [[V1]];
+ emitc.return %arg0 : i32
+}
+// CHECK: int32_t foo2(int32_t [[V1]]) {
+emitc.func @foo2(%arg0: i32) -> i32 {
+ // CHECK: return [[V1]];
+ emitc.return %arg0 : i32
+}
\ No newline at end of file
More information about the Mlir-commits
mailing list