[PATCH] D101093: [VPlan] Add VPBlockUtils::basicBlocksOnly helper.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 22 11:06:45 PDT 2021
fhahn created this revision.
fhahn added reviewers: gilr, Ayal, sguggill, a.elovikov, rengolin.
Herald added subscribers: tschuett, psnobl, rogfer01, bollu, hiraditya.
fhahn requested review of this revision.
Herald added a subscriber: vkmr.
Herald added a project: LLVM.
This patch adds basicBlocksOnly and constBasicBlocksOnly helpers which
take an iterator range over VPBlockBase * and const VPBlockBase *
respectively, and return an iterator range that only includes
VPBasicBlocks.
Unfortunately I could not think of a way to have a single version of
basicBlocksOnly that can take arbitrary iterators and also handle
filtering/mapping of both VPBlockBase * and const VPBlockBase *.
Unfortunately, filter_range does not work with iterators over pointers,
so we need to map it to a reference and back. Mapping back to a pointer
is optional, but more convenient when using the iterator range to
populate containers.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D101093
Files:
llvm/lib/Transforms/Vectorize/VPlan.h
llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
Index: llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
===================================================================
--- llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
+++ llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
@@ -405,6 +405,18 @@
EXPECT_EQ(R2BB2, FromIterator[6]);
EXPECT_EQ(R1BB3, FromIterator[7]);
+ // VPBasicBlocks only.
+ FromIterator.clear();
+ copy(VPBlockUtils::constBasicBlocksOnly(depth_first(Start)),
+ std::back_inserter(FromIterator));
+ EXPECT_EQ(6u, FromIterator.size());
+ EXPECT_EQ(R1BB1, FromIterator[0]);
+ EXPECT_EQ(R1BB2, FromIterator[1]);
+ EXPECT_EQ(R1BB4, FromIterator[2]);
+ EXPECT_EQ(R2BB1, FromIterator[3]);
+ EXPECT_EQ(R2BB2, FromIterator[4]);
+ EXPECT_EQ(R1BB3, FromIterator[5]);
+
// Post-order.
FromIterator.clear();
copy(post_order(Start), std::back_inserter(FromIterator));
@@ -599,6 +611,14 @@
EXPECT_EQ(R3BB1, FromIterator[5]);
EXPECT_EQ(VPBB2, FromIterator[6]);
+ FromIterator.clear();
+ copy(VPBlockUtils::basicBlocksOnly(depth_first(Start)),
+ std::back_inserter(FromIterator));
+ EXPECT_EQ(VPBB1, FromIterator[0]);
+ EXPECT_EQ(R2BB1, FromIterator[1]);
+ EXPECT_EQ(R3BB1, FromIterator[2]);
+ EXPECT_EQ(VPBB2, FromIterator[3]);
+
// Post-order.
FromIterator.clear();
copy(post_order(Start), std::back_inserter(FromIterator));
@@ -611,6 +631,15 @@
EXPECT_EQ(R1, FromIterator[5]);
EXPECT_EQ(VPBB1, FromIterator[6]);
+ FromIterator.clear();
+ copy(VPBlockUtils::basicBlocksOnly(post_order(Start)),
+ std::back_inserter(FromIterator));
+ EXPECT_EQ(FromIterator.size(), 4u);
+ EXPECT_EQ(VPBB2, FromIterator[0]);
+ EXPECT_EQ(R3BB1, FromIterator[1]);
+ EXPECT_EQ(R2BB1, FromIterator[2]);
+ EXPECT_EQ(VPBB1, FromIterator[3]);
+
// Use Plan to properly clean up created blocks.
VPlan Plan;
Plan.setEntry(VPBB1);
Index: llvm/lib/Transforms/Vectorize/VPlan.h
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlan.h
+++ llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2201,6 +2201,37 @@
}
return Count;
}
+
+ /// Return an iterator range over \p Iter which only includes VPBasicBlock *.
+ template <typename T> static auto basicBlocksOnly(T Iter) {
+ // We need to first create an iterator range over VPBlockBase & instead of
+ // VPBlockBase * for filter_range to work properly.
+ auto Mapped = map_range(
+ Iter, [](VPBlockBase *Block) -> VPBlockBase & { return *Block; });
+ auto Filter = make_filter_range(
+ Mapped, [](VPBlockBase &Block) { return isa<VPBasicBlock>(&Block); });
+ return map_range(Filter, [](VPBlockBase &Block) -> VPBasicBlock * {
+ return cast<VPBasicBlock>(&Block);
+ });
+ }
+
+ /// Return an iterator range over \p Iter which only includes const
+ /// VPBasicBlock *.
+ template <typename T> static auto constBasicBlocksOnly(T Iter) {
+ // We need to first create an iterator range over const VPBlockBase &
+ // instead of const VPBlockBase * for filter_range to work properly.
+ auto Mapped =
+ map_range(Iter, [](const VPBlockBase *Block) -> const VPBlockBase & {
+ return *Block;
+ });
+ auto Filter = make_filter_range(Mapped, [](const VPBlockBase &Block) {
+ return isa<VPBasicBlock>(&Block);
+ });
+ return map_range(Filter,
+ [](const VPBlockBase &Block) -> const VPBasicBlock * {
+ return cast<VPBasicBlock>(&Block);
+ });
+ }
};
class VPInterleavedAccessInfo {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101093.339727.patch
Type: text/x-patch
Size: 3637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210422/a5f3e0eb/attachment.bin>
More information about the llvm-commits
mailing list