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.

macros.cpp 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * \file imperative/tablegen/targets/macros.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "./macros.h"
  12. #include "../emitter.h"
  13. #include "./cpp_class.h"
  14. namespace mlir::tblgen {
  15. bool gen_enum_param_list_macro(raw_ostream& os, llvm::RecordKeeper& keeper) {
  16. std::vector<std::pair<std::string, std::string>> enums;
  17. std::vector<std::pair<std::string, std::string>> bit_enums;
  18. Environment env;
  19. foreach_operator(keeper, [&](MgbOp& op) {
  20. for (auto&& i : op.getAttributes()) {
  21. if (auto attr = llvm::dyn_cast<MgbEnumAttr>(&i.attr)) {
  22. auto insert = [&](const MgbEnumAttr& attr) {
  23. auto&& item = std::make_pair(
  24. attr.getParentNamespace(), attr.getEnumName());
  25. if (env.enumAlias
  26. .emplace(attr.getBaseRecord()->getID(), std::move(item))
  27. .second) {
  28. if (attr.getEnumCombinedFlag()) {
  29. bit_enums.emplace_back(item);
  30. } else {
  31. enums.emplace_back(item);
  32. }
  33. }
  34. };
  35. if (auto alias = llvm::dyn_cast<MgbAliasAttr>(attr)) {
  36. auto&& aliasBase = alias->getAliasBase();
  37. insert(llvm::cast<MgbEnumAttr>(aliasBase));
  38. } else {
  39. insert(*attr);
  40. }
  41. }
  42. }
  43. });
  44. os << "#define FOR_EACH_ENUM_PARAM(cb)";
  45. for (auto&& i : enums) {
  46. os << formatv(" \\\n cb({0}::{1});", i.first, i.second);
  47. }
  48. os << "\n";
  49. os << "#define FOR_EACH_BIT_COMBINED_ENUM_PARAM(cb)";
  50. for (auto&& i : bit_enums) {
  51. os << formatv(" \\\n cb({0}::{1});", i.first, i.second);
  52. }
  53. os << "\n";
  54. return false;
  55. }
  56. } // namespace mlir::tblgen