Skip to content
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

[AArch64] Refactor creation of a shuffle mask for TBL (NFC) #92529

Merged
merged 3 commits into from
Jun 17, 2024
Merged
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
84 changes: 47 additions & 37 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15824,48 +15824,49 @@ bool AArch64TargetLowering::shouldSinkOperands(
return false;
}

static bool createTblShuffleForZExt(ZExtInst *ZExt, FixedVectorType *DstTy,
bool IsLittleEndian) {
Value *Op = ZExt->getOperand(0);
auto *SrcTy = cast<FixedVectorType>(Op->getType());
auto SrcWidth = cast<IntegerType>(SrcTy->getElementType())->getBitWidth();
auto DstWidth = cast<IntegerType>(DstTy->getElementType())->getBitWidth();
static bool createTblShuffleMask(unsigned SrcWidth, unsigned DstWidth,
unsigned NumElts, bool IsLittleEndian,
SmallVectorImpl<int> &Mask) {
if (DstWidth % 8 != 0 || DstWidth <= 16 || DstWidth >= 64)
return false;

assert(DstWidth % SrcWidth == 0 &&
"TBL lowering is not supported for a ZExt instruction with this "
"source & destination element type.");
unsigned ZExtFactor = DstWidth / SrcWidth;
"TBL lowering is not supported for a conversion instruction with this "
"source and destination element type.");

unsigned Factor = DstWidth / SrcWidth;
unsigned MaskLen = NumElts * Factor;

Mask.clear();
Mask.resize(MaskLen, NumElts);

unsigned SrcIndex = 0;
for (unsigned I = IsLittleEndian ? 0 : Factor - 1; I < MaskLen; I += Factor)
Mask[I] = SrcIndex++;

return true;
}

static Value *createTblShuffleForZExt(IRBuilderBase &Builder, Value *Op,
FixedVectorType *ZExtTy,
FixedVectorType *DstTy,
bool IsLittleEndian) {
auto *SrcTy = cast<FixedVectorType>(Op->getType());
unsigned NumElts = SrcTy->getNumElements();
IRBuilder<> Builder(ZExt);
auto SrcWidth = cast<IntegerType>(SrcTy->getElementType())->getBitWidth();
auto DstWidth = cast<IntegerType>(DstTy->getElementType())->getBitWidth();

SmallVector<int> Mask;
// Create a mask that selects <0,...,Op[i]> for each lane of the destination
// vector to replace the original ZExt. This can later be lowered to a set of
// tbl instructions.
for (unsigned i = 0; i < NumElts * ZExtFactor; i++) {
if (IsLittleEndian) {
if (i % ZExtFactor == 0)
Mask.push_back(i / ZExtFactor);
else
Mask.push_back(NumElts);
} else {
if ((i + 1) % ZExtFactor == 0)
Mask.push_back((i - ZExtFactor + 1) / ZExtFactor);
else
Mask.push_back(NumElts);
}
}
if (!createTblShuffleMask(SrcWidth, DstWidth, NumElts, IsLittleEndian, Mask))
return nullptr;

auto *FirstEltZero = Builder.CreateInsertElement(
PoisonValue::get(SrcTy), Builder.getInt8(0), uint64_t(0));
Value *Result = Builder.CreateShuffleVector(Op, FirstEltZero, Mask);
Result = Builder.CreateBitCast(Result, DstTy);
if (DstTy != ZExt->getType())
Result = Builder.CreateZExt(Result, ZExt->getType());
ZExt->replaceAllUsesWith(Result);
ZExt->eraseFromParent();
return true;
if (DstTy != ZExtTy)
Result = Builder.CreateZExt(Result, ZExtTy);
return Result;
}

static void createTblForTrunc(TruncInst *TI, bool IsLittleEndian) {
Expand Down Expand Up @@ -16030,21 +16031,30 @@ bool AArch64TargetLowering::optimizeExtendOrTruncateConversion(

DstTy = TruncDstType;
}

return createTblShuffleForZExt(ZExt, DstTy, Subtarget->isLittleEndian());
IRBuilder<> Builder(ZExt);
Value *Result = createTblShuffleForZExt(
Builder, ZExt->getOperand(0), cast<FixedVectorType>(ZExt->getType()),
DstTy, Subtarget->isLittleEndian());
if (!Result)
return false;
ZExt->replaceAllUsesWith(Result);
ZExt->eraseFromParent();
return true;
}

auto *UIToFP = dyn_cast<UIToFPInst>(I);
if (UIToFP && SrcTy->getElementType()->isIntegerTy(8) &&
DstTy->getElementType()->isFloatTy()) {
IRBuilder<> Builder(I);
auto *ZExt = cast<ZExtInst>(
Builder.CreateZExt(I->getOperand(0), VectorType::getInteger(DstTy)));
Value *ZExt = createTblShuffleForZExt(
Builder, I->getOperand(0), FixedVectorType::getInteger(DstTy),
FixedVectorType::getInteger(DstTy), Subtarget->isLittleEndian());
if (!ZExt)
return false;
auto *UI = Builder.CreateUIToFP(ZExt, DstTy);
I->replaceAllUsesWith(UI);
I->eraseFromParent();
return createTblShuffleForZExt(ZExt, cast<FixedVectorType>(ZExt->getType()),
Subtarget->isLittleEndian());
return true;
}

// Convert 'fptoui <(8|16) x float> to <(8|16) x i8>' to a wide fptoui
Expand Down
Loading