intrin_rule.h 1.24 KB
Newer Older
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
/*!
 *  Copyright (c) 2017 by Contributors
 * \file intrin_rule.h
 * \brief Utility to generate intrinsic rules
 */
#ifndef TVM_CODEGEN_INTRIN_RULE_H_
#define TVM_CODEGEN_INTRIN_RULE_H_

#include <tvm/ir.h>
#include <tvm/expr.h>
#include <tvm/api_registry.h>
#include <tvm/runtime/registry.h>
#include <string>

namespace tvm {
namespace codegen {
namespace intrin {
using namespace ir;

// Add float suffix to the intrinsics
struct FloatSuffix {
  std::string operator()(Type t, std::string name) const {
    if (t == Float(32)) {
      return name + 'f';
    } else if (t == Float(64)) {
      return name;
    } else {
      return "";
    }
  }
};

33 34
// Return the intrinsic name
struct Direct {
35
  std::string operator()(Type t, std::string name) const {
36
    return name;
37 38 39
  }
};

40
// Call pure extern function.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
template<typename T>
inline void DispatchExtern(const TVMArgs& args, TVMRetValue* rv) {
  Expr e = args[0];
  const Call* call = e.as<Call>();
  CHECK(call != nullptr);
  std::string name = T()(call->type, call->name);
  if (name.length() != 0) {
    *rv = Call::make(
        call->type, name, call->args, Call::PureExtern);
  } else {
    *rv = e;
  }
}

}  // namespace intrin
}  // namespace codegen
}  // namespace tvm
#endif  // TVM_CODEGEN_INTRIN_RULE_H_