Simplify the emission of a few op parser diagnostics. This also adds the ability...
authorRiver Riddle <riverriddle@google.com>
Thu, 9 May 2019 05:42:58 +0000 (22:42 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 11 May 2019 02:25:12 +0000 (19:25 -0700)
--

PiperOrigin-RevId: 247359911

mlir/include/mlir/IR/Diagnostics.h
mlir/lib/IR/Diagnostics.cpp
mlir/lib/IR/StandardTypes.cpp
mlir/lib/LLVMIR/IR/LLVMDialect.cpp
mlir/lib/Linalg/IR/LinalgOps.cpp
mlir/lib/StandardOps/Ops.cpp

index c7b6266..9156e54 100644 (file)
@@ -63,6 +63,7 @@ public:
   /// Enum that represents the different kinds of diagnostic arguments
   /// supported.
   enum class DiagnosticArgumentKind {
+    Attribute,
     Double,
     Integer,
     String,
@@ -76,6 +77,9 @@ public:
   /// Returns the kind of this argument.
   DiagnosticArgumentKind getKind() const { return kind; }
 
+  /// Returns this argument as an Attribute.
+  Attribute getAsAttribute() const;
+
   /// Returns this argument as a double.
   double getAsDouble() const {
     assert(getKind() == DiagnosticArgumentKind::Double);
@@ -106,6 +110,9 @@ public:
 private:
   friend class Diagnostic;
 
+  // Construct from an Attribute.
+  explicit DiagnosticArgument(Attribute attr);
+
   // Construct from a floating point number.
   explicit DiagnosticArgument(double val)
       : kind(DiagnosticArgumentKind::Double), doubleVal(val) {}
index ea994bc..a265171 100644 (file)
@@ -16,6 +16,7 @@
 // =============================================================================
 
 #include "mlir/IR/Diagnostics.h"
+#include "mlir/IR/Attributes.h"
 #include "mlir/IR/Identifier.h"
 #include "mlir/IR/Location.h"
 #include "mlir/IR/MLIRContext.h"
@@ -32,11 +33,23 @@ using namespace mlir::detail;
 // DiagnosticArgument
 //===----------------------------------------------------------------------===//
 
+// Construct from an Attribute.
+DiagnosticArgument::DiagnosticArgument(Attribute attr)
+    : kind(DiagnosticArgumentKind::Attribute),
+      opaqueVal(reinterpret_cast<intptr_t>(attr.getAsOpaquePointer())) {}
+
 // Construct from a Type.
 DiagnosticArgument::DiagnosticArgument(Type val)
     : kind(DiagnosticArgumentKind::Type),
       opaqueVal(reinterpret_cast<intptr_t>(val.getAsOpaquePointer())) {}
 
+/// Returns this argument as an Attribute.
+Attribute DiagnosticArgument::getAsAttribute() const {
+  assert(getKind() == DiagnosticArgumentKind::Attribute);
+  return Attribute::getFromOpaquePointer(
+      reinterpret_cast<const void *>(opaqueVal));
+}
+
 /// Returns this argument as a Type.
 Type DiagnosticArgument::getAsType() const {
   assert(getKind() == DiagnosticArgumentKind::Type);
@@ -46,6 +59,9 @@ Type DiagnosticArgument::getAsType() const {
 /// Outputs this argument to a stream.
 void DiagnosticArgument::print(raw_ostream &os) const {
   switch (kind) {
+  case DiagnosticArgumentKind::Attribute:
+    os << getAsAttribute();
+    break;
   case DiagnosticArgumentKind::Double:
     os << getAsDouble();
     break;
index d727fb1..5af031e 100644 (file)
@@ -333,12 +333,11 @@ MemRefType MemRefType::getImpl(ArrayRef<int64_t> shape, Type elementType,
   for (const auto &affineMap : affineMapComposition) {
     if (affineMap.getNumDims() != dim) {
       if (location)
-        context->emitError(
-            *location,
-            "memref affine map dimension mismatch between " +
-                (i == 0 ? Twine("memref rank") : "affine map " + Twine(i)) +
-                " and affine map" + Twine(i + 1) + ": " + Twine(dim) +
-                " != " + Twine(affineMap.getNumDims()));
+        context->emitError(*location)
+            << "memref affine map dimension mismatch between "
+            << (i == 0 ? Twine("memref rank") : "affine map " + Twine(i))
+            << " and affine map" << i + 1 << ": " << dim
+            << " != " << affineMap.getNumDims();
       return nullptr;
     }
 
index 48cc476..c77c816 100644 (file)
@@ -151,10 +151,9 @@ static ParseResult parseICmpOp(OpAsmParser *parser, OperationState *result) {
                              "expected 'predicate' attribute of string type");
   int predicateValue = getICmpPredicateByName(predicateStr.getValue());
   if (predicateValue == -1)
-    return parser->emitError(
-        predicateLoc,
-        "'" + Twine(predicateStr.getValue()) +
-            "' is an incorrect value of the 'predicate' attribute");
+    return parser->emitError(predicateLoc)
+           << "'" << predicateStr.getValue()
+           << "' is an incorrect value of the 'predicate' attribute";
 
   attrs[0].second = parser->getBuilder().getI64IntegerAttr(predicateValue);
 
index daa2cd3..356a906 100644 (file)
@@ -212,9 +212,8 @@ ParseResult mlir::SliceOp::parse(OpAsmParser *parser, OperationState *result) {
     return parser->emitError(parser->getNameLoc(),
                              "view type expected for first type");
   if (indexingsInfo.size() != baseViewType.getRank())
-    return parser->emitError(parser->getNameLoc(),
-                             "expected " + Twine(baseViewType.getRank()) +
-                                 " indexings");
+    return parser->emitError(parser->getNameLoc(), "expected ")
+           << baseViewType.getRank() << " indexings";
   ViewType viewType = types.back().dyn_cast<ViewType>();
   if (!viewType)
     return parser->emitError(parser->getNameLoc(), "view type expected");
@@ -222,9 +221,8 @@ ParseResult mlir::SliceOp::parse(OpAsmParser *parser, OperationState *result) {
   ArrayRef<Type> indexingTypes =
       ArrayRef<Type>(types).drop_front(1).drop_back(1);
   if (indexingTypes.size() != baseViewType.getRank())
-    return parser->emitError(parser->getNameLoc(),
-                             "expected " + Twine(baseViewType.getRank()) +
-                                 " indexing types");
+    return parser->emitError(parser->getNameLoc(), "expected ")
+           << baseViewType.getRank() << " indexing types";
   return failure(
       parser->resolveOperand(baseInfo, baseViewType, result->operands) ||
       (!indexingsInfo.empty() &&
@@ -326,9 +324,8 @@ ParseResult mlir::ViewOp::parse(OpAsmParser *parser, OperationState *result) {
   if (!viewType)
     return parser->emitError(parser->getNameLoc(), "view type expected");
   if (viewType.getRank() != indexingsInfo.size())
-    return parser->emitError(parser->getNameLoc(),
-                             "expected" + Twine(viewType.getRank()) +
-                                 " range indexings");
+    return parser->emitError(parser->getNameLoc(), "expected")
+           << viewType.getRank() << " range indexings";
   return failure(
       parser->resolveOperand(
           bufferInfo,
index 9883b02..f9b13ce 100644 (file)
@@ -694,9 +694,8 @@ ParseResult CmpIOp::parse(OpAsmParser *parser, OperationState *result) {
   StringRef predicateName = predicateNameAttr.cast<StringAttr>().getValue();
   auto predicate = getPredicateByName(predicateName);
   if (predicate == CmpIPredicate::NumPredicates)
-    return parser->emitError(parser->getNameLoc(),
-                             "unknown comparison predicate \"" + predicateName +
-                                 "\"");
+    return parser->emitError(parser->getNameLoc())
+           << "unknown comparison predicate \"" << predicateName << "\"";
 
   auto builder = parser->getBuilder();
   Type i1Type = getCheckedI1SameShape(&builder, type);