Lunderberg commented on code in PR #13130: URL: https://github.com/apache/tvm/pull/13130#discussion_r1019586589
########## src/tir/analysis/control_flow_graph.h: ########## @@ -0,0 +1,458 @@ +/* + * 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 { + Read, + Write, + 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 + * + * 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; Review Comment: Not quite. The PrimExpr is an expression for the `loop_var` in terms of the buffer axis variables. This lets the buffer touches track knowns across loop iterations (e.g. Making a predicate `axis0 <= i` to indicate that a value is known for this and previous loop iterations, provided that it isn't subsequently overwritten.) ```python for i in T.serial(16): # Introduces a known value, A[axis0] == i, for all axis0 <= i. A[i] = i # May be simplified to "x = i+1", since substituting {axis0: i} # results in i <= i, which is true. x = A[i] + 1 # May not be simplified to "x = i+1", since substituting # {axis0: i+1} results in i+1 <= i, which is false. x = A[i+1] + 1 ``` I've added to the docstring for `loop_var_expressions` to indicate what the `PrimExpr` is. -- 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]
