masahi commented on code in PR #14574: URL: https://github.com/apache/tvm/pull/14574#discussion_r1191974844
########## src/ir/si_builder.cc: ########## @@ -0,0 +1,341 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +/*! + * \file tvm/ir/si_builder.cc + * \brief Implementation for building a source info during rewriting expressions. + */ +#include <tvm/ir/si_builder.h> +#include <tvm/ir/transform.h> +#include <tvm/tir/stmt_functor.h> + +#include <vector> + +namespace tvm { + +using RelayExprSet = std::unordered_set<relay::Expr, ObjectPtrHash, ObjectPtrEqual>; +using PrimExprSet = std::unordered_set<PrimExpr, ObjectPtrHash, ObjectPtrEqual>; +using StmtSet = std::unordered_set<tir::Stmt, ObjectPtrHash, ObjectPtrEqual>; + +class RelayCollapse : public relay::ExprVisitor { + public: + explicit RelayCollapse(const RelayExprSet& inputs = {}) : inputs_(inputs) {} + + Span Collapse(const relay::Expr& entry); Review Comment: I think `CollectSpans` will be a more descriptive name for `Collapse`? ########## python/tvm/ir/base.py: ########## @@ -69,6 +69,20 @@ def __init__(self, source_name, line, end_line, column, end_column): ) +@register_object("SequentialSpan") +class SequentialSpan(Object): + """Specifies a location in a source program. Review Comment: Please update this doc, since it refers to only a single span. Does this class need to be exposed to Python? If it is used only by c++ code, no need to define a python class. ########## include/tvm/ir/si_builder.h: ########## @@ -0,0 +1,103 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +/*! + * \file tvm/ir/si_builder.h + * \brief build a source info during rewriting expressions. + */ +#ifndef TVM_IR_SI_BUILDER_H_ +#define TVM_IR_SI_BUILDER_H_ + +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/tir/stmt.h> + +#include <memory> +#include <unordered_set> + +namespace tvm { + +/*! + * \brief SIBuilder provides helper APIs for filling spans, + * particularly useful for one-to-many, many-to-one and many-to-many pass transformations. + */ +class SIBuilder { + public: + /*! + * \brief Create SIBuilder from a given span + */ + explicit SIBuilder(const Span& span = Span()); + + /*! + * \brief Create SIBuilder from a given span sequence + */ + explicit SIBuilder(const Array<Span>& spans = Array<Span>()); + explicit SIBuilder(const std::initializer_list<Span>& init); + + /*! + * \brief Create SIBuilder via a subgraph, + * Will construct span based on the exprs in the subgraph. Including the inputs exprs. + * + * \param entry Entry expr for subgraph + * \param inputs End exprs for subgraph + */ + template <typename T, typename = std::enable_if_t<std::is_base_of<BaseExpr, T>::value>> + explicit SIBuilder(const T& entry, const tvm::Array<T>& inputs = {}); + explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<PrimExpr>& inputs = {}); + explicit SIBuilder(const tir::Stmt& entry, const tvm::Array<tir::Stmt>& inputs = {}); + + ~SIBuilder(); + + SIBuilder(const SIBuilder&) = delete; + SIBuilder& operator=(const SIBuilder&) = delete; + + /*! + * \brief create new source info based on the given span or subgraph. Review Comment: This description doesn't seem to match the signature ########## src/ir/si_builder.cc: ########## @@ -0,0 +1,341 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +/*! + * \file tvm/ir/si_builder.cc + * \brief Implementation for building a source info during rewriting expressions. + */ +#include <tvm/ir/si_builder.h> +#include <tvm/ir/transform.h> +#include <tvm/tir/stmt_functor.h> + +#include <vector> + +namespace tvm { + +using RelayExprSet = std::unordered_set<relay::Expr, ObjectPtrHash, ObjectPtrEqual>; +using PrimExprSet = std::unordered_set<PrimExpr, ObjectPtrHash, ObjectPtrEqual>; +using StmtSet = std::unordered_set<tir::Stmt, ObjectPtrHash, ObjectPtrEqual>; + +class RelayCollapse : public relay::ExprVisitor { + public: + explicit RelayCollapse(const RelayExprSet& inputs = {}) : inputs_(inputs) {} + + Span Collapse(const relay::Expr& entry); + + void VisitExpr(const relay::Expr& expr) final; + + private: + Array<Span> spans_; + const RelayExprSet& inputs_; +}; + +void RelayCollapse::VisitExpr(const relay::Expr& expr) { + if (visit_counter_.count(expr.get())) { + return; + } + if (expr->span.defined()) { + spans_.push_back(expr->span); + } + if (inputs_.find(expr) != inputs_.end()) { + // becuase it returns directly, it should be recorded as visted manually. + visit_counter_.insert({expr.get(), 1}); + return; + } + relay::ExprVisitor::VisitExpr(expr); +} + +Span RelayCollapse::Collapse(const relay::Expr& entry) { + VisitExpr(entry); + return SequentialSpan(spans_); +} + +class RelayRecursivelyFill : public relay::ExprMutator { + public: + explicit RelayRecursivelyFill(const Span& span, const RelayExprSet& inputs = {}) + : span_(span), inputs_(inputs) {} + + void Fill(const relay::Expr& entry); + + relay::Expr VisitExpr(const relay::Expr& expr) final; + + private: + const Span& span_; + const RelayExprSet& inputs_; +}; + +relay::Expr RelayRecursivelyFill::VisitExpr(const relay::Expr& expr) { + if (inputs_.find(expr) != inputs_.end()) { + return expr; + } + // Skip op node. Align with python frontend + if (!expr.as<OpNode>()) { + expr->span = span_; + } + + return relay::ExprMutator::VisitExpr(expr); +} + +void RelayRecursivelyFill::Fill(const relay::Expr& entry) { Mutate(entry); } + +class TirCollapse : public tir::StmtExprVisitor { + public: + explicit TirCollapse(const PrimExprSet& expr_inputs = {}, const StmtSet& stmt_inputs = {}) + : expr_inputs_(expr_inputs), stmt_inputs_(stmt_inputs) {} + + void VisitExpr(const PrimExpr& expr) final; + void VisitStmt(const tir::Stmt& stmt) final; + + bool IsInput(const PrimExpr& expr); + bool IsInput(const tir::Stmt& stmt); + + Span Collapse(const PrimExpr& expr); + Span Collapse(const tir::Stmt& stmt); + + private: + Array<Span> spans_; + std::unordered_map<const Object*, size_t> visit_counter_; + const PrimExprSet& expr_inputs_; + const StmtSet& stmt_inputs_; +}; + +Span TirCollapse::Collapse(const PrimExpr& expr) { + operator()(expr); + return SequentialSpan(spans_); +} + +Span TirCollapse::Collapse(const tir::Stmt& stmt) { + operator()(stmt); + return SequentialSpan(spans_); +} + +bool TirCollapse::IsInput(const PrimExpr& expr) { + return expr_inputs_.find(expr) != expr_inputs_.end(); +} + +bool TirCollapse::IsInput(const tir::Stmt& stmt) { + return stmt_inputs_.find(stmt) != stmt_inputs_.end(); +} + +void TirCollapse::VisitExpr(const PrimExpr& expr) { + if (visit_counter_.count(expr.get())) { + return; + } + if (expr->span.defined()) { + spans_.push_back(expr->span); + } + if (IsInput(expr)) { + // becuase it returns directly, it should be recorded as visted manually. + visit_counter_.insert({expr.get(), 1}); + return; + } + StmtExprVisitor::VisitExpr(expr); +} + +void TirCollapse::VisitStmt(const tir::Stmt& stmt) { + if (visit_counter_.count(stmt.get())) { + return; + } + if (stmt->span.defined()) { + spans_.push_back(stmt->span); + } + if (IsInput(stmt)) { + // becuase it returns directly, it should be recorded as visted manually. + visit_counter_.insert({stmt.get(), 1}); + return; + } + StmtExprVisitor::VisitStmt(stmt); +} + +class TirRecursivelyFill : public tir::StmtExprMutator { + public: + TirRecursivelyFill(const Span& span, const PrimExprSet& expr_inputs = {}, + const StmtSet& stmt_inputs = {}) + : span_(span), expr_inputs_(expr_inputs), stmt_inputs_(stmt_inputs) {} + + tir::Stmt Fill(const tir::Stmt& s) { return operator()(s); } + PrimExpr Fill(const PrimExpr& e) { return operator()(e); } + + bool IsInput(const PrimExpr& expr); + bool IsInput(const tir::Stmt& stmt); + + PrimExpr VisitExpr(const PrimExpr& expr) final; + tir::Stmt VisitStmt(const tir::Stmt& stmt) final; + + private: + const Span& span_; + const PrimExprSet& expr_inputs_; + const StmtSet& stmt_inputs_; +}; + +tir::Stmt TirRecursivelyFill::VisitStmt(const tir::Stmt& stmt) { + if (IsInput(stmt)) { + return stmt; + } + stmt->span = span_; + return StmtExprMutator::VisitStmt(stmt); +} + +bool TirRecursivelyFill::IsInput(const PrimExpr& expr) { + return expr_inputs_.find(expr) != expr_inputs_.end(); +} + +bool TirRecursivelyFill::IsInput(const tir::Stmt& stmt) { + return stmt_inputs_.find(stmt) != stmt_inputs_.end(); +} + +PrimExpr TirRecursivelyFill::VisitExpr(const PrimExpr& expr) { + if (IsInput(expr)) { + return expr; + } + expr->span = span_; + return StmtExprMutator::VisitExpr(expr); +} + +struct SIBuilder::Impl { + virtual Span CreateSpan() const = 0; + virtual void RecursivelyFillSpan(const relay::Expr& entry, const RelayExprSet& inputs) const = 0; + virtual void RecursivelyFillSpan(const PrimExpr& entry, const PrimExprSet& inputs) const = 0; + virtual void RecursivelyFillSpan(const tir::Stmt& entry, const PrimExprSet& inputs) const = 0; + virtual void RecursivelyFillSpan(const tir::Stmt& entry, const StmtSet& inputs) const = 0; + virtual void CollapseSpan(const relay::Expr& entry, const RelayExprSet& inputs) = 0; + virtual void CollapseSpan(const PrimExpr& entry, const PrimExprSet& inputs) = 0; + virtual void CollapseSpan(const tir::Stmt& entry, const PrimExprSet& inputs) = 0; + virtual void CollapseSpan(const tir::Stmt& entry, const StmtSet& inputs) = 0; +}; + +SIBuilder::~SIBuilder() = default; + +Span SIBuilder::CreateSpan() const { return impl_->CreateSpan(); } + +template <> +void SIBuilder::RecursivelyFillSpan(const relay::Expr& entry, const RelayExprSet& inputs) const { + impl_->RecursivelyFillSpan(entry, inputs); +} + +template <> +void SIBuilder::RecursivelyFillSpan(const PrimExpr& entry, const PrimExprSet& inputs) const { + impl_->RecursivelyFillSpan(entry, inputs); +} + +void SIBuilder::RecursivelyFillSpan(const tir::Stmt& entry, const PrimExprSet& inputs) const { + impl_->RecursivelyFillSpan(entry, inputs); +} + +void SIBuilder::RecursivelyFillSpan(const tir::Stmt& entry, const StmtSet& inputs) const { + impl_->RecursivelyFillSpan(entry, inputs); +} + +std::unique_ptr<SIBuilder::Impl> SIBuilder::CreateImpl(const Span& span) { + struct NullImpl : public SIBuilder::Impl { Review Comment: This class and the base class `SIBuilder::Impl ` don't seem to be necessary. If SI collection is not enabled, there is no need to collect spans or create the impl object. We should check `enable_si_builder` condition much earlier. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
