[llvm] [GISel][SDAG] Avoid push_back in loops for some shuffle mask handling. (PR #119434)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 10 15:51:18 PST 2024
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/119434
>From b8bc3d826bb7c489a720ff8630b4df9b26743e1c Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Dec 2024 10:45:01 -0800
Subject: [PATCH 1/2] [GISel][SDAG] Avoid push_back in loops for some shuffle
mask handling.
Each call to push_back contains a check to see if the vector needs
to grow. Using resize or the giving the size to the constructor can
reduce the number of checks for growing.
---
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp | 11 ++++-------
.../SelectionDAG/LegalizeVectorTypes.cpp | 17 ++++++-----------
2 files changed, 10 insertions(+), 18 deletions(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 2544f2479ddc68..6676cee8f618a8 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -6173,8 +6173,7 @@ LegalizerHelper::equalizeVectorShuffleLengths(MachineInstr &MI) {
// Extend mask to match new destination vector size with
// undef values.
SmallVector<int, 16> NewMask(Mask);
- for (unsigned I = MaskNumElts; I < SrcNumElts; ++I)
- NewMask.push_back(-1);
+ NewMask.resize(SrcNumElts, -1);
moreElementsVectorDst(MI, SrcTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
@@ -6254,16 +6253,14 @@ LegalizerHelper::moreElementsVectorShuffle(MachineInstr &MI,
moreElementsVectorSrc(MI, MoreTy, 2);
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned I = 0; I != NumElts; ++I) {
int Idx = Mask[I];
if (Idx < static_cast<int>(NumElts))
- NewMask.push_back(Idx);
+ NewMask[I] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[I] = Idx - NumElts + WidenNumElts;
}
- for (unsigned I = NumElts; I != WidenNumElts; ++I)
- NewMask.push_back(-1);
moreElementsVectorDst(MI, MoreTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
MIRBuilder.buildShuffleVector(MI.getOperand(0).getReg(),
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 465128099f4447..205ad899e4c278 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -6421,16 +6421,14 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
SDValue InOp2 = GetWidenedVector(N->getOperand(1));
// Adjust mask based on new input vector length.
- SmallVector<int, 16> NewMask;
+ SmallVector<int, 16> NewMask(WidenNumElts, -1);
for (unsigned i = 0; i != NumElts; ++i) {
int Idx = N->getMaskElt(i);
if (Idx < (int)NumElts)
- NewMask.push_back(Idx);
+ NewMask[i] = Idx;
else
- NewMask.push_back(Idx - NumElts + WidenNumElts);
+ NewMask[i] = Idx - NumElts + WidenNumElts;
}
- for (unsigned i = NumElts; i != WidenNumElts; ++i)
- NewMask.push_back(-1);
return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
}
@@ -6478,12 +6476,9 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
// Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
// fixed-vectors.
- SmallVector<int, 16> Mask;
- for (unsigned i = 0; i != VTNumElts; ++i) {
- Mask.push_back(IdxVal + i);
- }
- for (unsigned i = VTNumElts; i != WidenNumElts; ++i)
- Mask.push_back(-1);
+ SmallVector<int, 16> Mask(VTNumElts, -1);
+ for (unsigned i = 0; i != VTNumElts; ++i)
+ Mask[i] = IdxVal + i;
return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getUNDEF(WidenVT),
Mask);
>From 2c9d19f7cdd9f9a12e1c27d515cee760fec25013 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 10 Dec 2024 15:51:02 -0800
Subject: [PATCH 2/2] fixup! Address review comments.
---
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp | 4 ++--
llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp | 5 ++---
2 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 6676cee8f618a8..f56be39838ba7d 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -6172,8 +6172,8 @@ LegalizerHelper::equalizeVectorShuffleLengths(MachineInstr &MI) {
if (MaskNumElts < SrcNumElts) {
// Extend mask to match new destination vector size with
// undef values.
- SmallVector<int, 16> NewMask(Mask);
- NewMask.resize(SrcNumElts, -1);
+ SmallVector<int, 16> NewMask(SrcNumElts, -1);
+ llvm::copy(Mask, NewMask.begin());
moreElementsVectorDst(MI, SrcTy, 0);
MIRBuilder.setInstrAndDebugLoc(MI);
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 205ad899e4c278..107454a92e356c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -6476,9 +6476,8 @@ SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
// Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
// fixed-vectors.
- SmallVector<int, 16> Mask(VTNumElts, -1);
- for (unsigned i = 0; i != VTNumElts; ++i)
- Mask[i] = IdxVal + i;
+ SmallVector<int, 16> Mask(WidenNumElts, -1);
+ std::iota(Mask.begin(), Mask.begin() + VTNumElts, IdxVal);
return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getUNDEF(WidenVT),
Mask);
More information about the llvm-commits
mailing list