xref: /llvm-project/mlir/lib/Interfaces/RuntimeVerifiableOpInterface.cpp (revision 884221eddb9d395830704fac79fd04008e02e368)
1108b08f2SMatthias Springer //===- RuntimeVerifiableOpInterface.cpp - Op Verification -----------------===//
2108b08f2SMatthias Springer //
3108b08f2SMatthias Springer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4108b08f2SMatthias Springer // See https://llvm.org/LICENSE.txt for license information.
5108b08f2SMatthias Springer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6108b08f2SMatthias Springer //
7108b08f2SMatthias Springer //===----------------------------------------------------------------------===//
8108b08f2SMatthias Springer 
9108b08f2SMatthias Springer #include "mlir/Interfaces/RuntimeVerifiableOpInterface.h"
10108b08f2SMatthias Springer 
11108b08f2SMatthias Springer namespace mlir {
12108b08f2SMatthias Springer class Location;
13108b08f2SMatthias Springer class OpBuilder;
14d94aeb50SRyan Holt 
15d94aeb50SRyan Holt /// Generate an error message string for the given op and the specified error.
16d94aeb50SRyan Holt std::string
17d94aeb50SRyan Holt RuntimeVerifiableOpInterface::generateErrorMessage(Operation *op,
18d94aeb50SRyan Holt                                                    const std::string &msg) {
19d94aeb50SRyan Holt   std::string buffer;
20d94aeb50SRyan Holt   llvm::raw_string_ostream stream(buffer);
21d94aeb50SRyan Holt   OpPrintingFlags flags;
22d94aeb50SRyan Holt   // We may generate a lot of error messages and so we need to ensure the
23d94aeb50SRyan Holt   // printing is fast.
24d94aeb50SRyan Holt   flags.elideLargeElementsAttrs();
25d94aeb50SRyan Holt   flags.printGenericOpForm();
26d94aeb50SRyan Holt   flags.skipRegions();
27d94aeb50SRyan Holt   flags.useLocalScope();
28d94aeb50SRyan Holt   stream << "ERROR: Runtime op verification failed\n";
29d94aeb50SRyan Holt   op->print(stream, flags);
30d94aeb50SRyan Holt   stream << "\n^ " << msg;
31d94aeb50SRyan Holt   stream << "\nLocation: ";
32d94aeb50SRyan Holt   op->getLoc().print(stream);
33*884221edSJOE1994   return buffer;
34d94aeb50SRyan Holt }
35108b08f2SMatthias Springer } // namespace mlir
36108b08f2SMatthias Springer 
37108b08f2SMatthias Springer /// Include the definitions of the interface.
38108b08f2SMatthias Springer #include "mlir/Interfaces/RuntimeVerifiableOpInterface.cpp.inc"
39