api_pass.cc 5.59 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.
 */

tqchen committed
20
/*!
21
 *  Copyright (c) 2017 by Contributors
tqchen committed
22
 *  Exposre of pass functions.
23
 * \file api_pass.cc
tqchen committed
24 25 26
 */
#include <tvm/expr.h>
#include <tvm/ir.h>
27
#include <tvm/attrs.h>
tqchen committed
28
#include <tvm/ir_pass.h>
29
#include <tvm/ir_visitor.h>
30
#include <tvm/ir_mutator.h>
31
#include <tvm/api_registry.h>
tqchen committed
32 33 34 35

namespace tvm {
namespace ir {

36
TVM_REGISTER_API("ir_pass.Simplify")
37
.set_body([](TVMArgs args, TVMRetValue *ret) {
38
    if (args[0].IsObjectRef<Stmt>()) {
39 40 41 42 43
      if (args.size() > 1) {
        *ret = Simplify(args[0].operator Stmt(), args[1]);
      } else {
        *ret = Simplify(args[0].operator Stmt());
      }
44
    } else {
45 46 47 48 49
      if (args.size() > 1) {
        *ret = Simplify(args[0].operator Expr(), args[1]);
      } else {
        *ret = Simplify(args[0].operator Expr());
      }
50 51 52
    }
  });

53 54
TVM_REGISTER_API("ir_pass.CanonicalSimplify")
.set_body([](TVMArgs args, TVMRetValue *ret) {
55
    if (args[0].IsObjectRef<Stmt>()) {
56 57 58 59 60
      if (args.size() > 1) {
        *ret = CanonicalSimplify(args[0].operator Stmt(), args[1]);
      } else {
        *ret = CanonicalSimplify(args[0].operator Stmt());
      }
61
    } else {
62 63 64 65 66
      if (args.size() > 1) {
        *ret = CanonicalSimplify(args[0].operator Expr(), args[1]);
      } else {
        *ret = CanonicalSimplify(args[0].operator Expr());
      }
67 68 69
    }
  });

70 71
TVM_REGISTER_API("ir_pass.Substitute")
.set_body([](TVMArgs args, TVMRetValue *ret) {
72
    if (args[0].IsObjectRef<Stmt>()) {
73 74 75 76 77 78
      *ret = Substitute(args[0].operator Stmt(), args[1].operator Map<Var, Expr>());
    } else {
      *ret = Substitute(args[0].operator Expr(), args[1].operator Map<Var, Expr>());
    }
  });

79
TVM_REGISTER_API("ir_pass.Equal")
80
.set_body([](TVMArgs args, TVMRetValue *ret) {
81
    if (args[0].IsObjectRef<Stmt>()) {
82
      *ret = Equal(args[0].operator Stmt(), args[1].operator Stmt());
83
    } else {
84
      *ret = Equal(args[0].operator Expr(), args[1].operator Expr());
85 86 87
    }
  });

88 89 90 91 92 93 94 95
TVM_REGISTER_API("ir_pass.StorageFlatten")
.set_body([](TVMArgs args, TVMRetValue *ret) {
    if (args.size() <= 3) {
      *ret = StorageFlatten(args[0], args[1], args[2]);
    } else {
      *ret = StorageFlatten(args[0], args[1], args[2], args[3]);
    }
  });
96

97 98 99 100 101 102
TVM_REGISTER_API("ir_pass.RewriteForTensorCore")
.set_body_typed<Stmt(const Stmt&, const Schedule&, const Map<Tensor, Buffer>&)>
  ([](const Stmt& stmt, const Schedule& schedule, const Map<Tensor, Buffer>& extern_buffer) {
      return RewriteForTensorCore(stmt, schedule, extern_buffer);
  });

103
TVM_REGISTER_API("ir_pass.AttrsEqual")
104 105 106
.set_body_typed<bool(const NodeRef&, const NodeRef&)>([](const NodeRef& lhs, const NodeRef& rhs) {
    return AttrsEqual()(lhs, rhs);
  });
107 108

TVM_REGISTER_API("ir_pass.AttrsHash")
109 110 111
.set_body_typed<int64_t(const NodeRef&)>([](const NodeRef &node) {
    return AttrsHash()(node);
  });
112 113


114 115 116 117 118
TVM_REGISTER_API("ir_pass.ExprUseVar")
.set_body([](TVMArgs args, TVMRetValue *ret) {
    *ret = ExprUseVar(args[0].operator Expr(), args[1].operator Var());
  });

119
TVM_REGISTER_API("ir_pass.PostOrderVisit")
120 121 122 123 124 125 126
.set_body([](TVMArgs args, TVMRetValue *ret) {
    PackedFunc f = args[1];
    ir::PostOrderVisit(args[0], [f](const NodeRef& n) {
        f(n);
      });
  });

127 128 129 130 131 132 133 134
TVM_REGISTER_API("ir_pass.LowerStorageAccess")
.set_body([](TVMArgs args, TVMRetValue *ret) {
  LoweredFunc f = args[0];
  auto n = make_node<LoweredFuncNode>(*f.operator->());
  n->body = LowerStorageAccessInfo(f->body);
  *ret = LoweredFunc(n);
});

tqchen committed
135
// make from two arguments
136
#define REGISTER_PASS(PassName)                                   \
137
  TVM_REGISTER_API("ir_pass."#PassName)                           \
138 139 140 141 142 143 144 145 146
  .set_body_typed(PassName);                                     \


REGISTER_PASS(ConvertSSA);
REGISTER_PASS(VerifySSA);
REGISTER_PASS(RewriteUnsafeSelect);
REGISTER_PASS(Inline);
REGISTER_PASS(IRTransform);
REGISTER_PASS(VectorizeLoop);
147
REGISTER_PASS(SkipVectorize);
148 149 150 151 152 153 154 155 156
REGISTER_PASS(UnrollLoop);
REGISTER_PASS(InjectCopyIntrin);
REGISTER_PASS(ThreadSync);
REGISTER_PASS(MakeAPI);
REGISTER_PASS(BindDeviceType);
REGISTER_PASS(SplitHostDevice);
REGISTER_PASS(StorageRewrite);
REGISTER_PASS(CoProcSync);
REGISTER_PASS(LowerStorageAccessInfo);
157
REGISTER_PASS(LowerDeviceStorageAccessInfo)
158 159 160 161 162 163 164 165 166 167 168 169
REGISTER_PASS(InjectVirtualThread);
REGISTER_PASS(InjectPrefetch);
REGISTER_PASS(InjectDoubleBuffer);
REGISTER_PASS(LoopPartition);
REGISTER_PASS(RemoveNoOp);
REGISTER_PASS(SplitPipeline);
REGISTER_PASS(LiftAttrScope);
REGISTER_PASS(NarrowChannelAccess);
REGISTER_PASS(LowerThreadAllreduce);
REGISTER_PASS(LowerWarpMemory);
REGISTER_PASS(RemapThreadAxis);
REGISTER_PASS(LowerIntrin);
170
REGISTER_PASS(LowerCustomDatatypes);
171 172 173 174 175 176
REGISTER_PASS(LowerTVMBuiltin);
REGISTER_PASS(CombineContextCall);
REGISTER_PASS(VerifyMemory);
REGISTER_PASS(VerifyGPUCode);
REGISTER_PASS(DecorateDeviceScope);
REGISTER_PASS(InstrumentBoundCheckers);
177
REGISTER_PASS(VerifyCompactBuffer);
178
REGISTER_PASS(HoistIfThenElse);
179
REGISTER_PASS(InferFragment)
tqchen committed
180 181
}  // namespace ir
}  // namespace tvm