diff --git a/src/Compiler/CompilerUtils.cpp b/src/Compiler/CompilerUtils.cpp index ac88eb213e..96365f66c7 100644 --- a/src/Compiler/CompilerUtils.cpp +++ b/src/Compiler/CompilerUtils.cpp @@ -62,6 +62,7 @@ namespace onnx_mlir { // Values to report the current phase of compilation. uint64_t CURRENT_COMPILE_PHASE = 1; uint64_t TOTAL_COMPILE_PHASE = 0; +static DiagnosticEngine::HandlerID diagnosticHandlerID = 0; // Make a function that forces preserving all files using the runtime arguments // and/or the overridePreserveFiles enum. @@ -420,6 +421,12 @@ static int genLLVMBitcode(const mlir::OwningOpRef &module, return InvalidTemporaryFileAccess; } + // In the LLVM translation, we get some warnings, so disable in non-verbose + // mode. + if (diagnosticHandlerID && !VerboseOutput) { + module.get().getContext()->getDiagEngine().eraseHandler( + diagnosticHandlerID); + } llvm::LLVMContext llvmContext; mlir::registerBuiltinDialectTranslation(*(module.get().getContext())); mlir::registerLLVMDialectTranslation(*(module.get().getContext())); @@ -995,6 +1002,17 @@ int compileModule(mlir::OwningOpRef &module, configurePasses(); + // Enable printing for error handler on llvm error stream. Save ID if we want + // to disable it later. We currently disable for the llvm lowering, as + // otherwise we currently get an unrecognized warning for the "onnx.name" + // attribute in function operations. In Verbose mode, we keep the error + // handling all the way to the end. + diagnosticHandlerID = + context.getDiagEngine().registerHandler([](Diagnostic &diag) { + llvm::errs() << diag << "\n"; + return mlir::LogicalResult::success(); + }); + mlir::PassManager pm( module.get()->getName(), mlir::OpPassManager::Nesting::Implicit); // TODO(tung): Revise adding passes. The current mechanism does not work if diff --git a/src/Dialect/ONNX/ONNXOps/Canonicalize.cpp b/src/Dialect/ONNX/ONNXOps/Canonicalize.cpp index c29eabfcc4..66f129d96d 100644 --- a/src/Dialect/ONNX/ONNXOps/Canonicalize.cpp +++ b/src/Dialect/ONNX/ONNXOps/Canonicalize.cpp @@ -743,7 +743,7 @@ class LoopOpRewriteMaxTripCountPattern : public OpRewritePattern { bool isDefinedByIntegerConstantOp(Value v) const { if (mlir::isa(v)) return false; - Operation *definingOp = v.getDefiningOp(); + // Operation *definingOp = v.getDefiningOp(); if (mlir::isa( mlir::cast(v.getType()).getElementType()) && isDenseONNXConstant(v)) diff --git a/src/Support/Diagnostic.cpp b/src/Support/Diagnostic.cpp index cf882c3fc1..58a409158c 100644 --- a/src/Support/Diagnostic.cpp +++ b/src/Support/Diagnostic.cpp @@ -19,19 +19,22 @@ using namespace mlir; namespace onnx_mlir { template -LogicalResult Diagnostic::emitAttributeOutOfRangeError(Operation &op, - const llvm::Twine &attrName, T attrVal, Range validRange) { +LogicalResult Diagnostic::emitAttributeOutOfRangeError( + Operation &op, const llvm::Twine &attrName, T attrVal, Range range) { static_assert(std::is_arithmetic::value, "Expecting an arithmetic type"); llvm::Twine msg(op.getName().getStringRef() + ": "); + std::string rangeMessage = + range.isValid() ? "" : " <>"; return emitError(op.getLoc(), msg.concat("'" + attrName + "'") .concat(" value is ") .concat(std::to_string(attrVal)) .concat(", accepted range is [") - .concat(std::to_string(validRange.min)) + .concat(std::to_string(range.min)) .concat(", ") - .concat(std::to_string(validRange.max)) - .concat("]")); + .concat(std::to_string(range.max)) + .concat("]") + .concat(rangeMessage)); } template diff --git a/src/Support/Diagnostic.hpp b/src/Support/Diagnostic.hpp index 628abfe649..1187d456b0 100644 --- a/src/Support/Diagnostic.hpp +++ b/src/Support/Diagnostic.hpp @@ -36,15 +36,22 @@ class Diagnostic { T max; public: + // Range is used in error situations, so having an assert is not very useful + // as that assert may crash the program instead of reporting the error + // condition. New approach is to report the error with an additional + // warning. Range(T min, T max) : min(min), max(max) { - assert(min <= max && "Illegal range"); + if (!isValid()) + llvm::errs() << "Warning: badly formed range(min=" << min + << ", max=" << max << ")\n"; } + bool isValid() { return min <= max; } }; /// Diagnostic message for attribute value outside of a supplied range. template static mlir::LogicalResult emitAttributeOutOfRangeError(mlir::Operation &op, - const llvm::Twine &attrName, T attrVal, Range validRange); + const llvm::Twine &attrName, T attrVal, Range range); /// Verifies whether 2 inputs have the same rank. template