Issue 245049
Summary [X86] llc does not terminate on a v64i8 shuffle that splats the low 2 or 4 bytes of a one-use binop (AVX512BW)
Labels new issue
Assignees
Reporter he-weiwen
    On main (c9befbbe32a9) and with 22.1.2, `llc -mtriple=x86_64-- -mattr=+avx512bw` does not terminate on this function: it stays at 100% CPU and its memory use keeps growing. 

```llvm
define <64 x i8> @f(<64 x i8> %a, <64 x i8> %b) {
  %x = and <64 x i8> %a, %b
  ; mask is repeats of (0, 1), except for 1 poison at the end
  %s = shufflevector <64 x i8> %x, <64 x i8> poison, <64 x i32> <i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 poison>
  ret <64 x i8> %s
}
```

C version (its `-O2` IR is the function above). clang 22.1.2 does not terminate on it with `clang --target=x86_64-linux-gnu -O2 -mavx512bw -S`:

```c
typedef unsigned char v64u8 __attribute__((vector_size(64)));

v64u8 f(v64u8 a, v64u8 b) {
  v64u8 x = a & b;
  return __builtin_shufflevector(x, x, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, -1);
}
```

### What happens

With `X = and a, b`, the legalizer alternates between two lowerings of `lowerV64I8Shuffle`:

1. `lowerShuffleAsLanePermuteAndPermute` lowers `vector_shuffle<0,1,0,1,...,0,1> X` to an in-lane shuffle of `vector_shuffle<0,1,u x14, 0,1,u x14, 0,1,u x14, 0,1,u x14> X`. The undef elements are set by the `V1.hasOneUse()` block added in 7c3bbfdcf62e.
2. The AVX2 broadcast path of `lowerShuffleAsRepeatedMaskAndLanePermute` lowers that shuffle to `vector_shuffle<0,1,0,1,...,0,1> X`. Its `BroadcastMask == Mask` check (#70592) does not match a mask with undef elements.

`-debug-_only_=legalizedag` (masks shortened):

```
Legalizing: t14: v64i8 = vector_shuffle<0,1,0,1,...,0,1> t5, undef:v64i8
 ... replacing: t14 with: t16: v64i8 = vector_shuffle<0,1 x8, 16,17 x8, 32,33 x8, 48,49 x8> t15, undef:v64i8
Legalizing: t15: v64i8 = vector_shuffle<0,1,u x14, 0,1,u x14, 0,1,u x14, 0,1,u x14> t5, undef:v64i8
 ... replacing: t15 with: t21: v64i8 = vector_shuffle<0,1,0,1,...,0,1> t5, undef:v64i8
Legalizing: t21: v64i8 = vector_shuffle<0,1,0,1,...,0,1> t5, undef:v64i8
 ... replacing: t21 with: t23: v64i8 = vector_shuffle<0,1 x8, 16,17 x8, 32,33 x8, 48,49 x8> t22, undef:v64i8
Legalizing: t22: v64i8 = vector_shuffle<0,1,u x14, 0,1,u x14, 0,1,u x14, 0,1,u x14> t5, undef:v64i8
 ... replacing: t22 with: t34: v64i8 = vector_shuffle<0,1,0,1,...,0,1> t5, undef:v64i8
...
```

`lowerVECTOR_SHUFFLE` does not widen the mask to v32i16 or call `lowerShuffleAsBroadcast` for this shuffle, because `canCombineAsMaskOperation(V1, Subtarget)` is true.

### When it happens

- Type and mask: v64i8, splat of the low 2 or 4 bytes. Splats of the low 8 or 16 bytes, v32i16, and v32i8 with `+avx512vl` compile.
- Poison element: the mask needs at least one, at any position. With a fully defined mask main runs steps 1 and 2 once and then compiles the function.
- Shuffle input: a one-use `and`, `or`, `xor`, `add`, `sub` or `mul` (the ones tried). Compiles fine on direct function input.
- Target: `-mattr=+avx512bw`, `-mcpu=skylake-avx512`, `-mcpu=x86-64-v4`. With `+avx512vbmi` it compiles since #183109.
- `and (shufflevector %a, poison, M), (shufflevector %b, poison, M)` does not terminate either. VectorCombine on main produces it from `shufflevector (and %a, %b), (and %a, %b), <0, 65, 0, 65, ...>`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to