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

Support for allowing direct VEXTRACT to 20-bit registers #233

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

Conversation

abhinay-anubola
Copy link
Collaborator

@abhinay-anubola abhinay-anubola commented Nov 8, 2024

  • This update introduces a new generic combiner that simplifies the sequence sext(trunc x) directly to x when applicable.
  • Added VExtract combiner that enables above generic combiner, thus we have 20-bit vextract.
  • The MachineVerifier has been updated to allow G_AIE_SEXT_EXTRACT_VECTOR_ELT and G_AIE_ZEXT_EXTRACT_VECTOR_ELT to accept 20-bit outputs.
  • Additionally, tests have been added and updated to reflect these functional changes.

@krishnamtibrewala
Copy link
Collaborator

Given that you mentioned there are no QoR gain, I would recommend you to re look at the instruction that consume S20 type reg.
Because for the optimization starts to trace back from an instruction that consumes S20 type which might not be captured in isNativeS20Consumer function.

@abhinay-anubola abhinay-anubola force-pushed the sanubola.support.20bit.VEXTRACT branch from 650d8a9 to d5d7cf0 Compare November 12, 2024 11:21
@abhinay-anubola abhinay-anubola force-pushed the sanubola.support.20bit.VEXTRACT branch from d5d7cf0 to f12f1a4 Compare November 14, 2024 09:13
@abhinay-anubola abhinay-anubola force-pushed the sanubola.support.20bit.VEXTRACT branch from f12f1a4 to ba49cf1 Compare November 19, 2024 10:02
@abhinay-anubola abhinay-anubola force-pushed the sanubola.support.20bit.VEXTRACT branch 2 times, most recently from ccbdb07 to 6138a60 Compare November 20, 2024 08:23
@abhinay-anubola abhinay-anubola force-pushed the sanubola.support.20bit.VEXTRACT branch from 6138a60 to 11cd993 Compare December 13, 2024 06:30
@@ -2465,6 +2465,20 @@ bool CombinerHelper::matchCombineZextTrunc(MachineInstr &MI, Register &Reg) {
return false;
}

bool CombinerHelper::matchCombineSextTrunc(MachineInstr &MI, Register &Reg) {
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 everywhere:

 bool CombinerHelper::matchCombineSextTrunc(MachineInstr &MI, Register &Reg) {
   assert(MI.getOpcode() == TargetOpcode::G_SEXT && "Expected a G_SEXT");
-  Register DstReg = MI.getOperand(0).getReg();
-  Register SrcReg = MI.getOperand(1).getReg();
-  LLT DstTy = MRI.getType(DstReg);
+  const Register DstReg = MI.getOperand(0).getReg();
+  const Register SrcReg = MI.getOperand(1).getReg();
+  const LLT DstTy = MRI.getType(DstReg);
   if (mi_match(SrcReg, MRI,
                m_GTrunc(m_all_of(m_Reg(Reg), m_SpecificType(DstTy))))) {
-    unsigned DstSize = DstTy.getScalarSizeInBits();
-    unsigned SrcSize = MRI.getType(SrcReg).getScalarSizeInBits();
+    const unsigned DstSize = DstTy.getScalarSizeInBits();
+    const unsigned SrcSize = MRI.getType(SrcReg).getScalarSizeInBits();
     return KB->computeNumSignBits(Reg) >= (DstSize - SrcSize + 1);
   }
   return false;

MIRBuilder.buildAssertInstr(AssertExtOpcode, ExtReg20Bit, DstReg20Bit,
SrcEltSize);
MIRBuilder.buildInstr(ExtOpcode, {DstReg}, {ExtReg20Bit});
MI.eraseFromParent();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now we are safe ;-)

Register DstReg, bool SignVal, unsigned SrcEltSize) const {
// Returns the single non-debug use of a register with a specific opcode
// and destination size.
auto GetOneUseWithOpCode =
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: GetOneUseWithOpcode

const unsigned ExtOpcode =
SignVal ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
const Register UseDstReg = TruncMI->getOperand(0).getReg();
if (auto Ext = GetOneUseWithOpCode(UseDstReg, ExtOpcode, 20)) {
Copy link
Collaborator

@andcarminati andcarminati Dec 16, 2024

Choose a reason for hiding this comment

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

nit: we can use:

    if (GetOneUseWithOpcode(UseDstReg, ExtOpcode, 20)) {
      return true;
    }

Because there is no further use for auto Ext.

Or even:

bool AIE2PreLegalizerCombinerImpl::canCombineVExtractElt(
    Register DstReg, bool SignVal, unsigned SrcEltSize) const {
  // Returns the single non-debug use of a register with a specific opcode
  // and destination size.
  auto GetOneUseWithOpcode =
      [&](const Register Reg, const unsigned OpcodeToCheck,
          const unsigned DstSize) -> std::optional<MachineInstr *> {
    if (MRI.hasOneNonDBGUser(Reg)) {
      MachineInstr &Use = *MRI.use_nodbg_instructions(Reg).begin();
      if (Use.getOpcode() == OpcodeToCheck) {
        const LLT DstRegTy = MRI.getType(Use.getOperand(0).getReg());
        if (DstRegTy.getSizeInBits() == DstSize)
          return &Use;
      }
    }
    return std::nullopt;
  };

  auto Trunc = GetOneUseWithOpcode(DstReg, TargetOpcode::G_TRUNC, SrcEltSize);

  if (!Trunc)
    return false;

  const MachineInstr *TruncMI = *Trunc;
  const unsigned ExtOpcode =
      SignVal ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
  const Register UseDstReg = TruncMI->getOperand(0).getReg();

  return GetOneUseWithOpcode(UseDstReg, ExtOpcode, 20).has_value();
}

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.

5 participants