Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/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/ADT/Triple.h"
12
#include "llvm/Support/TargetParser.h"
13
#include "llvm/Support/raw_ostream.h"
14
#include <map>
15
16
namespace clang {
17
18
static const llvm::SmallVector<llvm::StringRef, 4>
19
getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T,
20
2.89k
                                     llvm::StringRef Proc) {
21
  // Entries in returned vector should be in alphabetical order.
22
2.89k
  llvm::SmallVector<llvm::StringRef, 4> Ret;
23
2.89k
  auto ProcKind = T.isAMDGCN() ? 
llvm::AMDGPU::parseArchAMDGCN(Proc)2.71k
24
2.89k
                               : 
llvm::AMDGPU::parseArchR600(Proc)176
;
25
2.89k
  if (ProcKind == llvm::AMDGPU::GK_NONE)
26
70
    return Ret;
27
2.82k
  auto Features = T.isAMDGCN() ? 
llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)2.71k
28
2.82k
                               : 
llvm::AMDGPU::getArchAttrR600(ProcKind)106
;
29
2.82k
  if (Features & llvm::AMDGPU::FEATURE_SRAMECC)
30
604
    Ret.push_back("sramecc");
31
2.82k
  if (Features & llvm::AMDGPU::FEATURE_XNACK)
32
1.58k
    Ret.push_back("xnack");
33
2.82k
  return Ret;
34
2.89k
}
35
36
const llvm::SmallVector<llvm::StringRef, 4>
37
getAllPossibleTargetIDFeatures(const llvm::Triple &T,
38
2.89k
                               llvm::StringRef Processor) {
39
2.89k
  llvm::SmallVector<llvm::StringRef, 4> Ret;
40
2.89k
  if (T.isAMDGPU())
41
2.89k
    return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);
42
0
  return Ret;
43
2.89k
}
44
45
/// Returns canonical processor name or empty string if \p Processor is invalid.
46
static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,
47
2.74k
                                                 llvm::StringRef Processor) {
48
2.74k
  if (T.isAMDGPU())
49
2.74k
    return llvm::AMDGPU::getCanonicalArchName(T, Processor);
50
0
  return Processor;
51
2.74k
}
52
53
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
54
1.27k
                                         llvm::StringRef TargetID) {
55
1.27k
  auto Split = TargetID.split(':');
56
1.27k
  return getCanonicalProcessorName(T, Split.first);
57
1.27k
}
58
59
// Parse a target ID with format checking only. Do not check whether processor
60
// name or features are valid for the processor.
61
//
62
// A target ID is a processor name followed by a list of target features
63
// delimited by colon. Each target feature is a string post-fixed by a plus
64
// or minus sign, e.g. gfx908:sramecc+:xnack-.
65
static llvm::Optional<llvm::StringRef>
66
parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
67
1.81k
                                    llvm::StringMap<bool> *FeatureMap) {
68
1.81k
  llvm::StringRef Processor;
69
70
1.81k
  if (TargetID.empty())
71
0
    return llvm::StringRef();
72
73
1.81k
  auto Split = TargetID.split(':');
74
1.81k
  Processor = Split.first;
75
1.81k
  if (Processor.empty())
76
0
    return llvm::None;
77
78
1.81k
  auto Features = Split.second;
79
1.81k
  if (Features.empty())
80
1.63k
    return Processor;
81
82
175
  llvm::StringMap<bool> LocalFeatureMap;
83
175
  if (!FeatureMap)
84
0
    FeatureMap = &LocalFeatureMap;
85
86
416
  while (!Features.empty()) {
87
251
    auto Splits = Features.split(':');
88
251
    auto Sign = Splits.first.back();
89
251
    auto Feature = Splits.first.drop_back();
90
251
    if (Sign != '+' && 
Sign != '-'87
)
91
5
      return llvm::None;
92
246
    bool IsOn = Sign == '+';
93
246
    auto Loc = FeatureMap->find(Feature);
94
    // Each feature can only show up at most once in target ID.
95
246
    if (Loc != FeatureMap->end())
96
5
      return llvm::None;
97
241
    (*FeatureMap)[Feature] = IsOn;
98
241
    Features = Splits.second;
99
241
  }
100
165
  return Processor;
101
175
}
102
103
llvm::Optional<llvm::StringRef>
104
parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
105
1.48k
              llvm::StringMap<bool> *FeatureMap) {
106
1.48k
  auto OptionalProcessor =
107
1.48k
      parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
108
109
1.48k
  if (!OptionalProcessor)
110
10
    return llvm::None;
111
112
1.47k
  llvm::StringRef Processor =
113
1.47k
      getCanonicalProcessorName(T, OptionalProcessor.getValue());
114
1.47k
  if (Processor.empty())
115
15
    return llvm::None;
116
117
1.46k
  llvm::SmallSet<llvm::StringRef, 4> AllFeatures;
118
1.46k
  for (auto &&F : getAllPossibleTargetIDFeatures(T, Processor))
119
1.21k
    AllFeatures.insert(F);
120
121
1.46k
  for (auto &&F : *FeatureMap)
122
197
    if (!AllFeatures.count(F.first()))
123
15
      return llvm::None;
124
125
1.44k
  return Processor;
126
1.46k
}
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
523
                                 const llvm::StringMap<bool> &Features) {
132
523
  std::string TargetID = Processor.str();
133
523
  std::map<const llvm::StringRef, bool> OrderedMap;
134
523
  for (const auto &F : Features)
135
42
    OrderedMap[F.first()] = F.second;
136
523
  for (auto F : OrderedMap)
137
42
    TargetID = TargetID + ':' + F.first.str() + (F.second ? 
"+"26
:
"-"16
);
138
523
  return TargetID;
139
523
}
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
llvm::Optional<std::pair<llvm::StringRef, llvm::StringRef>>
145
258
getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
146
258
  struct Info {
147
258
    llvm::StringRef TargetID;
148
258
    llvm::StringMap<bool> Features;
149
258
  };
150
258
  llvm::StringMap<Info> FeatureMap;
151
323
  for (auto &&ID : TargetIDs) {
152
323
    llvm::StringMap<bool> Features;
153
323
    llvm::StringRef Proc =
154
323
        parseTargetIDWithFormatCheckingOnly(ID, &Features).getValue();
155
323
    auto Loc = FeatureMap.find(Proc);
156
323
    if (Loc == FeatureMap.end())
157
317
      FeatureMap[Proc] = Info{ID, Features};
158
6
    else {
159
6
      auto &ExistingFeatures = Loc->second.Features;
160
9
      if (
llvm::any_of(Features, [&](auto &F) 6
{
161
9
            return ExistingFeatures.count(F.first()) == 0;
162
9
          }))
163
1
        return std::make_pair(Loc->second.TargetID, ID);
164
6
    }
165
323
  }
166
257
  return llvm::None;
167
258
}
168
169
} // namespace clang