Skip to content

Commit 9b50167

Browse files
authored
[mlir][python] add use_name_loc_as_prefix to value.get_name() (#135052)
Add `use_name_loc_as_prefix` to `value.get_name()`.
1 parent 8145659 commit 9b50167

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,11 +3977,13 @@ void mlir::python::populateIRCore(nb::module_ &m) {
39773977
kValueDunderStrDocstring)
39783978
.def(
39793979
"get_name",
3980-
[](PyValue &self, bool useLocalScope) {
3980+
[](PyValue &self, bool useLocalScope, bool useNameLocAsPrefix) {
39813981
PyPrintAccumulator printAccum;
39823982
MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
39833983
if (useLocalScope)
39843984
mlirOpPrintingFlagsUseLocalScope(flags);
3985+
if (useNameLocAsPrefix)
3986+
mlirOpPrintingFlagsPrintNameLocAsPrefix(flags);
39853987
MlirAsmState valueState =
39863988
mlirAsmStateCreateForValue(self.get(), flags);
39873989
mlirValuePrintAsOperand(self.get(), valueState,
@@ -3991,7 +3993,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
39913993
mlirAsmStateDestroy(valueState);
39923994
return printAccum.join();
39933995
},
3994-
nb::arg("use_local_scope") = false)
3996+
nb::arg("use_local_scope") = false,
3997+
nb::arg("use_name_loc_as_prefix") = false)
39953998
.def(
39963999
"get_name",
39974000
[](PyValue &self, PyAsmState &state) {

mlir/python/mlir/_mlir_libs/_mlir/ir.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ class Value:
577577
Dumps a debug representation of the object to stderr.
578578
"""
579579
@overload
580-
def get_name(self, use_local_scope: bool = False) -> str: ...
580+
def get_name(self, use_local_scope: bool = False, use_name_loc_as_prefix: bool = True) -> str: ...
581581
@overload
582582
def get_name(self, state: AsmState) -> str:
583583
"""
@@ -2382,7 +2382,7 @@ class Operation(_OperationBase):
23822382
attributes: Dict of str:Attribute.
23832383
successors: List of Block for the operation's successors.
23842384
regions: Number of regions to create.
2385-
location: A Location object (defaults to resolve from context manager).
2385+
loc: A Location object (defaults to resolve from context manager).
23862386
ip: An InsertionPoint (defaults to resolve from context manager or set to
23872387
False to disable insertion, even with an insertion point set in the
23882388
context manager).

mlir/test/python/ir/value.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ def testValuePrintAsOperand():
293293
print(value2.get_name())
294294

295295

296+
# CHECK-LABEL: TEST: testValuePrintAsOperandNamedLocPrefix
297+
@run
298+
def testValuePrintAsOperandNamedLocPrefix():
299+
ctx = Context()
300+
ctx.allow_unregistered_dialects = True
301+
with Location.unknown(ctx):
302+
i32 = IntegerType.get_signless(32)
303+
304+
module = Module.create()
305+
with InsertionPoint(module.body):
306+
named_value = Operation.create(
307+
"custom.op5", results=[i32], loc=Location.name("apple")
308+
).results[0]
309+
# CHECK: Value(%[[VAL5:.*]] = "custom.op5"() : () -> i32)
310+
print(named_value)
311+
312+
# CHECK: With use_name_loc_as_prefix
313+
# CHECK: %apple
314+
print("With use_name_loc_as_prefix")
315+
print(named_value.get_name(use_name_loc_as_prefix=True))
316+
317+
296318
# CHECK-LABEL: TEST: testValueSetType
297319
@run
298320
def testValueSetType():

0 commit comments

Comments
 (0)