compute_op.h 3.88 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
/*!
 * \brief Helper utilities to implement compute_op.
 * \file compute_op.h
 */
24 25
#ifndef TVM_TE_OPERATION_COMPUTE_OP_H_
#define TVM_TE_OPERATION_COMPUTE_OP_H_
26

27 28
#include <tvm/tir/expr.h>
#include <tvm/tir/expr.h>
29
#include <tvm/te/operation.h>
30 31 32 33
#include <vector>
#include <unordered_map>

namespace tvm {
34
namespace te {
35
// loop nest structure for general compute
36
// This the loop nest structured used in compute.
37 38 39 40 41
// Does not include the loop body.
struct ComputeLoopNest {
  // The common number of loops between init and main
  size_t num_common_loop;
  // predicates for the initialize loop
42
  std::vector<PrimExpr> init_predicates;
43 44 45
  // Initialization nest involved.
  std::vector<std::vector<Stmt> > init_nest;
  // Value map for the init code
46
  std::unordered_map<IterVar, PrimExpr> init_vmap;
47
  // Predicates for the main update loop
48
  std::vector<PrimExpr> main_predicates;
49 50 51
  // The general loop nest
  std::vector<std::vector<Stmt> > main_nest;
  // Value map for the IterVar.
52
  std::unordered_map<IterVar, PrimExpr> main_vmap;
53 54 55 56 57 58

  /*!
   * \brief constructor to build ComputeOpNest
   * \param self The pointer to compute op.
   * \param stage The scxhedule stage.
   * \param dom_map The domain map.
59
   * \param debug_keep_trivial_loop Whether keep trivial loops with extent of 1
60 61 62
   * \return The constructed loop nest
   */
  static ComputeLoopNest make(
63
      const BaseComputeOpNode* self,
64
      const Stage& stage,
65
      const std::unordered_map<IterVar, Range>& dom_map,
66
      bool debug_keep_trivial_loop);
67 68 69 70 71 72 73
};

/*!
 * \brief Build body of compute for cross thread reduction pattern.
 * \param self The pointer to ComputeOpNode
 * \param stage The schedule stage.
 * \param dom_map The domain map.
74
 * \param debug_keep_trivial_loop Whether keep trivial loops with extent of 1
75 76 77 78 79
 * \return The created statement.
 */
Stmt MakeCrossThreadReduction(
    const ComputeOpNode* self,
    const Stage& stage,
80
    const std::unordered_map<IterVar, Range>& dom_map,
81
    bool debug_keep_trivial_loop);
82 83 84 85 86 87

/*!
 * \brief Build body of compute for tensorization.
 * \param self The pointer to ComputeOpNode
 * \param stage The schedule stage.
 * \param dom_map The domain map.
88
 * \param debug_keep_trivial_loop Whether keep trivial loops with extent of 1
89 90 91 92
 * \return The created statement.
 */
Stmt MakeTensorize(const ComputeOpNode* self,
                   const Stage& stage,
93
                   const std::unordered_map<IterVar, Range>& dom_map,
94
                   bool debug_keep_trivial_loop);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

/*!
 * \brief Transform the update part when there is no init func in tensorizing
 * \param stage The stage for tensorizing.
 * \param dom_map The range of each iter var.
 * \param n The loop nest structured used in compute.
 * \param body The body func in tensorize intrin
 * \param update The update func in tensorize intrin
 * \return Transformed result.
 */
Stmt TransformUpdate(const Stage& stage,
                     const std::unordered_map<IterVar, Range>& dom_map,
                     const ComputeLoopNest& n,
                     Stmt body,
                     Stmt update);
110
}  // namespace te
111 112
}  // namespace tvm

113
#endif  // TVM_TE_OPERATION_COMPUTE_OP_H_