Skip to content

Commit 7d39664

Browse files
authored
Revert "[SelectionDAG] Expand CTTZ_ELTS[_ZERO_POISON] and handle splitting" (#188220)
Reverts llvm/llvm-project#185605 Buildbot failures caused by ISel crashes in https://lab.llvm.org/buildbot/#/builders/157/builds/45416 and https://lab.llvm.org/buildbot/#/builders/10/builds/25156
1 parent 80245f3 commit 7d39664

16 files changed

Lines changed: 485 additions & 646 deletions

‎llvm/include/llvm/CodeGen/BasicTTIImpl.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,8 +2133,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
21332133
VScaleRange = getVScaleRange(I->getCaller(), 64);
21342134

21352135
unsigned EltWidth = getTLI()->getBitWidthForCttzElements(
2136-
getTLI()->getValueType(DL, RetTy), ArgType.getVectorElementCount(),
2137-
ZeroIsPoison, &VScaleRange);
2136+
RetTy, ArgType.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
21382137
Type *NewEltTy = IntegerType::getIntNTy(RetTy->getContext(), EltWidth);
21392138

21402139
// Create the new vector type & get the vector length

‎llvm/include/llvm/CodeGen/ISDOpcodes.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ enum NodeType {
15801580
EXPERIMENTAL_VECTOR_HISTOGRAM,
15811581

15821582
/// Returns the number of number of trailing (least significant) zero elements
1583-
/// in a vector. Has a single vector operand. The result is poison if the
1583+
/// in a vector. Has a single i1 vector operand. The result is poison if the
15841584
/// return type isn't wide enough to hold the maximum number of elements in
15851585
/// the input vector.
15861586
CTTZ_ELTS,

‎llvm/include/llvm/CodeGen/TargetLowering.h‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class LLVM_ABI TargetLoweringBase {
498498

499499
/// Return the minimum number of bits required to hold the maximum possible
500500
/// number of trailing zero vector elements.
501-
unsigned getBitWidthForCttzElements(EVT RetVT, ElementCount EC,
501+
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC,
502502
bool ZeroIsPoison,
503503
const ConstantRange *VScaleRange) const;
504504

@@ -5820,10 +5820,6 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
58205820
/// temporarily, advance store position, before re-loading the final vector.
58215821
SDValue expandVECTOR_COMPRESS(SDNode *Node, SelectionDAG &DAG) const;
58225822

5823-
/// Expand a CTTZ_ELTS or CTTZ_ELTS_ZERO_POISON by calculating (VL - i) for
5824-
/// each active lane (i), getting the maximum and subtracting it from VL.
5825-
SDValue expandCttzElts(SDNode *Node, SelectionDAG &DAG) const;
5826-
58275823
/// Expands PARTIAL_REDUCE_S/UMLA nodes to a series of simpler operations,
58285824
/// consisting of zext/sext, extract_subvector, mul and add operations.
58295825
SDValue expandPartialReduceMLA(SDNode *Node, SelectionDAG &DAG) const;

‎llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,10 +2167,6 @@ bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
21672167
case ISD::VECTOR_FIND_LAST_ACTIVE:
21682168
Res = PromoteIntOp_VECTOR_FIND_LAST_ACTIVE(N, OpNo);
21692169
break;
2170-
case ISD::CTTZ_ELTS:
2171-
case ISD::CTTZ_ELTS_ZERO_POISON:
2172-
Res = PromoteIntOp_CTTZ_ELTS(N);
2173-
break;
21742170
case ISD::GET_ACTIVE_LANE_MASK:
21752171
Res = PromoteIntOp_GET_ACTIVE_LANE_MASK(N);
21762172
break;
@@ -3004,11 +3000,6 @@ SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N,
30043000
return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
30053001
}
30063002

3007-
SDValue DAGTypeLegalizer::PromoteIntOp_CTTZ_ELTS(SDNode *N) {
3008-
SDValue Op = GetPromotedInteger(N->getOperand(0));
3009-
return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
3010-
}
3011-
30123003
SDValue DAGTypeLegalizer::PromoteIntOp_GET_ACTIVE_LANE_MASK(SDNode *N) {
30133004
SmallVector<SDValue, 1> NewOps(N->ops());
30143005
NewOps[0] = ZExtPromotedInteger(N->getOperand(0));

‎llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ class LLVM_LIBRARY_VISIBILITY DAGTypeLegalizer {
419419
SDValue PromoteIntOp_VP_SPLICE(SDNode *N, unsigned OpNo);
420420
SDValue PromoteIntOp_VECTOR_HISTOGRAM(SDNode *N, unsigned OpNo);
421421
SDValue PromoteIntOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N, unsigned OpNo);
422-
SDValue PromoteIntOp_CTTZ_ELTS(SDNode *N);
423422
SDValue PromoteIntOp_GET_ACTIVE_LANE_MASK(SDNode *N);
424423
SDValue PromoteIntOp_PARTIAL_REDUCE_MLA(SDNode *N);
425424
SDValue PromoteIntOp_LOOP_DEPENDENCE_MASK(SDNode *N, unsigned OpNo);
@@ -988,7 +987,6 @@ class LLVM_LIBRARY_VISIBILITY DAGTypeLegalizer {
988987
SDValue SplitVecOp_FPOpDifferentTypes(SDNode *N);
989988
SDValue SplitVecOp_CMP(SDNode *N);
990989
SDValue SplitVecOp_FP_TO_XINT_SAT(SDNode *N);
991-
SDValue SplitVecOp_CttzElts(SDNode *N);
992990
SDValue SplitVecOp_VP_CttzElements(SDNode *N);
993991
SDValue SplitVecOp_VECTOR_HISTOGRAM(SDNode *N);
994992
SDValue SplitVecOp_PARTIAL_REDUCE_MLA(SDNode *N);

‎llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
517517
case ISD::VECREDUCE_FMIN:
518518
case ISD::VECREDUCE_FMINIMUM:
519519
case ISD::VECREDUCE_FMUL:
520-
case ISD::CTTZ_ELTS:
521-
case ISD::CTTZ_ELTS_ZERO_POISON:
522520
case ISD::VECTOR_FIND_LAST_ACTIVE:
523521
Action = TLI.getOperationAction(Node->getOpcode(),
524522
Node->getOperand(0).getValueType());
@@ -1356,10 +1354,6 @@ void VectorLegalizer::Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
13561354
case ISD::VECTOR_COMPRESS:
13571355
Results.push_back(TLI.expandVECTOR_COMPRESS(Node, DAG));
13581356
return;
1359-
case ISD::CTTZ_ELTS:
1360-
case ISD::CTTZ_ELTS_ZERO_POISON:
1361-
Results.push_back(TLI.expandCttzElts(Node, DAG));
1362-
return;
13631357
case ISD::VECTOR_FIND_LAST_ACTIVE:
13641358
Results.push_back(TLI.expandVectorFindLastActive(Node, DAG));
13651359
return;

‎llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,10 +3742,6 @@ bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
37423742
case ISD::VP_REDUCE_FMINIMUM:
37433743
Res = SplitVecOp_VP_REDUCE(N, OpNo);
37443744
break;
3745-
case ISD::CTTZ_ELTS:
3746-
case ISD::CTTZ_ELTS_ZERO_POISON:
3747-
Res = SplitVecOp_CttzElts(N);
3748-
break;
37493745
case ISD::VP_CTTZ_ELTS:
37503746
case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
37513747
Res = SplitVecOp_VP_CttzElements(N);
@@ -4832,26 +4828,6 @@ SDValue DAGTypeLegalizer::SplitVecOp_FP_TO_XINT_SAT(SDNode *N) {
48324828
return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
48334829
}
48344830

4835-
SDValue DAGTypeLegalizer::SplitVecOp_CttzElts(SDNode *N) {
4836-
SDLoc DL(N);
4837-
EVT ResVT = N->getValueType(0);
4838-
4839-
SDValue Lo, Hi;
4840-
SDValue VecOp = N->getOperand(0);
4841-
GetSplitVector(VecOp, Lo, Hi);
4842-
4843-
// if CTTZ_ELTS(Lo) != VL => CTTZ_ELTS(Lo).
4844-
// else => VL + (CTTZ_ELTS(Hi) or CTTZ_ELTS_ZERO_POISON(Hi)).
4845-
SDValue ResLo = DAG.getNode(ISD::CTTZ_ELTS, DL, ResVT, Lo);
4846-
SDValue VL =
4847-
DAG.getElementCount(DL, ResVT, Lo.getValueType().getVectorElementCount());
4848-
SDValue ResLoNotVL =
4849-
DAG.getSetCC(DL, getSetCCResultType(ResVT), ResLo, VL, ISD::SETNE);
4850-
SDValue ResHi = DAG.getNode(N->getOpcode(), DL, ResVT, Hi);
4851-
return DAG.getSelect(DL, ResVT, ResLoNotVL, ResLo,
4852-
DAG.getNode(ISD::ADD, DL, ResVT, VL, ResHi));
4853-
}
4854-
48554831
SDValue DAGTypeLegalizer::SplitVecOp_VP_CttzElements(SDNode *N) {
48564832
SDLoc DL(N);
48574833
EVT ResVT = N->getValueType(0);

‎llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8324,13 +8324,55 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
83248324
return;
83258325
}
83268326
case Intrinsic::experimental_cttz_elts: {
8327+
auto DL = getCurSDLoc();
83278328
SDValue Op = getValue(I.getOperand(0));
8329+
EVT OpVT = Op.getValueType();
83288330
EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
83298331
bool ZeroIsPoison =
83308332
!cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8331-
SDValue Ret =
8332-
DAG.getNode(ZeroIsPoison ? ISD::CTTZ_ELTS_ZERO_POISON : ISD::CTTZ_ELTS,
8333-
sdl, RetTy, Op);
8333+
8334+
if (!TLI.shouldExpandCttzElements(OpVT)) {
8335+
SDValue Ret = DAG.getNode(ZeroIsPoison ? ISD::CTTZ_ELTS_ZERO_POISON
8336+
: ISD::CTTZ_ELTS,
8337+
sdl, RetTy, Op);
8338+
setValue(&I, Ret);
8339+
return;
8340+
}
8341+
8342+
if (OpVT.getScalarType() != MVT::i1) {
8343+
// Compare the input vector elements to zero & use to count trailing zeros
8344+
SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8345+
OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8346+
OpVT.getVectorElementCount());
8347+
Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8348+
}
8349+
8350+
// If the zero-is-poison flag is set, we can assume the upper limit
8351+
// of the result is VF-1.
8352+
ConstantRange VScaleRange(1, true); // Dummy value.
8353+
if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8354+
VScaleRange = getVScaleRange(I.getCaller(), 64);
8355+
unsigned EltWidth = TLI.getBitWidthForCttzElements(
8356+
I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8357+
8358+
MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8359+
8360+
// Create the new vector type & get the vector length
8361+
EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8362+
OpVT.getVectorElementCount());
8363+
8364+
SDValue VL =
8365+
DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8366+
8367+
SDValue StepVec = DAG.getStepVector(DL, NewVT);
8368+
SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8369+
SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8370+
SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8371+
SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8372+
SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8373+
SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8374+
8375+
SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
83348376

83358377
setValue(&I, Ret);
83368378
return;

‎llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp‎

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10078,7 +10078,8 @@ SDValue TargetLowering::expandVectorFindLastActive(SDNode *N,
1007810078
VScaleRange = getVScaleRange(&DAG.getMachineFunction().getFunction(), 64);
1007910079
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1008010080
uint64_t EltWidth = TLI.getBitWidthForCttzElements(
10081-
EVT(getVectorIdxTy(DAG.getDataLayout())), MaskVT.getVectorElementCount(),
10081+
EVT(getVectorIdxTy(DAG.getDataLayout())).getTypeForEVT(*DAG.getContext()),
10082+
MaskVT.getVectorElementCount(),
1008210083
/*ZeroIsPoison=*/true, &VScaleRange);
1008310084
// If the step vector element type is smaller than the mask element type,
1008410085
// use the mask type directly to avoid widening issues.
@@ -12540,64 +12541,6 @@ SDValue TargetLowering::expandVECTOR_COMPRESS(SDNode *Node,
1254012541
return DAG.getLoad(VecVT, DL, Chain, StackPtr, PtrInfo);
1254112542
}
1254212543

12543-
SDValue TargetLowering::expandCttzElts(SDNode *Node, SelectionDAG &DAG) const {
12544-
SDLoc DL(Node);
12545-
EVT VT = Node->getValueType(0);
12546-
SDValue Op = Node->getOperand(0);
12547-
EVT OpVT = Op.getValueType();
12548-
12549-
if (OpVT.getVectorElementType() != MVT::i1) {
12550-
// Compare the input vector elements to zero & use to count trailing zeros.
12551-
SDValue AllZero = DAG.getConstant(0, DL, OpVT);
12552-
EVT I1OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
12553-
OpVT.getVectorElementCount());
12554-
// If cttz_elts is legal for the i1 type, use it instead of expanding.
12555-
if (isOperationLegalOrCustom(Node->getOpcode(), I1OpVT)) {
12556-
Op = DAG.getSetCC(DL, I1OpVT, Op, AllZero, ISD::SETNE);
12557-
return DAG.getNode(Node->getOpcode(), DL, VT, Op);
12558-
}
12559-
12560-
Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
12561-
}
12562-
12563-
// If the zero-is-poison flag is set, we can assume the upper limit
12564-
// of the result is VF-1.
12565-
bool ZeroIsPoison = Node->getOpcode() == ISD::CTTZ_ELTS_ZERO_POISON;
12566-
ConstantRange VScaleRange(1, true); // Dummy value.
12567-
if (OpVT.isScalableVector())
12568-
VScaleRange = getVScaleRange(&DAG.getMachineFunction().getFunction(), 64);
12569-
unsigned EltWidth = getBitWidthForCttzElements(
12570-
VT, OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
12571-
12572-
EVT NewEltVT = MVT::getIntegerVT(EltWidth);
12573-
12574-
// Create the new vector type & get the vector length
12575-
EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltVT,
12576-
OpVT.getVectorElementCount());
12577-
12578-
// Promote types now to avoid redundant zexts.
12579-
if (getTypeAction(NewVT.getSimpleVT()) == TypePromoteInteger) {
12580-
NewVT = getTypeToTransformTo(*DAG.getContext(), NewVT);
12581-
NewEltVT = NewVT.getVectorElementType();
12582-
}
12583-
if (getTypeAction(NewEltVT.getSimpleVT()) == TypePromoteInteger)
12584-
NewEltVT = getTypeToTransformTo(*DAG.getContext(), NewEltVT);
12585-
12586-
SDValue VL = DAG.getElementCount(DL, NewEltVT, NewVT.getVectorElementCount());
12587-
12588-
SDValue StepVec = DAG.getStepVector(DL, NewVT);
12589-
SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
12590-
SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
12591-
SDValue Ext = DAG.getSExtOrTrunc(Op, DL, NewVT);
12592-
SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
12593-
SDValue Max =
12594-
DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewVT.getVectorElementType(), And);
12595-
SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltVT, VL,
12596-
DAG.getZExtOrTrunc(Max, DL, NewEltVT));
12597-
12598-
return DAG.getZExtOrTrunc(Sub, DL, VT);
12599-
}
12600-
1260112544
SDValue TargetLowering::expandPartialReduceMLA(SDNode *N,
1260212545
SelectionDAG &DAG) const {
1260312546
SDLoc DL(N);

‎llvm/lib/CodeGen/TargetLoweringBase.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
13471347
}
13481348

13491349
unsigned TargetLoweringBase::getBitWidthForCttzElements(
1350-
EVT RetVT, ElementCount EC, bool ZeroIsPoison,
1350+
Type *RetTy, ElementCount EC, bool ZeroIsPoison,
13511351
const ConstantRange *VScaleRange) const {
13521352
// Find the smallest "sensible" element type to use for the expansion.
13531353
ConstantRange CR(APInt(64, EC.getKnownMinValue()));
@@ -1357,7 +1357,7 @@ unsigned TargetLoweringBase::getBitWidthForCttzElements(
13571357
if (ZeroIsPoison)
13581358
CR = CR.subtract(APInt(64, 1));
13591359

1360-
unsigned EltWidth = RetVT.getScalarSizeInBits();
1360+
unsigned EltWidth = RetTy->getScalarSizeInBits();
13611361
EltWidth = std::min(EltWidth, CR.getActiveBits());
13621362
EltWidth = std::max(llvm::bit_ceil(EltWidth), (unsigned)8);
13631363

0 commit comments

Comments
 (0)