Skip to content

[AIEX] Concat/Unmerge PHI Combiner #500

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

Open
wants to merge 2 commits into
base: aie-public
Choose a base branch
from

Conversation

F-Stuckmann
Copy link
Collaborator

Added a Concat/Unmerge PHI Combiner.

QoR does not show any results, because the kernels, that use this pattern are currently failing.

However, in ReduceL2C8Axis_1_aie2_bf16 the II can be reduced from 9 to 8.

combine_add_vector_elt_undef ]> {
combine_add_vector_elt_undef,
combine_concat_unmerge_phis
]> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we using post-legalizer and not pre-legalizer? We are touching types here, so we can generate not legal types as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the only instruction we generate is a concat in a dominating MBB, that we know is already legal.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We also generate a phi, whose legality could be not the same as concat. This type of optimization always fits better in pre-legalization.

/// used [ 0 (7, 9) or 1 (8, 10)]. Start the pattern checking from \p ConcatI .
/// Follow \p UseIdx (0 or 1) of G_CONCAT to identify the pattern. Collect all
/// relevant Results in \p MatchData .
static std::pair<MachineInstr *, unsigned>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we embed this return info into AIEConcatUnmergeCombineMatchData? In this way we can concentrate all information together.

const Register Reg = ConcatI.getOperand(OperandIdx).getReg();
auto *PhiInst = MRI.getVRegDef(Reg);
if (!PhiInst || !PhiInst->isPHI())
return {nullptr, /*UnmergeSubVecDefIdx=*/0};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use optional here?

auto [UnMergeInst, UnMergeDefMO] = FindPhiUnMergeComponents(PhiInst);

if (!UnMergeInst ||
UnMergeInst->getOpcode() != TargetOpcode::G_UNMERGE_VALUES ||
Copy link
Collaborator

@andcarminati andcarminati Jun 24, 2025

Choose a reason for hiding this comment

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

Unmerge can have more then 2 defs, we should check this too.

<< "\n");

auto UnMergeOrigin0 =
findUnmergeOrigin(ConcatI, /*UseIdx=*/0, MRI, Helper, MatchInfo);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking if we can find indexes 0 and 1 in a single sweep, storing relevant data in MatchInfo.


if (MatchData.ConcatMBB && MatchData.ConcatMBB != MBB.getMBB()) {
LLVM_DEBUG(dbgs()
<< "Both Parts of the Incoming Concat Values do not "
Copy link
Collaborator

Choose a reason for hiding this comment

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

Future concat?


// If Unmerge Origins diverge, skip
if (UnMergeOrigin0.first != UnMergeOrigin1.first)
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

CHECK: AFAIK, G_CONCAT can also take more than two operands

@@ -30,6 +30,23 @@ struct FrequentIndexResult {
unsigned NonMatchingCount;
};

struct AIEConcatUnmergeCombineMatchData {
// New Concat Instruction
Copy link
Collaborator

Choose a reason for hiding this comment

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

Concat instruction's MBB and rewrap?

<< " created " << *NewConcat.getInstr());

// set Insertion Point to top of phi-MBB
B.setInsertPt(*ConcatI.getParent(), ConcatI.getParent()->begin());
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: funny that we use three different methods to set the insertion point. I guess the earlier ones are specializations of this one?

// Concat Components
MatchData.ConcatMBB = MBB.getMBB();
if (UseIdx == 0)
MatchData.ConcatSub0 = IncomingMO;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks as if ConcatSub could be an array of two elements

return false;

// If Concat behaves like a vector broadcast, i.e. Concat uses the same
// Sub-vector in both source operands, skip
Copy link
Collaborator

@martien-de-jong martien-de-jong Jun 25, 2025

Choose a reason for hiding this comment

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

The (local) essence is that we only have one phi node, so the pattern doesn't match. If we want to leave this pattern to be canonicalized to broadcast and then be further optimized, I would say that we widen the combine to handle all CONCAT patterns and rewrite to broadcast here.
Alternatively, we recognize that rewriting CONCAT to broadcast should have a higher priority and we shouldn't expect occurrences here.

AIEConcatUnmergeCombineMatchData &MatchData) {
auto *FirstMI = &*ConcatI.getParent()->begin();

auto FindPhiUnMergeComponents = [UseIdx, &FirstMI, &Helper, &MRI,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense to specify the return type explicitly. Note that it also saves typing the dummy return value's type.

findUnmergeOrigin(MachineInstr &ConcatI, unsigned UseIdx,
MachineRegisterInfo &MRI, CombinerHelper &Helper,
AIEConcatUnmergeCombineMatchData &MatchData) {
auto *FirstMI = &*ConcatI.getParent()->begin();
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: avoid use of "auto" they are definitely useful for coding, but decreases readability of code significantly.

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.

4 participants