Skip to content

[AIEX][AIE2P] Several new combiners to simplify vector operations#772

Merged
andcarminati merged 16 commits intoaie-publicfrom
andreu.shift.combiner
Mar 25, 2026
Merged

[AIEX][AIE2P] Several new combiners to simplify vector operations#772
andcarminati merged 16 commits intoaie-publicfrom
andreu.shift.combiner

Conversation

@andcarminati
Copy link
Copy Markdown
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
Copy Markdown
Collaborator Author

andcarminati commented Feb 4, 2026

QoR:
image

Comment thread llvm/test/CodeGen/AIE/GlobalISel/combine-vshift-chain-to-zero-pad.mir Outdated
Comment thread llvm/lib/Target/AIE/AIECombinerHelper.cpp Outdated
Comment thread llvm/lib/Target/AIE/AIECombinerHelper.cpp Outdated
const uint64_t Mask = MaskVal->getZExtValue();
if (Mask != 0) {
const unsigned HighestBit = 63 - llvm::countl_zero(Mask);
MaxElement = std::max(MaxElement, HighestBit);
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@andcarminati andcarminati changed the title [AIEX][AIE2P] Vshift (Vshift) to CONCAT combiner [AIEX][AIE2P] Several new combiners to simplify vector operations Feb 11, 2026
/// %zero_broadcast = G_AIE_BROADCAST_VECTOR %zero_scalar
/// %zero_lo, %zero_hi = G_UNMERGE_VALUES %zero_broadcast
/// %result = G_CONCAT_VECTORS %src, %zero_hi
bool llvm::matchVShiftChainToZeroPad(MachineInstr &MI, MachineRegisterInfo &MRI,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

wouldn't it be more generic to first combine two vshifts into a single one and in the second stop combine shift to a unmerge?

// First shift: adds padding (quarter of source size)
const unsigned ExpectedShift1 = SrcSizeInBytes / 4;
// Second shift: brings in zeros (1.5x source size)
const unsigned ExpectedShift2 = (SrcSizeInBytes + SrcSizeInBytes / 2) / 2;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: I think it is sufficient to check the total shift size, not some predetermined shift amounts.

Copy link
Copy Markdown
Collaborator Author

@andcarminati andcarminati Feb 12, 2026

Choose a reason for hiding this comment

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

I am taking a look.

; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
;
; (c) Copyright 2025 Advanced Micro Devices, Inc. or its affiliates
; (c) Copyright 2025-2025 Advanced Micro Devices, Inc. or its affiliates
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: this line can remain unchanged for now

# RUN: -verify-machineinstrs -o - | FileCheck %s

# Tests for VSHIFT chain combiner that optimizes VSHIFT sequences into
# CONCAT operations with usage-aware padding (UNDEF vs zeros).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: also add a unit-test with different shift constants

@andcarminati andcarminati force-pushed the andreu.shift.combiner branch 2 times, most recently from e4f21e8 to 0619d47 Compare February 21, 2026 12:00
@andcarminati andcarminati force-pushed the andreu.shift.combiner branch from e9cecaa to 681e6e2 Compare March 10, 2026 14:59
@andcarminati andcarminati force-pushed the andreu.shift.combiner branch from 681e6e2 to 686e173 Compare March 23, 2026 09:55
@@ -419,6 +419,96 @@ bool isUseOf(const MachineInstr &MI, const MachineInstr &Def) {
return false;
}

bool llvm::matchUnpadUnmerge(MachineInstr &UnpadMI, MachineRegisterInfo &MRI,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can I have a short description of what we're trying to match?

}

// 4. Check dominance (one must dominate the other)
const bool UnpadDominates = Helper.dominates(UnpadMI, *UnmergeMI);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I suggest to setup a dedicated transform lambda and return if it dominates. Otherwise, check the reverse dominance and setup the reverse transform lambda. We don't need to check things we don't need, and we don't need to capture unnecessary data in the lambda.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I will keep as is this part.

// 3 elements, then 8 % 3 = 2 (NOT aligned), and we can't
// cleanly extract 8 elements using 3-element chunks.
if (UnpadElements % ConcatOpElements != 0)
return false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we have automatic guarantees about matching element types?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I guess yes, but I prefer to be defensive here.


if (OpMI->getOpcode() == TargetOpcode::G_CONCAT_VECTORS) {
// Nested CONCAT - flatten by extracting all its operands
for (unsigned J = 1; J < OpMI->getNumOperands(); ++J) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: I think we could match recursively, but there's probably very little return on investment.

const unsigned PaddingSubVecs = NumSubVecs - 1;

for (unsigned K = 0; K < PaddingSubVecs; ++K) {
FlattenedOps.push_back(
Copy link
Copy Markdown
Collaborator

@martien-de-jong martien-de-jong Mar 24, 2026

Choose a reason for hiding this comment

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

FlattenedOps.resize(size+PaddingSubVecs?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I prefer to keep it explicitly to self document what we are doing here.

// Verify all valid operands in FlattenedOps have the same type
// and ensure we have at least one valid operand
LLT SubVecTy;
bool FoundValidOp = false;
Copy link
Copy Markdown
Collaborator

@martien-de-jong martien-de-jong Mar 24, 2026

Choose a reason for hiding this comment

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

optional<LLT> ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Nice suggestion.

AllMatch = false;
break;
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you move the opcode check down, you could bundle this in a nice generic isEquivalentMI(I1, I2) helper.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Collaborator

@martien-de-jong martien-de-jong left a comment

Choose a reason for hiding this comment

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

I'm getting into real nitpicking territory. I see nothing really blocking, it's time it went in.

@andcarminati andcarminati force-pushed the andreu.shift.combiner branch from 15ee34d to 4dad714 Compare March 25, 2026 08:20
@andcarminati andcarminati merged commit b02daa9 into aie-public Mar 25, 2026
7 checks passed
@andcarminati andcarminati deleted the andreu.shift.combiner branch March 25, 2026 09:36
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.

3 participants