[llvm] [RISCV] Use subreg extract for extract_vector_elt when vlen is known (PR #72666)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 17:50:05 PST 2023


================
@@ -7895,6 +7895,29 @@ SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
     Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
   }
 
+  // If we're compiling for an exact VLEN value and we have a known
+  // constant index, we can always perform the extract in m1 (or
+  // smaller) as we can determine the register corresponding to
+  // the index in the register group.
+  const unsigned MinVLen = Subtarget.getRealMinVLen();
+  const unsigned MaxVLen = Subtarget.getRealMaxVLen();
+  if (auto *IdxC = dyn_cast<ConstantSDNode>(Idx);
+      IdxC && MinVLen == MaxVLen &&
+      VecVT.getSizeInBits().getKnownMinValue() > MinVLen) {
+    unsigned OrigIdx = IdxC->getZExtValue();
+    EVT ElemVT = VecVT.getVectorElementType();
+    unsigned ElemSize = ElemVT.getSizeInBits().getKnownMinValue();
+    unsigned ElemsPerVReg = MinVLen / ElemSize;
+    unsigned RemIdx = OrigIdx % ElemsPerVReg;
+    unsigned SubRegIdx = OrigIdx / ElemsPerVReg;
+    unsigned ExtractIdx =
+      SubRegIdx * ContainerVT.getVectorElementCount().getKnownMinValue();
+    ContainerVT = getLMUL1VT(ContainerVT);
----------------
preames wrote:

This is weird.  I know exactly what the problem is here; I moved the redefinition of ContainerVT below the ElementIdx initialization.  The weird part is that I swear I saw that bug downstream, and fixed it before pushing this branch for review.  And yet, the broken code is both in the remote and local branches.  All I can think is that I screwed up a rebase and lost a change.  

Sorry for the noise here, will republish correct patch once back from holiday.  

https://github.com/llvm/llvm-project/pull/72666


More information about the llvm-commits mailing list