Lunderberg commented on code in PR #13130: URL: https://github.com/apache/tvm/pull/13130#discussion_r1023063820
########## src/tir/analysis/control_flow_graph.h: ########## @@ -0,0 +1,597 @@ +/* + * 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 control_flow_graph.h + * \brief Utility for extracting and interacting with buffer touch points + */ + +#include <tvm/arith/analyzer.h> +#include <tvm/arith/int_solver.h> +#include <tvm/runtime/container/array.h> +#include <tvm/tir/buffer.h> +#include <tvm/tir/stmt.h> +#include <tvm/tir/var.h> + +#include <unordered_map> +#include <utility> +#include <vector> + +#ifndef TVM_TIR_ANALYSIS_CONTROL_FLOW_GRAPH_H_ +#define TVM_TIR_ANALYSIS_CONTROL_FLOW_GRAPH_H_ + +namespace tvm { +namespace tir { + +/*! \brief Represents an interaction with a buffer */ +struct BufferTouch { + enum class AccessType { + /*! \brief Buffer access occurs in BufferLoad */ + Read, + + /*! \brief Buffer access occurs in BufferStore */ + Write, + + /*! \brief Buffer access occurs in tir::builtin::assume() */ + Assume, + }; + + BufferTouch(Buffer buffer, PrimExpr predicate, PrimExpr value) + : buffer(buffer), + predicate(predicate), + value(value), + loop_var_expressions({}), + touch_type(AccessType::Assume) {} + + BufferTouch(Buffer buffer, PrimExpr predicate, PrimExpr value, + std::vector<std::pair<Var, PrimExpr>> loop_var_expressions, AccessType touch_type) + : buffer(buffer), + predicate(predicate), + value(value), + loop_var_expressions(loop_var_expressions), + touch_type(touch_type) {} + + /*! \brief The buffer being touched */ + Buffer buffer; + + /*! \brief A predicate that is true when this touch applies + * + * May be in terms of axis variables to indicate touches that impact + * only a portion of a buffer. + */ + PrimExpr predicate; + + /*! \brief The value in this buffer after the touch + * + * May be in terms of axis variables to indicate a known + * non-constant value. May be in terms of a BufferLoad to indicate + * an unknown value. + */ + PrimExpr value; + + /*! \brief Active loops during the buffer touch + * + * The vector contains one entry for each loop that contains the + * buffer touch. The `Var` item in each entry is the loop variable + * itself. The `PrimExpr` item is an expression for the loop + * variable in terms of the buffer axis variables in + * `ControlFlowGraph::axis_var_lookup_`. + * + * Used to construct boolean expressions indicating whether the loop + * iteration that performs this touch has been reached. + */ + std::vector<std::pair<Var, PrimExpr>> loop_var_expressions; + + /*! \brief How the buffer was interacted with + * + * When used as a constraint (e.g. in BufferState), should use + * Assume. + */ + AccessType touch_type{AccessType::Assume}; Review Comment: The default type here doesn't have an impact, as both of the constructors explicitly set a value for the `touch_type`. I mainly included a default to avoid any possibility of having uninitialized values. I'd want to avoid adding another touch type, since that would be an additional state that would need to be checked for when in-use, even though it shouldn't ever occur. This class interface was partly a compromise while cleaning up the dev branch, as this one class serves two purposes. During collection, it is a 1:1 mapping of buffer touch points in TIR. During propagation, it tracks the known values in each control flow block. There was too much shared functionality for that to be a reasonable usage, but also not enough distinction to warrant pulling out the shared functionality into a common base class. -- 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]
