[llvm] Perform bitreverse using AVX512 GFNI for i32 and i64. (PR #81764)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 09:03:08 PST 2024


================
@@ -31040,17 +31044,63 @@ static SDValue LowerBITREVERSE_XOP(SDValue Op, SelectionDAG &DAG) {
   return DAG.getBitcast(VT, Res);
 }
 
+static auto createBSWAPShuffleMask(EVT VT) {
+  SmallVector<int, 16> ShuffleMask;
+  int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
+  for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
+    for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
+      ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
+
+  return ShuffleMask;
+}
+
 static SDValue LowerBITREVERSE(SDValue Op, const X86Subtarget &Subtarget,
                                SelectionDAG &DAG) {
   MVT VT = Op.getSimpleValueType();
+  SDValue In = Op.getOperand(0);
+  SDLoc DL(Op);
+
+  auto HasGFNI = Subtarget.hasGFNI();
+  auto ScalarType = VT.getScalarType();
+
+  if (HasGFNI && ((ScalarType == MVT::i32) || (ScalarType == MVT::i64))) {
+    if (VT.isVector()) {
+      SmallVector<int, 16> BSWAPMask = createBSWAPShuffleMask(VT);
----------------
shamithoke wrote:

I am not sure if I entirely follow your suggestion, but I had tried extracting elements and applying BSWAP on each element (every 4 bytes for integer). This, however, resulted in 4 byte-swaps. 
If this is not what you meant, can you please elaborate a bit more? 

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


More information about the llvm-commits mailing list