Skip to content

Commit 03d0449

Browse files
[ADT] Use a dedicated empty type for StringSet (NFC) (#165967)
This patch introduces StringSetTag, a dedicated empty struct to serve as the "value type" for llvm::StringSet. This change is part of an effort to reduce the use of std::nullopt_t outside the context of std::optional.
1 parent 421ba7f commit 03d0449

File tree

8 files changed

+16
-13
lines changed

8 files changed

+16
-13
lines changed

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ DataAggregator::writeAggregatedFile(StringRef OutputFilename) const {
22152215
OutFile << "boltedcollection\n";
22162216
if (opts::BasicAggregation) {
22172217
OutFile << "no_lbr";
2218-
for (const StringMapEntry<std::nullopt_t> &Entry : EventNames)
2218+
for (const StringMapEntry<EmptyStringSetTag> &Entry : EventNames)
22192219
OutFile << " " << Entry.getKey();
22202220
OutFile << "\n";
22212221

@@ -2291,7 +2291,7 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
22912291

22922292
ListSeparator LS(",");
22932293
raw_string_ostream EventNamesOS(BP.Header.EventNames);
2294-
for (const StringMapEntry<std::nullopt_t> &EventEntry : EventNames)
2294+
for (const StringMapEntry<EmptyStringSetTag> &EventEntry : EventNames)
22952295
EventNamesOS << LS << EventEntry.first().str();
22962296

22972297
BP.Header.Flags = opts::BasicAggregation ? BinaryFunction::PF_BASIC

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ std::error_code YAMLProfileWriter::writeProfile(const RewriteInstance &RI) {
382382
StringSet<> EventNames = RI.getProfileReader()->getEventNames();
383383
if (!EventNames.empty()) {
384384
std::string Sep;
385-
for (const StringMapEntry<std::nullopt_t> &EventEntry : EventNames) {
385+
for (const StringMapEntry<EmptyStringSetTag> &EventEntry : EventNames) {
386386
BP.Header.EventNames += Sep + EventEntry.first().str();
387387
Sep = ",";
388388
}

llvm/include/llvm/ADT/StringMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
302302
if (FindInRHS == RHS.end())
303303
return false;
304304

305-
if constexpr (!std::is_same_v<ValueTy, std::nullopt_t>) {
305+
if constexpr (!std::is_same_v<ValueTy, EmptyStringSetTag>) {
306306
if (!(KeyValue.getValue() == FindInRHS->getValue()))
307307
return false;
308308
}

llvm/include/llvm/ADT/StringMapEntry.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
namespace llvm {
2323

24+
/// The "value type" of StringSet represented as an empty struct.
25+
struct EmptyStringSetTag {};
26+
2427
/// StringMapEntryBase - Shared base class of StringMapEntry instances.
2528
class StringMapEntryBase {
2629
size_t keyLength;
@@ -85,14 +88,13 @@ class StringMapEntryStorage : public StringMapEntryBase {
8588
};
8689

8790
template <>
88-
class StringMapEntryStorage<std::nullopt_t> : public StringMapEntryBase {
91+
class StringMapEntryStorage<EmptyStringSetTag> : public StringMapEntryBase {
8992
public:
90-
explicit StringMapEntryStorage(size_t keyLength,
91-
std::nullopt_t = std::nullopt)
93+
explicit StringMapEntryStorage(size_t keyLength, EmptyStringSetTag = {})
9294
: StringMapEntryBase(keyLength) {}
9395
StringMapEntryStorage(StringMapEntryStorage &entry) = delete;
9496

95-
std::nullopt_t getValue() const { return std::nullopt; }
97+
EmptyStringSetTag getValue() const { return {}; }
9698
};
9799

98100
/// StringMapEntry - This is used to represent one value that is inserted into

llvm/include/llvm/ADT/StringSet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace llvm {
2222

2323
/// StringSet - A wrapper for StringMap that provides set-like functionality.
2424
template <class AllocatorTy = MallocAllocator>
25-
class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
26-
using Base = StringMap<std::nullopt_t, AllocatorTy>;
25+
class StringSet : public StringMap<EmptyStringSetTag, AllocatorTy> {
26+
using Base = StringMap<EmptyStringSetTag, AllocatorTy>;
2727

2828
public:
2929
StringSet() = default;

llvm/include/llvm/DWARFLinker/StringPool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace dwarf_linker {
2020

2121
/// StringEntry keeps data of the string: the length, external offset
2222
/// and a string body which is placed right after StringEntry.
23-
using StringEntry = StringMapEntry<std::nullopt_t>;
23+
using StringEntry = StringMapEntry<EmptyStringSetTag>;
2424

2525
class StringPoolEntryInfo {
2626
public:

mlir/include/mlir/Support/Timing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DefaultTimingManagerImpl;
4444
/// This is a POD type with pointer size, so it should be passed around by
4545
/// value. The underlying data is owned by the `TimingManager`.
4646
class TimingIdentifier {
47-
using EntryType = llvm::StringMapEntry<std::nullopt_t>;
47+
using EntryType = llvm::StringMapEntry<llvm::EmptyStringSetTag>;
4848

4949
public:
5050
TimingIdentifier(const TimingIdentifier &) = default;

mlir/lib/Support/Timing.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class TimingManagerImpl {
5050
llvm::sys::SmartRWMutex<true> identifierMutex;
5151

5252
/// A thread local cache of identifiers to reduce lock contention.
53-
ThreadLocalCache<llvm::StringMap<llvm::StringMapEntry<std::nullopt_t> *>>
53+
ThreadLocalCache<
54+
llvm::StringMap<llvm::StringMapEntry<llvm::EmptyStringSetTag> *>>
5455
localIdentifierCache;
5556

5657
TimingManagerImpl() : identifiers(identifierAllocator) {}

0 commit comments

Comments
 (0)