inject_fence_proxy.cc 5.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * 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 inject_fence_proxy.cc
 * \brief Inject fence between generic and async proxies (sm90+)
 */

#include <tvm/tir/analysis.h>
#include <tvm/tir/builtin.h>
#include <tvm/tir/op.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/transform.h>

#include "../op/builtin.h"

namespace tvm {
namespace tl {

using namespace tir;

enum class Proxy { kGeneric, kAsync, kBoth };

class ProxyMarker : public StmtVisitor {
41
public:
42
43
  ProxyMarker() = default;

44
  Proxy GetProxy(const StmtNode *stmt) const {
45
46
47
48
49
50
51
52
53
    auto it = map_.find(stmt);
    // ICHECK(it != map_.end());
    // TODO: This is a hack implementation to avoid the ICHECK failure.
    if (it == map_.end()) {
      return Proxy::kGeneric;
    }
    return it->second;
  }

54
  Proxy GetProxy(const Stmt &stmt) const { return GetProxy(stmt.get()); }
55

56
  void VisitStmt_(const EvaluateNode *op) final {
57
58
59
60
61
62
63
64
65
    Proxy proxy = Proxy::kAsync;
    if (auto call = op->value.as<CallNode>()) {
      if (call->op.same_as(LDMatrixOp()) || call->op.same_as(STMatrixOp())) {
        proxy = Proxy::kGeneric;
      }
    }
    SetProxy(op, proxy);
  }

66
  void VisitStmt_(const BufferStoreNode *op) final {
67
68
69
70
    Proxy proxy = Proxy::kGeneric;
    SetProxy(op, proxy);
  }

71
  void VisitStmt_(const SeqStmtNode *op) final {
72
73
74
75
76
77
78
79
80
81
82
    StmtVisitor::VisitStmt_(op);
    auto role = GetProxy(op->seq[0]);
    for (auto stmt : op->seq) {
      if (role != GetProxy(stmt)) {
        role = Proxy::kBoth;
        break;
      }
    }
    SetProxy(op, role);
  }

83
  void VisitStmt_(const IfThenElseNode *op) final {
84
85
86
87
    StmtVisitor::VisitStmt_(op);
    auto role = GetProxy(op->then_case);
    if (op->else_case.defined()) {
      auto role_else = GetProxy(op->else_case.value());
88
89
      if (role != role_else)
        role = Proxy::kBoth;
90
91
92
93
    }
    SetProxy(op, role);
  }

94
  void VisitStmt_(const BlockRealizeNode *op) final {
95
96
97
98
    StmtVisitor::VisitStmt_(op);
    SetProxy(op, GetProxy(op->block));
  }

99
  template <class NodeType> void HandleBodyStmt(const NodeType *op) {
100
101
102
103
    StmtVisitor::VisitStmt_(op);
    SetProxy(op, GetProxy(op->body));
  }

104
105
106
107
108
  void VisitStmt_(const ForNode *op) final { HandleBodyStmt(op); }
  void VisitStmt_(const LetStmtNode *op) final { HandleBodyStmt(op); }
  void VisitStmt_(const AttrStmtNode *op) final { HandleBodyStmt(op); }
  void VisitStmt_(const AssertStmtNode *op) final { HandleBodyStmt(op); }
  void VisitStmt_(const BlockNode *op) final { HandleBodyStmt(op); }
109

110
111
112
private:
  void SetProxy(const StmtNode *stmt, Proxy proxy) { map_[stmt] = proxy; }
  std::unordered_map<const StmtNode *, Proxy> map_;
113
114
115
};

class InjectFenceProxy : public StmtExprMutator {
116
public:
117
118
119
120
121
122
  static PrimFunc Substitute(PrimFunc f) {
    auto T = InjectFenceProxy();
    f.CopyOnWrite()->body = T(f->body);
    return f;
  }

123
124
private:
  Proxy get_generic_proxy(const Stmt &stmt) {
125
126
127
128
129
    auto marker = ProxyMarker();
    marker(stmt);
    return marker.GetProxy(stmt);
  }

130
  Stmt VisitStmt_(const SeqStmtNode *op) final {
131
132
133
    ICHECK(op->seq.size() > 0);
    Array<Stmt> new_body;
    Proxy cur_proxy, prev_proxy;
134
135
    auto fence_stmt =
        Evaluate(Call(DataType::Handle(), FenceProxyAsyncOp(), {}));
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    prev_proxy = get_generic_proxy(op->seq[0]);
    new_body.push_back(VisitStmt(op->seq[0]));
    if (op->seq.size() > 1) {
      for (int i = 1; i < static_cast<int>(op->seq.size()); i++) {
        cur_proxy = get_generic_proxy(op->seq[i]);
        if (cur_proxy == Proxy::kAsync && prev_proxy == Proxy::kGeneric) {
          new_body.push_back(fence_stmt);
        }
        new_body.push_back(VisitStmt(op->seq[i]));
        prev_proxy = cur_proxy;
      }
    }
    ICHECK(new_body.size() > 0);
    return new_body.size() == 1 ? new_body[0] : SeqStmt(std::move(new_body));
  }

  // Stmt VisitStmt_(const ForNode* op) final {
  //   std::cout << "ForNode:" << op->body->GetTypeKey() << std::endl;
  //   return StmtExprMutator::VisitStmt_(op);
  // }

  InjectFenceProxy() = default;
};

using namespace tir::transform;

tvm::transform::Pass InjectFenceProxy() {
  auto pass_func = [=](PrimFunc f, IRModule m, PassContext ctx) {
    return InjectFenceProxy::Substitute(f);
  };
  return CreatePrimFuncPass(pass_func, 0, "tl.InjectFenceProxy", {});
}

TVM_REGISTER_GLOBAL("tl.transform.InjectFenceProxy")
    .set_body_typed(InjectFenceProxy);

172
173
} // namespace tl
} // namespace tvm