junrushao1994 commented on a change in pull request #7765: URL: https://github.com/apache/tvm/pull/7765#discussion_r608159960
########## File path: include/tvm/tir/schedule/block_scope.h ########## @@ -0,0 +1,249 @@ +/* + * 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/tir/schedule/block_scope.h + * \brief Definition of two pillar data structure for TensorIR scheduling: StmtSRef, BlockScope. + * \sa StmtSRefNode + * \sa BlockScopeNode + */ +#ifndef TVM_TIR_SCHEDULE_BLOCK_SCOPE_H_ +#define TVM_TIR_SCHEDULE_BLOCK_SCOPE_H_ + +#include <tvm/tir/stmt.h> + +#include <unordered_map> + +namespace tvm { +namespace tir { + +/*! + * \brief An object that refers to schedulable elements (block/for-loop) in TensorIR, aka "sref". + * + * Glossary + * - Block sref: An StmtSRef that points to a TensorIR block. + * - Loop sref: An StmtSRef that points to a TensorIR for loop. + * - Parent sref: The parent sref of an sref is the block/loop sref that points to its closest + * schedulable statement of its ancestors on the TensorIR AST. + * - Root sref: Sref to the root block. Every sref has exactly one parent sref except for root sref. + * - Sref tree: The parent-children-relationship of srefs that forms a tree, uniquely determined by + * the TensorIR AST. + */ +class StmtSRefNode : public Object { + public: + /*! + * \brief The block/for stmt the object refers to + * \note Non-owned reference (raw pointer) is used here, so that we can perform copy-on-write + * optimization on statements when possible. The strong reference is held in the ScheduleState. + */ + const StmtNode* stmt; + /*! \brief The parent sref. */ + StmtSRefNode* parent; + /*! + * \brief If the statement the sref points to is an element of a SeqStmt in the AST, + * then `seq_index` is set to its index; otherwise `seq_index` is -1 + */ + int64_t seq_index; + + void VisitAttrs(AttrVisitor* v) { + // `stmt` is not visited + // `parent` is not visited + v->Visit("seq_index", &seq_index); + } + + static constexpr const char* _type_key = "tir.StmtSRef"; + TVM_DECLARE_FINAL_OBJECT_INFO(StmtSRefNode, Object); + + /*! \brief Reset the object inplace to the invalid state */ + void Reset() { + this->stmt = nullptr; + this->parent = nullptr; + this->seq_index = -1; + } + + /*! + * \brief Get the referenced statement with proper type checking. + * It serves the same purpose as `ObjectRef::as`, but does not acquire strong reference to `stmt` + * \tparam StmtType The type that `this->stmt` to be downcasted to. Presumably + * tvm::tir::BlockNode or tvm::tir::ForNode + * \return nullptr if type check fails, otherwise the casted result for `this->stmt` + */ + template <typename StmtType> Review comment: Yeah we did use a lot of non-owned pointers (raw pointers) in the schedule's internal state, and it is intentional to avoid cyclic dependency. Introducing weak references is indeed an overkill to represent those objects, but the mechanism doesn't help in our particular case, because it cannot guarantee weak objects are not released (otherwise it is strong reference), so we still need to manually provide such guarantee in the `Replace` API. (and that's why `Replace` is so complicated and we wrote a lot of tests and some proof to make sure it works) ########## File path: include/tvm/tir/schedule/block_scope.h ########## @@ -0,0 +1,249 @@ +/* + * 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/tir/schedule/block_scope.h + * \brief Definition of two pillar data structure for TensorIR scheduling: StmtSRef, BlockScope. + * \sa StmtSRefNode + * \sa BlockScopeNode + */ +#ifndef TVM_TIR_SCHEDULE_BLOCK_SCOPE_H_ +#define TVM_TIR_SCHEDULE_BLOCK_SCOPE_H_ + +#include <tvm/tir/stmt.h> + +#include <unordered_map> + +namespace tvm { +namespace tir { + +/*! + * \brief An object that refers to schedulable elements (block/for-loop) in TensorIR, aka "sref". + * + * Glossary + * - Block sref: An StmtSRef that points to a TensorIR block. + * - Loop sref: An StmtSRef that points to a TensorIR for loop. + * - Parent sref: The parent sref of an sref is the block/loop sref that points to its closest + * schedulable statement of its ancestors on the TensorIR AST. + * - Root sref: Sref to the root block. Every sref has exactly one parent sref except for root sref. + * - Sref tree: The parent-children-relationship of srefs that forms a tree, uniquely determined by + * the TensorIR AST. + */ +class StmtSRefNode : public Object { + public: + /*! + * \brief The block/for stmt the object refers to + * \note Non-owned reference (raw pointer) is used here, so that we can perform copy-on-write + * optimization on statements when possible. The strong reference is held in the ScheduleState. + */ + const StmtNode* stmt; + /*! \brief The parent sref. */ + StmtSRefNode* parent; + /*! + * \brief If the statement the sref points to is an element of a SeqStmt in the AST, + * then `seq_index` is set to its index; otherwise `seq_index` is -1 + */ + int64_t seq_index; + + void VisitAttrs(AttrVisitor* v) { + // `stmt` is not visited + // `parent` is not visited + v->Visit("seq_index", &seq_index); + } + + static constexpr const char* _type_key = "tir.StmtSRef"; + TVM_DECLARE_FINAL_OBJECT_INFO(StmtSRefNode, Object); + + /*! \brief Reset the object inplace to the invalid state */ + void Reset() { + this->stmt = nullptr; + this->parent = nullptr; + this->seq_index = -1; + } + + /*! + * \brief Get the referenced statement with proper type checking. + * It serves the same purpose as `ObjectRef::as`, but does not acquire strong reference to `stmt` + * \tparam StmtType The type that `this->stmt` to be downcasted to. Presumably + * tvm::tir::BlockNode or tvm::tir::ForNode + * \return nullptr if type check fails, otherwise the casted result for `this->stmt` + */ + template <typename StmtType> Review comment: Yeah we did use a lot of non-owned pointers (raw pointers) in the schedule's internal state, and it is intentional to avoid cyclic dependency. Introducing weak references is indeed an overkill to represent those objects, but the mechanism doesn't help in our particular case, because it cannot guarantee weak objects are not released (otherwise it is strong reference), so we still need to manually provide such guarantee in the `Replace` API. (and that's why `Replace` is so complicated and we wrote a lot of tests and some proof to make sure it works as expected) -- 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. For queries about this service, please contact Infrastructure at: [email protected]
