1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | #ifndef TVM_TIR_SCHEDULE_ERROR_H_ |
20 | #define TVM_TIR_SCHEDULE_ERROR_H_ |
21 | #include <tvm/tir/schedule/state.h> |
22 | |
23 | #include <string> |
24 | #include <utility> |
25 | |
26 | namespace tvm { |
27 | namespace tir { |
28 | |
29 | /*! \brief Error that happens during TensorIR scheduling */ |
30 | class ScheduleError : public tvm::runtime::Error { |
31 | public: |
32 | /*! \brief Base constructor */ |
33 | ScheduleError() : tvm::runtime::Error("" ) {} |
34 | /*! \brief The error occurred in this IRModule */ |
35 | virtual IRModule mod() const = 0; |
36 | /*! \brief The locations of interest that we want to point out */ |
37 | virtual Array<ObjectRef> LocationsOfInterest() const = 0; |
38 | /*! |
39 | * \brief Returns an error string template for rendering, corresponds to the "detail" mode. |
40 | * \sa ScheduleErrorRenderLevel |
41 | * \note The template is a string, e.g. |
42 | * "Some error occurred on block {0} and loop {1} blah blah" |
43 | * And renderer will replace {0} and {1} according to the list provided LocationsOfInterest. Right |
44 | * now it only printed out all the locations in plain text, but in the future, we may want to mark |
45 | * the IR with underscores and attach names to each location of interest. |
46 | */ |
47 | virtual String DetailRenderTemplate() const = 0; |
48 | /*! |
49 | * \brief Returns an error string without needing to render, corresponds to the "fast" mode |
50 | * \sa ScheduleErrorRenderLevel |
51 | */ |
52 | virtual String FastErrorString() const = 0; |
53 | /*! \brief Render the ScheduleError with the template provided by `DetailRenderTemplate` */ |
54 | String RenderReport(const String& primitive) const; |
55 | }; |
56 | |
57 | class LoopPositionError : public ScheduleError { |
58 | public: |
59 | explicit LoopPositionError(IRModule mod, For loop, Block block, const std::string& primitive) |
60 | : mod_(std::move(mod)), |
61 | loop_(std::move(loop)), |
62 | block_(std::move(block)), |
63 | primitive_(primitive) {} |
64 | |
65 | String FastErrorString() const final { |
66 | return "ScheduleError: " + primitive_ + " expect the loop to be an ancestor of block" ; |
67 | } |
68 | |
69 | String DetailRenderTemplate() const final { |
70 | std::ostringstream os; |
71 | os << "ScheduleError: The input loop {0} of " << primitive_ |
72 | << " is required to be be an ancestor of block {1}." ; |
73 | return os.str(); |
74 | } |
75 | |
76 | IRModule mod() const final { return mod_; } |
77 | Array<ObjectRef> LocationsOfInterest() const final { return {loop_, block_}; } |
78 | |
79 | IRModule mod_; |
80 | For loop_; |
81 | Block block_; |
82 | std::string primitive_; |
83 | }; |
84 | |
85 | } // namespace tir |
86 | } // namespace tvm |
87 | |
88 | #endif // TVM_TIR_SCHEDULE_ERROR_H_ |
89 | |