Skip to content

Commit 5954e9c

Browse files
authored
[MLIR][Target/Cpp] Fix variable naming conflict for function declarations (#147927)
This is a fix for #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 iree-org/iree#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`.
1 parent 18627e9 commit 5954e9c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
12581258
DeclareFuncOp declareFuncOp) {
12591259
raw_indented_ostream &os = emitter.ostream();
12601260

1261+
CppEmitter::FunctionScope scope(emitter);
12611262
auto functionOp = SymbolTable::lookupNearestSymbolFrom<emitc::FuncOp>(
12621263
declareFuncOp, declareFuncOp.getSymNameAttr());
12631264

mlir/test/Target/Cpp/declare_func.mlir

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ emitc.declare_func @array_arg
2424
emitc.func @array_arg(%arg0: !emitc.array<3xi32>) {
2525
emitc.return
2626
}
27+
28+
// CHECK: int32_t foo1(int32_t [[V1:[^ ]*]]);
29+
emitc.declare_func @foo1
30+
// CHECK: int32_t foo2(int32_t [[V1]]);
31+
emitc.declare_func @foo2
32+
// CHECK: int32_t foo1(int32_t [[V1]]) {
33+
emitc.func @foo1(%arg0: i32) -> i32 {
34+
// CHECK-NOT: int32_t [[V1]] = 0;
35+
%0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
36+
// CHECK: return [[V1]];
37+
emitc.return %arg0 : i32
38+
}
39+
// CHECK: int32_t foo2(int32_t [[V1]]) {
40+
emitc.func @foo2(%arg0: i32) -> i32 {
41+
// CHECK: return [[V1]];
42+
emitc.return %arg0 : i32
43+
}

0 commit comments

Comments
 (0)