Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be00637

Browse files
[mlir][OpPrintingFlags] Allow to disable ElementsAttr hex printing (llvm#85766)
At present, large ElementsAttr is unconditionally printed with a hex string. This means that in IR large constant values often look like: dense<"0x000000000004000000080000000004000000080000000..."> : tensor<10x10xi32> Hoisting hex printing control to the user level for tooling means that one can disable the feature and get human-readable values when necessary: dense<[16, 32, 48, 500...]> : tensor<10x10xi32> Note: AsmPrinterOptions::printElementsAttrWithHexIfLarger is not always possible to be used as it requires that one exposes MLIR's command-line options in user tooling (including an actual compiler). Co-authored-by: Harald Rotuna <[email protected]>
1 parent 3b74f8c commit be00637

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,13 @@ class OpPrintingFlags {
11361136
/// elements.
11371137
OpPrintingFlags &elideLargeElementsAttrs(int64_t largeElementLimit = 16);
11381138

1139+
/// Enables the printing of large element attributes with a hex string. The
1140+
/// `largeElementLimit` is used to configure what is considered to be a
1141+
/// "large" ElementsAttr by providing an upper limit to the number of
1142+
/// elements. Use -1 to disable the hex printing.
1143+
OpPrintingFlags &
1144+
printLargeElementsAttrWithHex(int64_t largeElementLimit = 100);
1145+
11391146
/// Enables the elision of large resources strings by omitting them from the
11401147
/// `dialect_resources` section. The `largeResourceLimit` is used to configure
11411148
/// what is considered to be a "large" resource by providing an upper limit to
@@ -1169,9 +1176,15 @@ class OpPrintingFlags {
11691176
/// Return if the given ElementsAttr should be elided.
11701177
bool shouldElideElementsAttr(ElementsAttr attr) const;
11711178

1179+
/// Return if the given ElementsAttr should be printed as hex string.
1180+
bool shouldPrintElementsAttrWithHex(ElementsAttr attr) const;
1181+
11721182
/// Return the size limit for printing large ElementsAttr.
11731183
std::optional<int64_t> getLargeElementsAttrLimit() const;
11741184

1185+
/// Return the size limit for printing large ElementsAttr as hex string.
1186+
int64_t getLargeElementsAttrHexLimit() const;
1187+
11751188
/// Return the size limit in chars for printing large resources.
11761189
std::optional<uint64_t> getLargeResourceStringLimit() const;
11771190

@@ -1204,6 +1217,10 @@ class OpPrintingFlags {
12041217
/// Elide printing large resources based on size of string.
12051218
std::optional<uint64_t> resourceStringCharLimit;
12061219

1220+
/// Print large element attributes with hex strings if the number of elements
1221+
/// is larger than the upper limit.
1222+
int64_t elementsAttrHexElementLimit = 100;
1223+
12071224
/// Print debug information.
12081225
bool printDebugInfoFlag : 1;
12091226
bool printDebugInfoPrettyFormFlag : 1;

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ OpPrintingFlags::OpPrintingFlags()
212212
return;
213213
if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
214214
elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
215+
if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences())
216+
elementsAttrHexElementLimit =
217+
clOptions->printElementsAttrWithHexIfLarger.getValue();
215218
if (clOptions->elideResourceStringsIfLarger.getNumOccurrences())
216219
resourceStringCharLimit = clOptions->elideResourceStringsIfLarger;
217220
printDebugInfoFlag = clOptions->printDebugInfoOpt;
@@ -233,6 +236,12 @@ OpPrintingFlags::elideLargeElementsAttrs(int64_t largeElementLimit) {
233236
return *this;
234237
}
235238

239+
OpPrintingFlags &
240+
OpPrintingFlags::printLargeElementsAttrWithHex(int64_t largeElementLimit) {
241+
elementsAttrHexElementLimit = largeElementLimit;
242+
return *this;
243+
}
244+
236245
OpPrintingFlags &
237246
OpPrintingFlags::elideLargeResourceString(int64_t largeResourceLimit) {
238247
resourceStringCharLimit = largeResourceLimit;
@@ -287,11 +296,24 @@ bool OpPrintingFlags::shouldElideElementsAttr(ElementsAttr attr) const {
287296
!llvm::isa<SplatElementsAttr>(attr);
288297
}
289298

299+
/// Return if the given ElementsAttr should be printed as hex string.
300+
bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const {
301+
// -1 is used to disable hex printing.
302+
return (elementsAttrHexElementLimit != -1) &&
303+
(elementsAttrHexElementLimit < int64_t(attr.getNumElements())) &&
304+
!llvm::isa<SplatElementsAttr>(attr);
305+
}
306+
290307
/// Return the size limit for printing large ElementsAttr.
291308
std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
292309
return elementsAttrElementLimit;
293310
}
294311

312+
/// Return the size limit for printing large ElementsAttr as hex string.
313+
int64_t OpPrintingFlags::getLargeElementsAttrHexLimit() const {
314+
return elementsAttrHexElementLimit;
315+
}
316+
295317
/// Return the size limit for printing large ElementsAttr.
296318
std::optional<uint64_t> OpPrintingFlags::getLargeResourceStringLimit() const {
297319
return resourceStringCharLimit;
@@ -328,23 +350,6 @@ bool OpPrintingFlags::shouldPrintValueUsers() const {
328350
return printValueUsersFlag;
329351
}
330352

331-
/// Returns true if an ElementsAttr with the given number of elements should be
332-
/// printed with hex.
333-
static bool shouldPrintElementsAttrWithHex(int64_t numElements) {
334-
// Check to see if a command line option was provided for the limit.
335-
if (clOptions.isConstructed()) {
336-
if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences()) {
337-
// -1 is used to disable hex printing.
338-
if (clOptions->printElementsAttrWithHexIfLarger == -1)
339-
return false;
340-
return numElements > clOptions->printElementsAttrWithHexIfLarger;
341-
}
342-
}
343-
344-
// Otherwise, default to printing with hex if the number of elements is >100.
345-
return numElements > 100;
346-
}
347-
348353
//===----------------------------------------------------------------------===//
349354
// NewLineCounter
350355
//===----------------------------------------------------------------------===//
@@ -2435,9 +2440,7 @@ void AsmPrinter::Impl::printDenseIntOrFPElementsAttr(
24352440
auto elementType = type.getElementType();
24362441

24372442
// Check to see if we should format this attribute as a hex string.
2438-
auto numElements = type.getNumElements();
2439-
if (!attr.isSplat() && allowHex &&
2440-
shouldPrintElementsAttrWithHex(numElements)) {
2443+
if (allowHex && printerFlags.shouldPrintElementsAttrWithHex(attr)) {
24412444
ArrayRef<char> rawData = attr.getRawData();
24422445
if (llvm::endianness::native == llvm::endianness::big) {
24432446
// Convert endianess in big-endian(BE) machines. `rawData` is BE in BE

0 commit comments

Comments
 (0)