Skip to content

Commit

Permalink
Merge pull request #289 from Xilinx/bump_to_be006372
Browse files Browse the repository at this point in the history
Merge with fixes to be00637 (30)
  • Loading branch information
mgehre-amd authored Aug 21, 2024
2 parents de3b04f + 0c4b814 commit 4fa5aa7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
17 changes: 17 additions & 0 deletions mlir/include/mlir/IR/OperationSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,13 @@ class OpPrintingFlags {
/// elements.
OpPrintingFlags &elideLargeElementsAttrs(int64_t largeElementLimit = 16);

/// Enables the printing of large element attributes with a hex string. The
/// `largeElementLimit` is used to configure what is considered to be a
/// "large" ElementsAttr by providing an upper limit to the number of
/// elements. Use -1 to disable the hex printing.
OpPrintingFlags &
printLargeElementsAttrWithHex(int64_t largeElementLimit = 100);

/// Enables breaking attributes on individual lines when there are more than
/// the given number of attributes on an operation.
OpPrintingFlags& newlineAfterAttribute(int64_t attributeLimit = 2);
Expand Down Expand Up @@ -1173,9 +1180,15 @@ class OpPrintingFlags {
/// Return if the given ElementsAttr should be elided.
bool shouldElideElementsAttr(ElementsAttr attr) const;

/// Return if the given ElementsAttr should be printed as hex string.
bool shouldPrintElementsAttrWithHex(ElementsAttr attr) const;

/// Return the size limit for printing large ElementsAttr.
std::optional<int64_t> getLargeElementsAttrLimit() const;

/// Return the size limit for printing large ElementsAttr as hex string.
int64_t getLargeElementsAttrHexLimit() const;

/// Return the size limit for printing newlines after attributes.
std::optional<unsigned> getNewlineAfterAttrLimit() const;

Expand Down Expand Up @@ -1215,6 +1228,10 @@ class OpPrintingFlags {
/// Elide printing large resources based on size of string.
std::optional<uint64_t> resourceStringCharLimit;

/// Print large element attributes with hex strings if the number of elements
/// is larger than the upper limit.
int64_t elementsAttrHexElementLimit = 100;

/// Print debug information.
bool printDebugInfoFlag : 1;
bool printDebugInfoPrettyFormFlag : 1;
Expand Down
43 changes: 23 additions & 20 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ OpPrintingFlags::OpPrintingFlags()
return;
if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences())
elementsAttrHexElementLimit =
clOptions->printElementsAttrWithHexIfLarger.getValue();
if (clOptions->newlineAfterAttr.getNumOccurrences())
newlineAfterAttr = clOptions->newlineAfterAttr;
if (clOptions->elideResourceStringsIfLarger.getNumOccurrences())
Expand All @@ -240,6 +243,12 @@ OpPrintingFlags::elideLargeElementsAttrs(int64_t largeElementLimit) {
return *this;
}

OpPrintingFlags &
OpPrintingFlags::printLargeElementsAttrWithHex(int64_t largeElementLimit) {
elementsAttrHexElementLimit = largeElementLimit;
return *this;
}

/// Enables breaking attributes on individual lines when there are more than
/// the given number of attributes on an operation.
OpPrintingFlags &
Expand Down Expand Up @@ -302,11 +311,24 @@ bool OpPrintingFlags::shouldElideElementsAttr(ElementsAttr attr) const {
!llvm::isa<SplatElementsAttr>(attr);
}

/// Return if the given ElementsAttr should be printed as hex string.
bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const {
// -1 is used to disable hex printing.
return (elementsAttrHexElementLimit != -1) &&
(elementsAttrHexElementLimit < int64_t(attr.getNumElements())) &&
!llvm::isa<SplatElementsAttr>(attr);
}

/// Return the size limit for printing large ElementsAttr.
std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
return elementsAttrElementLimit;
}

/// Return the size limit for printing large ElementsAttr as hex string.
int64_t OpPrintingFlags::getLargeElementsAttrHexLimit() const {
return elementsAttrHexElementLimit;
}

/// Return the size limit for printing newlines after attributes.
std::optional<unsigned> OpPrintingFlags::getNewlineAfterAttrLimit() const {
return newlineAfterAttr;
Expand Down Expand Up @@ -348,23 +370,6 @@ bool OpPrintingFlags::shouldPrintValueUsers() const {
return printValueUsersFlag;
}

/// Returns true if an ElementsAttr with the given number of elements should be
/// printed with hex.
static bool shouldPrintElementsAttrWithHex(int64_t numElements) {
// Check to see if a command line option was provided for the limit.
if (clOptions.isConstructed()) {
if (clOptions->printElementsAttrWithHexIfLarger.getNumOccurrences()) {
// -1 is used to disable hex printing.
if (clOptions->printElementsAttrWithHexIfLarger == -1)
return false;
return numElements > clOptions->printElementsAttrWithHexIfLarger;
}
}

// Otherwise, default to printing with hex if the number of elements is >100.
return numElements > 100;
}

//===----------------------------------------------------------------------===//
// NewLineCounter
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2509,9 +2514,7 @@ void AsmPrinter::Impl::printDenseIntOrFPElementsAttr(
auto elementType = type.getElementType();

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

0 comments on commit 4fa5aa7

Please sign in to comment.