[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
This commit is contained in:
Vladislav Vinogradov 2021-10-11 17:02:03 +03:00
parent 477f5f4fca
commit d6296c3b00
5 changed files with 16 additions and 5 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -717,8 +717,8 @@ private:
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;
}

View File

@ -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;
}

View File

@ -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