-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm] Split out DenseMapInfo<variant> specialization
Remove the `DenseMapInfo<std::variant<Ts...>>` variant out from `llvm/ADT/DenseMapInfo.h` into a separate header `llvm/ADT/DenseMapInfoVariant.h` This allows us to remove the `<variant>` include, which is being transitively and unncessary included in all translation units that include `llvm/ADT/DenseMap.h`. There have been similar changes to move out specializations for * `APInt.h` fd7e309 and * `StringRef.h`/`ArrayRef.h` 983565a to reduce the compilation time. As we are unable to move the specialization into `<variant>`, we create a separate `DenseMapInfoVariant.h` header that can be used by anyone who needs this specialization. This reduces the total number of preprocessing tokens across the LLVM source files in lib from (roughly) 1,964,876,961 to 1,936,551,496 - a reduction of ~1.44%. This should result in a small improvement in compilation time. Differential Revision: https://reviews.llvm.org/D150997
- Loading branch information
1 parent
791c3cb
commit cea0eea
Showing
11 changed files
with
90 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//===- DenseMapInfoVariant.h - Type traits for DenseMap<variant> *- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file defines DenseMapInfo traits for DenseMap<std::variant<Ts...>>. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ADT_DENSEMAPINFOVARIANT_H | ||
#define LLVM_ADT_DENSEMAPINFOVARIANT_H | ||
|
||
#include "llvm/ADT/DenseMapInfo.h" | ||
#include <utility> | ||
#include <variant> | ||
|
||
namespace llvm { | ||
|
||
// Provide DenseMapInfo for variants whose all alternatives have DenseMapInfo. | ||
template <typename... Ts> struct DenseMapInfo<std::variant<Ts...>> { | ||
using Variant = std::variant<Ts...>; | ||
using FirstT = std::variant_alternative_t<0, Variant>; | ||
|
||
static inline Variant getEmptyKey() { | ||
return Variant(std::in_place_index<0>, DenseMapInfo<FirstT>::getEmptyKey()); | ||
} | ||
|
||
static inline Variant getTombstoneKey() { | ||
return Variant(std::in_place_index<0>, | ||
DenseMapInfo<FirstT>::getTombstoneKey()); | ||
} | ||
|
||
static unsigned getHashValue(const Variant &Val) { | ||
return std::visit( | ||
[&Val](auto &&Alternative) { | ||
using T = std::decay_t<decltype(Alternative)>; | ||
// Include index in hash to make sure same value as different | ||
// alternatives don't collide. | ||
return DenseMapInfo<std::pair<size_t, T>>::getHashValuePiecewise( | ||
Val.index(), Alternative); | ||
}, | ||
Val); | ||
} | ||
|
||
static bool isEqual(const Variant &LHS, const Variant &RHS) { | ||
if (LHS.index() != RHS.index()) | ||
return false; | ||
if (LHS.valueless_by_exception()) | ||
return true; | ||
// We want to dispatch to DenseMapInfo<T>::isEqual(LHS.get(I), RHS.get(I)) | ||
// We know the types are the same, but std::visit(V, LHS, RHS) doesn't. | ||
// We erase the type held in LHS to void*, and dispatch over RHS. | ||
const void *ErasedLHS = | ||
std::visit([](const auto &LHS) -> const void * { return &LHS; }, LHS); | ||
return std::visit( | ||
[&](const auto &RHS) -> bool { | ||
using T = std::remove_cv_t<std::remove_reference_t<decltype(RHS)>>; | ||
return DenseMapInfo<T>::isEqual(*static_cast<const T *>(ErasedLHS), | ||
RHS); | ||
}, | ||
RHS); | ||
} | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_ADT_DENSEMAPINFOVARIANT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters