From: River Riddle Date: Thu, 9 May 2019 05:42:58 +0000 (-0700) Subject: Simplify the emission of a few op parser diagnostics. This also adds the ability... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e05eda9d2237f3907aa9166095b3be8d3c7a24d7;p=platform%2Fupstream%2Fllvm.git Simplify the emission of a few op parser diagnostics. This also adds the ability to stream an attribute into a Diagnostic. -- PiperOrigin-RevId: 247359911 --- diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h index c7b6266..9156e54 100644 --- a/mlir/include/mlir/IR/Diagnostics.h +++ b/mlir/include/mlir/IR/Diagnostics.h @@ -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) {} diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp index ea994bc..a265171 100644 --- a/mlir/lib/IR/Diagnostics.cpp +++ b/mlir/lib/IR/Diagnostics.cpp @@ -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(attr.getAsOpaquePointer())) {} + // Construct from a Type. DiagnosticArgument::DiagnosticArgument(Type val) : kind(DiagnosticArgumentKind::Type), opaqueVal(reinterpret_cast(val.getAsOpaquePointer())) {} +/// Returns this argument as an Attribute. +Attribute DiagnosticArgument::getAsAttribute() const { + assert(getKind() == DiagnosticArgumentKind::Attribute); + return Attribute::getFromOpaquePointer( + reinterpret_cast(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; diff --git a/mlir/lib/IR/StandardTypes.cpp b/mlir/lib/IR/StandardTypes.cpp index d727fb1..5af031e 100644 --- a/mlir/lib/IR/StandardTypes.cpp +++ b/mlir/lib/IR/StandardTypes.cpp @@ -333,12 +333,11 @@ MemRefType MemRefType::getImpl(ArrayRef 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; } diff --git a/mlir/lib/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/LLVMIR/IR/LLVMDialect.cpp index 48cc476..c77c816 100644 --- a/mlir/lib/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/LLVMIR/IR/LLVMDialect.cpp @@ -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); diff --git a/mlir/lib/Linalg/IR/LinalgOps.cpp b/mlir/lib/Linalg/IR/LinalgOps.cpp index daa2cd3..356a906 100644 --- a/mlir/lib/Linalg/IR/LinalgOps.cpp +++ b/mlir/lib/Linalg/IR/LinalgOps.cpp @@ -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(); if (!viewType) return parser->emitError(parser->getNameLoc(), "view type expected"); @@ -222,9 +221,8 @@ ParseResult mlir::SliceOp::parse(OpAsmParser *parser, OperationState *result) { ArrayRef indexingTypes = ArrayRef(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, diff --git a/mlir/lib/StandardOps/Ops.cpp b/mlir/lib/StandardOps/Ops.cpp index 9883b02..f9b13ce 100644 --- a/mlir/lib/StandardOps/Ops.cpp +++ b/mlir/lib/StandardOps/Ops.cpp @@ -694,9 +694,8 @@ ParseResult CmpIOp::parse(OpAsmParser *parser, OperationState *result) { StringRef predicateName = predicateNameAttr.cast().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);