Skip to content

Conversation

@andcarminati
Copy link
Collaborator

@andcarminati andcarminati commented Feb 2, 2026

The combiner eliminates a VSHIFT chain and replaces it with a simpler G_CONCAT_VECTORS operation with zero-padded upper half. The transformation is generalized to work with any vector size by calculating expected shift amounts based on source and destination types (e.g., for 512 bit expansion: shift1=16 bytes, shift2=48 bytes). The combiner includes usage-aware optimization: it analyzes how the result vector is actually used (via a new getMaxUsedVectorElement utility) and chooses between padding with UNDEF or zeros.

This PR also includes two redundant pad/unpad removal combiners.

@andcarminati
Copy link
Collaborator Author

QoR:
image
image
image

if (Opcode == TII.getGenericVSelOpcode()) {
const Register MaskReg = User.getOperand(3).getReg();
const auto MaskVal = getIConstantVRegVal(MaskReg, MRI);
if (MaskVal) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can reduce nesting if we directly reutn std::nullopt if !MaskVal

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, definitely.

const unsigned Opcode = User.getOpcode();

// VSEL: Check which elements are selected via the mask
if (Opcode == TII.getGenericVSelOpcode()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to move these big if conditions into lambdas, so that the high level flow of the function is clearer

const uint64_t Mask = MaskVal->getZExtValue();
if (Mask != 0) {
const unsigned HighestBit = 63 - llvm::countl_zero(Mask);
MaxElement = std::max(MaxElement, HighestBit);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we expect multiple vsels in the register users?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We look to all the users, it is too restrictive to assume just one user.

@andcarminati andcarminati force-pushed the andreu.shift.combiner branch from 2fbd949 to 0780d74 Compare February 5, 2026 14:57
const Register IdxReg = User.getOperand(2).getReg();
const auto Idx = getIConstantVRegVal(IdxReg, MRI);
if (Idx) {
MaxElement =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we return here? why should we have multiple G_EXTRACT_VECTOR_ELT s coupled with G_UNMERGE_VALUES and return the maximum? aren't we then confusing combine patterns?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see the name of the function, this is just a helper function that we use to try prove that the upper part of a vector is not used, so we cannot just return max here because we may have other users accessing even higher elements. But, maybe you are talking about an early return in case if !Idx - this I agree, it will be part of the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants