Skip to content

[CodeGen][GlobalISel] Add a getVectorIdxWidth and getVectorIdxLLT. #131526

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
Mar 18, 2025
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
7 changes: 3 additions & 4 deletions llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1375,10 +1375,9 @@ class MachineIRBuilder {
MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res,
const SrcOp &Val,
const int Idx) {
auto TLI = getMF().getSubtarget().getTargetLowering();
unsigned VecIdxWidth = TLI->getVectorIdxTy(getDataLayout()).getSizeInBits();
return buildExtractVectorElement(
Res, Val, buildConstant(LLT::scalar(VecIdxWidth), Idx));
const TargetLowering *TLI = getMF().getSubtarget().getTargetLowering();
LLT IdxTy = TLI->getVectorIdxLLT(getDataLayout());
return buildExtractVectorElement(Res, Val, buildConstant(IdxTy, Idx));
}

/// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
Expand Down
18 changes: 16 additions & 2 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,25 @@ class TargetLoweringBase {
return ShiftValueTy;
}

/// Returns the type to be used for the index operand vector operations. By
/// default we assume it will have the same size as an address space 0
/// pointer.
virtual unsigned getVectorIdxWidth(const DataLayout &DL) const {
return DL.getPointerSizeInBits(0);
}

/// Returns the type to be used for the index operand of:
/// ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT,
/// ISD::INSERT_SUBVECTOR, and ISD::EXTRACT_SUBVECTOR
virtual MVT getVectorIdxTy(const DataLayout &DL) const {
return getPointerTy(DL);
MVT getVectorIdxTy(const DataLayout &DL) const {
return MVT::getIntegerVT(getVectorIdxWidth(DL));
}

/// Returns the type to be used for the index operand of:
/// G_INSERT_VECTOR_ELT, G_EXTRACT_VECTOR_ELT,
/// G_INSERT_SUBVECTOR, and G_EXTRACT_SUBVECTOR
LLT getVectorIdxLLT(const DataLayout &DL) const {
return LLT::scalar(getVectorIdxWidth(DL));
}

/// Returns the type to be used for the EVL/AVL operand of VP nodes:
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3174,7 +3174,7 @@ bool IRTranslator::translateInsertElement(const User &U,
Register Res = getOrCreateVReg(U);
Register Val = getOrCreateVReg(*U.getOperand(0));
Register Elt = getOrCreateVReg(*U.getOperand(1));
unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
Register Idx;
if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(2))) {
if (CI->getBitWidth() != PreferredVecIdxWidth) {
Expand All @@ -3200,7 +3200,7 @@ bool IRTranslator::translateInsertVector(const User &U,
Register Elt = getOrCreateVReg(*U.getOperand(1));

ConstantInt *CI = cast<ConstantInt>(U.getOperand(2));
unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);

// Resize Index to preferred index width.
if (CI->getBitWidth() != PreferredVecIdxWidth) {
Expand Down Expand Up @@ -3255,7 +3255,7 @@ bool IRTranslator::translateExtractElement(const User &U,

Register Res = getOrCreateVReg(U);
Register Val = getOrCreateVReg(*U.getOperand(0));
unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
Register Idx;
if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(1))) {
if (CI->getBitWidth() != PreferredVecIdxWidth) {
Expand All @@ -3279,7 +3279,7 @@ bool IRTranslator::translateExtractVector(const User &U,
Register Res = getOrCreateVReg(U);
Register Vec = getOrCreateVReg(*U.getOperand(0));
ConstantInt *CI = cast<ConstantInt>(U.getOperand(1));
unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);

// Resize Index to preferred index width.
if (CI->getBitWidth() != PreferredVecIdxWidth) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4229,7 +4229,7 @@ LegalizerHelper::scalarizeVectorBooleanStore(GStore &StoreMI) {
unsigned NumBits = MemTy.getSizeInBits();
LLT IntTy = LLT::scalar(NumBits);
auto CurrVal = MIRBuilder.buildConstant(IntTy, 0);
LLT IdxTy = getLLTForMVT(TLI.getVectorIdxTy(MF.getDataLayout()));
LLT IdxTy = TLI.getVectorIdxLLT(MF.getDataLayout());

for (unsigned I = 0, E = MemTy.getNumElements(); I < E; ++I) {
auto Elt = MIRBuilder.buildExtractVectorElement(
Expand Down Expand Up @@ -6277,7 +6277,7 @@ LegalizerHelper::moreElementsVector(MachineInstr &MI, unsigned TypeIdx,
auto NeutralElement = getNeutralElementForVecReduce(
MI.getOpcode(), MIRBuilder, MoreTy.getElementType());

LLT IdxTy(TLI.getVectorIdxTy(MIRBuilder.getDataLayout()));
LLT IdxTy(TLI.getVectorIdxLLT(MIRBuilder.getDataLayout()));
for (size_t i = OrigTy.getNumElements(), e = MoreTy.getNumElements();
i != e; i++) {
auto Idx = MIRBuilder.buildConstant(IdxTy, i);
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/CodeGen/MachineVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1993,8 +1993,7 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
}

auto TLI = MF->getSubtarget().getTargetLowering();
if (IdxTy.getSizeInBits() !=
TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) {
if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
report("Index type must match VectorIdxTy", MI);
break;
}
Expand Down Expand Up @@ -2023,8 +2022,7 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
}

auto TLI = MF->getSubtarget().getTargetLowering();
if (IdxTy.getSizeInBits() !=
TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) {
if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
report("Index type must match VectorIdxTy", MI);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7548,7 +7548,7 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
N1VT.getVectorMinNumElements()) &&
"Extract subvector overflow!");
assert(N2C->getAPIntValue().getBitWidth() ==
TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
TLI->getVectorIdxWidth(getDataLayout()) &&
"Constant index for EXTRACT_SUBVECTOR has an invalid size");

// Trivial extraction.
Expand Down Expand Up @@ -7782,7 +7782,7 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
VT.getVectorMinNumElements()) &&
"Insert subvector overflow!");
assert(N3->getAsAPIntVal().getBitWidth() ==
TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
TLI->getVectorIdxWidth(getDataLayout()) &&
"Constant index for INSERT_SUBVECTOR has an invalid size");

// Trivial insertion.
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ class AArch64TargetLowering : public TargetLowering {
}
}

unsigned getVectorIdxWidth(const DataLayout &DL) const override {
// The VectorIdx type is i64, with both normal and ilp32.
return 64;
}

bool targetShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits,
const APInt &DemandedElts,
TargetLoweringOpt &TLO) const override;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,8 @@ EVT AMDGPUTargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT,
return EVT::getIntegerVT(Context, 32 * ((Size + 31) / 32));
}

MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
return MVT::i32;
unsigned AMDGPUTargetLowering::getVectorIdxWidth(const DataLayout &) const {
return 32;
}

bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class AMDGPUTargetLowering : public TargetLowering {
EVT getTypeForExtReturn(LLVMContext &Context, EVT VT,
ISD::NodeType ExtendKind) const override;

MVT getVectorIdxTy(const DataLayout &) const override;
unsigned getVectorIdxWidth(const DataLayout &) const override;
bool isSelectSupported(SelectSupportKind) const override;

bool isFPImmLegal(const APFloat &Imm, EVT VT,
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/SPIRV/SPIRVISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class SPIRVTargetLowering : public TargetLowering {

// This is to prevent sexts of non-i64 vector indices which are generated
// within general IRTranslator hence type generation for it is omitted.
MVT getVectorIdxTy(const DataLayout &DL) const override {
return MVT::getIntegerVT(32);
}
unsigned getVectorIdxWidth(const DataLayout &DL) const override { return 32; }
unsigned getNumRegistersForCallingConv(LLVMContext &Context,
CallingConv::ID CC,
EVT VT) const override;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/SystemZ/SystemZISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ class SystemZTargetLowering : public TargetLowering {
MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override {
return MVT::i32;
}
MVT getVectorIdxTy(const DataLayout &DL) const override {
unsigned getVectorIdxWidth(const DataLayout &DL) const override {
// Only the lower 12 bits of an element index are used, so we don't
// want to clobber the upper 32 bits of a GPR unnecessarily.
return MVT::i32;
return 32;
}
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT)
const override {
Expand Down