https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129263
Bug ID: 129263
Summary: 32 bit widening multiply plus pack for multiply high
x86 vectorizer miss
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: jtl at jacen dot li
Target Milestone: ---
gcc misses an optimization for a vectorized 32-bit mulh when the multiplier is
a loop-invariant runtime value (not a compile-time constant).
void mulh(const unsigned *restrict a, unsigned *restrict o, unsigned long n,
unsigned m)
{
for (unsigned long i = 0; i < n; i++)
o[i] = (unsigned)(((unsigned long long)m * a[i]) >> 32);
}
gcc -O3 -mavx2, x86_64-pc-linux-gnu.
same output on trunk (17.0.0 20261202), 16.2.0, 15.3.0, 14.4.0, 13.4.0, 12.5.0
and 11.2.0: https://godbolt.org/z/4xs4K3f1e
actual output:
vpermq ymm1, YMMWORD PTR [rdi+rax], 216
vpshufd ymm0, ymm1, 80
vpshufd ymm1, ymm1, 250
vpmuludq ymm0, ymm4, ymm0
vpmuludq ymm1, ymm3, ymm1
vpsrlq ymm0, ymm0, 32
vpsrlq ymm1, ymm1, 32
vperm2i128 ymm2, ymm0, ymm1, 32
vperm2i128 ymm0, ymm0, ymm1, 49
vpshufd ymm0, ymm0, 216
vpshufd ymm1, ymm2, 216
vpunpcklqdq ymm0, ymm1, ymm0
expected output (this is what gcc emits for o[i] = a[i] / 7):
vpmuludq ymm2, ymm0, ymm3
vpsrlq ymm1, ymm0, 32
vpmuludq ymm1, ymm1, ymm3
vpshufd ymm2, ymm2, 245
vpblendd ymm1, ymm1, ymm2, 85
-fdump-tree-vect-details shows the pattern is seen but .MULH is not used:
vect_recog_mulhs_pattern: detected: _7 = _6 >> 32;
widen_mult pattern recognized: patt_35 = m_14(D) w* _4;
the same loop on unsigned short does become .MULH (one vpmulhuw), and on trunk
the
64-bit version (unsigned __int128, >> 64) is recognized as .MULH too, so it
looks like only 32-bit lanes are affected.
performance impact: 2.4-2.6x slower than the expected sequence on alder lake
and
ice lake sp, 2.5x on compiler explorer (xeon 6975p-c):
https://godbolt.org/z/aofWY1x8K
possibly related: PR109764 (same idiom with V2SI), PR100696 (added IFN_MULH)