https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94500
Bug ID: 94500 Summary: Wrong maximum value with small integer types and AVX-512 Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: translation Assignee: unassigned at gcc dot gnu.org Reporter: ben at rockshrub dot de Target Milestone: --- Created attachment 48211 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48211&action=edit C++ program that triggers the bug Both g++ and gfortran produce incorrect code for computing maximum values of arrays of integer types smaller than 32 bit when optimizing for usage of AVX-512. This can be triggered by using std::max_element in C++ or Fortran's maxval. The generated program uses 32-bit shuffles to reduce a 512-bit vector into a final 8-bit or 16-bit result (set smallint to int8_t or int16_t), so if the true maximum value never ends up in the least significant bytes of one of the 32-bit elements in the vector, it is never found. The issue persists on GCC 8.4.0, but seems to be fixed on 9.X versions of GCC. The attached program fills a vector in a way that triggers the error and then computes the maximum value once "by hand" and once using std::max_element. It prints the results of the two computations which should be identical. Compiler command lines: g++ -O3 -march=skylake-avx512 -mprefer-vector-width=512 -o bug_cxx bug_cxx.cxx gfortran -O3 -march=skylake-avx512 -mprefer-vector-width=512 -o bug_f90 bug_f90.f90 No errors or warnings are encountered at -Wall -Wextra. gcc -v: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/gpfs/software/juwels/stages/2019a/software/GCCcore/8.3.0/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-plugins --enable-gold=default --enable-ld --with-plugin-ld=ld.gold --prefix=/gpfs/software/juwels/stages/2019a/software/GCCcore/8.3.0 --with-local-prefix=/gpfs/software/juwels/stages/2019a/software/GCCcore/8.3.0 --enable-bootstrap --with-isl=/dev/shm/swmanage/GCCcore/8.3.0/dummy-/gcc-8.3.0/stage2_stuff Thread model: posix gcc version 8.3.0 (GCC)