Simplify the emission of a few op parser diagnostics. This also adds the ability to stream an attribute into a Diagnostic.

--

PiperOrigin-RevId: 247359911
This commit is contained in:
River Riddle 2019-05-08 22:42:58 -07:00 committed by Mehdi Amini
parent a4b56174bd
commit e05eda9d22
6 changed files with 39 additions and 22 deletions

View 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) {}

View 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;

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

View 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);

View 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,

View 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);