TargetID.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //===--- TargetID.cpp - Utilities for parsing target ID -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Basic/TargetID.h"
  9. #include "llvm/ADT/SmallSet.h"
  10. #include "llvm/ADT/Triple.h"
  11. #include "llvm/Support/TargetParser.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <map>
  14. namespace clang {
  15. static llvm::SmallVector<llvm::StringRef, 4>
  16. getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T,
  17. llvm::StringRef Proc) {
  18. // Entries in returned vector should be in alphabetical order.
  19. llvm::SmallVector<llvm::StringRef, 4> Ret;
  20. auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)
  21. : llvm::AMDGPU::parseArchR600(Proc);
  22. if (ProcKind == llvm::AMDGPU::GK_NONE)
  23. return Ret;
  24. auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)
  25. : llvm::AMDGPU::getArchAttrR600(ProcKind);
  26. if (Features & llvm::AMDGPU::FEATURE_SRAMECC)
  27. Ret.push_back("sramecc");
  28. if (Features & llvm::AMDGPU::FEATURE_XNACK)
  29. Ret.push_back("xnack");
  30. return Ret;
  31. }
  32. llvm::SmallVector<llvm::StringRef, 4>
  33. getAllPossibleTargetIDFeatures(const llvm::Triple &T,
  34. llvm::StringRef Processor) {
  35. llvm::SmallVector<llvm::StringRef, 4> Ret;
  36. if (T.isAMDGPU())
  37. return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);
  38. return Ret;
  39. }
  40. /// Returns canonical processor name or empty string if \p Processor is invalid.
  41. static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,
  42. llvm::StringRef Processor) {
  43. if (T.isAMDGPU())
  44. return llvm::AMDGPU::getCanonicalArchName(T, Processor);
  45. return Processor;
  46. }
  47. llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
  48. llvm::StringRef TargetID) {
  49. auto Split = TargetID.split(':');
  50. return getCanonicalProcessorName(T, Split.first);
  51. }
  52. // Parse a target ID with format checking only. Do not check whether processor
  53. // name or features are valid for the processor.
  54. //
  55. // A target ID is a processor name followed by a list of target features
  56. // delimited by colon. Each target feature is a string post-fixed by a plus
  57. // or minus sign, e.g. gfx908:sramecc+:xnack-.
  58. static llvm::Optional<llvm::StringRef>
  59. parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
  60. llvm::StringMap<bool> *FeatureMap) {
  61. llvm::StringRef Processor;
  62. if (TargetID.empty())
  63. return llvm::StringRef();
  64. auto Split = TargetID.split(':');
  65. Processor = Split.first;
  66. if (Processor.empty())
  67. return llvm::None;
  68. auto Features = Split.second;
  69. if (Features.empty())
  70. return Processor;
  71. llvm::StringMap<bool> LocalFeatureMap;
  72. if (!FeatureMap)
  73. FeatureMap = &LocalFeatureMap;
  74. while (!Features.empty()) {
  75. auto Splits = Features.split(':');
  76. auto Sign = Splits.first.back();
  77. auto Feature = Splits.first.drop_back();
  78. if (Sign != '+' && Sign != '-')
  79. return llvm::None;
  80. bool IsOn = Sign == '+';
  81. auto Loc = FeatureMap->find(Feature);
  82. // Each feature can only show up at most once in target ID.
  83. if (Loc != FeatureMap->end())
  84. return llvm::None;
  85. (*FeatureMap)[Feature] = IsOn;
  86. Features = Splits.second;
  87. }
  88. return Processor;
  89. }
  90. llvm::Optional<llvm::StringRef>
  91. parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
  92. llvm::StringMap<bool> *FeatureMap) {
  93. auto OptionalProcessor =
  94. parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
  95. if (!OptionalProcessor)
  96. return llvm::None;
  97. llvm::StringRef Processor =
  98. getCanonicalProcessorName(T, OptionalProcessor.getValue());
  99. if (Processor.empty())
  100. return llvm::None;
  101. llvm::SmallSet<llvm::StringRef, 4> AllFeatures;
  102. for (auto &&F : getAllPossibleTargetIDFeatures(T, Processor))
  103. AllFeatures.insert(F);
  104. for (auto &&F : *FeatureMap)
  105. if (!AllFeatures.count(F.first()))
  106. return llvm::None;
  107. return Processor;
  108. }
  109. // A canonical target ID is a target ID containing a canonical processor name
  110. // and features in alphabetical order.
  111. std::string getCanonicalTargetID(llvm::StringRef Processor,
  112. const llvm::StringMap<bool> &Features) {
  113. std::string TargetID = Processor.str();
  114. std::map<const llvm::StringRef, bool> OrderedMap;
  115. for (const auto &F : Features)
  116. OrderedMap[F.first()] = F.second;
  117. for (auto F : OrderedMap)
  118. TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");
  119. return TargetID;
  120. }
  121. // For a specific processor, a feature either shows up in all target IDs, or
  122. // does not show up in any target IDs. Otherwise the target ID combination
  123. // is invalid.
  124. llvm::Optional<std::pair<llvm::StringRef, llvm::StringRef>>
  125. getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
  126. struct Info {
  127. llvm::StringRef TargetID;
  128. llvm::StringMap<bool> Features;
  129. };
  130. llvm::StringMap<Info> FeatureMap;
  131. for (auto &&ID : TargetIDs) {
  132. llvm::StringMap<bool> Features;
  133. llvm::StringRef Proc =
  134. parseTargetIDWithFormatCheckingOnly(ID, &Features).getValue();
  135. auto Loc = FeatureMap.find(Proc);
  136. if (Loc == FeatureMap.end())
  137. FeatureMap[Proc] = Info{ID, Features};
  138. else {
  139. auto &ExistingFeatures = Loc->second.Features;
  140. if (llvm::any_of(Features, [&](auto &F) {
  141. return ExistingFeatures.count(F.first()) == 0;
  142. }))
  143. return std::make_pair(Loc->second.TargetID, ID);
  144. }
  145. }
  146. return llvm::None;
  147. }
  148. } // namespace clang