make_packed_api.cc 18.3 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
/*
 * 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 make_packed_api.cc Lower PrimFunc to use the packed function API.
 */
23
24
#include <tvm/ffi/function.h>
#include <tvm/ffi/reflection/registry.h>
25
#include <tvm/runtime/device_api.h>
26
#include <tvm/runtime/module.h>
27
28
29
30
31
32
33
34
35
36
37
#include <tvm/target/target.h>
#include <tvm/tir/analysis.h>
#include <tvm/tir/buffer.h>
#include <tvm/tir/builtin.h>
#include <tvm/tir/expr.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/transform.h>

#include <utility>
#include <vector>

38
#include "../op/builtin.h"
39
#include "arg_binder.h"
40
41
42
43
44
#include "tir/transforms/ir_utils.h"

namespace tvm {
namespace tl {
using namespace tir;
45
using namespace ffi;
46
47
48
49
50
51
static constexpr const char *kDeviceContextVar = "device_api_context";

namespace {
class ReturnRewriter : public StmtMutator {
public:
  explicit ReturnRewriter(Var ret_var, Var ret_tcode)
52
      : ret_var_(std::move(ret_var)), ret_tcode_(std::move(ret_tcode)) {}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

  Stmt VisitStmt_(const ForNode *node) override {
    if (node->kind == ForKind::kParallel)
      in_parallel_ += 1;
    Stmt ret = StmtMutator::VisitStmt_(node);
    if (node->kind == ForKind::kParallel)
      in_parallel_ -= 1;
    return ret;
  }

  Stmt VisitStmt_(const EvaluateNode *node) override {
    Stmt ret = StmtMutator::VisitStmt_(node);
    const EvaluateNode *eval = ret.as<EvaluateNode>();
    ICHECK(eval);
    if (const CallNode *call = eval->value.as<CallNode>()) {
      if (call->op.same_as(builtin::ret())) {
        ICHECK_EQ(in_parallel_, 0)
            << "tir.ret cannot be used in parallel scope.";
        ICHECK_EQ(call->args.size(), 1) << "tir.ret expect a single argument.";
        ret = WriteToOut(call->args[0]);
      }
    }
    return ret;
  }

private:
  struct ConvertedInfo {
80
    int type_index{-1};
81
82
83
84
85
    PrimExpr expr;
    Buffer dummy_val_buffer;
    Buffer dummy_tcode_buffer;
  };

86
  ConvertedInfo ConvertForFFI(const PrimExpr &val) {
87
88
89
90
91
    ConvertedInfo info;

    // convert val's data type to FFI data type, return type code
    DataType dtype = val.dtype();
    if (dtype.is_int() || dtype.is_uint()) {
92
      info.type_index = ffi::TypeIndex::kTVMFFIInt;
93
94
      info.expr = Cast(DataType::Int(64), val);
    } else if (dtype.is_float()) {
95
      info.type_index = ffi::TypeIndex::kTVMFFIFloat;
96
97
      info.expr = Cast(DataType::Float(64), val);
    } else if (dtype.is_void()) {
98
      info.type_index = ffi::TypeIndex::kTVMFFINone;
99
100
101
102
103
104
105
      info.expr = val;
    } else {
      LOG(FATAL) << "data type " << dtype << " not supported yet";
    }

    // If multiple return locations have the same data type, use the
    // same dummy buffer declaration.
106
    auto it = dummy_val_buffer_map_.find(info.type_index);
107
108
109
110
111
112
    if (it != dummy_val_buffer_map_.end()) {
      info.dummy_val_buffer = it->second;
    } else {
      info.dummy_val_buffer =
          Buffer(ret_var_, info.expr.dtype(), {1}, {1}, ConstInt32(0),
                 ret_var_->name_hint, 0, 0, kDefault);
113
      dummy_val_buffer_map_[info.type_index] = info.dummy_val_buffer;
114
115
    }

116
117
    // The type_index is always a 32-bit int, so we don't need to have a
    // separate map.
118
119
120
121
122
123
124
125
126
127
    if (!dummy_tcode_buffer_.defined()) {
      dummy_tcode_buffer_ =
          Buffer(ret_tcode_, DataType::Int(32), {1}, {1}, ConstInt32(0),
                 ret_tcode_->name_hint, 0, 0, kDefault);
    }
    info.dummy_tcode_buffer = dummy_tcode_buffer_;

    return info;
  }

128
  Stmt WriteToOut(const PrimExpr &val) {
129
130
    auto info = ConvertForFFI(val);
    Stmt store_val = BufferStore(info.dummy_val_buffer, info.expr, {0});
131
132
    Stmt store_tcode =
        BufferStore(info.dummy_tcode_buffer, info.type_index, {0});
133
134
135
136
137
138
139
140
141
142
143
144
145
    Stmt ret_zero = Evaluate(tvm::ret(0));
    return SeqStmt({store_val, store_tcode, ret_zero});
  }

  Var ret_var_;
  Var ret_tcode_;
  int in_parallel_{0};

  std::unordered_map<int, Buffer> dummy_val_buffer_map_;
  Buffer dummy_tcode_buffer_;
};

Stmt RewriteReturn(Stmt body, Var ret_var, Var ret_tcode) {
146
147
  ReturnRewriter rewriter(std::move(ret_var), std::move(ret_tcode));
  return rewriter(std::move(body));
148
149
150
151
152
153
154
}

class SubroutineCallRewriter : public StmtExprMutator {
public:
  static Optional<Stmt> Apply(const Map<GlobalVar, String> &packed_func_methods,
                              Stmt stmt) {
    SubroutineCallRewriter rewriter(packed_func_methods);
155
    stmt = rewriter.VisitStmt(stmt);
156
157
158
    if (rewriter.made_change_) {
      return stmt;
    } else {
159
      return std::nullopt;
160
161
162
163
164
165
166
167
168
169
170
171
    }
  }

private:
  explicit SubroutineCallRewriter(
      const Map<GlobalVar, String> &packed_func_methods)
      : packed_func_methods(packed_func_methods) {}

  PrimExpr VisitExpr_(const CallNode *op) override {
    auto node = Downcast<Call>(StmtExprMutator::VisitExpr_(op));

    if (auto *gvar_ptr = node->op.as<GlobalVarNode>()) {
172
      auto gvar = tvm::ffi::GetRef<GlobalVar>(gvar_ptr);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
      if (auto symbol = packed_func_methods.Get(gvar)) {
        Array<PrimExpr> cpacked_args;
        cpacked_args.push_back(tir::StringImm(symbol.value()));
        for (auto arg : node->args) {
          cpacked_args.push_back(arg);
        }

        // push an empty handle to be compatible with current cpacked convention
        cpacked_args.push_back(tir::make_zero(DataType::Handle()));
        made_change_ = true;
        return tir::Call(node->dtype, tir::builtin::tvm_call_cpacked(),
                         cpacked_args);
      }
    }

    return node;
  }
  const Map<GlobalVar, String> &packed_func_methods;
  bool made_change_{false};
};

} // namespace

196
197
198
inline Stmt MakeAssertEQ(PrimExpr lhs, PrimExpr rhs, const std::string &msg) {
  return AssertStmt(std::move(lhs) == std::move(rhs), tvm::tir::StringImm(msg),
                    Evaluate(0));
199
200
}

201
202
inline Stmt MakeAssertNotNull(PrimExpr ptr, const std::string &msg) {
  Call isnull(DataType::Bool(), builtin::isnullptr(), {std::move(ptr)});
203
204
205
206
207
208
209
210
  return AssertStmt(!isnull, tvm::tir::StringImm(msg), Evaluate(0));
}

/* \brief Return the global_symbol of the function, if it should be updated
 *
 * \param func The function to be inspected
 *
 * \returns The global_symbol to be used for the function at call
211
 * sites, or std::nullopt if the function is to remain unchanged.
212
213
214
215
216
217
 */
Optional<String> RequiresPackedAPI(const PrimFunc &func) {
  // A function with an explicit calling convention has already been
  // lowered, and should not be modified.
  if (auto opt = func->GetAttr<Integer>(tvm::attr::kCallingConv)) {
    if (CallingConv(opt.value()->value) != CallingConv::kDefault) {
218
      return std::nullopt;
219
220
221
222
223
    }
  }

  // Internal function calls do not need the PackedFunc API
  auto global_symbol = func->GetAttr<String>(tvm::attr::kGlobalSymbol);
224
  if (!global_symbol) {
225
    return std::nullopt;
226
227
228
229
230
231
232
  }

  return global_symbol;
}

PrimFunc MakePackedAPI(PrimFunc func) {
  auto global_symbol = RequiresPackedAPI(func);
233
  if (!global_symbol) {
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    return func;
  }
  std::string name_hint = global_symbol.value();

  Target target = [&]() {
    auto opt = func->GetAttr<Target>(tvm::attr::kTarget);
    ICHECK(opt) << "MakePackedAPI required the function to be annotated with "
                   "tvm::attr::kTarget ("
                << tvm::attr::kTarget
                << "), but the function only has attributes " << func->attrs;
    return opt.value();
  }();
  int target_device_type = target->GetTargetDeviceType();

  // A function without a host target has already been lowered.
  Target target_host;
  if (auto opt = target->GetHost()) {
    target_host = opt.value();
  } else {
    return func;
  }

  auto *func_ptr = func.CopyOnWrite();
  const Stmt nop = Evaluate(0);
  int num_args = static_cast<int>(func_ptr->params.size());

  // Data field definitions
  // The packed fields
  Var v_packed_args("args", DataType::Handle());
  Buffer buf_packed_arg_type_ids =
      decl_buffer({IntImm(DataType::Int(32), func_ptr->params.size())},
                  DataType::Int(32), "arg_type_ids");
  Var v_num_packed_args("num_args", DataType::Int(32));
  Var v_out_ret_value("out_ret_value", PointerType(PrimType(DataType::Void())));
  Var v_out_ret_tcode("out_ret_tcode",
                      PointerType(PrimType(DataType::Int(32))));
  Var v_resource_handle("resource_handle", DataType::Handle());
  // The arguments of the function.

  // The device context
  Var device_id("dev_id");
  Integer device_type(target_device_type);
  // seq_init gives sequence of initialization
  // seq_check gives sequence of later checks after init
  std::vector<Stmt> seq_init, seq_check, arg_buffer_declarations;
  std::unordered_map<const VarNode *, PrimExpr> vmap;
  ArgBinder binder(&vmap);
281
282
283
284
  std::vector<Stmt> shape_checks;
  tvm::transform::PassContext ctxt = tvm::transform::PassContext::Current();
  bool disable_dynamic_tail_split =
      ctxt->GetConfig<Bool>(kDisableDynamicTailSplit, Bool(true)).value();
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

  // ---------------------------
  // local function definitions
  // load i-th argument as type t
  auto f_arg_value = [&](DataType t, int i) {
    Array<PrimExpr> call_args{
        v_packed_args, IntImm(DataType::Int(32), i),
        IntImm(DataType::Int(32), builtin::kTVMValueContent)};
    // load 64 bit version
    DataType api_type = APIType(t);
    PrimExpr res = Call(api_type, builtin::tvm_struct_get(), call_args);
    // cast to the target version.
    if (api_type != t) {
      res = Cast(t, res);
    }
    return res;
  };

  // Find the device API context argument based on name
  for (const auto &param : func_ptr->params) {
    if (param->name_hint == kDeviceContextVar) {
      num_args--;
      v_resource_handle = param;
      break;
    }
  }

  // Assert correct type codes for each argument.  This must be done
  // *before* any initialization steps produced by
  // `binder.BindDLTensor()`.  The validity of those initialization
  // steps depends on the correct types being present, and must not
  // occur before the type codes are actually checked.
  seq_init.push_back(
      MakeAssertEQ(v_num_packed_args, num_args, [&]() -> std::string {
        std::ostringstream error_message;
        error_message << name_hint << ": num_args should be " << num_args;
        return error_message.str();
      }()));

  seq_init.push_back(MakeAssertNotNull(
      v_packed_args, name_hint + ": TVMValue* arg pointer was NULL"));
  seq_init.push_back(MakeAssertNotNull(
      buf_packed_arg_type_ids->data, name_hint + ": int* type_codes was NULL"));

  seq_init.emplace_back(DeclBuffer(buf_packed_arg_type_ids, nop));

  // Need to delay binding of the buffers, in case some arguments also
  // appear in the buffer.
  std::vector<std::pair<PrimExpr, Var>> var_def;
  std::vector<std::pair<Var, Buffer>> buffer_def;

  for (int i = 0; i < static_cast<int>(func_ptr->params.size()); ++i) {
    Var param = func_ptr->params[i];

    // Ignore the device context argument, as it will still be passed
    // as a native argument.
    if (param->name_hint == kDeviceContextVar) {
      continue;
    }

    var_def.emplace_back(f_arg_value(param.dtype(), i), param);
    if (func_ptr->buffer_map.count(param)) {
      buffer_def.emplace_back(param, func_ptr->buffer_map[param]);
    }

    // type code checks
351
    Var type_index(param->name_hint + ".code", DataType::Int(32));
352
    seq_init.emplace_back(LetStmt(
353
        type_index,
354
355
356
357
358
359
360
        BufferLoad(buf_packed_arg_type_ids, {IntImm(DataType::Int(32), i)}),
        nop));
    DataType t = param.dtype();
    if (t.is_handle()) {
      std::ostringstream msg;
      msg << name_hint << ": Expect arg[" << i << "] to be pointer";
      seq_init.emplace_back(
361
362
363
364
          AssertStmt(type_index == ffi::TypeIndex::kTVMFFINone ||
                         type_index == ffi::TypeIndex::kTVMFFIOpaquePtr ||
                         type_index == ffi::TypeIndex::kTVMFFIDLTensorPtr ||
                         type_index >= ffi::TypeIndex::kTVMFFIStaticObjectBegin,
365
366
367
368
                     tvm::tir::StringImm(msg.str()), nop));
    } else if (t.is_int() || t.is_uint()) {
      std::ostringstream msg;
      msg << name_hint << ": Expect arg[" << i << "] to be int";
369
370
      seq_init.emplace_back(AssertStmt(type_index == kDLInt,
                                       tvm::tir::StringImm(msg.str()), nop));
371
372
373
374
    } else {
      ICHECK(t.is_float());
      std::ostringstream msg;
      msg << name_hint << ": Expect arg[" << i << "] to be float";
375
376
      seq_init.emplace_back(AssertStmt(type_index == kDLFloat,
                                       tvm::tir::StringImm(msg.str()), nop));
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    }
  }

  Array<Var> args{v_packed_args,     buf_packed_arg_type_ids->data,
                  v_num_packed_args, v_out_ret_value,
                  v_out_ret_tcode,   v_resource_handle};

  // Arg definitions are defined before buffer binding to avoid the use before
  // def errors.
  //
  // For example, for auto broadcasting, checks are required to guarantee that
  // either 0 or the original stride will be correctly used. Checks here have
  // to use the args that may have no let binding yet. Therefore, hoisting let
  // binding for args before buffer declaration is needed.
  for (const auto &[expr, param] : var_def) {
    binder.Bind(param, expr, name_hint + "." + param->name_hint, true);
  }

  for (const auto &kv : buffer_def) {
    binder.BindDLTensor(kv.second, device_type, device_id, kv.first,
                        name_hint + "." + kv.first->name_hint);
    arg_buffer_declarations.push_back(DeclBuffer(kv.second, nop));
  }

  func =
      WithAttrs(std::move(func),
                {{tvm::attr::kCallingConv, Integer(CallingConv::kCPackedFunc)},
                 {tvm::attr::kTarget, target_host}});
  Stmt body = RewriteReturn(func_ptr->body, v_out_ret_value, v_out_ret_tcode);
  body = AttrStmt(make_zero(DataType::Int(32)), tir::attr::compute_scope,
                  StringImm(name_hint + "_compute_"), body);
  // Set device context
  if (vmap.count(device_id.get())) {
410
    auto node = String("default");
411
412
413
414
    seq_check.push_back(AttrStmt(node, tir::attr::device_id, device_id, nop));
    seq_check.push_back(
        AttrStmt(node, tir::attr::device_type, device_type, nop));

415
    if (runtime::DeviceAPI::NeedSetDevice(target_device_type)) {
416
417
418
419
420
421
422
423
      Stmt set_device =
          Evaluate(Call(DataType::Int(32), builtin::tvm_call_packed(),
                        {StringImm(runtime::symbol::tvm_set_device),
                         device_type, device_id}));
      body = SeqStmt({set_device, body});
    }
  }

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  // (zhengju) For dynamic constraint, we need to check the buffer shape and
  // dtype to make sure the buffer can be vectorized.
  for (const auto &kv : buffer_def) {
    if (disable_dynamic_tail_split) {
      Optional<Integer> opt_dynamic_alignment =
          ctxt->GetConfig(kDynamicAlignment, Optional<Integer>());
      int dynamic_alignment = opt_dynamic_alignment.value_or(Integer(8))->value;
      // The vectorize dimension will be the last dimension of the buffer
      auto vectorize_dim = kv.second->shape[kv.second->shape.size() - 1];
      auto shape_vectorize_expr = [&]() -> PrimExpr {
        PrimExpr result = IntImm(kv.second->DefaultIndexType(), 1);
        result = result * vectorize_dim;
        result = FloorMod(result, dynamic_alignment);
        return result;
      }();
      shape_checks.emplace_back(AssertStmt(
          shape_vectorize_expr == 0,
          tvm::tir::StringImm(
              kv.second->name +
              ": Vectorize dimension in buffer must be divisible by " +
              std::to_string(dynamic_alignment)),
          nop));
    }
  }

449
450
451
  // Return error code of zero on success
  body = SeqStmt({body, Evaluate(ret(Integer(0)))});

452
453
454
455
456
457
458
459
460
461
  if (!disable_dynamic_tail_split) {
    body = MergeNest({seq_init, binder.init_nest(), seq_check, binder.asserts(),
                      arg_buffer_declarations},
                     body);
  } else {
    body = MergeNest({seq_init, binder.init_nest(), seq_check, binder.asserts(),
                      arg_buffer_declarations, shape_checks},
                     body);
  }

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  func_ptr->body = body;
  func_ptr->params = args;

  Array<Var> undefined = UndefinedVars(func_ptr->body, func_ptr->params);
  ICHECK_EQ(undefined.size(), 0)
      << "In PrimFunc " << name_hint << " variables " << undefined
      << " are used, but are not passed in as API arguments";

  func_ptr->buffer_map = Map<Var, Buffer>();
  func_ptr->ret_type = PrimType(DataType::Int(32)); // return the function.
  return func;
}

tvm::transform::Pass MakePackedAPI() {
  using tvm::transform::Pass;
477
  auto pass_func = [](IRModule mod, const tvm::transform::PassContext &ctx) {
478
479
480
    Map<GlobalVar, String> packed_func_methods;
    for (const auto &[gvar, base_func] : mod->functions) {
      if (auto opt = base_func.as<PrimFunc>()) {
481
        const auto &prim_func = opt.value();
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        if (auto global_symbol = RequiresPackedAPI(prim_func)) {
          packed_func_methods.Set(gvar, global_symbol.value());
        }
      }
    }

    IRModuleNode *mptr = mod.CopyOnWrite();
    IRModule updates;

    for (const auto &[gvar, base_func] : mptr->functions) {
      if (auto opt = base_func.as<PrimFunc>()) {
        auto func = opt.value();
        auto orig_func = func;

        if (auto body = SubroutineCallRewriter::Apply(packed_func_methods,
                                                      func->body)) {
          func.CopyOnWrite()->body = body.value();
        }
        func = MakePackedAPI(std::move(func));

        if (!func.same_as(orig_func)) {
          updates->Add(gvar, func);
        }
      }
    }

508
    if (!updates->functions.empty()) {
509
510
511
512
513
514
515
516
      mod.CopyOnWrite()->Update(updates);
    }
    return mod;
  };

  return tvm::transform::CreateModulePass(pass_func, 0, "tl.MakePackedAPI", {});
}

517
TVM_FFI_STATIC_INIT_BLOCK() {
518
519
520
  namespace refl = tvm::ffi::reflection;
  refl::GlobalDef().def("tl.transform.MakePackedAPI",
                        []() { return MakePackedAPI(); });
521
}
522
523
524

} // namespace tl
} // namespace tvm