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 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
|
#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>
#include <c10/util/irange.h>
namespace torch::jit::tensorexpr {
template <
typename Op,
std::enable_if_t<std::is_same_v<
decltype(detail::bin_op_deducer(std::declval<Op>())),
void>>* = nullptr>
static void visit_binary_op(const NodePtr<Op>& v, IRVisitor* visitor) {
v->lhs()->accept(visitor);
v->rhs()->accept(visitor);
}
void IRVisitor::visit(const AddPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const SubPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const MulPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const DivPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const ModPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const MaxPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const MinPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const AndPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const OrPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const XorPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const LshiftPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const RshiftPtr& v) {
visit_binary_op(v, this);
}
void IRVisitor::visit(const CompareSelectPtr& v) {
v->lhs()->accept(this);
v->rhs()->accept(this);
v->ret_val1()->accept(this);
v->ret_val2()->accept(this);
}
#define IMM_VISIT(Type, Name) \
void IRVisitor::visit(const Name##ImmPtr& v) {}
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_VISIT)
#undef IMM_VISIT
void IRVisitor::visit(const CastPtr& v) {
v->src_value()->accept(this);
}
void IRVisitor::visit(const BitCastPtr& v) {
v->src_value()->accept(this);
}
void IRVisitor::visit(const VarPtr& v) {}
void IRVisitor::visit(const RampPtr& v) {
v->base()->accept(this);
v->stride()->accept(this);
}
void IRVisitor::visit(const LoadPtr& v) {
v->buf()->accept(this);
for (const ExprPtr& ind : v->indices()) {
ind->accept(this);
}
}
void IRVisitor::visit(const BufPtr& v) {
v->base_handle()->accept(this);
if (v->qscale()) {
v->qscale()->accept(this);
}
if (v->qzero()) {
v->qzero()->accept(this);
}
}
void IRVisitor::visit(const StorePtr& v) {
v->buf()->accept(this);
for (const ExprPtr& ind : v->indices()) {
ind->accept(this);
}
v->value()->accept(this);
}
void IRVisitor::visit(const AtomicAddPtr& v) {
v->buf()->accept(this);
for (const ExprPtr& ind : v->indices()) {
ind->accept(this);
}
v->value()->accept(this);
}
void IRVisitor::visit(const SyncThreadsPtr& v) {}
void IRVisitor::visit(const ExternalCallPtr& v) {
v->buf()->accept(this);
for (const BufPtr& buf_arg : v->buf_args()) {
buf_arg->accept(this);
}
for (const ExprPtr& arg : v->args()) {
arg->accept(this);
}
}
void IRVisitor::visit(const ExternalCallWithAllocPtr& v) {
for (const auto& buf_out_arg : v->buf_out_args()) {
buf_out_arg->accept(this);
}
for (const auto& buf_arg : v->buf_args()) {
buf_arg->accept(this);
}
for (const auto& arg : v->args()) {
arg->accept(this);
}
}
void IRVisitor::visit(const FreeExtPtr& v) {
for (const auto& buf : v->bufs()) {
buf->accept(this);
}
}
void IRVisitor::visit(const BlockPtr& v) {
for (const StmtPtr& s : *v) {
s->accept(this);
}
}
void IRVisitor::visit(const ForPtr& v) {
v->var()->accept(this);
v->start()->accept(this);
v->stop()->accept(this);
if (v->body()) {
v->body()->accept(this);
}
}
void IRVisitor::visit(const BroadcastPtr& v) {
v->value()->accept(this);
}
void IRVisitor::visit(const IfThenElsePtr& v) {
v->condition()->accept(this);
v->true_value()->accept(this);
v->false_value()->accept(this);
}
void IRVisitor::visit(const IntrinsicsPtr& v) {
for (const auto i : c10::irange(v->nparams())) {
v->param(i)->accept(this);
}
}
void IRVisitor::visit(const AllocatePtr& v) {
v->buffer_var()->accept(this);
std::vector<ExprPtr> dims = v->dims();
for (const ExprPtr& dim : dims) {
dim->accept(this);
}
}
void IRVisitor::visit(const FreePtr& v) {
v->buffer_var()->accept(this);
}
void IRVisitor::visit(const PlacementAllocatePtr& v) {
v->buf()->accept(this);
v->buf_to_reuse()->accept(this);
}
void IRVisitor::visit(const LetPtr& v) {
v->var()->accept(this);
v->value()->accept(this);
}
void IRVisitor::visit(const CondPtr& v) {
ExprPtr condition = v->condition();
StmtPtr true_stmt = v->true_stmt();
StmtPtr 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 TermPtr& v) {
v->scalar()->accept(this);
for (const auto& t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const PolynomialPtr& v) {
v->scalar()->accept(this);
for (const auto& t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const RoundOffPtr& v) {
v->lhs()->accept(this);
v->rhs()->accept(this);
}
void IRVisitor::visit(const MaxTermPtr& v) {
if (v->scalar()) {
v->scalar()->accept(this);
}
for (const auto& t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const MinTermPtr& v) {
if (v->scalar()) {
v->scalar()->accept(this);
}
for (const auto& t : v->variables()) {
t->accept(this);
}
}
void IRVisitor::visit(const ReduceOpPtr& v) {
v->body()->accept(this);
for (const auto& r : v->reduce_args()) {
r->accept(this);
}
}
} // namespace torch::jit::tensorexpr
|