1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-31 22:04:40 +03:00

Use __attribute__((target(...))) for AVX-512 support.

Presently, we check for compiler support for the required
intrinsics both with and without extra compiler flags (e.g.,
-mxsave), and then depending on the results of those checks, we
pick which files to compile with which flags.  This is tedious and
complicated, and it results in unsustainable coding patterns such
as separate files for each portion of code may need to be built
with different compiler flags.

This commit introduces support for __attribute__((target(...))) and
uses it for the AVX-512 code.  This simplifies both the
configure-time checks and the build scripts, and it allows us to
place the functions that use the intrinsics in files that we
otherwise do not want to build with special CPU instructions.  We
are careful to avoid using __attribute__((target(...))) on
compilers that do not understand it, but we still perform the
configure-time checks in case the compiler allows using the
intrinsics without it (e.g., MSVC).

A similar change could likely be made for some of the CRC-32C code,
but that is left as a future exercise.

Suggested-by: Andres Freund
Reviewed-by: Raghuveer Devulapalli, Andres Freund
Discussion: https://postgr.es/m/20240731205254.vfpap7uxwmebqeaf%40awork3.anarazel.de
This commit is contained in:
Nathan Bossart
2024-11-07 13:58:43 -06:00
parent f56a01ebdb
commit f78667bd91
11 changed files with 185 additions and 312 deletions

View File

@ -2153,25 +2153,22 @@ endforeach
# Check for the availability of XSAVE intrinsics.
###############################################################
cflags_xsave = []
if host_cpu == 'x86' or host_cpu == 'x86_64'
prog = '''
#include <immintrin.h>
#if defined(__has_attribute) && __has_attribute (target)
__attribute__((target("xsave")))
#endif
int main(void)
{
return _xgetbv(0) & 0xe0;
}
'''
if cc.links(prog, name: 'XSAVE intrinsics without -mxsave',
args: test_c_args)
if cc.links(prog, name: 'XSAVE intrinsics', args: test_c_args)
cdata.set('HAVE_XSAVE_INTRINSICS', 1)
elif cc.links(prog, name: 'XSAVE intrinsics with -mxsave',
args: test_c_args + ['-mxsave'])
cdata.set('HAVE_XSAVE_INTRINSICS', 1)
cflags_xsave += '-mxsave'
endif
endif
@ -2181,12 +2178,14 @@ endif
# Check for the availability of AVX-512 popcount intrinsics.
###############################################################
cflags_popcnt = []
if host_cpu == 'x86_64'
prog = '''
#include <immintrin.h>
#if defined(__has_attribute) && __has_attribute (target)
__attribute__((target("avx512vpopcntdq","avx512bw")))
#endif
int main(void)
{
const char buf[sizeof(__m512i)];
@ -2201,13 +2200,9 @@ int main(void)
}
'''
if cc.links(prog, name: 'AVX-512 popcount without -mavx512vpopcntdq -mavx512bw',
if cc.links(prog, name: 'AVX-512 popcount',
args: test_c_args + ['-DINT64=@0@'.format(cdata.get('PG_INT64_TYPE'))])
cdata.set('USE_AVX512_POPCNT_WITH_RUNTIME_CHECK', 1)
elif cc.links(prog, name: 'AVX-512 popcount with -mavx512vpopcntdq -mavx512bw',
args: test_c_args + ['-DINT64=@0@'.format(cdata.get('PG_INT64_TYPE'))] + ['-mavx512vpopcntdq'] + ['-mavx512bw'])
cdata.set('USE_AVX512_POPCNT_WITH_RUNTIME_CHECK', 1)
cflags_popcnt += ['-mavx512vpopcntdq'] + ['-mavx512bw']
endif
endif