Skip to content
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

[mlir] Add use nameloc to OpPrintingFlags #129584

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)");

op->destroy();
}

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