Skip to content

Commit c19124a

Browse files
committed
[MapVector] Introduce {keys,values} iterator, use in VPlan
Similar to DenseMap::{keys,values}, introduce MapVector::{keys,values}, and use it to simplify some code in VPlan.
1 parent e3de8ff commit c19124a

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

llvm/include/llvm/ADT/MapVector.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ class MapVector {
9999
return try_emplace_impl(Key).first->second;
100100
}
101101

102+
[[nodiscard]] inline auto keys() {
103+
return map_range(
104+
Vector, [](const std::pair<KeyT, ValueT> &KV) { return KV.first; });
105+
}
106+
107+
[[nodiscard]] inline auto values() {
108+
return map_range(
109+
Vector, [](const std::pair<KeyT, ValueT> &KV) { return KV.second; });
110+
}
111+
112+
[[nodiscard]] inline auto keys() const {
113+
return map_range(
114+
Vector, [](const std::pair<KeyT, ValueT> &KV) { return KV.first; });
115+
}
116+
117+
[[nodiscard]] inline auto values() const {
118+
return map_range(
119+
Vector, [](const std::pair<KeyT, ValueT> &KV) { return KV.second; });
120+
}
121+
102122
// Returns a copy of the value. Only allowed if ValueT is copyable.
103123
[[nodiscard]] ValueT lookup(const KeyT &Key) const {
104124
static_assert(std::is_copy_constructible_v<ValueT>,

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
2626

2727
#include "VPlanValue.h"
28-
#include "llvm/ADT/DenseMap.h"
28+
#include "llvm/ADT/MapVector.h"
2929
#include "llvm/ADT/SmallPtrSet.h"
3030
#include "llvm/ADT/SmallVector.h"
3131
#include "llvm/ADT/Twine.h"
@@ -4336,13 +4336,9 @@ class VPlan {
43364336
/// Represents the loop-invariant VF * UF of the vector loop region.
43374337
VPValue VFxUF;
43384338

4339-
/// Holds a mapping between Values and their corresponding VPValue inside
4340-
/// VPlan.
4341-
Value2VPValueTy Value2VPValue;
4342-
4343-
/// Contains all the external definitions created for this VPlan. External
4344-
/// definitions are VPValues that hold a pointer to their underlying IR.
4345-
SmallVector<VPValue *, 16> VPLiveIns;
4339+
/// Contains all the external definitions created for this VPlan, as a mapping
4340+
/// from IR Values to VPValues.
4341+
SmallMapVector<Value *, VPValue *, 16> LiveIns;
43464342

43474343
/// Blocks allocated and owned by the VPlan. They will be deleted once the
43484344
/// VPlan is destroyed.
@@ -4539,10 +4535,9 @@ class VPlan {
45394535
/// yet) for \p V.
45404536
VPValue *getOrAddLiveIn(Value *V) {
45414537
assert(V && "Trying to get or add the VPValue of a null Value");
4542-
auto [It, Inserted] = Value2VPValue.try_emplace(V);
4538+
auto [It, Inserted] = LiveIns.try_emplace(V);
45434539
if (Inserted) {
45444540
VPValue *VPV = new VPValue(V);
4545-
VPLiveIns.push_back(VPV);
45464541
assert(VPV->isLiveIn() && "VPV must be a live-in.");
45474542
It->second = VPV;
45484543
}
@@ -4574,17 +4569,10 @@ class VPlan {
45744569
}
45754570

45764571
/// Return the live-in VPValue for \p V, if there is one or nullptr otherwise.
4577-
VPValue *getLiveIn(Value *V) const { return Value2VPValue.lookup(V); }
4572+
VPValue *getLiveIn(Value *V) const { return LiveIns.lookup(V); }
45784573

45794574
/// Return the list of live-in VPValues available in the VPlan.
4580-
ArrayRef<VPValue *> getLiveIns() const {
4581-
assert(all_of(Value2VPValue,
4582-
[this](const auto &P) {
4583-
return is_contained(VPLiveIns, P.second);
4584-
}) &&
4585-
"all VPValues in Value2VPValue must also be in VPLiveIns");
4586-
return VPLiveIns;
4587-
}
4575+
auto getLiveIns() const { return LiveIns.values(); }
45884576

45894577
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
45904578
/// Print the live-ins of this VPlan to \p O.

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
2121
#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_VALUE_H
2222

23-
#include "llvm/ADT/DenseMap.h"
2423
#include "llvm/ADT/STLExtras.h"
2524
#include "llvm/ADT/SmallVector.h"
26-
#include "llvm/ADT/StringMap.h"
2725
#include "llvm/ADT/TinyPtrVector.h"
2826
#include "llvm/ADT/iterator_range.h"
2927
#include "llvm/Support/Compiler.h"
@@ -196,9 +194,6 @@ class LLVM_ABI_FOR_TEST VPValue {
196194
}
197195
};
198196

199-
typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
200-
typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
201-
202197
LLVM_ABI_FOR_TEST raw_ostream &operator<<(raw_ostream &OS,
203198
const VPRecipeBase &R);
204199

llvm/unittests/ADT/MapVectorTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,24 @@ TEST(MapVectorTest, AtTest) {
307307
EXPECT_EQ(ConstMV.at(1), 12);
308308
}
309309

310+
TEST(MapVectorTest, KeysValuesIterator) {
311+
MapVector<int, int> MV;
312+
313+
MV.insert(std::make_pair(1, 11));
314+
MV.insert(std::make_pair(2, 12));
315+
MV.insert(std::make_pair(3, 13));
316+
MV.insert(std::make_pair(4, 14));
317+
MV.insert(std::make_pair(5, 15));
318+
MV.insert(std::make_pair(6, 16));
319+
320+
EXPECT_THAT(MV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
321+
EXPECT_THAT(MV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));
322+
323+
const MapVector<int, int> &ConstMV = MV;
324+
EXPECT_THAT(ConstMV.keys(), testing::ElementsAre(1, 2, 3, 4, 5, 6));
325+
EXPECT_THAT(ConstMV.values(), testing::ElementsAre(11, 12, 13, 14, 15, 16));
326+
}
327+
310328
template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
311329
using int_type = IntType;
312330
};

0 commit comments

Comments
 (0)