[llvm] db60e64 - [Hexagon] Handle additional shuffles that can be made perfect

Krzysztof Parzyszek via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 29 17:09:32 PDT 2020


Author: Krzysztof Parzyszek
Date: 2020-10-29T19:09:00-05:00
New Revision: db60e64036d11178f09048d4984f871527e543ea

URL: https://github.com/llvm/llvm-project/commit/db60e64036d11178f09048d4984f871527e543ea
DIFF: https://github.com/llvm/llvm-project/commit/db60e64036d11178f09048d4984f871527e543ea.diff

LOG: [Hexagon] Handle additional shuffles that can be made perfect

Added: 
    llvm/test/CodeGen/Hexagon/autohvx/shuff-perfect-inverted-pair.ll

Modified: 
    llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp
index 508eb42e2ae3..29e76b53910e 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp
@@ -789,6 +789,12 @@ struct ShuffleMask {
     OS << " }";
   }
 };
+
+LLVM_ATTRIBUTE_UNUSED
+raw_ostream &operator<<(raw_ostream &OS, const ShuffleMask &SM) {
+  SM.print(OS);
+  return OS;
+}
 } // namespace
 
 // --------------------------------------------------------------------
@@ -1680,7 +1686,7 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
   // The result length must be the same as the length of a single vector,
   // or a vector pair.
   assert(LogLen == HwLog || LogLen == HwLog+1);
-  bool Extend = (LogLen == HwLog);
+  bool HavePairs = LogLen == HwLog+1;
 
   if (!isPermutation(SM.Mask))
     return OpRef::fail();
@@ -1764,6 +1770,22 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
   //  E  1 1 1 0      7  0 1 1 1      7  0 1 1 1      7  0 1 1 1
   //  F  1 1 1 1      F  1 1 1 1      F  1 1 1 1      F  1 1 1 1
 
+  // There is one special case that is not a perfect shuffle, but
+  // can be turned into one easily: when the shuffle operates on
+  // a vector pair, but the two vectors in the pair are swapped.
+  // The code below that identifies perfect shuffles will reject
+  // it, unless the order is reversed.
+  SmallVector<int,128> MaskStorage(SM.Mask.begin(), SM.Mask.end());
+  bool InvertedPair = false;
+  if (HavePairs && SM.Mask[0] >= int(HwLen)) {
+    for (int i = 0, e = SM.Mask.size(); i != e; ++i) {
+      int M = SM.Mask[i];
+      MaskStorage[i] = M >= int(HwLen) ? M-HwLen : M+HwLen;
+    }
+    InvertedPair = true;
+  }
+  ArrayRef<int> LocalMask(MaskStorage);
+
   auto XorPow2 = [] (ArrayRef<int> Mask, unsigned Num) {
     unsigned X = Mask[0] ^ Mask[Num/2];
     // Check that the first half has the X's bits clear.
@@ -1783,12 +1805,12 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
   assert(VecLen > 2);
   for (unsigned I = VecLen; I >= 2; I >>= 1) {
     // Examine the initial segment of Mask of size I.
-    unsigned X = XorPow2(SM.Mask, I);
+    unsigned X = XorPow2(LocalMask, I);
     if (!isPowerOf2_32(X))
       return OpRef::fail();
     // Check the other segments of Mask.
     for (int J = I; J < VecLen; J += I) {
-      if (XorPow2(SM.Mask.slice(J, I), I) != X)
+      if (XorPow2(LocalMask.slice(J, I), I) != X)
         return OpRef::fail();
     }
     Perm[Log2_32(X)] = Log2_32(I)-1;
@@ -1909,7 +1931,7 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
   //   input 2: ....GOOD
   // Then at the end, this needs to be undone. To accomplish this,
   // artificially add "LogLen-1" at both ends of the sequence.
-  if (Extend)
+  if (!HavePairs)
     SwapElems.push_back(LogLen-1);
   for (const CycleType &C : Cycles) {
     // Do the transformation: (a1..an) -> (M a1..an)(M a1).
@@ -1918,12 +1940,14 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
     if (First == 0)
       SwapElems.push_back(C[0]);
   }
-  if (Extend)
+  if (!HavePairs)
     SwapElems.push_back(LogLen-1);
 
   const SDLoc &dl(Results.InpNode);
-  OpRef Arg = !Extend ? Va
-                      : concat(Va, OpRef::undef(SingleTy), Results);
+  OpRef Arg = HavePairs ? Va
+                        : concat(Va, OpRef::undef(SingleTy), Results);
+  if (InvertedPair)
+    Arg = concat(OpRef::hi(Arg), OpRef::lo(Arg), Results);
 
   for (unsigned I = 0, E = SwapElems.size(); I != E; ) {
     bool IsInc = I == E-1 || SwapElems[I] < SwapElems[I+1];
@@ -1947,7 +1971,7 @@ OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
     Arg = OpRef::res(Results.top());
   }
 
-  return !Extend ? Arg : OpRef::lo(Arg);
+  return HavePairs ? Arg : OpRef::lo(Arg);
 }
 
 OpRef HvxSelector::butterfly(ShuffleMask SM, OpRef Va, ResultStack &Results) {

diff  --git a/llvm/test/CodeGen/Hexagon/autohvx/shuff-perfect-inverted-pair.ll b/llvm/test/CodeGen/Hexagon/autohvx/shuff-perfect-inverted-pair.ll
new file mode 100644
index 000000000000..94d338dab52c
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/autohvx/shuff-perfect-inverted-pair.ll
@@ -0,0 +1,20 @@
+; RUN: llc -march=hexagon < %s | FileCheck %s
+
+; CHECK-LABEL: f0:
+; CHECK: r[[R0:[0-9]+]] = #60
+; CHECK: v1:0 = vshuff(v0,v2,r[[R0]])
+define <128 x i8> @f0(<128 x i8> %a0, <128 x i8> %a1) #0 {
+  %v0 = shufflevector <128 x i8> %a0, <128 x i8> %a1, <128 x i32> <i32 128, i32 129, i32 130, i32 131, i32 0, i32 1, i32 2, i32 3, i32 132, i32 133, i32 134, i32 135, i32 4, i32 5, i32 6, i32 7, i32 136, i32 137, i32 138, i32 139, i32 8, i32 9, i32 10, i32 11, i32 140, i32 141, i32 142, i32 143, i32 12, i32 13, i32 14, i32 15, i32 144, i32 145, i32 146, i32 147, i32 16, i32 17, i32 18, i32 19, i32 148, i32 149, i32 150, i32 151, i32 20, i32 21, i32 22, i32 23, i32 152, i32 153, i32 154, i32 155, i32 24, i32 25, i32 26, i32 27, i32 156, i32 157, i32 158, i32 159, i32 28, i32 29, i32 30, i32 31, i32 160, i32 161, i32 162, i32 163, i32 32, i32 33, i32 34, i32 35, i32 164, i32 165, i32 166, i32 167, i32 36, i32 37, i32 38, i32 39, i32 168, i32 169, i32 170, i32 171, i32 40, i32 41, i32 42, i32 43, i32 172, i32 173, i32 174, i32 175, i32 44, i32 45, i32 46, i32 47, i32 176, i32 177, i32 178, i32 179, i32 48, i32 49, i32 50, i32 51, i32 180, i32 181, i32 182, i32 183, i32 52, i32 53, i32 54, i32 55, i32 184, i32 185, i32 186, i32 187, i32 56, i32 57, i32 58, i32 59, i32 188, i32 189, i32 190, i32 191, i32 60, i32 61, i32 62, i32 63>
+  ret <128 x i8> %v0
+}
+
+; CHECK-LABEL: f1:
+; CHECK: r[[R0:[0-9]+]] = #124
+; CHECK: v1:0 = vshuff(v0,v2,r[[R0]])
+define <256 x i8> @f1(<256 x i8> %a0, <256 x i8> %a1) #1 {
+  %v0 = shufflevector <256 x i8> %a0, <256 x i8> %a1, <256 x i32> <i32 256, i32 257, i32 258, i32 259, i32 0, i32 1, i32 2, i32 3, i32 260, i32 261, i32 262, i32 263, i32 4, i32 5, i32 6, i32 7, i32 264, i32 265, i32 266, i32 267, i32 8, i32 9, i32 10, i32 11, i32 268, i32 269, i32 270, i32 271, i32 12, i32 13, i32 14, i32 15, i32 272, i32 273, i32 274, i32 275, i32 16, i32 17, i32 18, i32 19, i32 276, i32 277, i32 278, i32 279, i32 20, i32 21, i32 22, i32 23, i32 280, i32 281, i32 282, i32 283, i32 24, i32 25, i32 26, i32 27, i32 284, i32 285, i32 286, i32 287, i32 28, i32 29, i32 30, i32 31, i32 288, i32 289, i32 290, i32 291, i32 32, i32 33, i32 34, i32 35, i32 292, i32 293, i32 294, i32 295, i32 36, i32 37, i32 38, i32 39, i32 296, i32 297, i32 298, i32 299, i32 40, i32 41, i32 42, i32 43, i32 300, i32 301, i32 302, i32 303, i32 44, i32 45, i32 46, i32 47, i32 304, i32 305, i32 306, i32 307, i32 48, i32 49, i32 50, i32 51, i32 308, i32 309, i32 310, i32 311, i32 52, i32 53, i32 54, i32 55, i32 312, i32 313, i32 314, i32 315, i32 56, i32 57, i32 58, i32 59, i32 316, i32 317, i32 318, i32 319, i32 60, i32 61, i32 62, i32 63, i32 320, i32 321, i32 322, i32 323, i32 64, i32 65, i32 66, i32 67, i32 324, i32 325, i32 326, i32 327, i32 68, i32 69, i32 70, i32 71, i32 328, i32 329, i32 330, i32 331, i32 72, i32 73, i32 74, i32 75, i32 332, i32 333, i32 334, i32 335, i32 76, i32 77, i32 78, i32 79, i32 336, i32 337, i32 338, i32 339, i32 80, i32 81, i32 82, i32 83, i32 340, i32 341, i32 342, i32 343, i32 84, i32 85, i32 86, i32 87, i32 344, i32 345, i32 346, i32 347, i32 88, i32 89, i32 90, i32 91, i32 348, i32 349, i32 350, i32 351, i32 92, i32 93, i32 94, i32 95, i32 352, i32 353, i32 354, i32 355, i32 96, i32 97, i32 98, i32 99, i32 356, i32 357, i32 358, i32 359, i32 100, i32 101, i32 102, i32 103, i32 360, i32 361, i32 362, i32 363, i32 104, i32 105, i32 106, i32 107, i32 364, i32 365, i32 366, i32 367, i32 108, i32 109, i32 110, i32 111, i32 368, i32 369, i32 370, i32 371, i32 112, i32 113, i32 114, i32 115, i32 372, i32 373, i32 374, i32 375, i32 116, i32 117, i32 118, i32 119, i32 376, i32 377, i32 378, i32 379, i32 120, i32 121, i32 122, i32 123, i32 380, i32 381, i32 382, i32 383, i32 124, i32 125, i32 126, i32 127>
+  ret <256 x i8> %v0
+}
+
+attributes #0 = { "target-cpu"="hexagonv66" "target-features"="+hvx,+hvx-length64b" }
+attributes #1 = { "target-cpu"="hexagonv66" "target-features"="+hvx,+hvx-length128b" }


        


More information about the llvm-commits mailing list