Skip to content

Commit 1c04cee

Browse files
[ADT] Modernize IndexedMap to use llvm::identity_cxx20 (NFC)
The legacy llvm::identity differs from std::identity in C++20. llvm::identity is a template struct with an ::argument_type member. In contrast, llvm::identity_cxx20 (and std::identity) is a non-template struct with a templated call operator and no ::argument_type. This patch modernizes llvm::IndexedMap by updating its default functor to llvm::identity_cxx20. A new template parameter IndexT now fulfills the role of ::argument_type. To avoid modifying numerous existing IndexedMap uses with custom functors (e.g., VirtReg2IndexFunctor), the detail::DeduceIndexType helper automatically deduces IndexT. It deduces ::argument_type for legacy functors and unsigned for llvm::identity_cxx20.
1 parent 048070b commit 1c04cee

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

llvm/include/llvm/ADT/IndexedMap.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,33 @@
2121
#define LLVM_ADT_INDEXEDMAP_H
2222

2323
#include "llvm/ADT/STLExtras.h"
24+
#include "llvm/ADT/STLForwardCompat.h"
2425
#include "llvm/ADT/SmallVector.h"
25-
#include "llvm/ADT/identity.h"
2626
#include <cassert>
27+
#include <type_traits>
2728

2829
namespace llvm {
2930

30-
template <typename T, typename ToIndexT = identity<unsigned>> class IndexedMap {
31-
using IndexT = typename ToIndexT::argument_type;
31+
namespace detail {
32+
// Helper to compute the IndexT for IndexedMap.
33+
template <typename ToIndexT, typename = void> struct DeduceIndexType {
34+
using type =
35+
std::conditional_t<std::is_same_v<ToIndexT, llvm::identity_cxx20>,
36+
unsigned, void>;
37+
};
38+
39+
template <typename ToIndexT>
40+
struct DeduceIndexType<ToIndexT,
41+
std::void_t<typename ToIndexT::argument_type>> {
42+
using type = typename ToIndexT::argument_type;
43+
};
44+
} // namespace detail
45+
46+
template <typename T, typename ToIndexT = llvm::identity_cxx20,
47+
typename IndexT = typename detail::DeduceIndexType<ToIndexT>::type>
48+
class IndexedMap {
49+
static_assert(!std::is_same_v<IndexT, void>,
50+
"Could not deduce index type from the provided functor.");
3251
// Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
3352
// can grow very large and SmallVector grows more efficiently as long as T
3453
// is trivially copyable.

0 commit comments

Comments
 (0)