Skip to content

Commit bb671db

Browse files
committed
Address review comments
1 parent 78b8c7f commit bb671db

File tree

6 files changed

+456
-683
lines changed

6 files changed

+456
-683
lines changed

llvm/docs/LangRef.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20002,18 +20002,15 @@ the follow sequence of operations:
2000220002

2000320003
The ``mask`` operand will apply to at least the gather and scatter operations.
2000420004

20005-
'``llvm.experimental.vector.masked.extract.last.active``' Intrinsic
20006-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20005+
'``llvm.experimental.vector.extract.last.active``' Intrinsic
20006+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2000720007

2000820008
This is an overloaded intrinsic.
2000920009

20010-
This intrinsic will extract the value from a single lane of a vector, based
20011-
on a supplied mask vector.
20012-
2001320010
::
2001420011

20015-
declare i32 @llvm.experimental.vector.masked.extract.last.active.v4i32(<4 x i32> %data, <4 x i1> %mask, i32 %passthru)
20016-
declare i16 @llvm.experimental.vector.masked.extract.last.active.nxv8i16(<vscale x 8 x i16> %data, <vscale x 8 x i1> %mask, i16 %passthru)
20012+
declare i32 @llvm.experimental.vector.extract.last.active.v4i32(<4 x i32> %data, <4 x i1> %mask, i32 %passthru)
20013+
declare i16 @llvm.experimental.vector.extract.last.active.nxv8i16(<vscale x 8 x i16> %data, <vscale x 8 x i1> %mask, i16 %passthru)
2001720014

2001820015
Arguments:
2001920016
""""""""""
@@ -20028,10 +20025,10 @@ the passthru value must match that of the elements of the data vector.
2002820025
Semantics:
2002920026
""""""""""
2003020027

20031-
The '``llvm.experimental.vector.masked.extract.last.active``' intrinsic will
20032-
find the index of the most significant active lane in the mask vector, and
20033-
extract the element at that index in the corresponding data vector. If no mask
20034-
lanes are active then the passthru value is returned instead.
20028+
The '``llvm.experimental.vector.extract.last.active``' intrinsic will extract an
20029+
element from the data vector at the index matching the highest active lane of
20030+
the mask vector. If no mask lanes are active then the passthru value is
20031+
returned instead.
2003520032

2003620033
.. _int_vector_compress:
2003720034

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ def int_experimental_vector_histogram_add : DefaultAttrsIntrinsic<[],
19211921
[ IntrArgMemOnly ]>;
19221922

19231923
// Extract based on mask bits
1924-
def int_experimental_vector_masked_extract_last_active:
1924+
def int_experimental_vector_extract_last_active:
19251925
DefaultAttrsIntrinsic<[LLVMVectorElementType<0>],
19261926
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
19271927
LLVMVectorElementType<0>], [IntrNoMem]>;

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8207,24 +8207,40 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
82078207
visitVectorHistogram(I, Intrinsic);
82088208
return;
82098209
}
8210-
case Intrinsic::experimental_vector_masked_extract_last_active: {
8210+
case Intrinsic::experimental_vector_extract_last_active: {
82118211
SDValue Data = getValue(I.getOperand(0));
82128212
SDValue Mask = getValue(I.getOperand(1));
82138213
SDValue PassThru = getValue(I.getOperand(2));
82148214

82158215
EVT DataVT = Data.getValueType();
82168216
EVT ScalarVT = PassThru.getValueType();
82178217
EVT BoolVT = Mask.getValueType().getScalarType();
8218-
EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
8219-
EVT IdxVecVT = DataVT.changeVectorElementType(IdxVT);
82208218

8221-
SDValue Zeroes = DAG.getConstant(0, sdl, IdxVecVT);
8222-
SDValue StepVec = DAG.getStepVector(sdl, IdxVecVT);
8223-
SDValue ActiveElts = DAG.getSelect(sdl, IdxVecVT, Mask, StepVec, Zeroes);
8219+
// Find a suitable type for a stepvector.
8220+
ConstantRange VScaleRange(1, /*isFullSet=*/true); // Dummy value.
8221+
if (DataVT.isScalableVector())
8222+
VScaleRange = getVScaleRange(I.getCaller(), 64);
8223+
unsigned EltWidth = TLI.getBitWidthForCttzElements(
8224+
I.getType(), DataVT.getVectorElementCount(), /*ZeroIsPoison=*/true,
8225+
&VScaleRange);
8226+
MVT StepVT = MVT::getIntegerVT(EltWidth);
8227+
EVT StepVecVT = DataVT.changeVectorElementType(StepVT);
8228+
8229+
// Zero out lanes with inactive elements, then find the highest remaining
8230+
// value from the stepvector.
8231+
SDValue Zeroes = DAG.getConstant(0, sdl, StepVecVT);
8232+
SDValue StepVec = DAG.getStepVector(sdl, StepVecVT);
8233+
SDValue ActiveElts = DAG.getSelect(sdl, StepVecVT, Mask, StepVec, Zeroes);
82248234
SDValue HighestIdx =
8225-
DAG.getNode(ISD::VECREDUCE_UMAX, sdl, IdxVT, ActiveElts);
8235+
DAG.getNode(ISD::VECREDUCE_UMAX, sdl, StepVT, ActiveElts);
8236+
8237+
// Extract the corresponding lane from the data vector
8238+
EVT ExtVT = TLI.getVectorIdxTy(DAG.getDataLayout());
8239+
SDValue Idx = DAG.getZExtOrTrunc(HighestIdx, sdl, ExtVT);
82268240
SDValue Extract =
8227-
DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ScalarVT, Data, HighestIdx);
8241+
DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ScalarVT, Data, Idx);
8242+
8243+
// If all mask lanes were inactive, choose the passthru value instead.
82288244
SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
82298245
SDValue Result = DAG.getSelect(sdl, ScalarVT, AnyActive, Extract, PassThru);
82308246
setValue(&I, Result);

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
11191119
if (Name.consume_front("experimental.vector.")) {
11201120
Intrinsic::ID ID =
11211121
StringSwitch<Intrinsic::ID>(Name)
1122+
// Skip over extract.last.active, otherwise it will be 'upgraded'
1123+
// to a regular vector extract which is a different operation.
1124+
.StartsWith("extract.last.active.", Intrinsic::not_intrinsic)
11221125
.StartsWith("extract.", Intrinsic::vector_extract)
11231126
.StartsWith("insert.", Intrinsic::vector_insert)
11241127
.StartsWith("splice.", Intrinsic::vector_splice)

0 commit comments

Comments
 (0)