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

[AIE2P] Legalize G_UNMERGE_VALUES 256-bit vector to 2 128-bit vectors #282

Merged
merged 2 commits into from
Jan 21, 2025

Conversation

niwinanto
Copy link
Collaborator

@niwinanto niwinanto commented Jan 17, 2025

This PR introduces G_AIE_VSHIFT_RIGHT gMIR opcode which is selected to vshift instruction. And this is utilized to legalize G_UNMERGE_VALUES 256-bit vector to 2 128-bit vector.

@@ -551,8 +551,10 @@ AIE2PLegalizerInfo::AIE2PLegalizerInfo(const AIE2PSubtarget &ST)
const LLT &DstTy = Query.Types[0];
const LLT &SrcTy = Query.Types[1];

return SrcTy.isVector() && DstTy.isScalar() &&
DstTy == SrcTy.getElementType();
return (DstTy.isVector() && SrcTy.getSizeInBits() == 256 &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a comment to explain which cases this handles? (for both the existing and new case)

@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch from 8df98bb to a2d74e1 Compare January 20, 2025 08:37
@@ -231,6 +231,57 @@ bool AIELegalizerHelper::legalizeG_BUILD_VECTOR(LegalizerHelper &Helper,
return true;
}

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 have a description of the input->output of this custom legalization rule?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.


// Concatenate the src1 and src2 vectors, shift right
// and extract the resulting lower 512-bit vector
def G_AIE_VSHIFT : AIEGenericInstruction {
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 expecting "right" to be visible in the opcode itself. If I see this opcode there is no way to discover that it does right shift without looking to the comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

const AIEBaseInstrInfo *II = ST.getInstrInfo();
const unsigned UnpadOpc = II->getGenericUnpadVectorOpcode();
const Register SrcReg = MI.getOperand(MI.getNumOperands() - 1).getReg();
const Register Dst1Reg = MI.getOperand(0).getReg();
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe DstRegLow and DstRegHigh to improve readability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

MIRBuilder.buildInstr(TargetOpcode::G_IMPLICIT_DEF, {SrcTy}, {})
.getReg(0);

const LLT Vec512 =
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: const LLT Vec512 = SrcTy.multiplyElements(2);


// VSHIFT operates on 512-bit inputs. We need to pad the 256-bit source
// operand to 512-bit
const Register ImplicitDef256 =
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: const Register ImplicitDef256 = MIRBuilder.buildUndef(SrcTy).getReg(0);

MIRBuilder.buildConcatVectors({Vec512}, {SrcReg, ImplicitDef256});

// The second input will be ignored. Just create a dummy input
auto ImplicitDef512 =
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: auto ImplicitDef512 = MIRBuilder.buildUndef(Vec512);

@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch from a2d74e1 to fa09112 Compare January 21, 2025 07:38
@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch from fa09112 to 9f4905e Compare January 21, 2025 08:26
// to
// %1:_(<4 x s32>) = G_AIE_UNPAD_VECTOR %0(<8 x s32>)
// %3:_(s32) = G_CONSTANT i32 16
// %4:_(<16 x s32>) = G_CONCAT_VECTORS %0(<8 x s32>), %0(<8 x s32>)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess that %0 is G_IMPLICIT_DEF, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that was not important. That`s why I left it.

const LLT Vec512 = SrcTy.multiplyElements(2);

// Create the first 512-bit vector input
auto MergeValue =
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you don't mind, I think ConcatValue sounds better here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

const Register DstRegLow = MI.getOperand(0).getReg();
const Register DstRegHigh = MI.getOperand(1).getReg();
const LLT SrcTy = MRI.getType(SrcReg);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can assert that SrcTy is a vector type to avoid misuse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added assert for destination register, it ensure src to be vector as well.

@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch 2 times, most recently from 83a1eff to ea7999a Compare January 21, 2025 09:38
@@ -68,6 +68,7 @@ class AIELegalizerHelper {
Register SourceReg) const;
bool unpack32BitVector(LegalizerHelper &Helper, MachineInstr &MI,
Register SourceReg) const;
bool legalize_unmerge_128bit(LegalizerHelper &Helper, MachineInstr &MI) const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: what about the standard naming convention legalizeUnmergeTo128bit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, other function names in the file follow this, legalizeG_SEXT_INREG, legalizeG_VASTART ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, legalizeG_UNMERGE... would fit in this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch from ea7999a to c86cb0a Compare January 21, 2025 12:29
const Register DstRegHigh = MI.getOperand(1).getReg();
const LLT SrcTy = MRI.getType(SrcReg);

assert(MRI.getType(DstRegLow).isVector() && "Expected vector restor");
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: restor is register?

@@ -241,6 +299,11 @@ bool AIELegalizerHelper::legalizeG_UNMERGE_VALUES(LegalizerHelper &Helper,
const Register LastReg = MI.getOperand(MI.getNumOperands() - 1).getReg();
const LLT FirstTy = MRI.getType(FirstReg);
const LLT LastTy = MRI.getType(LastReg);

if (ST.isAIE2P() && FirstTy.getSizeInBits() == 128 &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe check for vector types here?

@niwinanto niwinanto force-pushed the niwin.vshift.unmerge branch from c86cb0a to d6e46e6 Compare January 21, 2025 13:52
Copy link
Collaborator

@andcarminati andcarminati left a comment

Choose a reason for hiding this comment

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

LGTM.

@niwinanto niwinanto merged commit 7108f7f into aie-public Jan 21, 2025
8 checks passed
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