[mlir][python] add use_name_loc_as_prefix to value.get_name() (#135052)

Add `use_name_loc_as_prefix` to `value.get_name()`.
This commit is contained in:
Maksim Levental 2025-04-09 19:28:59 -04:00 committed by GitHub
parent 8145659c39
commit 9b50167ed9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 4 deletions

View File

@ -3977,11 +3977,13 @@ void mlir::python::populateIRCore(nb::module_ &m) {
kValueDunderStrDocstring)
.def(
"get_name",
[](PyValue &self, bool useLocalScope) {
[](PyValue &self, bool useLocalScope, bool useNameLocAsPrefix) {
PyPrintAccumulator printAccum;
MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
if (useLocalScope)
mlirOpPrintingFlagsUseLocalScope(flags);
if (useNameLocAsPrefix)
mlirOpPrintingFlagsPrintNameLocAsPrefix(flags);
MlirAsmState valueState =
mlirAsmStateCreateForValue(self.get(), flags);
mlirValuePrintAsOperand(self.get(), valueState,
@ -3991,7 +3993,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
mlirAsmStateDestroy(valueState);
return printAccum.join();
},
nb::arg("use_local_scope") = false)
nb::arg("use_local_scope") = false,
nb::arg("use_name_loc_as_prefix") = false)
.def(
"get_name",
[](PyValue &self, PyAsmState &state) {

View File

@ -577,7 +577,7 @@ class Value:
Dumps a debug representation of the object to stderr.
"""
@overload
def get_name(self, use_local_scope: bool = False) -> str: ...
def get_name(self, use_local_scope: bool = False, use_name_loc_as_prefix: bool = True) -> str: ...
@overload
def get_name(self, state: AsmState) -> str:
"""
@ -2382,7 +2382,7 @@ class Operation(_OperationBase):
attributes: Dict of str:Attribute.
successors: List of Block for the operation's successors.
regions: Number of regions to create.
location: A Location object (defaults to resolve from context manager).
loc: A Location object (defaults to resolve from context manager).
ip: An InsertionPoint (defaults to resolve from context manager or set to
False to disable insertion, even with an insertion point set in the
context manager).

View File

@ -293,6 +293,28 @@ def testValuePrintAsOperand():
print(value2.get_name())
# CHECK-LABEL: TEST: testValuePrintAsOperandNamedLocPrefix
@run
def testValuePrintAsOperandNamedLocPrefix():
ctx = Context()
ctx.allow_unregistered_dialects = True
with Location.unknown(ctx):
i32 = IntegerType.get_signless(32)
module = Module.create()
with InsertionPoint(module.body):
named_value = Operation.create(
"custom.op5", results=[i32], loc=Location.name("apple")
).results[0]
# CHECK: Value(%[[VAL5:.*]] = "custom.op5"() : () -> i32)
print(named_value)
# CHECK: With use_name_loc_as_prefix
# CHECK: %apple
print("With use_name_loc_as_prefix")
print(named_value.get_name(use_name_loc_as_prefix=True))
# CHECK-LABEL: TEST: testValueSetType
@run
def testValueSetType():