197 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "clang/Basic/TargetID.h"10#include "llvm/ADT/STLExtras.h"11#include "llvm/ADT/SmallSet.h"12#include "llvm/ADT/SmallVector.h"13#include "llvm/Support/Path.h"14#include "llvm/TargetParser/TargetParser.h"15#include "llvm/TargetParser/Triple.h"16#include <map>17#include <optional>18#include <string>19 20namespace clang {21 22static llvm::SmallVector<llvm::StringRef, 4>23getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T,24 llvm::StringRef Proc) {25 // Entries in returned vector should be in alphabetical order.26 llvm::SmallVector<llvm::StringRef, 4> Ret;27 auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)28 : llvm::AMDGPU::parseArchR600(Proc);29 if (ProcKind == llvm::AMDGPU::GK_NONE)30 return Ret;31 auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)32 : llvm::AMDGPU::getArchAttrR600(ProcKind);33 if (Features & llvm::AMDGPU::FEATURE_SRAMECC)34 Ret.push_back("sramecc");35 if (Features & llvm::AMDGPU::FEATURE_XNACK)36 Ret.push_back("xnack");37 return Ret;38}39 40llvm::SmallVector<llvm::StringRef, 4>41getAllPossibleTargetIDFeatures(const llvm::Triple &T,42 llvm::StringRef Processor) {43 llvm::SmallVector<llvm::StringRef, 4> Ret;44 if (T.isAMDGPU())45 return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);46 return Ret;47}48 49/// Returns canonical processor name or empty string if \p Processor is invalid.50static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,51 llvm::StringRef Processor) {52 if (T.isAMDGPU())53 return llvm::AMDGPU::getCanonicalArchName(T, Processor);54 return Processor;55}56 57llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,58 llvm::StringRef TargetID) {59 auto Split = TargetID.split(':');60 return getCanonicalProcessorName(T, Split.first);61}62 63// Parse a target ID with format checking only. Do not check whether processor64// name or features are valid for the processor.65//66// A target ID is a processor name followed by a list of target features67// delimited by colon. Each target feature is a string post-fixed by a plus68// or minus sign, e.g. gfx908:sramecc+:xnack-.69static std::optional<llvm::StringRef>70parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,71 llvm::StringMap<bool> *FeatureMap) {72 llvm::StringRef Processor;73 74 if (TargetID.empty())75 return llvm::StringRef();76 77 auto Split = TargetID.split(':');78 Processor = Split.first;79 if (Processor.empty())80 return std::nullopt;81 82 auto Features = Split.second;83 if (Features.empty())84 return Processor;85 86 llvm::StringMap<bool> LocalFeatureMap;87 if (!FeatureMap)88 FeatureMap = &LocalFeatureMap;89 90 while (!Features.empty()) {91 auto Splits = Features.split(':');92 auto Sign = Splits.first.back();93 auto Feature = Splits.first.drop_back();94 if (Sign != '+' && Sign != '-')95 return std::nullopt;96 bool IsOn = Sign == '+';97 // Each feature can only show up at most once in target ID.98 if (!FeatureMap->try_emplace(Feature, IsOn).second)99 return std::nullopt;100 Features = Splits.second;101 }102 return Processor;103}104 105std::optional<llvm::StringRef>106parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,107 llvm::StringMap<bool> *FeatureMap) {108 auto OptionalProcessor =109 parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);110 111 if (!OptionalProcessor)112 return std::nullopt;113 114 llvm::StringRef Processor = getCanonicalProcessorName(T, *OptionalProcessor);115 if (Processor.empty())116 return std::nullopt;117 118 llvm::SmallSet<llvm::StringRef, 4> AllFeatures(119 llvm::from_range, getAllPossibleTargetIDFeatures(T, Processor));120 121 for (auto &&F : *FeatureMap)122 if (!AllFeatures.count(F.first()))123 return std::nullopt;124 125 return Processor;126}127 128// A canonical target ID is a target ID containing a canonical processor name129// and features in alphabetical order.130std::string getCanonicalTargetID(llvm::StringRef Processor,131 const llvm::StringMap<bool> &Features) {132 std::string TargetID = Processor.str();133 std::map<const llvm::StringRef, bool> OrderedMap;134 for (const auto &F : Features)135 OrderedMap[F.first()] = F.second;136 for (const auto &F : OrderedMap)137 TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");138 return TargetID;139}140 141// For a specific processor, a feature either shows up in all target IDs, or142// does not show up in any target IDs. Otherwise the target ID combination143// is invalid.144std::optional<std::pair<llvm::StringRef, llvm::StringRef>>145getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {146 struct Info {147 llvm::StringRef TargetID;148 llvm::StringMap<bool> Features;149 Info(llvm::StringRef TargetID, const llvm::StringMap<bool> &Features)150 : TargetID(TargetID), Features(Features) {}151 };152 llvm::StringMap<Info> FeatureMap;153 for (auto &&ID : TargetIDs) {154 llvm::StringMap<bool> Features;155 llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);156 auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);157 if (!Inserted) {158 auto &ExistingFeatures = Loc->second.Features;159 if (llvm::any_of(Features, [&](auto &F) {160 return ExistingFeatures.count(F.first()) == 0;161 }))162 return std::make_pair(Loc->second.TargetID, ID);163 }164 }165 return std::nullopt;166}167 168bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested) {169 llvm::StringMap<bool> ProvidedFeatures, RequestedFeatures;170 llvm::StringRef ProvidedProc =171 *parseTargetIDWithFormatCheckingOnly(Provided, &ProvidedFeatures);172 llvm::StringRef RequestedProc =173 *parseTargetIDWithFormatCheckingOnly(Requested, &RequestedFeatures);174 if (ProvidedProc != RequestedProc)175 return false;176 for (const auto &F : ProvidedFeatures) {177 auto Loc = RequestedFeatures.find(F.first());178 // The default (unspecified) value of a feature is 'All', which can match179 // either 'On' or 'Off'.180 if (Loc == RequestedFeatures.end())181 return false;182 // If a feature is specified, it must have exact match.183 if (Loc->second != F.second)184 return false;185 }186 return true;187}188 189std::string sanitizeTargetIDInFileName(llvm::StringRef TargetID) {190 std::string FileName = TargetID.str();191 if (llvm::sys::path::is_style_windows(llvm::sys::path::Style::native))192 llvm::replace(FileName, ':', '@');193 return FileName;194}195 196} // namespace clang197