You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper.h 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <string>
  2. #include <vector>
  3. #include "llvm/Support/CommandLine.h"
  4. #include "llvm/Support/FormatVariadic.h"
  5. #include "llvm/Support/InitLLVM.h"
  6. #include "llvm/Support/Signals.h"
  7. #include "llvm/TableGen/Main.h"
  8. #include "llvm/TableGen/Record.h"
  9. #include "llvm/TableGen/TableGenBackend.h"
  10. #include "mlir/TableGen/Attribute.h"
  11. #include "mlir/TableGen/Format.h"
  12. #include "mlir/TableGen/Operator.h"
  13. using llvm::formatv;
  14. using llvm::StringRef;
  15. using llvm::Record;
  16. #define ASSERT(stmt, msg) \
  17. if (!(stmt)) { \
  18. std::cerr << "\033[1;31m" \
  19. << "tablegen autogen abort due to: " << msg \
  20. << "\033[0m" << std::endl; \
  21. exit(1); \
  22. }
  23. namespace mlir {
  24. namespace tblgen {
  25. template<typename ConcreteType>
  26. struct MgbInterface : public ConcreteType {
  27. MgbInterface() = delete;
  28. MgbInterface(const MgbInterface&) = delete;
  29. MgbInterface(MgbInterface&&) = delete;
  30. ~MgbInterface() = delete;
  31. };
  32. struct MgbAttrWrapperBase : public MgbInterface<Attribute> {
  33. private:
  34. struct RecordVisitor : public MgbInterface<Constraint> {
  35. public:
  36. static bool classof(const Constraint*) {
  37. return true;
  38. }
  39. const llvm::Record* getDef() const {
  40. return def;
  41. }
  42. };
  43. public:
  44. static bool classof(const Attribute* attr) {
  45. return attr->isSubClassOf("MgbAttrWrapperBase");
  46. }
  47. const llvm::Record* getBaseRecord() const {
  48. auto baseAttr = getBaseAttr();
  49. return llvm::cast<RecordVisitor>(baseAttr).getDef();
  50. }
  51. llvm::StringRef getUnderlyingType() const {
  52. return def->getValueAsString("underlyingType");
  53. }
  54. };
  55. struct MgbEnumAttrMixin : public MgbAttrWrapperBase {
  56. static bool classof(const Attribute* attr) {
  57. return attr->getBaseAttr().isSubClassOf("MgbEnumAttrMixin");
  58. }
  59. llvm::StringRef getParentNamespace() const {
  60. return getBaseRecord()->getValueAsString("parentNamespace");
  61. }
  62. llvm::StringRef getEnumName() const {
  63. return getBaseRecord()->getValueAsString("enumName");
  64. }
  65. std::vector<StringRef> getEnumMembers() const {
  66. return getBaseRecord()->getValueAsListOfStrings("enumMembers");
  67. }
  68. bool supportToString() const {
  69. return getBaseRecord()->getValueAsBit("supportToString");
  70. }
  71. };
  72. struct MgbHashableAttrMixin : public MgbAttrWrapperBase {
  73. static bool classof(const Attribute* attr) {
  74. return attr->getBaseAttr().isSubClassOf("MgbHashableAttrMixin");
  75. }
  76. llvm::StringRef getHashFunctionTemplate() const {
  77. return getBaseRecord()->getValueAsString("hashFunction");
  78. }
  79. llvm::StringRef getCmpFunctionTemplate() const {
  80. return getBaseRecord()->getValueAsString("cmpFunction");
  81. }
  82. llvm::StringRef getReprFunctionTemplate() const {
  83. return getBaseRecord()->getValueAsString("reprFunction");
  84. }
  85. };
  86. struct MgbAliasAttrMixin : public MgbAttrWrapperBase {
  87. static bool classof(const Attribute* attr) {
  88. return attr->getBaseAttr().isSubClassOf("MgbAliasAttrMixin");
  89. }
  90. Attribute getAliasBase() const {
  91. return Attribute(getBaseRecord()->getValueAsDef("aliasBase"));
  92. }
  93. };
  94. class MgbPackedParam {
  95. public:
  96. MgbPackedParam(Record* def_): def(def_) {
  97. auto&& dag = def->getValueAsDag("fields");
  98. for (size_t i = 0; i < dag->getNumArgs(); ++ i) {
  99. fields.push_back({
  100. dag->getArgNameStr(i),
  101. Attribute(llvm::cast<llvm::DefInit>(dag->getArg(i)))
  102. });
  103. }
  104. }
  105. llvm::StringRef getFullName() const {
  106. return def->getValueAsString("fullName");
  107. }
  108. std::vector<NamedAttribute> getFields() const {
  109. return fields;
  110. }
  111. llvm::StringRef getAccessor() const {
  112. return def->getValueAsString("paramAccessor");
  113. }
  114. private:
  115. std::vector<NamedAttribute> fields;
  116. Record* def;
  117. };
  118. struct MgbOpBase : public MgbInterface<Operator> {
  119. static bool isPackedParam(Record* def) {
  120. return def->isSubClassOf("MgbPackedParamBase");
  121. }
  122. public:
  123. static bool classof(const Operator* op) {
  124. return op->getDef().isSubClassOf("MgbOp");
  125. }
  126. std::vector<NamedAttribute> getMgbAttributes() const {
  127. std::vector<NamedAttribute> ret;
  128. for (auto&& i: getAttributes()) {
  129. if (isa<MgbAttrWrapperBase>(i.attr)) {
  130. ret.push_back(i);
  131. }
  132. }
  133. return ret;
  134. }
  135. std::vector<NamedAttribute> getExtraArguments() const {
  136. std::vector<NamedAttribute> ret;
  137. auto&& dag = getDef().getValueAsDag("extraArguments");
  138. for (size_t i = 0; i < dag->getNumArgs(); ++ i) {
  139. ret.push_back({
  140. dag->getArgNameStr(i),
  141. Attribute(llvm::cast<llvm::DefInit>(dag->getArg(i)))
  142. });
  143. }
  144. return ret;
  145. }
  146. llvm::Optional<StringRef> getExtraOpdefDecl() const {
  147. return getDef().getValueAsOptionalString("extraOpdefDecl");
  148. }
  149. std::vector<MgbPackedParam> getPackedParams() const {
  150. std::vector<MgbPackedParam> ret;
  151. for (auto&& i : getDef().getValueAsListOfDefs("dnnParams")) {
  152. if (isPackedParam(i)) {
  153. ret.emplace_back(i);
  154. }
  155. }
  156. return ret;
  157. }
  158. std::string getNameFunctionTemplate() const {
  159. if (auto f = getDef().getValueAsOptionalString("nameFunction")) {
  160. return f.getValue().str();
  161. }
  162. return formatv(" return \"{0}\";\n", getCppClassName());
  163. }
  164. };
  165. struct MgbHashableOpMixin : public MgbOpBase {
  166. private:
  167. std::string getDefaultHashFunction() const {
  168. std::string body = " size_t val = mgb::hash($_self.dyn_typeinfo());\n";
  169. if (!getMgbAttributes().empty()) {
  170. auto getHashFunc = [&](auto&& iter) {
  171. auto&& attr = llvm::cast<MgbHashableAttrMixin>(iter.attr);
  172. return attr.getHashFunctionTemplate();
  173. };
  174. mlir::tblgen::FmtContext ctx;
  175. for (auto&& it: getMgbAttributes()) {
  176. body += formatv(
  177. " val = mgb::hash_pair_combine(val, {0});\n",
  178. mlir::tblgen::tgfmt(getHashFunc(it), &ctx, "$_self." + it.name)
  179. );
  180. }
  181. }
  182. body += " return val;\n";
  183. return body;
  184. }
  185. std::string getDefaultCmpFunction() const {
  186. std::string body;
  187. if (!getMgbAttributes().empty()) {
  188. mlir::tblgen::FmtContext ctx;
  189. for (auto&& it : getMgbAttributes()) {
  190. auto&& attr = llvm::cast<MgbHashableAttrMixin>(it.attr);
  191. body += formatv(
  192. " if ({0}) return false;\n",
  193. mlir::tblgen::tgfmt(attr.getCmpFunctionTemplate(),
  194. &ctx, "$0." + it.name, "$1." + it.name)
  195. );
  196. }
  197. }
  198. body += " return true;\n";
  199. return body;
  200. }
  201. std::string getDefaultPropsFunction() const {
  202. std::string body = " std::vector<std::pair<const char*, std::string>> props_;\n";
  203. if (!getMgbAttributes().empty()) {
  204. mlir::tblgen::FmtContext ctx;
  205. for (auto&& it : getMgbAttributes()) {
  206. if (auto* enumAttr = llvm::dyn_cast<MgbEnumAttrMixin>(&it.attr)) {
  207. body += formatv(" switch ({0}){{\n", "$_self." + it.name);
  208. for (auto&& enumMember: enumAttr->getEnumMembers()) {
  209. body += formatv(
  210. " case {0}::{1}::{2}:\n",
  211. getCppClassName(), enumAttr->getEnumName(), enumMember
  212. );
  213. body += formatv(
  214. " props_.emplace_back(\"{0}\", \"{1}\");\n",
  215. it.name, enumMember
  216. );
  217. body += " break;\n";
  218. }
  219. body += " default: break;\n";
  220. body += " }\n";
  221. } else {
  222. auto&& attr = llvm::cast<MgbHashableAttrMixin>(it.attr);
  223. body += formatv(
  224. " props_.emplace_back(\"{0}\", {1});\n", it.name,
  225. mlir::tblgen::tgfmt(attr.getReprFunctionTemplate(),
  226. &ctx, "$_self." + it.name)
  227. );
  228. }
  229. }
  230. }
  231. body += " return props_;\n";
  232. return body;
  233. }
  234. public:
  235. static bool classof(const Operator* op) {
  236. return op->getDef().isSubClassOf("MgbHashableOpMixin");
  237. }
  238. std::string getHashFunctionTemplate() const {
  239. if (auto f = getDef().getValueAsOptionalString("hashFunction")) {
  240. return f.getValue().str();
  241. }
  242. return getDefaultHashFunction();
  243. }
  244. std::string getCmpFunctionTemplate() const {
  245. if (auto f = getDef().getValueAsOptionalString("cmpFunction")) {
  246. return f.getValue().str();
  247. }
  248. return getDefaultCmpFunction();
  249. }
  250. std::string getPropsFunctionTemplate() const {
  251. if (auto f = getDef().getValueAsOptionalString("propsFunction")) {
  252. return f.getValue().str();
  253. }
  254. return getDefaultPropsFunction();
  255. }
  256. };
  257. } // namespace tblgen
  258. } // namespace mlir

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台