Skip to content

[X86] Attempt to canonicalize vXf64 SHUFPD shuffle masks with undef elts to improve further folding #116419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9926,6 +9926,42 @@ static SDValue getV4X86ShuffleImm8ForMask(ArrayRef<int> Mask, const SDLoc &DL,
return DAG.getTargetConstant(getV4X86ShuffleImm(Mask), DL, MVT::i8);
}

// Canonicalize SHUFPD mask to improve chances of further folding.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no further folding showing in test cases. Does it need a follow up patch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a number of test cases which now further fold to VBLENDPD/S (allowing commutation yay!) or VUNPCK?PD - the only further fold that leads to an actual instruction reduction so far is the add_v4f64_024u_135u_reverse test in vector-shuffle-256-v4.ll

// Mask elements are assumed to be -1, 0 or 1 to match the SHUFPD lo/hi pattern.
static unsigned getSHUFPDImm(ArrayRef<int> Mask) {
assert((Mask.size() == 2 || Mask.size() == 4 || Mask.size() == 8) &&
"Unexpected SHUFPD mask size");
assert(all_of(Mask, [](int M) { return -1 <= M && M <= 1; }) &&
"Unexpected SHUFPD mask elements");

// If the mask only uses one non-undef element, then fully 'splat' it to
// improve later broadcast matching.
int FirstIndex = find_if(Mask, [](int M) { return M >= 0; }) - Mask.begin();
assert(0 <= FirstIndex && FirstIndex < 4 && "All undef shuffle mask");

int FirstElt = Mask[FirstIndex];
if (all_of(Mask, [FirstElt](int M) { return M < 0 || M == FirstElt; }) &&
count_if(Mask, [FirstElt](int M) { return M == FirstElt; }) > 1) {
unsigned Imm = 0;
for (unsigned I = 0, E = Mask.size(); I != E; ++I)
Imm |= FirstElt << I;
return Imm;
}

// Attempt to keep any undef elements in place to improve chances of the
// shuffle becoming a (commutative) blend.
unsigned Imm = 0;
for (unsigned I = 0, E = Mask.size(); I != E; ++I)
Imm |= (Mask[I] < 0 ? (I & 1) : Mask[I]) << I;

return Imm;
}

static SDValue getSHUFPDImmForMask(ArrayRef<int> Mask, const SDLoc &DL,
SelectionDAG &DAG) {
return DAG.getTargetConstant(getSHUFPDImm(Mask), DL, MVT::i8);
}

// The Shuffle result is as follow:
// 0*a[0]0*a[1]...0*a[n] , n >=0 where a[] elements in a ascending order.
// Each Zeroable's element correspond to a particular Mask's element.
Expand Down Expand Up @@ -14871,7 +14907,7 @@ static SDValue lowerShuffleAsLanePermuteAndSHUFP(const SDLoc &DL, MVT VT,

int LHSMask[4] = {-1, -1, -1, -1};
int RHSMask[4] = {-1, -1, -1, -1};
unsigned SHUFPMask = 0;
int SHUFPDMask[4] = {-1, -1, -1, -1};

// As SHUFPD uses a single LHS/RHS element per lane, we can always
// perform the shuffle once the lanes have been shuffled in place.
Expand All @@ -14882,13 +14918,13 @@ static SDValue lowerShuffleAsLanePermuteAndSHUFP(const SDLoc &DL, MVT VT,
int LaneBase = i & ~1;
auto &LaneMask = (i & 1) ? RHSMask : LHSMask;
LaneMask[LaneBase + (M & 1)] = M;
SHUFPMask |= (M & 1) << i;
SHUFPDMask[i] = M & 1;
}

SDValue LHS = DAG.getVectorShuffle(VT, DL, V1, V2, LHSMask);
SDValue RHS = DAG.getVectorShuffle(VT, DL, V1, V2, RHSMask);
return DAG.getNode(X86ISD::SHUFP, DL, VT, LHS, RHS,
DAG.getTargetConstant(SHUFPMask, DL, MVT::i8));
getSHUFPDImmForMask(SHUFPDMask, DL, DAG));
}

/// Lower a vector shuffle crossing multiple 128-bit lanes as
Expand Down Expand Up @@ -15800,9 +15836,9 @@ static bool matchShuffleWithSHUFPD(MVT VT, SDValue &V1, SDValue &V2,

// Mask for V8F64: 0/1, 8/9, 2/3, 10/11, 4/5, ..
// Mask for V4F64; 0/1, 4/5, 2/3, 6/7..
ShuffleImm = 0;
bool ShufpdMask = true;
bool CommutableMask = true;
bool IsSHUFPD = true;
bool IsCommutable = true;
SmallVector<int, 8> SHUFPDMask(NumElts, -1);
for (int i = 0; i < NumElts; ++i) {
if (Mask[i] == SM_SentinelUndef || ZeroLane[i & 1])
continue;
Expand All @@ -15811,20 +15847,21 @@ static bool matchShuffleWithSHUFPD(MVT VT, SDValue &V1, SDValue &V2,
int Val = (i & 6) + NumElts * (i & 1);
int CommutVal = (i & 0xe) + NumElts * ((i & 1) ^ 1);
if (Mask[i] < Val || Mask[i] > Val + 1)
ShufpdMask = false;
IsSHUFPD = false;
if (Mask[i] < CommutVal || Mask[i] > CommutVal + 1)
CommutableMask = false;
ShuffleImm |= (Mask[i] % 2) << i;
IsCommutable = false;
SHUFPDMask[i] = Mask[i] % 2;
}

if (!ShufpdMask && !CommutableMask)
if (!IsSHUFPD && !IsCommutable)
return false;

if (!ShufpdMask && CommutableMask)
if (!IsSHUFPD && IsCommutable)
std::swap(V1, V2);

ForceV1Zero = ZeroLane[0];
ForceV2Zero = ZeroLane[1];
ShuffleImm = getSHUFPDImm(SHUFPDMask);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4756,7 +4756,7 @@ define void @vec384_i64_widen_to_i128_factor2_broadcast_to_v3i128_factor3(ptr %i
; AVX-NEXT: vpaddb 48(%rsi), %xmm2, %xmm2
; AVX-NEXT: vpaddb (%rsi), %xmm0, %xmm0
; AVX-NEXT: vinsertf128 $1, %xmm0, %ymm0, %ymm3
; AVX-NEXT: vshufpd {{.*#+}} ymm2 = ymm3[0],ymm2[1],ymm3[2],ymm2[2]
; AVX-NEXT: vblendps {{.*#+}} ymm2 = ymm3[0,1],ymm2[2,3],ymm3[4,5],ymm2[6,7]
; AVX-NEXT: vextractf128 $1, %ymm2, %xmm3
; AVX-NEXT: vpaddb 16(%rdx), %xmm3, %xmm3
; AVX-NEXT: vpaddb (%rdx), %xmm2, %xmm2
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/subvector-broadcast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,13 @@ define <8 x float> @broadcast_v8f32_v2f32_u1uu0uEu(ptr %vp, <8 x float> %default
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: vbroadcastsd (%eax), %ymm1
; X86-NEXT: vshufpd {{.*#+}} ymm0 = ymm1[0],ymm0[0],ymm1[2],ymm0[3]
; X86-NEXT: vunpckhpd {{.*#+}} ymm0 = ymm1[1],ymm0[1],ymm1[3],ymm0[3]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uops shows the TP of vunpckhpd is worse than vshufpd. Is this expected?

Copy link
Contributor

@phoebewang phoebewang Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's only for Alder Lake-P. But others like SKX, ZEN4 don't have difference between both.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC X86FixupInstTunings should handle these cases for us based off the sched models - CC @goldsteinn

; X86-NEXT: retl
;
; X64-LABEL: broadcast_v8f32_v2f32_u1uu0uEu:
; X64: # %bb.0:
; X64-NEXT: vbroadcastsd (%rdi), %ymm1
; X64-NEXT: vshufpd {{.*#+}} ymm0 = ymm1[0],ymm0[0],ymm1[2],ymm0[3]
; X64-NEXT: vunpckhpd {{.*#+}} ymm0 = ymm1[1],ymm0[1],ymm1[3],ymm0[3]
; X64-NEXT: retq
%vec = load <2 x float>, ptr %vp
%shuf = shufflevector <2 x float> %vec, <2 x float> undef, <8 x i32> <i32 undef, i32 1, i32 undef, i32 undef, i32 0, i32 2, i32 3, i32 undef>
Expand Down
Loading
Loading