Coverage Report

Created: 2024-01-17 10:31

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