Skip to content

[AArch64] Prevent unnecessary truncation in bool vector reduce code generation #120096

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
Dec 18, 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
25 changes: 20 additions & 5 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15928,17 +15928,32 @@ static SDValue getVectorBitwiseReduce(unsigned Opcode, SDValue Vec, EVT VT,
return getVectorBitwiseReduce(Opcode, HalfVec, VT, DL, DAG);
}

// Vectors that are less than 64 bits get widened to neatly fit a 64 bit
// register, so e.g. <4 x i1> gets lowered to <4 x i16>. Sign extending to
// this element size leads to the best codegen, since e.g. setcc results
// might need to be truncated otherwise.
EVT ExtendedVT = MVT::getIntegerVT(std::max(64u / NumElems, 8u));
// Results of setcc operations get widened to 128 bits if their input
// operands are 128 bits wide, otherwise vectors that are less than 64 bits
// get widened to neatly fit a 64 bit register, so e.g. <4 x i1> gets
// lowered to either <4 x i16> or <4 x i32>. Sign extending to this element
// size leads to the best codegen, since e.g. setcc results might need to be
// truncated otherwise.
unsigned ExtendedWidth = 64;
if (Vec.getOpcode() == ISD::SETCC &&
Vec.getOperand(0).getValueSizeInBits() >= 128) {
ExtendedWidth = 128;
}
EVT ExtendedVT = MVT::getIntegerVT(std::max(ExtendedWidth / NumElems, 8u));

// any_ext doesn't work with umin/umax, so only use it for uadd.
unsigned ExtendOp =
ScalarOpcode == ISD::XOR ? ISD::ANY_EXTEND : ISD::SIGN_EXTEND;
SDValue Extended = DAG.getNode(
ExtendOp, DL, VecVT.changeVectorElementType(ExtendedVT), Vec);
// The uminp/uminv and umaxp/umaxv instructions don't have .2d variants, so
// in that case we bitcast the sign extended values from v2i64 to v4i32
// before reduction for optimal code generation.
if ((ScalarOpcode == ISD::AND || ScalarOpcode == ISD::OR) &&
NumElems == 2 && ExtendedWidth == 128) {
Extended = DAG.getBitcast(MVT::v4i32, Extended);
ExtendedVT = MVT::i32;
}
switch (ScalarOpcode) {
case ISD::AND:
Result = DAG.getNode(ISD::VECREDUCE_UMIN, DL, ExtendedVT, Extended);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ define i1 @unordered_floating_point_compare_on_v8f32(<8 x float> %a_vec) {
; CHECK-NEXT: mov w8, #1 // =0x1
; CHECK-NEXT: uzp1 v0.8h, v0.8h, v1.8h
; CHECK-NEXT: mvn v0.16b, v0.16b
; CHECK-NEXT: xtn v0.8b, v0.8h
; CHECK-NEXT: umaxv b0, v0.8b
; CHECK-NEXT: umaxv h0, v0.8h
; CHECK-NEXT: fmov w9, s0
; CHECK-NEXT: bic w0, w8, w9
; CHECK-NEXT: ret
Expand Down
Loading
Loading