feature_visitor.h 2.58 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23 24 25 26 27 28
/*!
 * \file feature_visitor.h
 * \brief Base class for feature extractor.
 *        These features are used for machine learning cost model
 */

#ifndef TVM_AUTOTVM_FEATURE_VISITOR_H_
#define TVM_AUTOTVM_FEATURE_VISITOR_H_

29 30 31
#include <tvm/tir/expr.h>
#include <tvm/tir/stmt.h>
#include <tvm/tir/stmt_functor.h>
32 33 34 35 36
#include <string>

namespace tvm {
namespace autotvm {

37
using namespace tvm::tir;
38 39 40 41 42 43 44 45 46 47 48 49 50 51

/*!
 * \brief Type of for loop, used as one-hot encoding in features
 */
enum AnnotationType {
  kBlockX, kBlockY, kBlockZ, kThreadX, kThreadY, kThreadZ,
  kUnrolled, kVectorized, kParallel, kSerial, kVirtualThread,
  kNum,
};

/*!
 * \brief A base class for feature extractor, used for processing
 * for loop and memory access in the IR
 */
52
class FeatureVisitor : public StmtExprVisitor {
53 54
 public:
  // for loop
55 56
  void VisitStmt_(const ForNode* op) final;
  void VisitStmt_(const AttrStmtNode* op) final;
57 58

  // memory access
59 60
  void VisitExpr_(const LoadNode* op) final;
  void VisitStmt_(const StoreNode* op) final;
61 62 63

  using StmtExprVisitor::VisitStmt_;
  using StmtExprVisitor::VisitExpr_;
64 65 66 67 68 69 70 71 72

 protected:
  /*!
 * \brief Enter a for loop node
 * \param var The expression to be printed.
 * \param length The output stream
 * \param ann_type The type for the for loop
 * \return skip Whether skip this node
 */
73
  virtual bool EnterItervar_(tir::Var var, int64_t length, AnnotationType ann_type) = 0;
74 75 76 77 78 79 80
  /*! \brief Exit a for loop subtree */
  virtual void ExitItervar_() = 0;
  /*!
   * \brief Enter a memory access node
   * \param buffer_var The buffer to access.
   * \param index Index expression
   */
81
  virtual void EnterMem_(tir::Var buffer_var, tvm::PrimExpr index) = 0;
82 83 84 85 86 87 88 89
  /*! \brief Exit a memory access node */
  virtual void ExitMem_() = 0;
};

}  // namespace autotvm
}  // namespace tvm

#endif  // TVM_AUTOTVM_FEATURE_VISITOR_H_