TargetID.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <optional>
  15. namespace clang {
  16. static llvm::SmallVector<llvm::StringRef, 4>
  17. getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T,
  18. llvm::StringRef Proc) {
  19. // Entries in returned vector should be in alphabetical order.
  20. llvm::SmallVector<llvm::StringRef, 4> Ret;
  21. auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)
  22. : llvm::AMDGPU::parseArchR600(Proc);
  23. if (ProcKind == llvm::AMDGPU::GK_NONE)
  24. return Ret;
  25. auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)
  26. : llvm::AMDGPU::getArchAttrR600(ProcKind);
  27. if (Features & llvm::AMDGPU::FEATURE_SRAMECC)
  28. Ret.push_back("sramecc");
  29. if (Features & llvm::AMDGPU::FEATURE_XNACK)
  30. Ret.push_back("xnack");
  31. return Ret;
  32. }
  33. llvm::SmallVector<llvm::StringRef, 4>
  34. getAllPossibleTargetIDFeatures(const llvm::Triple &T,
  35. llvm::StringRef Processor) {
  36. llvm::SmallVector<llvm::StringRef, 4> Ret;
  37. if (T.isAMDGPU())
  38. return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);
  39. return Ret;
  40. }
  41. /// Returns canonical processor name or empty string if \p Processor is invalid.
  42. static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,
  43. llvm::StringRef Processor) {
  44. if (T.isAMDGPU())
  45. return llvm::AMDGPU::getCanonicalArchName(T, Processor);
  46. return Processor;
  47. }
  48. llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
  49. llvm::StringRef TargetID) {
  50. auto Split = TargetID.split(':');
  51. return getCanonicalProcessorName(T, Split.first);
  52. }
  53. // Parse a target ID with format checking only. Do not check whether processor
  54. // name or features are valid for the processor.
  55. //
  56. // A target ID is a processor name followed by a list of target features
  57. // delimited by colon. Each target feature is a string post-fixed by a plus
  58. // or minus sign, e.g. gfx908:sramecc+:xnack-.
  59. static std::optional<llvm::StringRef>
  60. parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
  61. llvm::StringMap<bool> *FeatureMap) {
  62. llvm::StringRef Processor;
  63. if (TargetID.empty())
  64. return llvm::StringRef();
  65. auto Split = TargetID.split(':');
  66. Processor = Split.first;
  67. if (Processor.empty())
  68. return std::nullopt;
  69. auto Features = Split.second;
  70. if (Features.empty())
  71. return Processor;
  72. llvm::StringMap<bool> LocalFeatureMap;
  73. if (!FeatureMap)
  74. FeatureMap = &LocalFeatureMap;
  75. while (!Features.empty()) {
  76. auto Splits = Features.split(':');
  77. auto Sign = Splits.first.back();
  78. auto Feature = Splits.first.drop_back();
  79. if (Sign != '+' && Sign != '-')
  80. return std::nullopt;
  81. bool IsOn = Sign == '+';
  82. auto Loc = FeatureMap->find(Feature);
  83. // Each feature can only show up at most once in target ID.
  84. if (Loc != FeatureMap->end())
  85. return std::nullopt;
  86. (*FeatureMap)[Feature] = IsOn;
  87. Features = Splits.second;
  88. }
  89. return Processor;
  90. }
  91. std::optional<llvm::StringRef>
  92. parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
  93. llvm::StringMap<bool> *FeatureMap) {
  94. auto OptionalProcessor =
  95. parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
  96. if (!OptionalProcessor)
  97. return std::nullopt;
  98. llvm::StringRef Processor = getCanonicalProcessorName(T, *OptionalProcessor);
  99. if (Processor.empty())
  100. return std::nullopt;
  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 std::nullopt;
  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. std::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 = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
  134. auto Loc = FeatureMap.find(Proc);
  135. if (Loc == FeatureMap.end())
  136. FeatureMap[Proc] = Info{ID, Features};
  137. else {
  138. auto &ExistingFeatures = Loc->second.Features;
  139. if (llvm::any_of(Features, [&](auto &F) {
  140. return ExistingFeatures.count(F.first()) == 0;
  141. }))
  142. return std::make_pair(Loc->second.TargetID, ID);
  143. }
  144. }
  145. return std::nullopt;
  146. }
  147. bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested) {
  148. llvm::StringMap<bool> ProvidedFeatures, RequestedFeatures;
  149. llvm::StringRef ProvidedProc =
  150. *parseTargetIDWithFormatCheckingOnly(Provided, &ProvidedFeatures);
  151. llvm::StringRef RequestedProc =
  152. *parseTargetIDWithFormatCheckingOnly(Requested, &RequestedFeatures);
  153. if (ProvidedProc != RequestedProc)
  154. return false;
  155. for (const auto &F : ProvidedFeatures) {
  156. auto Loc = RequestedFeatures.find(F.first());
  157. // The default (unspecified) value of a feature is 'All', which can match
  158. // either 'On' or 'Off'.
  159. if (Loc == RequestedFeatures.end())
  160. return false;
  161. // If a feature is specified, it must have exact match.
  162. if (Loc->second != F.second)
  163. return false;
  164. }
  165. return true;
  166. }
  167. } // namespace clang