combine_context_call.cc 3.54 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
/*!
 *  Combine calls into context related function into one.
 *
 * \file combine_context_call.cc
 */
25 26 27 28
#include <tvm/tir/expr.h>
#include <tvm/tir/stmt.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/ir_pass.h>
29 30 31
#include <map>

namespace tvm {
32
namespace tir {
33 34 35

// Calculate the statistics of packed function.
// These information are needed during codegen.
36
class ContextCallCombiner final : public StmtExprMutator {
37 38
 public:
  struct CompareExpr {
39
    bool operator()(const PrimExpr& lhs, const PrimExpr& rhs) const {
40 41 42 43
      return Compare(lhs, rhs) < 0;
    }
  };

44
  PrimExpr VisitExpr_(const CallNode* op) final {
45 46
    if (op->is_intrinsic(intrinsic::tvm_thread_context)) {
      CHECK_EQ(op->args.size(), 1U);
47
      PrimExpr ctx = op->args[0];
48 49 50 51
      auto it  = ctx_map_.find(ctx);
      if (it != ctx_map_.end()) {
        return it->second;
      } else {
52
        CHECK(ctx.dtype().is_handle());
53
        std::string name;
54
        if (const CallNode* call = ctx.as<CallNode>()) {
55 56 57 58
          name = call->name + "_cache";
        } else {
          name = "ctx_cache_";
        }
59
        Var ctx_var(name, ctx.dtype());
60
        ctx_map_[ctx] = ctx_var;
61
        return std::move(ctx_var);
62 63
      }
    } else {
64
      return StmtExprMutator::VisitExpr_(op);
65 66 67
    }
  }

68
  Stmt VisitStmt_(const AttrStmtNode* op) final {
69 70
    if (op->attr_key == attr::thread_extent ||
        op->attr_key == attr::coproc_uop_scope) {
71
      // Map of comparison expression to variable
72
      std::map<PrimExpr, Var, CompareExpr> temp;
73
      std::swap(temp, ctx_map_);
74
      Stmt stmt = StmtExprMutator::VisitStmt_(op);
75 76 77
      std::swap(temp, ctx_map_);
      return BuildContext(temp, stmt);
    } else {
78
      return StmtExprMutator::VisitStmt_(op);
79 80 81
    }
  }

82
  Stmt VisitStmt_(const ForNode* op) final {
83 84
    if (op->for_type == ForType::Parallel) {
      // Map of comparison expression to variable
85
      std::map<PrimExpr, Var, CompareExpr> temp;
86
      std::swap(temp, ctx_map_);
87
      Stmt stmt = StmtExprMutator::VisitStmt_(op);
88 89 90
      std::swap(temp, ctx_map_);
      return BuildContext(temp, stmt);
    } else {
91
      return StmtExprMutator::VisitStmt_(op);
92 93 94 95
    }
  }

  Stmt Combine(Stmt stmt) {
96
    return BuildContext(ctx_map_, this->VisitStmt(stmt));
97 98 99
  }

 private:
100
  static Stmt BuildContext(const std::map<PrimExpr, Var, CompareExpr>& cmap,
101 102
                           Stmt body) {
    for (const auto& kv : cmap) {
103
      body = LetStmtNode::make(kv.second, kv.first, body);
104 105 106 107
    }
    return body;
  }
  // Map of comparison expression to variable
108
  std::map<PrimExpr, Var, CompareExpr> ctx_map_;
109 110 111
};

LoweredFunc CombineContextCall(LoweredFunc f) {
112
  auto n = make_object<LoweredFuncNode>(*f.operator->());
113 114 115 116
  n->body = ContextCallCombiner().Combine(n->body);
  return LoweredFunc(n);
}

117
}  // namespace tir
118
}  // namespace tvm