Skip to content

Commit 5ee3ae4

Browse files
committed
address comment
1 parent 569b5ad commit 5ee3ae4

File tree

6 files changed

+70
-49
lines changed

6 files changed

+70
-49
lines changed

llvm/include/llvm/MC/MCSubtargetInfo.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,7 @@ class MCInst;
3434

3535
/// Used to provide key value pairs for feature and CPU bit flags.
3636
struct BasicSubtargetFeatureKV {
37-
const char *Key; ///< K-V key string
38-
unsigned Value; ///< K-V integer value
39-
FeatureBitArray Implies; ///< K-V bit mask
40-
};
41-
42-
/// Used to provide key value pairs for feature and CPU bit flags.
43-
struct SubtargetFeatureKV {
4437
const char *Key; ///< K-V key string
45-
const char *Desc; ///< Help descriptor
4638
unsigned Value; ///< K-V integer value
4739
FeatureBitArray Implies; ///< K-V bit mask
4840

@@ -52,9 +44,19 @@ struct SubtargetFeatureKV {
5244
}
5345

5446
/// Compare routine for std::is_sorted.
55-
bool operator<(const SubtargetFeatureKV &Other) const {
47+
bool operator<(const BasicSubtargetFeatureKV &Other) const {
5648
return StringRef(Key) < StringRef(Other.Key);
5749
}
50+
constexpr BasicSubtargetFeatureKV(const char *Key, unsigned Value,
51+
FeatureBitArray Implies)
52+
: Key(Key), Value(Value), Implies(Implies) {}
53+
};
54+
55+
struct SubtargetFeatureKV : BasicSubtargetFeatureKV {
56+
const char *Desc; ///< Help descriptor
57+
SubtargetFeatureKV(const char *Key, const char *Desc, unsigned Value,
58+
FeatureBitArray Implies)
59+
: BasicSubtargetFeatureKV(Key, Value, Implies), Desc(Desc) {}
5860
};
5961

6062
//===----------------------------------------------------------------------===//
@@ -68,26 +70,17 @@ struct BasicSubtargetSubTypeKV {
6870
bool operator<(StringRef S) const {
6971
return StringRef(Key) < S;
7072
}
71-
};
7273

73-
struct SubtargetSubTypeKV {
74-
const char *Key; ///< K-V key string
75-
FeatureBitArray Implies; ///< K-V bit mask
76-
FeatureBitArray TuneImplies; ///< K-V bit mask
77-
const MCSchedModel *SchedModel;
78-
79-
/// Compare routine for std::lower_bound
80-
bool operator<(StringRef S) const { return StringRef(Key) < S; }
8174
/// Compare routine for std::is_sorted.
82-
bool operator<(const SubtargetSubTypeKV &Other) const {
75+
bool operator<(const BasicSubtargetSubTypeKV &Other) const {
8376
return StringRef(Key) < StringRef(Other.Key);
8477
}
8578
};
8679

87-
std::optional<llvm::StringMap<bool>>
88-
getCPUDefaultTargetFeatures(StringRef CPU,
89-
ArrayRef<BasicSubtargetSubTypeKV> ProcDesc,
90-
ArrayRef<BasicSubtargetFeatureKV> ProcFeatures);
80+
struct SubtargetSubTypeKV : BasicSubtargetSubTypeKV {
81+
FeatureBitArray TuneImplies; ///< K-V bit mask
82+
const MCSchedModel *SchedModel;
83+
};
9184

9285
//===----------------------------------------------------------------------===//
9386
///

llvm/include/llvm/TargetParser/PPCTargetParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#ifndef LLVM_TARGETPARSER_PPCTARGETPARSER_H
1515
#define LLVM_TARGETPARSER_PPCTARGETPARSER_H
1616

17+
#include "TargetParser.h"
1718
#include "llvm/ADT/StringMap.h"
1819
#include "llvm/ADT/StringRef.h"
19-
#include "llvm/MC/MCSubtargetInfo.h"
2020
#include "llvm/TargetParser/Triple.h"
2121

2222
namespace llvm {

llvm/include/llvm/TargetParser/TargetParser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/StringMap.h"
1818
#include "llvm/ADT/StringRef.h"
19+
#include "llvm/MC/MCSubtargetInfo.h"
1920

2021
namespace llvm {
2122

@@ -189,6 +190,11 @@ insertWaveSizeFeature(StringRef GPU, const Triple &T,
189190
StringMap<bool> &Features);
190191

191192
} // namespace AMDGPU
193+
194+
std::optional<llvm::StringMap<bool>>
195+
getCPUDefaultTargetFeatures(StringRef CPU,
196+
ArrayRef<BasicSubtargetSubTypeKV> ProcDesc,
197+
ArrayRef<BasicSubtargetFeatureKV> ProcFeatures);
192198
} // namespace llvm
193199

194200
#endif

llvm/lib/MC/MCSubtargetInfo.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -226,30 +226,6 @@ static FeatureBitset getFeatures(MCSubtargetInfo &STI, StringRef CPU,
226226
return Bits;
227227
}
228228

229-
std::optional<llvm::StringMap<bool>> llvm::getCPUDefaultTargetFeatures(
230-
StringRef CPU, ArrayRef<BasicSubtargetSubTypeKV> ProcDesc,
231-
ArrayRef<BasicSubtargetFeatureKV> ProcFeatures) {
232-
if (CPU.empty())
233-
return std::nullopt;
234-
235-
const BasicSubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc);
236-
if (!CPUEntry)
237-
return std::nullopt;
238-
239-
// Set the features implied by this CPU feature if there is a match.
240-
FeatureBitset Bits;
241-
llvm::StringMap<bool> DefaultFeatures;
242-
SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), ProcFeatures);
243-
244-
unsigned BitSize = Bits.size();
245-
for (const BasicSubtargetFeatureKV &FE : ProcFeatures) {
246-
assert(FE.Value < BitSize && "Target Feature is out of range");
247-
if (Bits[FE.Value])
248-
DefaultFeatures[FE.Key] = true;
249-
}
250-
return DefaultFeatures;
251-
}
252-
253229
void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,
254230
StringRef FS) {
255231
FeatureBits =

llvm/lib/TargetParser/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ add_llvm_component_library(LLVMTargetParser
4343

4444
LINK_COMPONENTS
4545
Support
46-
MC
4746

4847
DEPENDS
4948
ARMTargetParserTableGen

llvm/lib/TargetParser/TargetParser.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,53 @@
1818
using namespace llvm;
1919
using namespace AMDGPU;
2020

21+
/// Find KV in array using binary search.
22+
static const BasicSubtargetSubTypeKV *
23+
Find(StringRef S, ArrayRef<BasicSubtargetSubTypeKV> A) {
24+
// Binary search the array
25+
auto F = llvm::lower_bound(A, S);
26+
// If not found then return NULL
27+
if (F == A.end() || StringRef(F->Key) != S)
28+
return nullptr;
29+
// Return the found array item
30+
return F;
31+
}
32+
33+
/// For each feature that is (transitively) implied by this feature, set it.
34+
static void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
35+
ArrayRef<BasicSubtargetFeatureKV> FeatureTable) {
36+
// OR the Implies bits in outside the loop. This allows the Implies for CPUs
37+
// which might imply features not in FeatureTable to use this.
38+
Bits |= Implies;
39+
for (const auto &FE : FeatureTable)
40+
if (Implies.test(FE.Value))
41+
SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable);
42+
}
43+
44+
std::optional<llvm::StringMap<bool>> llvm::getCPUDefaultTargetFeatures(
45+
StringRef CPU, ArrayRef<BasicSubtargetSubTypeKV> ProcDesc,
46+
ArrayRef<BasicSubtargetFeatureKV> ProcFeatures) {
47+
if (CPU.empty())
48+
return std::nullopt;
49+
50+
const BasicSubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc);
51+
if (!CPUEntry)
52+
return std::nullopt;
53+
54+
// Set the features implied by this CPU feature if there is a match.
55+
FeatureBitset Bits;
56+
llvm::StringMap<bool> DefaultFeatures;
57+
SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), ProcFeatures);
58+
59+
unsigned BitSize = Bits.size();
60+
for (const BasicSubtargetFeatureKV &FE : ProcFeatures) {
61+
assert(FE.Value < BitSize && "Target Feature is out of range");
62+
if (Bits[FE.Value])
63+
DefaultFeatures[FE.Key] = true;
64+
}
65+
return DefaultFeatures;
66+
}
67+
2168
namespace {
2269

2370
struct GPUInfo {

0 commit comments

Comments
 (0)