Skip to content

Commit edb7292

Browse files
authored
[mlir] Add use nameloc to OpPrintingFlags (#129584)
1 parent f6e8366 commit edb7292

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,9 @@ class OpPrintingFlags {
11951195
/// conflicts across all regions
11961196
OpPrintingFlags &printUniqueSSAIDs(bool enable = true);
11971197

1198+
/// Print SSA IDs using their NameLoc, if provided, as prefix.
1199+
OpPrintingFlags &printNameLocAsPrefix(bool enable = true);
1200+
11981201
/// Return if the given ElementsAttr should be elided.
11991202
bool shouldElideElementsAttr(ElementsAttr attr) const;
12001203

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const {
327327
!llvm::isa<SplatElementsAttr>(attr);
328328
}
329329

330+
OpPrintingFlags &OpPrintingFlags::printNameLocAsPrefix(bool enable) {
331+
useNameLocAsPrefix = enable;
332+
return *this;
333+
}
334+
330335
/// Return the size limit for printing large ElementsAttr.
331336
std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
332337
return elementsAttrElementLimit;

mlir/unittests/IR/OperationSupportTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ TEST(OperationFormatPrintTest, CanUseVariadicFormat) {
230230
op->destroy();
231231
}
232232

233+
TEST(OperationFormatPrintTest, CanPrintNameAsPrefix) {
234+
MLIRContext context;
235+
Builder builder(&context);
236+
237+
context.allowUnregisteredDialects();
238+
Operation *op = Operation::create(
239+
NameLoc::get(StringAttr::get(&context, "my_named_loc")),
240+
OperationName("t.op", &context), builder.getIntegerType(16), std::nullopt,
241+
std::nullopt, nullptr, std::nullopt, 0);
242+
243+
std::string str;
244+
OpPrintingFlags flags;
245+
flags.printNameLocAsPrefix(true);
246+
llvm::raw_string_ostream os(str);
247+
op->print(os, flags);
248+
ASSERT_STREQ(str.c_str(), "%my_named_loc = \"t.op\"() : () -> i16\n");
249+
250+
op->destroy();
251+
}
252+
233253
TEST(NamedAttrListTest, TestAppendAssign) {
234254
MLIRContext ctx;
235255
NamedAttrList attrs;

0 commit comments

Comments
 (0)