[Mlir-commits] [mlir] d6296c3 - [mlir] Allow to use vformat utility with MLIR classes

Vladislav Vinogradov llvmlistbot at llvm.org
Tue Oct 12 03:27:16 PDT 2021


Author: Vladislav Vinogradov
Date: 2021-10-12T13:28:32+03:00
New Revision: d6296c3b0088915c6175587dda207bca81598293

URL: https://github.com/llvm/llvm-project/commit/d6296c3b0088915c6175587dda207bca81598293
DIFF: https://github.com/llvm/llvm-project/commit/d6296c3b0088915c6175587dda207bca81598293.diff

LOG: [mlir] Allow to use vformat utility with MLIR classes

Make `raw_ostream operator<<` follow const correctness semantic,
since it is a requirement of FormatVariadic implementation.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D111547

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/MemRef/IR/MemRef.h
    mlir/include/mlir/IR/OpDefinition.h
    mlir/include/mlir/IR/Operation.h
    mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
    mlir/unittests/IR/OperationSupportTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h b/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h
index a022ffc8dbc20..afb5a89e655a9 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h
@@ -24,7 +24,7 @@ namespace mlir {
 class Location;
 class OpBuilder;
 
-raw_ostream &operator<<(raw_ostream &os, Range &range);
+raw_ostream &operator<<(raw_ostream &os, const Range &range);
 
 /// Return the list of Range (i.e. offset, size, stride). Each Range
 /// entry contains either the dynamic value or a ConstantIndexOp constructed

diff  --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index e7f644f456b22..b64eb7de73868 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -222,7 +222,7 @@ inline raw_ostream &operator<<(raw_ostream &os, OpFoldResult ofr) {
 }
 
 /// Allow printing to a stream.
-inline raw_ostream &operator<<(raw_ostream &os, OpState &op) {
+inline raw_ostream &operator<<(raw_ostream &os, OpState op) {
   op.print(os, OpPrintingFlags().useLocalScope());
   return os;
 }

diff  --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index 05f8e408cf880..ac55edd9b0033 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -717,8 +717,8 @@ class alignas(8) Operation final
   size_t numTrailingObjects(OverloadToken<Region>) const { return numRegions; }
 };
 
-inline raw_ostream &operator<<(raw_ostream &os, Operation &op) {
-  op.print(os, OpPrintingFlags().useLocalScope());
+inline raw_ostream &operator<<(raw_ostream &os, const Operation &op) {
+  const_cast<Operation &>(op).print(os, OpPrintingFlags().useLocalScope());
   return os;
 }
 

diff  --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 32bd0a29867b4..67041abe69565 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -2066,7 +2066,7 @@ static LogicalResult verify(SubViewOp op) {
   return produceSubViewErrorMsg(result, op, expectedType, errMsg);
 }
 
-raw_ostream &mlir::operator<<(raw_ostream &os, Range &range) {
+raw_ostream &mlir::operator<<(raw_ostream &os, const Range &range) {
   return os << "range " << range.offset << ":" << range.size << ":"
             << range.stride;
 }

diff  --git a/mlir/unittests/IR/OperationSupportTest.cpp b/mlir/unittests/IR/OperationSupportTest.cpp
index b4fbc4be4b899..3d03329216db3 100644
--- a/mlir/unittests/IR/OperationSupportTest.cpp
+++ b/mlir/unittests/IR/OperationSupportTest.cpp
@@ -10,6 +10,7 @@
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/BuiltinTypes.h"
 #include "llvm/ADT/BitVector.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "gtest/gtest.h"
 
 using namespace mlir;
@@ -214,4 +215,14 @@ TEST(OperationOrderTest, OrderIsAlwaysValid) {
   containerOp->destroy();
 }
 
+TEST(OperationFormatPrintTest, CanUseVariadicFormat) {
+  MLIRContext context;
+  Builder builder(&context);
+
+  Operation *op = createOp(&context);
+
+  std::string str = formatv("{0}", *op).str();
+  ASSERT_STREQ(str.c_str(), "\"foo.bar\"() : () -> ()");
+}
+
 } // end namespace


        


More information about the Mlir-commits mailing list