From baaaa6a2fdb8c9e2dc17c1b0ce8a782c5ca6b3f5 Mon Sep 17 00:00:00 2001 From: Jian Weng Date: Mon, 11 Feb 2019 13:03:20 -0800 Subject: [PATCH] temp commit --- src/contrib/hybrid/codegen_hybrid.cc | 93 ++++++++++++++-------------- src/contrib/hybrid/codegen_hybrid.h | 29 ++++++--- 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/contrib/hybrid/codegen_hybrid.cc b/src/contrib/hybrid/codegen_hybrid.cc index 7a9d57829b24..7c641934db54 100644 --- a/src/contrib/hybrid/codegen_hybrid.cc +++ b/src/contrib/hybrid/codegen_hybrid.cc @@ -10,17 +10,24 @@ namespace contrib { using namespace ir; -std::string CodeGenHybrid::GetUniqueName(std::string name) { - for (size_t i = 0; i < name.size(); ++i) { - if (name[i] == '.') - name[i] = '_'; +std::string CodeGenHybrid::GetUniqueName(std::string prefix) { + for (size_t i = 0; i < prefix.size(); ++i) { + if (prefix[i] == '.') prefix[i] = '_'; } - auto iter = ids_allocated_.find(name); - if (iter == ids_allocated_.end()) { - ids_allocated_[name] = 1; - return name; + auto it = ids_allocated_.find(prefix); + if (it != ids_allocated_.end()) { + while (true) { + std::ostringstream os; + os << prefix << (++it->second); + std::string name = os.str(); + if (ids_allocated_.count(name) == 0) { + prefix = name; + break; + } + } } - return name + std::to_string(ids_allocated_[name]++); + ids_allocated_[prefix] = 0; + return prefix; } std::string CodeGenHybrid::Finish() { @@ -52,7 +59,7 @@ void CodeGenHybrid::VisitExpr_(const UIntImm *op, std::ostream& os) { // NOLINT } void CodeGenHybrid::VisitExpr_(const FloatImm *op, std::ostream& os) { // NOLINT(*) PrintType(op->type, os); - os << "(" << op->value << ")"; + os << "(" << std::setprecision(20) << op->value << ")"; } void CodeGenHybrid::VisitExpr_(const StringImm *op, std::ostream& os) { // NOLINT(*) os << "\"" << op->value << "\""; @@ -63,22 +70,19 @@ inline void PrintBinaryExpr(const T* op, const char *opstr, std::ostream& os, // NOLINT(*) CodeGenHybrid* p) { - if (op->type.lanes() == 1) { - if (isalpha(opstr[0])) { - os << opstr << '('; - p->PrintExpr(op->a, os); - os << ", "; - p->PrintExpr(op->b, os); - os << ')'; - } else { - os << '('; - p->PrintExpr(op->a, os); - os << ' ' << opstr << ' '; - p->PrintExpr(op->b, os); - os << ')'; - } + CHECK(op->type.lanes() == 1) << "vec bin op not implemented"; + if (isalpha(opstr[0])) { + os << opstr << '('; + p->PrintExpr(op->a, os); + os << ", "; + p->PrintExpr(op->b, os); + os << ')'; } else { - LOG(FATAL) << "vec bin op to be implemented"; + os << '('; + p->PrintExpr(op->a, os); + os << ' ' << opstr << ' '; + p->PrintExpr(op->b, os); + os << ')'; } } @@ -86,16 +90,13 @@ inline void PrintBinaryIntrinsitc(const Call* op, const char *opstr, std::ostream& os, // NOLINT(*) CodeGenHybrid* p) { - if (op->type.lanes() == 1) { - CHECK_EQ(op->args.size(), 2U); - os << '('; - p->PrintExpr(op->args[0], os); - os << opstr; - p->PrintExpr(op->args[1], os); - os << ')'; - } else { - LOG(FATAL) << "vec bin intrin to be implemented"; - } + CHECK(op->type.lanes() == 1) << "vec bin intrin not implemented"; + CHECK_EQ(op->args.size(), 2U); + os << '('; + p->PrintExpr(op->args[0], os); + os << opstr; + p->PrintExpr(op->args[1], os); + os << ')'; } void CodeGenHybrid::VisitExpr_(const Cast *op, std::ostream& os) { // NOLINT(*) @@ -174,23 +175,21 @@ void CodeGenHybrid::VisitExpr_(const Call *op, std::ostream& os) { // NOLINT(*) os << idx.str(); } os << "]"; - } if (op->call_type == Call::Extern || - op->call_type == Call::PureExtern) { } else if (op->is_intrinsic(Call::bitwise_and)) { - PrintBinaryIntrinsitc(op, " & ", os, this); + PrintBinaryIntrinsitc(op, "&", os, this); } else if (op->is_intrinsic(Call::bitwise_xor)) { - PrintBinaryIntrinsitc(op, " ^ ", os, this); + PrintBinaryIntrinsitc(op, "^", os, this); } else if (op->is_intrinsic(Call::bitwise_or)) { - PrintBinaryIntrinsitc(op, " | ", os, this); + PrintBinaryIntrinsitc(op, "|", os, this); + } else if (op->is_intrinsic(Call::shift_left)) { + PrintBinaryIntrinsitc(op, "<<", os, this); + } else if (op->is_intrinsic(Call::shift_right)) { + PrintBinaryIntrinsitc(op, ">>", os, this); } else if (op->is_intrinsic(Call::bitwise_not)) { CHECK_EQ(op->args.size(), 1U); os << "(~"; PrintExpr(op->args[0], os); os << ')'; - } else if (op->is_intrinsic(Call::shift_left)) { - PrintBinaryIntrinsitc(op, " << ", os, this); - } else if (op->is_intrinsic(Call::shift_right)) { - PrintBinaryIntrinsitc(op, " >> ", os, this); } else if (op->is_intrinsic(intrinsic::tvm_if_then_else)) { PrintExpr(op->args[1], os); os << " if "; @@ -362,9 +361,13 @@ std::string CodeGenHybrid::GetTensorID(const FunctionRef &func, int value_index) } std::string name_hint = func->func_name(); if (func->num_outputs() != 0) { - name_hint += ".v" + std::to_string(value_index); + name_hint += "_v" + std::to_string(value_index); } return id_map_[node] = GetUniqueName(name_hint); } + +void CodeGenHybrid::DumpSchedule(const Schedule &sch) { + sch->outputs; +} } // namespace contrib } // namespace tvm diff --git a/src/contrib/hybrid/codegen_hybrid.h b/src/contrib/hybrid/codegen_hybrid.h index 9eaede94cb64..28874839c59c 100644 --- a/src/contrib/hybrid/codegen_hybrid.h +++ b/src/contrib/hybrid/codegen_hybrid.h @@ -107,28 +107,41 @@ class CodeGenHybrid : void VisitStmt_(const Block* op) override; void VisitStmt_(const ProducerConsumer* op) override; /*! - * Print Type represetnation of type t. + * \brief Print Type represetnation of type t. * \param t The type representation. * \param os The stream to print the ctype into */ virtual void PrintType(Type t, std::ostream& os); // NOLINT(*) - // Get a cast type from to virtual std::string CastFromTo(std::string value, Type from, Type target); private: - // + /*! \brief The current indent of the code dump. */ int indent_{0}; + /*! \brief The tab size of code indent. */ const int tab_{2}; + /*! \brief Print the current indent spaces. */ inline void PrintIndent(); - // + /*! \brief Keys are ids allocated, and values are the suffix to prevent double-name. */ std::map ids_allocated_; + /*! \brief Keys are either tensors or variables. Values are the corresponding IDs.*/ std::map id_map_; - // - std::string GetUniqueName(std::string s); - // + /*! + * \brief Find an unallocated name for the given prefix. + * \param prefix The given prefix. + */ + std::string GetUniqueName(std::string prefix); + /*! \brief The output code string builder. */ std::stringstream stream; - // + /*! + * \brief Get or allocate the ID for the given variable. + * \param v The given variable. + */ std::string GetVarID(const Variable *v); + /*! + * \brief Get or allocate the ID for the given tensor. + * \param func The tensor to allocate a name. + * \param value_index The value index of the given tensor. + */ std::string GetTensorID(const FunctionRef &func, int value_index); /*! \brief the storage scope of allocation */ std::map alloc_storage_scope_;