Skip to content

Commit 03b655c

Browse files
authored
feat(printer): Print address for duplicated variable names (#66)
Should fix #62
1 parent 81d8c25 commit 03b655c

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

include/mlc/printer/ast.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ struct PrinterConfigObj : public Object {
1414
int32_t indent_spaces = 2;
1515
int8_t print_line_numbers = 0;
1616
int32_t num_context_lines = -1;
17+
bool print_addr_on_dup_var = false;
1718
mlc::List<ObjectPath> path_to_underline;
1819

1920
PrinterConfigObj() = default;
2021
explicit PrinterConfigObj(bool def_free_var, int32_t indent_spaces, int8_t print_line_numbers,
21-
int32_t num_context_lines, mlc::List<ObjectPath> path_to_underline)
22+
int32_t num_context_lines, bool print_addr_on_dup_var,
23+
mlc::List<ObjectPath> path_to_underline)
2224
: def_free_var(def_free_var), indent_spaces(indent_spaces), print_line_numbers(print_line_numbers),
23-
num_context_lines(num_context_lines), path_to_underline(path_to_underline) {}
25+
num_context_lines(num_context_lines), print_addr_on_dup_var(print_addr_on_dup_var),
26+
path_to_underline(path_to_underline) {}
2427
MLC_DEF_DYN_TYPE(MLC_EXPORTS, PrinterConfigObj, Object, "mlc.printer.PrinterConfig");
2528
};
2629

@@ -31,11 +34,13 @@ struct PrinterConfig : public ObjectRef {
3134
.Field("print_line_numbers", &PrinterConfigObj::print_line_numbers)
3235
.Field("num_context_lines", &PrinterConfigObj::num_context_lines)
3336
.Field("path_to_underline", &PrinterConfigObj::path_to_underline)
34-
.StaticFn("__init__", InitOf<PrinterConfigObj, bool, int32_t, int8_t, int32_t, mlc::List<ObjectPath>>);
37+
.Field("print_addr_on_dup_var", &PrinterConfigObj::print_addr_on_dup_var)
38+
.StaticFn("__init__", InitOf<PrinterConfigObj, bool, int32_t, int8_t, int32_t, bool, mlc::List<ObjectPath>>);
3539
explicit PrinterConfig(bool def_free_var = true, int32_t indent_spaces = 2, int8_t print_line_numbers = 0,
36-
int32_t num_context_lines = -1, mlc::List<ObjectPath> path_to_underline = {})
40+
int32_t num_context_lines = -1, bool print_addr_on_dup_var = false,
41+
mlc::List<ObjectPath> path_to_underline = {})
3742
: PrinterConfig(PrinterConfig::New(def_free_var, indent_spaces, print_line_numbers, num_context_lines,
38-
path_to_underline)) {}
43+
print_addr_on_dup_var, path_to_underline)) {}
3944
};
4045

4146
} // namespace printer

include/mlc/printer/ir_printer.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,18 @@ struct IRPrinterObj : public Object {
6969
}
7070
}
7171
// Find a unique name
72-
Str name = name_hint;
73-
for (int i = 1; defined_names.count(name) > 0; ++i) {
74-
name = name_hint.ToStdString() + '_' + std::to_string(i);
72+
std::string name_hint_str = name_hint.ToStdString();
73+
Str name(name_hint_str);
74+
if (defined_names->count(name)) {
75+
if (this->cfg->print_addr_on_dup_var) {
76+
std::ostringstream os;
77+
os << name_hint_str << "_0x" << std::setfill('0') << std::setw(12) << std::hex << (uintptr_t)(obj.get());
78+
name = Str(os.str());
79+
} else {
80+
for (int i = 1; defined_names.count(name) > 0; ++i) {
81+
name = name_hint_str + '_' + std::to_string(i);
82+
}
83+
}
7584
}
7685
defined_names->Set(name, 1);
7786
this->_VarDef(VarInfo(name, Func([name]() { return Id(name); })), obj, frame);

python/mlc/printer/ast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class PrinterConfig(Object):
1313
indent_spaces: int = 2
1414
print_line_numbers: int = 0
1515
num_context_lines: int = -1
16+
print_addr_on_dup_var: bool = False
1617
path_to_underline: list[ObjectPath] = mlcd.field(default_factory=list)
1718

1819

tests/python/test_printer_ir_printer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import mlc.printer as mlcp
24
from mlc.testing.toy_ir import Add, Assign, Func, Var
35

@@ -75,3 +77,31 @@ def test_print_bool() -> None:
7577
path = mlcp.ObjectPath.root()
7678
node = printer(True, path)
7779
assert node.to_python() == "True"
80+
81+
82+
def test_duplicated_vars() -> None:
83+
a = Var(name="a")
84+
b = Var(name="a")
85+
stmts = [
86+
Assign(lhs=b, rhs=Add(a, a)),
87+
]
88+
f = Func(
89+
name="f",
90+
args=[a],
91+
stmts=stmts,
92+
ret=b,
93+
)
94+
assert (
95+
mlcp.to_python(f)
96+
== """
97+
def f(a):
98+
a_1 = a + a
99+
return a_1
100+
""".strip()
101+
)
102+
assert re.fullmatch(
103+
r"^def f\(a\):\n"
104+
r" a_0x[0-9A-Fa-f]+ = a \+ a\n"
105+
r" return a_0x[0-9A-Fa-f]+$",
106+
mlcp.to_python(f, mlcp.PrinterConfig(print_addr_on_dup_var=True)),
107+
)

0 commit comments

Comments
 (0)