|
| 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 | + |
| 20 | +/*! |
| 21 | + * \file tvm/ir/si_builder.h |
| 22 | + * \brief build a source info during rewriting expressions. |
| 23 | + */ |
| 24 | +#ifndef TVM_IR_SI_BUILDER_H_ |
| 25 | +#define TVM_IR_SI_BUILDER_H_ |
| 26 | + |
| 27 | +#include <tvm/relay/expr.h> |
| 28 | +#include <tvm/relay/expr_functor.h> |
| 29 | +#include <tvm/tir/stmt.h> |
| 30 | + |
| 31 | +#include <memory> |
| 32 | +#include <unordered_set> |
| 33 | + |
| 34 | +namespace tvm { |
| 35 | + |
| 36 | +/*! |
| 37 | + * \brief Source Information Builder, SIBuilder provides helper APIs for filling spans, |
| 38 | + * particularly useful for one-to-many, many-to-one and many-to-many IR transformations. |
| 39 | + */ |
| 40 | +class SIBuilder { |
| 41 | + public: |
| 42 | + /*! |
| 43 | + * \brief Create SIBuilder from a given span |
| 44 | + */ |
| 45 | + explicit SIBuilder(const Span& span = Span()); |
| 46 | + |
| 47 | + /*! |
| 48 | + * \brief Create SIBuilder from a given span sequence |
| 49 | + */ |
| 50 | + explicit SIBuilder(const Array<Span>& spans = Array<Span>()); |
| 51 | + explicit SIBuilder(const std::initializer_list<Span>& init); |
| 52 | + |
| 53 | + /*! |
| 54 | + * \brief Create SIBuilder via a subgraph, |
| 55 | + * Will construct span based on the exprs in the subgraph. Including the inputs exprs. |
| 56 | + * |
| 57 | + * \param entry Entry expr for subgraph |
| 58 | + * \param inputs End exprs for subgraph |
| 59 | + */ |
| 60 | + template <typename T, typename = std::enable_if_t<std::is_base_of<BaseExpr, T>::value>> |
| 61 | + explicit SIBuilder(const T& entry, const tvm::Array<T>& inputs = {}); |
| 62 | + explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<PrimExpr>& inputs = {}); |
| 63 | + explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<tir::Stmt>& inputs = {}); |
| 64 | + |
| 65 | + ~SIBuilder(); |
| 66 | + |
| 67 | + SIBuilder(const SIBuilder&) = delete; |
| 68 | + SIBuilder& operator=(const SIBuilder&) = delete; |
| 69 | + |
| 70 | + /*! |
| 71 | + * \brief build a span of source information, which is based on the given span or subgraph. |
| 72 | + * |
| 73 | + * \return the built span |
| 74 | + */ |
| 75 | + Span Build() const; |
| 76 | + |
| 77 | + /*! |
| 78 | + * \brief Recursively fill all span of exprs in subgraph from entry until inputs. |
| 79 | + * |
| 80 | + * \param entry Entry expr for subgraph. |
| 81 | + * \param inputs End exprs for subgraph, will not be filled with new span. |
| 82 | + */ |
| 83 | + template <typename T, typename = std::enable_if_t<std::is_base_of<BaseExpr, T>::value>> |
| 84 | + void RecursivelyFillSpan( |
| 85 | + const T& entry, const std::unordered_set<T, ObjectPtrHash, ObjectPtrEqual>& inputs) const; |
| 86 | + |
| 87 | + void RecursivelyFillSpan( |
| 88 | + const tir::Stmt& entry, |
| 89 | + const std::unordered_set<PrimExpr, ObjectPtrHash, ObjectPtrEqual>& inputs) const; |
| 90 | + void RecursivelyFillSpan( |
| 91 | + const tir::Stmt& entry, |
| 92 | + const std::unordered_set<tir::Stmt, ObjectPtrHash, ObjectPtrEqual>& inputs) const; |
| 93 | + |
| 94 | + private: |
| 95 | + struct Impl; |
| 96 | + std::unique_ptr<Impl> impl_; |
| 97 | + |
| 98 | + std::unique_ptr<Impl> CreateImpl(const Span& span); |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace tvm |
| 102 | + |
| 103 | +#endif // TVM_IR_SI_BUILDER_H_ |
0 commit comments