Skip to content

Commit 224b21d

Browse files
authored
[SLP] Sort PHIs by ExtractElements when relevant (llvm#131229) (llvm#1321)
Auto-submit by Jenkins
2 parents 879f258 + 2f285af commit 224b21d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
634634
}
635635

636636
/// \returns True if Extract{Value,Element} instruction extracts element Idx.
637-
static std::optional<unsigned> getExtractIndex(Instruction *E) {
637+
static std::optional<unsigned> getExtractIndex(const Instruction *E) {
638638
unsigned Opcode = E->getOpcode();
639639
assert((Opcode == Instruction::ExtractElement ||
640640
Opcode == Instruction::ExtractValue) &&
@@ -18576,8 +18576,38 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
1857618576
if (NodeI1 != NodeI2)
1857718577
return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
1857818578
InstructionsState S = getSameOpcode({I1, I2}, *TLI);
18579-
if (S.getOpcode() && !S.isAltShuffle())
18579+
if (S.getOpcode() && !S.isAltShuffle()) {
18580+
const auto *E1 = dyn_cast<ExtractElementInst>(I1);
18581+
const auto *E2 = dyn_cast<ExtractElementInst>(I2);
18582+
if (!E1 || !E2)
18583+
continue;
18584+
18585+
// Sort on ExtractElementInsts primarily by vector operands. Prefer
18586+
// program order of the vector operands.
18587+
const auto *V1 = dyn_cast<Instruction>(E1->getVectorOperand());
18588+
const auto *V2 = dyn_cast<Instruction>(E2->getVectorOperand());
18589+
if (V1 != V2) {
18590+
if (!V1 || !V2)
18591+
continue;
18592+
if (V1->getParent() != V2->getParent())
18593+
continue;
18594+
return V1->comesBefore(V2);
18595+
}
18596+
// If we have the same vector operand, try to sort by constant
18597+
// index.
18598+
std::optional<unsigned> Id1 = getExtractIndex(E1);
18599+
std::optional<unsigned> Id2 = getExtractIndex(E2);
18600+
// Bring constants to the top
18601+
if (Id1 && !Id2)
18602+
return true;
18603+
if (!Id1 && Id2)
18604+
return false;
18605+
// First elements come first.
18606+
if (Id1 && Id2)
18607+
return *Id1 < *Id2;
18608+
1858018609
continue;
18610+
}
1858118611
return I1->getOpcode() < I2->getOpcode();
1858218612
}
1858318613
if (I1)

llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ bb1:
4949
define <4 x half> @phis_reverse(i1 %cmp1, <4 x half> %in1, <4 x half> %in2) {
5050
; CHECK-LABEL: @phis_reverse(
5151
; CHECK-NEXT: entry:
52-
; CHECK-NEXT: [[TMP0:%.*]] = shufflevector <4 x half> [[IN1:%.*]], <4 x half> poison, <2 x i32> <i32 2, i32 3>
53-
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x half> [[IN1]], <4 x half> poison, <2 x i32> <i32 0, i32 1>
52+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x half> [[IN1:%.*]], <4 x half> poison, <2 x i32> <i32 0, i32 1>
53+
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x half> [[IN1]], <4 x half> poison, <2 x i32> <i32 2, i32 3>
5454
; CHECK-NEXT: br i1 [[CMP1:%.*]], label [[BB1:%.*]], label [[BB0:%.*]]
5555
; CHECK: bb0:
56-
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x half> [[IN2:%.*]], <4 x half> poison, <2 x i32> <i32 2, i32 3>
57-
; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <4 x half> [[IN2]], <4 x half> poison, <2 x i32> <i32 0, i32 1>
56+
; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <4 x half> [[IN2:%.*]], <4 x half> poison, <2 x i32> <i32 0, i32 1>
57+
; CHECK-NEXT: [[TMP9:%.*]] = shufflevector <4 x half> [[IN2]], <4 x half> poison, <2 x i32> <i32 2, i32 3>
5858
; CHECK-NEXT: br label [[BB1]]
5959
; CHECK: bb1:
60-
; CHECK-NEXT: [[TMP4:%.*]] = phi <2 x half> [ [[TMP0]], [[ENTRY:%.*]] ], [ [[TMP2]], [[BB0]] ]
61-
; CHECK-NEXT: [[TMP5:%.*]] = phi <2 x half> [ [[TMP1]], [[ENTRY]] ], [ [[TMP3]], [[BB0]] ]
60+
; CHECK-NEXT: [[TMP5:%.*]] = phi <2 x half> [ [[TMP1]], [[ENTRY:%.*]] ], [ [[TMP3]], [[BB0]] ]
61+
; CHECK-NEXT: [[TMP4:%.*]] = phi <2 x half> [ [[TMP2]], [[ENTRY]] ], [ [[TMP9]], [[BB0]] ]
6262
; CHECK-NEXT: [[TMP6:%.*]] = shufflevector <2 x half> [[TMP5]], <2 x half> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
6363
; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <2 x half> [[TMP4]], <2 x half> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
64-
; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <4 x half> [[TMP6]], <4 x half> [[TMP7]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
64+
; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <2 x half> [[TMP5]], <2 x half> [[TMP4]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
6565
; CHECK-NEXT: ret <4 x half> [[TMP8]]
6666
;
6767
entry:

0 commit comments

Comments
 (0)