From 89105bcd4af50aeeab150c1ef47668ead786ed43 Mon Sep 17 00:00:00 2001 From: Jacques Pienaar Date: Mon, 3 Mar 2025 20:11:51 +0000 Subject: [PATCH 1/2] [mlir] Add use nameloc to OpPrintingFlags --- mlir/include/mlir/IR/OperationSupport.h | 3 +++ mlir/lib/IR/AsmPrinter.cpp | 5 +++++ mlir/unittests/IR/OperationSupportTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h index fbe0ed29a4d11..2b0e50437afcc 100644 --- a/mlir/include/mlir/IR/OperationSupport.h +++ b/mlir/include/mlir/IR/OperationSupport.h @@ -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; diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index 1f22d4f37a813..16bc857ad3416 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -327,6 +327,11 @@ bool OpPrintingFlags::shouldPrintElementsAttrWithHex(ElementsAttr attr) const { !llvm::isa(attr); } +OpPrintingFlags &OpPrintingFlags::printNameLocAsPrefix(bool enable) { + useNameLocAsPrefix = enable; + return *this; +} + /// Return the size limit for printing large ElementsAttr. std::optional OpPrintingFlags::getLargeElementsAttrLimit() const { return elementsAttrElementLimit; diff --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp index f94dc78445807..c03b79aae3321 100644 --- a/mlir/unittests/IR/OperationSupportTest.cpp +++ b/mlir/unittests/IR/OperationSupportTest.cpp @@ -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; From 561ad95d684132478ba7fe7e9adfec1fdba57ed7 Mon Sep 17 00:00:00 2001 From: Jacques Pienaar Date: Mon, 3 Mar 2025 14:21:55 -0800 Subject: [PATCH 2/2] Update OperationSupportTest.cpp Fix typo introduced. --- mlir/unittests/IR/OperationSupportTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp index c03b79aae3321..bac2b72b68deb 100644 --- a/mlir/unittests/IR/OperationSupportTest.cpp +++ b/mlir/unittests/IR/OperationSupportTest.cpp @@ -245,7 +245,7 @@ TEST(OperationFormatPrintTest, CanPrintNameAsPrefix) { flags.printNameLocAsPrefix(true); llvm::raw_string_ostream os(str); op->print(os, flags); - ASSERT_STREQ(str.c_str(), "%my_named_loc = \"t.op\"() : () -> (i16)"); + ASSERT_STREQ(str.c_str(), "%my_named_loc = \"t.op\"() : () -> i16\n"); op->destroy(); }