Skip to content

[mlir] Python: Extend print large elements limit to resources #125738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,10 @@ void PyOperationBase::print(std::optional<int64_t> largeElementsLimit,
fileObject = nb::module_::import_("sys").attr("stdout");

MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
if (largeElementsLimit)
if (largeElementsLimit) {
mlirOpPrintingFlagsElideLargeElementsAttrs(flags, *largeElementsLimit);
mlirOpPrintingFlagsElideLargeResourceString(flags, *largeElementsLimit);
}
if (enableDebugInfo)
mlirOpPrintingFlagsEnableDebugInfo(flags, /*enable=*/true,
/*prettyForm=*/prettyDebugInfo);
Expand Down
16 changes: 14 additions & 2 deletions mlir/test/python/ir/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,24 @@ def testOperationPrint():
r"""
func.func @f1(%arg0: i32) -> i32 {
%0 = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32> loc("nom")
%1 = arith.constant dense_resource<resource1> : tensor<3xi64>
return %arg0 : i32
}

{-#
dialect_resources: {
builtin: {
resource1: "0x08000000010000000000000002000000000000000300000000000000"
}
}
#-}
""",
ctx,
)

# Test print to stdout.
# CHECK: return %arg0 : i32
# CHECK: resource1: "0x08
module.operation.print()

# Test print to text file.
Expand All @@ -607,7 +617,8 @@ def testOperationPrint():
module.operation.write_bytecode(bytecode_stream, desired_version=1)
bytecode = bytecode_stream.getvalue()
assert bytecode.startswith(b"ML\xefR"), "Expected bytecode to start with MLïR"
module_roundtrip = Module.parse(bytecode, ctx)
ctx2 = Context()
module_roundtrip = Module.parse(bytecode, ctx2)
f = io.StringIO()
module_roundtrip.operation.print(file=f)
roundtrip_value = f.getvalue()
Expand All @@ -633,7 +644,8 @@ def testOperationPrint():

# Test print with options.
# CHECK: value = dense_resource<__elided__> : tensor<4xi32>
# CHECK: "func.return"(%arg0) : (i32) -> () -:4:7
# CHECK: "func.return"(%arg0) : (i32) -> () -:5:7
# CHECK-NOT: resource1: "0x08
module.operation.print(
large_elements_limit=2,
enable_debug_info=True,
Expand Down