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 41 42 43 44 45 46 47 48 49 50 51 52 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/reduction.h>
#include <torch/csrc/jit/tensorexpr/tensor.h>
namespace torch {
namespace jit {
namespace tensorexpr {
template <typename Op>
static void visit_binary_op(const BinaryOpNode<Op>* v, IRVisitor* visitor) {
v->lhs()->accept(visitor);
v->rhs()->accept(visitor);
}
void IRVisitor::visit(const Add* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Sub* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Mul* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Div* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Mod* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Max* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Min* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const And* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Or* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Xor* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Lshift* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const Rshift* v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const CompareSelect* v) {
v->lhs()->accept(this);
v->rhs()->accept(this);
v->ret_val1()->accept(this);
v->ret_val2()->accept(this);
}
// NOLINTNEXTLINE
#define IMM_VISIT(Type, Name) \
void IRVisitor::visit(const Name##Imm* v) {}
AT_FORALL_SCALAR_TYPES_AND2(Bool, Half, IMM_VISIT);
#undef IMM_VISIT
void IRVisitor::visit(const Cast* v) {
v->src_value()->accept(this);
}
void IRVisitor::visit(const Var* v) {}
void IRVisitor::visit(const Ramp* v) {
v->base()->accept(this);
v->stride()->accept(this);
}
void IRVisitor::visit(const Load* v) {
v->buf()->accept(this);
for (const Expr* ind : v->indices()) {
ind->accept(this);
}
v->mask()->accept(this);
}
void IRVisitor::visit(const Buf* v) {
v->base_handle()->accept(this);
}
void IRVisitor::visit(const Store* v) {
v->buf()->accept(this);
for (const Expr* ind : v->indices()) {
ind->accept(this);
}
v->value()->accept(this);
v->mask()->accept(this);
}
void IRVisitor::visit(const AtomicAdd* v) {
v->buf()->accept(this);
for (const Expr* ind : v->indices()) {
ind->accept(this);
}
v->value()->accept(this);
}
void IRVisitor::visit(const SyncThreads* v) {}
void IRVisitor::visit(const Block* v) {
for (Stmt* s : *v) {
s->accept(this);
}
}
void IRVisitor::visit(const For* v) {
v->var()->accept(this);
v->start()->accept(this);
v->stop()->accept(this);
if (v->body()) {
v->body()->accept(this);
}
}
void IRVisitor::visit(const Broadcast* v) {
v->value()->accept(this);
}
void IRVisitor::visit(const IfThenElse* v) {
v->condition()->accept(this);
v->true_value()->accept(this);
v->false_value()->accept(this);
}
void IRVisitor::visit(const BaseCallNode* v) {
for (int i = 0; i < v->nparams(); i++) {
v->param(i)->accept(this);
}
}
void IRVisitor::visit(const Intrinsics* v) {
const BaseCallNode* base = v;
this->visit(base);
}
void IRVisitor::visit(const FunctionCall* v) {
const BaseCallNode* base = v;
this->visit(base);
}
void IRVisitor::visit(const Allocate* v) {
v->buffer_var()->accept(this);
std::vector<const Expr*> dims = v->dims();
for (const Expr* dim : dims) {
dim->accept(this);
}
}
void IRVisitor::visit(const Free* v) {
v->buffer_var()->accept(this);
}
void IRVisitor::visit(const Let* v) {
v->var()->accept(this);
v->value()->accept(this);
}
void IRVisitor::visit(const Cond* v) {
const Expr* condition = v->condition();
Stmt* true_stmt = v->true_stmt();
Stmt* false_stmt = v->false_stmt();
condition->accept(this);
if (true_stmt) {
true_stmt->accept(this);
}
if (false_stmt) {
false_stmt->accept(this);
}
}
void IRVisitor::visit(const Term* v) {
v->scalar()->accept(this);
for (auto* t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const Polynomial* v) {
v->scalar()->accept(this);
for (auto* t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const RoundOff* v) {
v->lhs()->accept(this);
v->rhs()->accept(this);
}
void IRVisitor::visit(const MaxTerm* v) {
if (v->scalar()) {
v->scalar()->accept(this);
}
for (auto* t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const MinTerm* v) {
if (v->scalar()) {
v->scalar()->accept(this);
}
for (auto* t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const ReduceOp* v) {
v->accumulator()->accept(this);
v->body().node()->accept(this);
for (auto* e : v->output_args()) {
e->accept(this);
}
for (auto* r : v->reduce_args()) {
r->accept(this);
}
}
} // namespace tensorexpr
} // namespace jit
} // namespace torch
|