expr.cc 5.88 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 22 23 24 25
/*!
 * \file expr.cc
 */
#include <tvm/base.h>
#include <tvm/expr.h>
#include <tvm/ir.h>
26
#include <tvm/expr_operator.h>
tqchen committed
27
#include <memory>
28
#include <limits>
tqchen committed
29 30

namespace tvm {
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
// maximum and min values
Expr DataType::max() const {
  using namespace ir;
  CHECK_EQ(lanes(), 1);
  if (is_int()) {
    if (bits() == 64) {
      return IntImm::make(*this, std::numeric_limits<int64_t>::max());
    } else if (bits() < 64) {
      int64_t val = 1;
      val = (val << (bits() - 1)) - 1;
      return IntImm::make(*this, val);
    }
  } else if (is_uint()) {
    if (bits() == 64) {
      return UIntImm::make(*this, std::numeric_limits<uint64_t>::max());
    } else if (bits() < 64) {
      uint64_t val = 1;
      val = (val << static_cast<uint64_t>(bits())) - 1;
      return UIntImm::make(*this, val);
    }
  } else if (is_float()) {
    if (bits() == 64) {
      return FloatImm::make(*this, std::numeric_limits<double>::max());
    } else if (bits() == 32) {
      return FloatImm::make(*this, std::numeric_limits<float>::max());
    } else if (bits() == 16) {
      return FloatImm::make(*this, 65504.0);
    }
  }
  LOG(FATAL) << "Cannot decide max_value for type" << *this;
  return Expr();
}

Expr DataType::min() const {
  using namespace ir;
  CHECK_EQ(lanes(), 1);
  if (is_int()) {
    if (bits() == 64) {
      return IntImm::make(*this, std::numeric_limits<int64_t>::lowest());
    } else if (bits() < 64) {
      int64_t val = 1;
      val = -(val << (bits() - 1));
      return IntImm::make(*this, val);
    }
  } else if (is_uint()) {
    return UIntImm::make(*this, 0);
  } else if (is_float()) {
    if (bits() == 64) {
      return FloatImm::make(*this, std::numeric_limits<double>::lowest());
    } else if (bits() == 32) {
      return FloatImm::make(*this, std::numeric_limits<float>::lowest());
    } else if (bits() == 16) {
      return FloatImm::make(*this, -65504.0);
    }
  }
  LOG(FATAL) << "Cannot decide min_value for type" << *this;
  return Expr();
}

Expr::Expr(int32_t value)
    : Expr(IntImm::make(Int(32), value)) {}

Expr::Expr(float value)
    : Expr(ir::FloatImm::make(Float(32), value)) {}

Expr::Expr(std::string str)
    : Expr(ir::StringImm::make(str)) {}

Var::Var(std::string name_hint, DataType t)
    : Var(Variable::make(t, name_hint)) {}

Var Variable::make(DataType t, std::string name_hint) {
  NodePtr<Variable> node = make_node<Variable>();
  node->type = t;
  node->name_hint = std::move(name_hint);
  return Var(node);
}
109

tqchen committed
110
Range::Range(Expr begin, Expr end)
111
    : Range(make_node<RangeNode>(
112 113
          begin,
          is_zero(begin) ? end : (end - begin))) {
tqchen committed
114 115
}

116 117 118 119 120 121 122 123 124
Integer IntImm::make(Type t, int64_t value) {
  CHECK(t.is_int() && t.is_scalar())
      << "ValueError: IntImm can only take scalar.";
  NodePtr<IntImm> node = make_node<IntImm>();
  node->type = t;
  node->value = value;
  return Integer(node);
}

125
Range Range::make_by_min_extent(Expr min, Expr extent) {
126
  return Range(make_node<RangeNode>(min, extent));
tqchen committed
127 128
}

129 130 131 132
IterVar IterVarNode::make(Range dom,
                          Var var,
                          IterVarType t,
                          std::string thread_tag) {
133
  NodePtr<IterVarNode> n = make_node<IterVarNode>();
tqchen committed
134
  n->dom = dom;
135
  n->var = var;
136
  n->iter_type = t;
tqchen committed
137 138 139 140
  n->thread_tag = thread_tag;
  return IterVar(n);
}

141 142 143 144 145 146 147 148 149 150
IterVar thread_axis(Range dom, std::string tag) {
  return IterVarNode::make(
      dom, Var(tag), kThreadIndex, tag);
}

IterVar reduce_axis(Range dom, std::string name) {
  return IterVarNode::make(
      dom, Var(name), kCommReduce);
}

151 152 153 154
void Dump(const NodeRef& n) {
  std::cerr << n << "\n";
}

155
Var var(std::string name_hint, Type t) {
156 157 158
  return Var(name_hint, t);
}

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
void IRPrinter::Print(const NodeRef& ir) {
  static const FType& f = vtable();
  if (!ir.defined()) {
    stream << "(nullptr)";
  } else {
    if (f.can_dispatch(ir)) {
      f(ir, this);
    } else {
      // default value, output type key and addr.
      stream << ir->type_key() << "(" << ir.get() << ")";
    }
  }
}

void IRPrinter::PrintIndent() {
  for (int i = 0; i < indent; ++i) {
    stream << ' ';
  }
}

IRPrinter::FType& IRPrinter::vtable() {
  static FType inst;
  return inst;
}

TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable)
.set_dispatch<IntImm>([](const IntImm *op, IRPrinter *p) {
    if (op->type == Int(32)) {
      p->stream << op->value;
    } else {
      p->stream << "(" << op->type << ")" << op->value;
    }
  });

tqchen committed
193 194 195 196 197 198
TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable)
.set_dispatch<IterVarNode>([](const IterVarNode *op, IRPrinter *p) {
    p->stream << "iter_var(";
    if (op->var->name_hint.length() != 0) {
      p->stream  << op->var->name_hint << ", ";
    }
tqchen committed
199 200 201
    if (op->dom.defined()) {
      p->stream << op->dom;
    }
tqchen committed
202 203 204 205 206 207 208
    if (op->thread_tag.length() != 0) {
      p->stream << ", " << op->thread_tag;
    }
    p->stream << ")";
  });

TVM_STATIC_IR_FUNCTOR(IRPrinter, vtable)
209
.set_dispatch<RangeNode>([](const RangeNode* op, IRPrinter* p) {
tqchen committed
210 211 212
    p->stream << "range(min=" << op->min << ", ext=" << op->extent << ')';
  });

213 214
TVM_REGISTER_NODE_TYPE(ArrayNode);
TVM_REGISTER_NODE_TYPE(MapNode);
215
TVM_REGISTER_NODE_TYPE(StrMapNode);
216
TVM_REGISTER_NODE_TYPE(RangeNode);
tqchen committed
217 218 219
TVM_REGISTER_NODE_TYPE(IterVarNode);

}  // namespace tvm