ir_operator.cc 14.2 KB
Newer Older
1 2 3 4 5 6
/*!
 *  Copyright (c) 2017 by Contributors
 * \file ir_operator.cc
 */
#include <tvm/base.h>
#include <tvm/ir.h>
7
#include <tvm/ir_operator.h>
8 9 10

namespace tvm {

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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 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
/*!
 * \brief Check whether type is used to represent index.
 *
 * Index types are frequently used in shape computation
 * and need to be aggressively constant-folded.
 *
 * \param type The type to represent index.
 * \return the checked result.
 */
inline bool IsIndexType(const Type& type) {
  return type.is_int() && type.lanes() == 1 &&
      (type.bits() == 32 || type.bits() == 64);
}

// simple cast that only checks if type matches and cast
inline Expr SimpleCast(const Type& t, Expr value) {
  if (value.type() == t) return value;
  return ir::Cast::make(t, value);
}

// The public function with a quick checking path.
void BinaryOpMatchTypes(Expr& lhs, Expr& rhs) {  // NOLINT(*)
  if (lhs.type() == rhs.type()) return;
  Type ltype = lhs.type();
  Type rtype = rhs.type();
  if (ltype.lanes() == 1 && rtype.lanes() != 1) {
    lhs = ir::Broadcast::make(lhs, rtype.lanes());
  } else if (rtype.lanes() == 1 && ltype.lanes() != 1) {
    rhs = ir::Broadcast::make(rhs, ltype.lanes());
  } else {
    CHECK(ltype.lanes() == rtype.lanes())
        << "Cannot match type " << ltype << " vs " << rtype;
  }
  if (lhs.type() == rhs.type()) return;
  // Only do very simple type coversion
  // int->float, int(32)->int(64)
  // require the types to be relatively consistent
  // This will the reduce amount code generated by operators
  // and also help user to find potential type conversion problems.
  if (!lhs.type().is_float() && rhs.type().is_float()) {
    // int->float
    lhs = ir::Cast::make(rhs.type(), lhs);
  } else if (lhs.type().is_float() && !rhs.type().is_float()) {
    // int->float
    rhs = ir::Cast::make(lhs.type(), rhs);
  } else if ((lhs.type().is_int() && rhs.type().is_int()) ||
             (lhs.type().is_uint() && rhs.type().is_uint())) {
    // promote int to higher bits
    if (lhs.type().bits() < rhs.type().bits()) {
      lhs = ir::Cast::make(rhs.type(), lhs);
    } else {
      rhs = ir::Cast::make(lhs.type(), rhs);
    }
  } else if ((lhs.type().is_int() && rhs.type().is_uint()) ||
             (lhs.type().is_uint() && rhs.type().is_int())) {
    int bits = std::max(lhs.type().bits(), rhs.type().bits());
    lhs = SimpleCast(Int(bits, lhs.type().lanes()), lhs);
    rhs = SimpleCast(Int(bits, rhs.type().lanes()), rhs);
  } else {
    LOG(FATAL) << "Cannot match type " << ltype << " vs " << rtype;
  }
}


template<typename ValueType>
inline bool ConstPowerHelper(ValueType val, int *shift) {
  if (val <= 0) return false;
  shift[0] = 0;
  while (val != 0) {
    if (val & 1) {
      return (val == 1);
    }
    ++shift[0];
    val = val >> 1;
  }
  return true;
}

bool is_const_power_of_two_integer(const Expr& x, int* shift) {
  if (const auto* op = x.as<ir::IntImm>()) {
    return ConstPowerHelper(op->value, shift);
  } else if (const auto* op = x.as<ir::UIntImm>()) {
    return ConstPowerHelper(op->value, shift);
  } else {
    return false;
  }
}

Expr cast(const Type& t, Expr value) {
  using ir::IntImm;
  if (value.type() == t) return value;
  // const fold IntImm as they are used in index computations
  if (t.lanes() == 1) {
    if (const IntImm* op = value.as<IntImm>()) {
      return make_const(t, op->value);
    }
    return ir::Cast::make(t, value);
  } else {
    if (value.type().lanes() == 1) {
      // manually unroll cast
      Type vtype = t.element_of();
      if (value.type() != vtype) {
        if (const IntImm* op = value.as<IntImm>()) {
          value = make_const(vtype, op->value);
        } else {
          value = ir::Cast::make(vtype, value);
        }
      }
      return ir::Broadcast::make(value, t.lanes());
    } else {
      CHECK(value.type().lanes() == t.lanes());
      return ir::Cast::make(t, value);
    }
  }
}

Expr reinterpret(const Type& t, Expr value) {
  if (value.type() == t) return value;
  return ir::Call::make(t, ir::Call::reinterpret, { value }, ir::Call::PureIntrinsic);
}

#define TVM_CONST_PROPAGATION(BODY)                                     \
  using ir::IntImm;                                                     \
  using ir::UIntImm;                                                    \
  const IntImm* pa = a.as<IntImm>();                                    \
  const IntImm* pb = b.as<IntImm>();                                    \
  const Type& ta = a.type();                                            \
  const Type& tb = b.type();                                            \
  if (IsIndexType(ta) && IsIndexType(tb)) {                             \
    BODY;                                                               \
  }                                                                     \
  BinaryOpMatchTypes(a, b);


Expr operator+(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, pa->value + pb->value);
      if (pa && pa->value == 0) return SimpleCast(rtype, b);
      if (pb && pb->value == 0) return SimpleCast(rtype, a);
    });
  return ir::Add::make(a, b);
}

Expr operator-(Expr a) {
  using ir::IntImm;
  const IntImm* pa = a.as<IntImm>();
  if (pa) {
    return ir::IntImm::make(a.type(), -pa->value);
  }
  return make_zero(a.type()) - a;
}

Expr operator-(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, pa->value - pb->value);
      if (pb && pb->value == 0) return SimpleCast(rtype, a);
    });
  return ir::Sub::make(a, b);
}

Expr operator*(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, pa->value * pb->value);
      if (pa) {
        if (pa->value == 1) return SimpleCast(rtype, b);
        if (pa->value == 0) return SimpleCast(rtype, a);
      }
      if (pb) {
        if (pb->value == 1) return SimpleCast(rtype, a);
        if (pb->value == 0) return SimpleCast(rtype, b);
      }
    });
  return ir::Mul::make(a, b);
}

Expr operator/(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      // due to division and mod can have different modes
      // only constant fold positive number where rule is fixed.
      if (pa && pb && pa->value >= 0 && pb->value > 0) {
        return IntImm::make(rtype, pa->value / pb->value);
      }
      if (pa) {
        if (pa->value == 0) return SimpleCast(rtype, a);
      }
      if (pb) {
        if (pb->value == 1) return SimpleCast(rtype, a);
        CHECK_NE(pb->value, 0) << "Divide by zero";
      }
    });
  return ir::Div::make(a, b);
}

Expr operator%(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      // due to division and mod can have different modes
      // only constant fold positive number where rule is fixed.
      if (pa && pb && pa->value >= 0 && pb->value > 0) {
        return IntImm::make(rtype, pa->value % pb->value);
      }
      if (pa) {
        if (pa->value == 0) return SimpleCast(rtype, a);
      }
      if (pb) {
        if (pb->value == 1) return make_zero(rtype);
        CHECK_NE(pb->value, 0) << "Divide by zero";
      }
    });
  return ir::Mod::make(a, b);
}

Expr min(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, std::min(pa->value, pb->value));
    });
  return ir::Min::make(a, b);
}

Expr max(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, std::max(pa->value, pb->value));
    });
  return ir::Max::make(a, b);
}

Expr select(Expr cond, Expr true_value, Expr false_value) {
  using ir::IntImm;
  using ir::UIntImm;
  CHECK(cond.type().is_bool());
  BinaryOpMatchTypes(true_value, false_value);
  if (const UIntImm* op = cond.as<UIntImm>()) {
    if (op->value != 0) {
      return true_value;
    } else {
      return false_value;
    }
  } else if (const IntImm* op = cond.as<IntImm>()) {
    if (op->value != 0) {
      return true_value;
    } else {
      return false_value;
    }
  }
  return ir::Select::make(cond, true_value, false_value);
}

Expr likely(Expr cond) {
  if (is_const(cond)) return cond;
  return ir::Call::make(cond.type(), ir::Call::likely, { cond }, ir::Call::PureIntrinsic);
}

Expr operator>(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value > pb->value);
    });
  return ir::GT::make(a, b);
}

Expr operator>=(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value >= pb->value);
    });
  return ir::GE::make(a, b);
}

Expr operator<(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value < pb->value);
    });
  return ir::LT::make(a, b);
}

Expr operator<=(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value <= pb->value);
    });
  return ir::LE::make(a, b);
}

Expr operator==(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value == pb->value);
    });
  return ir::EQ::make(a, b);
}

Expr operator!=(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      if (pa && pb) return UIntImm::make(UInt(1), pa->value != pb->value);
    });
  return ir::NE::make(a, b);
}

Expr operator&&(Expr a, Expr b) {
  using ir::UIntImm;
313 314 315 316 317 318 319
  if (a.type().is_bool() && b.type().is_bool()) {
    const UIntImm* pa = a.as<UIntImm>();
    const UIntImm* pb = b.as<UIntImm>();
    if (pa && pa->value) return b;
    if (pa && !pa->value) return a;
    if (pb && pb->value) return a;
    if (pb && !pb->value) return b;
320 321 322 323 324 325
  }
  return ir::And::make(a, b);
}

Expr operator||(Expr a, Expr b) {
  using ir::UIntImm;
326 327 328 329 330 331 332
  if (a.type().is_bool() && b.type().is_bool()) {
    const UIntImm* pa = a.as<UIntImm>();
    const UIntImm* pb = b.as<UIntImm>();
    if (pa && pa->value) return a;
    if (pa && !pa->value) return b;
    if (pb && pb->value) return b;
    if (pb && !pb->value) return a;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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 410 411 412 413 414 415 416
  }
  return ir::Or::make(a, b);
}

Expr operator!(Expr a) {
  using ir::UIntImm;
  const UIntImm* pa = a.as<UIntImm>();
  if (pa) {
    return UIntImm::make(UInt(1), !(pa->value));
  }
  return ir::Not::make(a);
}

Expr operator>>(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, (pa->value >> pb->value));
      if (pb) {
        if (pb->value == 0) return SimpleCast(rtype, a);
      }
    });
  return ir::Call::make(a.type(), ir::Call::shift_right, { a, b }, ir::Call::PureIntrinsic);
}

Expr operator<<(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, (pa->value << pb->value));
      if (pb) {
        if (pb->value == 0) return SimpleCast(rtype, a);
      }
    });
  return ir::Call::make(a.type(), ir::Call::shift_left, { a, b }, ir::Call::PureIntrinsic);
}

Expr operator&(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, (pa->value & pb->value));
    });
  return ir::Call::make(a.type(), ir::Call::bitwise_and, { a, b }, ir::Call::PureIntrinsic);
}

Expr operator|(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, (pa->value | pb->value));
    });
  return ir::Call::make(a.type(), ir::Call::bitwise_or, { a, b }, ir::Call::PureIntrinsic);
}

Expr operator^(Expr a, Expr b) {
  TVM_CONST_PROPAGATION({
      Type rtype = ta.bits() >= tb.bits() ? ta : tb;
      if (pa && pb) return IntImm::make(rtype, (pa->value ^ pb->value));
    });
  return ir::Call::make(a.type(), ir::Call::bitwise_xor, { a, b }, ir::Call::PureIntrinsic);
}

Expr operator~(Expr a) {
  CHECK(a.type().is_int() || a.type().is_uint());
  return ir::Call::make(a.type(), ir::Call::bitwise_not, { a }, ir::Call::PureIntrinsic);
}

Expr pow(Expr x, Expr y) {
  BinaryOpMatchTypes(x, y);
  CHECK(x.type().is_float()) << "power only applies to float";
  return ir::Call::make(x.type(), "pow", { x, y }, ir::Call::PureIntrinsic);
}

Expr abs(Expr x) {
  if (x.type().is_int()) {
    return select(x >= make_zero(x.type()), x, -x);
  } else if (x.type().is_float()) {
    return ir::Call::make(x.type(), "fabs", {x}, ir::Call::PureIntrinsic);
  } else if (x.type().is_uint()) {
    return x;
  } else {
    LOG(FATAL) << "Data type " << x.type()
               <<" not supported for absolute op. Skipping absolute op...";
    return x;
  }
}

417
Expr sum(Expr source, Array<IterVar> rdom) {
418
  Var x("x", source.type()), y("y", source.type());
419 420 421 422 423 424 425 426
  Expr result = ir::Add::make(x, y);
  Expr identity_element = make_zero(source.type());
  ir::CommReducer combiner =
    ir::CommReducerNode::make({x}, {y}, {result}, {identity_element});
  return ir::Reduce::make(combiner, {source}, rdom, make_const(Bool(1), true), 0);
}

Expr max(Expr source, Array<IterVar> rdom) {
427
  Var x("x", source.type()), y("y", source.type());
428 429 430 431 432 433 434 435
  Expr result = ir::Max::make(x, y);
  Expr identity_element = source.type().min();
  ir::CommReducer combiner =
    ir::CommReducerNode::make({x}, {y}, {result}, {identity_element});
  return ir::Reduce::make(combiner, {source}, rdom, make_const(Bool(1), true), 0);
}

Expr min(Expr source, Array<IterVar> rdom) {
436
  Var x("x", source.type()), y("y", source.type());
437 438 439 440 441 442 443
  Expr result = ir::Min::make(x, y);
  Expr identity_element = source.type().max();
  ir::CommReducer combiner =
    ir::CommReducerNode::make({x}, {y}, {result}, {identity_element});
  return ir::Reduce::make(combiner, {source}, rdom, make_const(Bool(1), true), 0);
}

444
Expr prod(Expr source, Array<IterVar> rdom) {
445
  Var x("x", source.type()), y("y", source.type());
446
  Expr result = ir::Mul::make(x, y);
447
  Expr identity_element = make_const(source.type(), 1);
448 449 450 451 452
  ir::CommReducer combiner =
    ir::CommReducerNode::make({x}, {y}, {result}, {identity_element});
  return ir::Reduce::make(combiner, {source}, rdom, make_const(Bool(1), true), 0);
}

453 454 455 456 457 458
Expr fmod(Expr x, Expr y) {
  BinaryOpMatchTypes(x, y);
  CHECK(x.type().is_float()) << "fmod only applies to float";
  return ir::Call::make(x.type(), "fmod", { x, y }, ir::Call::PureIntrinsic);
}

459
}  // namespace tvm