error.cc 4.51 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20
/*!
21 22
 * \file ir/error.cc
 * \brief Utilities for error tracking and reporting.
23 24
 */

25
#include <tvm/ir/module.h>
26
#include <tvm/ir/error.h>
27 28 29 30 31
// NOTE: reverse dependency on relay.
// These dependencies do not happen at the interface-level,
// and are only used in minimum cases where they are clearly marked.
//
// Rationale: use relay's printer for astext.
32 33
#include <tvm/relay/expr.h>

34 35 36 37 38 39 40
#include <string>
#include <vector>
#include <rang.hpp>

namespace tvm {

template<typename T, typename U>
41
using NodeMap = std::unordered_map<T, U, ObjectHash, ObjectEqual>;
42

43
void ErrorReporter::RenderErrors(const IRModule& module, bool use_color) {
44 45 46
  // First we pick an error reporting strategy for each error.
  // TODO(@jroesch): Spanned errors are currently not supported.
  for (auto err : this->errors_) {
47
    CHECK(!err.span.defined()) << "attempting to use spanned errors, currently not supported";
48 49
  }

50
  NodeMap<GlobalVar, NodeMap<ObjectRef, std::string>> error_maps;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  // Set control mode in order to produce colors;
  if (use_color) {
    rang::setControlMode(rang::control::Force);
  }

  for (auto pair : this->node_to_gv_) {
    auto node = pair.first;
    auto global = Downcast<GlobalVar>(pair.second);

    auto has_errs = this->node_to_error_.find(node);

    CHECK(has_errs != this->node_to_error_.end());

    const auto& error_indicies = has_errs->second;

    std::stringstream err_msg;

    err_msg << rang::fg::red;
70
    err_msg << " ";
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    for (auto index : error_indicies) {
      err_msg << this->errors_[index].what() << "; ";
    }
    err_msg << rang::fg::reset;

    // Setup error map.
    auto it = error_maps.find(global);
    if (it != error_maps.end()) {
      it->second.insert({ node, err_msg.str() });
    } else {
      error_maps.insert({ global, { { node, err_msg.str() }}});
    }
  }

  // Now we will construct the fully-annotated program to display to
  // the user.
  std::stringstream annotated_prog;

  // First we output a header for the errors.
  annotated_prog <<
  rang::style::bold << std::endl <<
92
  "Error(s) have occurred. The program has been annotated with them:"
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  << std::endl << std::endl << rang::style::reset;

  // For each global function which contains errors, we will
  // construct an annotated function.
  for (auto pair : error_maps) {
    auto global = pair.first;
    auto err_map = pair.second;
    auto func = module->Lookup(global);

    // We output the name of the function before displaying
    // the annotated program.
    annotated_prog <<
      rang::style::bold <<
      "In `" << global->name_hint << "`: " <<
      std::endl <<
      rang::style::reset;

    // We then call into the Relay printer to generate the program.
    //
    // The annotation callback will annotate the error messages
    // contained in the map.
114
    annotated_prog << AsText(func, false, [&err_map](const ObjectRef& expr) {
115 116
      auto it = err_map.find(expr);
      if (it != err_map.end()) {
117
        CHECK_NE(it->second.size(), 0);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        return it->second;
      } else {
        return std::string("");
      }
    });
  }

  auto msg = annotated_prog.str();

  if (use_color) {
    rang::setControlMode(rang::control::Auto);
  }

  // Finally we report the error, currently we do so to LOG(FATAL),
  // it may be good to instead report it to std::cout.
  LOG(FATAL) << annotated_prog.str() << std::endl;
}

136
void ErrorReporter::ReportAt(const GlobalVar& global, const ObjectRef& node, const Error& err) {
137 138 139 140 141 142 143 144 145 146 147 148
  size_t index_to_insert = this->errors_.size();
  this->errors_.push_back(err);
  auto it = this->node_to_error_.find(node);
  if (it != this->node_to_error_.end()) {
    it->second.push_back(index_to_insert);
  } else {
    this->node_to_error_.insert({ node, { index_to_insert }});
  }
  this->node_to_gv_.insert({ node, global });
}

}  // namespace tvm