Skip to content

Commit

Permalink
[mlir] Add use nameloc to OpPrintingFlags (llvm#129584)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpienaar authored Mar 3, 2025
1 parent f6e8366 commit edb7292
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mlir/include/mlir/IR/OperationSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,9 @@ class OpPrintingFlags {
/// conflicts across all regions
OpPrintingFlags &printUniqueSSAIDs(bool enable = true);

/// Print SSA IDs using their NameLoc, if provided, as prefix.
OpPrintingFlags &printNameLocAsPrefix(bool enable = true);

/// Return if the given ElementsAttr should be elided.
bool shouldElideElementsAttr(ElementsAttr attr) const;

Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const {
!llvm::isa<SplatElementsAttr>(attr);
}

OpPrintingFlags &OpPrintingFlags::printNameLocAsPrefix(bool enable) {
useNameLocAsPrefix = enable;
return *this;
}

/// Return the size limit for printing large ElementsAttr.
std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
return elementsAttrElementLimit;
Expand Down
20 changes: 20 additions & 0 deletions mlir/unittests/IR/OperationSupportTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ TEST(OperationFormatPrintTest, CanUseVariadicFormat) {
op->destroy();
}

TEST(OperationFormatPrintTest, CanPrintNameAsPrefix) {
MLIRContext context;
Builder builder(&context);

context.allowUnregisteredDialects();
Operation *op = Operation::create(
NameLoc::get(StringAttr::get(&context, "my_named_loc")),
OperationName("t.op", &context), builder.getIntegerType(16), std::nullopt,
std::nullopt, nullptr, std::nullopt, 0);

std::string str;
OpPrintingFlags flags;
flags.printNameLocAsPrefix(true);
llvm::raw_string_ostream os(str);
op->print(os, flags);
ASSERT_STREQ(str.c_str(), "%my_named_loc = \"t.op\"() : () -> i16\n");

op->destroy();
}

TEST(NamedAttrListTest, TestAppendAssign) {
MLIRContext ctx;
NamedAttrList attrs;
Expand Down

0 comments on commit edb7292

Please sign in to comment.