clang: lib/Basic/TargetID.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

10#include "llvm/ADT/STLExtras.h"

11#include "llvm/ADT/SmallSet.h"

12#include "llvm/Support/raw_ostream.h"

13#include "llvm/TargetParser/TargetParser.h"

14#include "llvm/TargetParser/Triple.h"

15#include

16#include

17

19

22 llvm::StringRef Proc) {

23

25 auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)

26 : llvm::AMDGPU::parseArchR600(Proc);

27 if (ProcKind == llvm::AMDGPU::GK_NONE)

28 return Ret;

29 auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)

30 : llvm::AMDGPU::getArchAttrR600(ProcKind);

31 if (Features & llvm::AMDGPU::FEATURE_SRAMECC)

32 Ret.push_back("sramecc");

33 if (Features & llvm::AMDGPU::FEATURE_XNACK)

34 Ret.push_back("xnack");

35 return Ret;

36}

37

40 llvm::StringRef Processor) {

42 if (T.isAMDGPU())

44 return Ret;

45}

46

47

49 llvm::StringRef Processor) {

50 if (T.isAMDGPU())

51 return llvm::AMDGPU::getCanonicalArchName(T, Processor);

52 return Processor;

53}

54

56 llvm::StringRef TargetID) {

57 auto Split = TargetID.split(':');

59}

60

61

62

63

64

65

66

67static std::optionalllvm::StringRef

69 llvm::StringMap *FeatureMap) {

70 llvm::StringRef Processor;

71

72 if (TargetID.empty())

73 return llvm::StringRef();

74

75 auto Split = TargetID.split(':');

76 Processor = Split.first;

77 if (Processor.empty())

78 return std::nullopt;

79

80 auto Features = Split.second;

81 if (Features.empty())

82 return Processor;

83

84 llvm::StringMap LocalFeatureMap;

85 if (!FeatureMap)

86 FeatureMap = &LocalFeatureMap;

87

88 while (!Features.empty()) {

89 auto Splits = Features.split(':');

90 auto Sign = Splits.first.back();

91 auto Feature = Splits.first.drop_back();

92 if (Sign != '+' && Sign != '-')

93 return std::nullopt;

94 bool IsOn = Sign == '+';

95

96 if (!FeatureMap->try_emplace(Feature, IsOn).second)

97 return std::nullopt;

98 Features = Splits.second;

99 }

100 return Processor;

101}

102

103std::optionalllvm::StringRef

105 llvm::StringMap *FeatureMap) {

106 auto OptionalProcessor =

108

109 if (!OptionalProcessor)

110 return std::nullopt;

111

113 if (Processor.empty())

114 return std::nullopt;

115

116 llvm::SmallSet<llvm::StringRef, 4> AllFeatures;

118 AllFeatures.insert(F);

119

120 for (auto &&F : *FeatureMap)

121 if (!AllFeatures.count(F.first()))

122 return std::nullopt;

123

124 return Processor;

125}

126

127

128

130 const llvm::StringMap &Features) {

131 std::string TargetID = Processor.str();

132 std::map<const llvm::StringRef, bool> OrderedMap;

133 for (const auto &F : Features)

134 OrderedMap[F.first()] = F.second;

135 for (const auto &F : OrderedMap)

136 TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");

137 return TargetID;

138}

139

140

141

142

143std::optional<std::pair<llvm::StringRef, llvm::StringRef>>

145 struct Info {

146 llvm::StringRef TargetID;

147 llvm::StringMap Features;

148 Info(llvm::StringRef TargetID, const llvm::StringMap &Features)

149 : TargetID(TargetID), Features(Features) {}

150 };

151 llvm::StringMap FeatureMap;

152 for (auto &&ID : TargetIDs) {

153 llvm::StringMap Features;

155 auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);

156 if (!Inserted) {

157 auto &ExistingFeatures = Loc->second.Features;

158 if (llvm::any_of(Features, [&](auto &F) {

159 return ExistingFeatures.count(F.first()) == 0;

160 }))

161 return std::make_pair(Loc->second.TargetID, ID);

162 }

163 }

164 return std::nullopt;

165}

166

168 llvm::StringMap ProvidedFeatures, RequestedFeatures;

169 llvm::StringRef ProvidedProc =

171 llvm::StringRef RequestedProc =

173 if (ProvidedProc != RequestedProc)

174 return false;

175 for (const auto &F : ProvidedFeatures) {

176 auto Loc = RequestedFeatures.find(F.first());

177

178

179 if (Loc == RequestedFeatures.end())

180 return false;

181

182 if (Loc->second != F.second)

183 return false;

184 }

185 return true;

186}

187

188}

The JSON file list parser is used to communicate input to InstallAPI.

static llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Proc)

std::optional< llvm::StringRef > parseTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch, llvm::StringMap< bool > *FeatureMap)

Parse a target ID to get processor and feature map.

static std::optional< llvm::StringRef > parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID, llvm::StringMap< bool > *FeatureMap)

llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)

Get processor name from target ID.

std::optional< std::pair< llvm::StringRef, llvm::StringRef > > getConflictTargetIDCombination(const std::set< llvm::StringRef > &TargetIDs)

Get the conflicted pair of target IDs for a compilation or a bundled code object, assuming TargetIDs ...

static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T, llvm::StringRef Processor)

Returns canonical processor name or empty string if Processor is invalid.

bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested)

Check whether the provided target ID is compatible with the requested target ID.

llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Processor)

Get all feature strings that can be used in target ID for Processor.

const FunctionProtoType * T

std::string getCanonicalTargetID(llvm::StringRef Processor, const llvm::StringMap< bool > &Features)

Returns canonical target ID, assuming Processor is canonical and all entries in Features are valid.