https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129020
Bug ID: 129020
Summary: x86: -mapxf -mavx512dq -mno-avx512vl emits EVEX.256
vbroadcasti64x2/vbroadcastf64x2/vbroadcastf32x4 for an
EGPR address
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
CC: hjl.tools at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
Fails with a native trunk build (gcc 17.0.0 20261125, 7543d3c4c4a4) and with
every release that accepts -mapxf (14.1.0 through 16.2.0). 14.4.0 shows the
EVEX.256 forms at -O1 rather than at -O2 for this testcase.
Target: x86_64-pc-linux-gnu.
With -mavx512dq -mno-avx512vl -mapxf, a broadcast from an EGPR base (%r16-%r31)
is emitted in a form that GNU as rejects when AVX512VL is not enabled. AVX512DQ
alone provides only the 512-bit EVEX broadcasts; the 256-bit ymm forms need
AVX512VL, and VEX vbroadcasti128/vbroadcastf128 cannot encode an EGPR.
#include <immintrin.h>
__m256i bq (const __m128i **pp) { return _mm256_broadcastsi128_si256
(_mm_loadu_si128 (pp[0])) ^ _mm256_broadcastsi128_si256 (_mm_loadu_si128
(pp[1])); }
__m256d bd (const __m128d **pp) { return _mm256_broadcast_pd (pp[0]) +
_mm256_broadcast_pd (pp[1]); }
__m256 bs (const __m128 **pp) { return _mm256_broadcast_ps (pp[0]) +
_mm256_broadcast_ps (pp[1]); }
gcc -O2 -mavx512dq -mno-avx512vl -mapxf -ffixed-rax -ffixed-rbx -ffixed-rcx
-ffixed-rdx -ffixed-rbp -ffixed-rsi -ffixed-r8 -ffixed-r9 -ffixed-r10
-ffixed-r11 -ffixed-r12 -ffixed-r13 -ffixed-r14 -ffixed-r15 -S t.c
The -ffixed options only force a pointer into %r16. Trunk output at -O2:
vbroadcasti64x2 (%r16), %ymm0 # avx2_vbroadcasti128_v4di/1
vbroadcastf64x2 (%r16), %ymm1 # avx_vbroadcastf128_v4df/3
vbroadcastf32x4 (%r16), %ymm1 # avx_vbroadcastf128_v8sf/3
GNU as with -march=generic64+avx+avx2+avx512f+avx512dq+avx512bw+apx_f (no
avx512vl) rejects all three with "operand size mismatch". Expected: reload the
pointer into a legacy GPR and emit VEX vbroadcasti128/vbroadcastf128, as
-mavx512f -mno-avx512dq already does, and as this trunk does at -O1.
15.3.0 and 16.2.0 match trunk. 15.1.0 and 15.2.0 already emit the EVEX.256
float forms, but still print vbroadcasti128 (%r16) for the integer case, which
as rejects with "no EVEX encoding for `vbroadcasti128'". 14.1.0, 14.2.0 and
14.3.0 emit that same VEX mnemonic with an EGPR base at -O2 and as rejects it
the same way. 14.4.0 is correct at -O2 (VEX with a legacy base) and fails at
-O1 like trunk's -O2.
Godbolt (trunk): https://godbolt.org/z/9W8x19q6n
Cause (config/i386/sse.md): avx2_vbroadcasti128_<mode> alternative 1 and
avx_vbroadcastf128_<mode> alternatives 3 and 4 have isa "avx512dq" and print
the EVEX.256 forms. Alternative 0 is the VEX form, and its "jm" / gpr16 address
constraint rejects an EGPR base, so LRA picks the avx512dq alternative instead
of reloading the address. That isa attribute is true for AVX512DQ alone, but
the ymm encoding also needs AVX512VL. PR127459 fixed the same kind of mistake
in avx_vec_concat and did not change these two patterns.
Possible fix (not tested): give those avx512dq alternatives an isa that also
requires AVX512VL, so LRA reloads the EGPR and uses the VEX form when VL is
disabled.
Related: PR127459 and PR113751.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.