Skip to content

Commit 8e02164

Browse files
committed
[VPlan] Add unit test for createAndOptimizeReplicateRegions. NFC
In llvm#160449 some of the tests end up merging less blocks so we end up losing test coverage. This adds a unit test to try and cover it elsewhere in a more predictable way, so it won't be influenced by e.g. whether or not the cost model decides to scalarize an instruction. Two parts in createReplicateRegion/addReplicateRegions had to be relaxed to allow using a fake Instruction with no BasicBlock parent. I wasn't able to create a fake BasicBlock in the test without hitting iterator assertions.
1 parent 3155b05 commit 8e02164

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ static VPRegionBlock *createReplicateRegion(VPReplicateRecipe *PredRecipe,
345345
Instruction *Instr = PredRecipe->getUnderlyingInstr();
346346
// Build the triangular if-then region.
347347
std::string RegionName = (Twine("pred.") + Instr->getOpcodeName()).str();
348-
assert(Instr->getParent() && "Predicated instruction not in any basic block");
349348
auto *BlockInMask = PredRecipe->getMask();
350349
auto *MaskDef = BlockInMask->getDefiningRecipe();
351350
auto *BOMRecipe = new VPBranchOnMaskRecipe(
@@ -399,8 +398,9 @@ static void addReplicateRegions(VPlan &Plan) {
399398
VPBasicBlock *SplitBlock = CurrentBlock->splitAt(RepR->getIterator());
400399

401400
BasicBlock *OrigBB = RepR->getUnderlyingInstr()->getParent();
402-
SplitBlock->setName(
403-
OrigBB->hasName() ? OrigBB->getName() + "." + Twine(BBNum++) : "");
401+
SplitBlock->setName((OrigBB && OrigBB->hasName())
402+
? OrigBB->getName() + "." + Twine(BBNum++)
403+
: "");
404404
// Record predicated instructions for above packing optimizations.
405405
VPRegionBlock *Region = createReplicateRegion(RepR, Plan);
406406
Region->setParent(CurrentBlock->getParent());

llvm/unittests/Transforms/Vectorize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_llvm_unittest(VectorizeTests
1414
VPlanHCFGTest.cpp
1515
VPlanPatternMatchTest.cpp
1616
VPlanSlpTest.cpp
17+
VPlanTransformsTest.cpp
1718
VPlanUncountableExitTest.cpp
1819
VPlanVerifierTest.cpp
1920
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===- llvm/unittests/Transforms/Vectorize/VPlanTransformsTest.cpp --------===//
2+
//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "../lib/Transforms/Vectorize/VPlanTransforms.h"
11+
#include "../lib/Transforms/Vectorize/VPlan.h"
12+
#include "VPlanTestBase.h"
13+
#include "gtest/gtest.h"
14+
15+
namespace llvm {
16+
17+
namespace {
18+
using VPlanTransformsTest = VPlanTestBase;
19+
20+
// Test we create and merge replicate regions:
21+
// VPBB1:
22+
// REPLICATE %Rep0 = add
23+
// REPLICATE %Rep1 = add
24+
// REPLICATE %Rep2 = add
25+
// No successors
26+
//
27+
// ->
28+
//
29+
// <xVFxUF> pred.add: {
30+
// pred.add.entry:
31+
// BRANCH-ON-MASK %Mask
32+
// Successor(s): pred.add.if, pred.add.continue
33+
//
34+
// pred.add.if:
35+
// REPLICATE %Rep0 = add
36+
// REPLICATE %Rep1 = add
37+
// REPLICATE %Rep2 = add
38+
// Successor(s): pred.add.continue
39+
//
40+
// pred.add.continue:
41+
// No successors
42+
// }
43+
TEST_F(VPlanTransformsTest, createAndOptimizeReplicateRegions) {
44+
VPlan &Plan = getPlan();
45+
46+
IntegerType *Int32 = IntegerType::get(C, 32);
47+
auto *AI = BinaryOperator::CreateAdd(PoisonValue::get(Int32),
48+
PoisonValue::get(Int32));
49+
50+
auto *VPBB0 = Plan.getEntry();
51+
auto *VPBB1 = Plan.createVPBasicBlock("VPBB1");
52+
VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB1, VPBB1, "R1");
53+
VPBlockUtils::connectBlocks(VPBB0, R1);
54+
auto *Mask = new VPInstruction(0, {});
55+
auto *Rep0 = new VPReplicateRecipe(AI, {}, false, Mask);
56+
auto *Rep1 = new VPReplicateRecipe(AI, {}, false, Mask);
57+
auto *Rep2 = new VPReplicateRecipe(AI, {}, false, Mask);
58+
VPBB1->appendRecipe(Rep0);
59+
VPBB1->appendRecipe(Rep1);
60+
VPBB1->appendRecipe(Rep2);
61+
62+
VPlanTransforms::createAndOptimizeReplicateRegions(Plan);
63+
64+
auto *Replicator = cast<VPRegionBlock>(R1->getEntry()->getSingleSuccessor());
65+
EXPECT_TRUE(Replicator->isReplicator());
66+
auto *ReplicatorEntry = cast<VPBasicBlock>(Replicator->getEntry());
67+
EXPECT_EQ(ReplicatorEntry->size(), 1u);
68+
EXPECT_TRUE(isa<VPBranchOnMaskRecipe>(ReplicatorEntry->front()));
69+
auto *ReplicatorIf = cast<VPBasicBlock>(ReplicatorEntry->getSuccessors()[0]);
70+
EXPECT_EQ(ReplicatorIf->size(), 3u);
71+
EXPECT_EQ(ReplicatorEntry->getSuccessors()[1],
72+
ReplicatorIf->getSingleSuccessor());
73+
EXPECT_EQ(ReplicatorIf->getSingleSuccessor(), Replicator->getExiting());
74+
}
75+
76+
} // namespace
77+
} // namespace llvm

0 commit comments

Comments
 (0)