Skip to content

Commit a80282f

Browse files
Pipe the info about which gcc version clang is using for its supporting libraries into a preprocessor define.
1 parent ed24de1 commit a80282f

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

include/qt_128b_atomics.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@
1616
// architecture-defined behavior and not standard defined behavior.
1717
// That should make trying other architectures easier.
1818

19-
// user can define QTHREADS_USE_STANDARD_128_BIT_ATOMICS in their flags to override this detection logic.
19+
// User can define QTHREADS_USE_STANDARD_128_BIT_ATOMICS in their flags to override this detection logic.
2020
#ifndef QTHREADS_USE_STANDARD_128_BIT_ATOMICS
21+
22+
// Get the version of the gcc supporting libraries being used.
23+
// Clang currently says it's gcc 4.2 via the __GNUC__ and __GNUC_MINOR__ macros
24+
// so instead we have to parse the version info from it's selected gcc install at configure time
25+
// and pipe it through to here via our own preprocessor define.
26+
// icc exactly mimics gcc in this case, and icx and acfl behave the same as clang but do
27+
// not require their own detection logic here.
28+
#if defined(__clang__)
29+
#define QTHREADS_GCC_LIB_MAJOR_VERSION QTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION
30+
#define QTHREADS_GCC_LIB_MINOR_VERSION QTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION
31+
#else
32+
#define QTHREADS_GCC_LIB_MAJOR_VERSION __GNUC__
33+
#define QTHREADS_GCC_LIB_MINOR_VERSION __GNUC_MINOR__
34+
#endif
2135
#ifdef __x86_64__
2236
#ifdef __AVX__
2337
// Intel and AMD both specify that 128 bit loads and stores are atomic (with reasonable alignment constraints)
@@ -35,14 +49,10 @@
3549
// then fall back to the vendored implementation which just uses the old cmpxchg16b instruction if it would use locks.
3650
// With clang, it'll just inline the appropriate atomic instructions as long as optimizations are on.
3751
// In debug mode it falls back to the not quite equivalent libatomic implementation from gcc though.
38-
// In our configure script we set clang up to tell us which gcc version it's using via the __GNUC__ and __GNUC_MINOR__
39-
// defines instead of its old weird defaults so we can check for the libatomic version at compile time here.
4052
// This all works assuming that qthreads is never compiled with a newer libatomic than is available at runtime.
41-
// icc also just passes through the __GNUC__ values from the underlying gcc it's using.
42-
// icx and acfl just do what clang does.
4353
#if defined(__clang__) && defined(__OPTIMIZE__)
4454
#define QTHREADS_USE_STANDARD_128_BIT_ATOMICS
45-
#elif __GNUC__ >= 13 || (__GNUC__ == 12 && __GNUC_MINOR__ >= 3) || (__GNUC__ == 11 && __GNUC_MINOR__ >= 4)
55+
#elif QTHREADS_GCC_LIB_MAJOR_VERSION >= 13 || (QTHREADS_GCC_LIB_MAJOR_VERSION == 12 && QTHREADS_GCC_LIB_MINOR_VERSION >= 3) || (QTHREADS_GCC_LIB_MAJOR_VERSION == 11 && QTHREADS_GCC_LIB_MINOR_VERSION >= 4)
4656
#define QTHREADS_USE_STANDARD_128_BIT_ATOMICS
4757
#endif
4858
#endif // #ifdef __AVX__
@@ -54,11 +64,9 @@
5464
#if defined(__clang) && defined(__OPTIMIZE__)
5565
// clang inlines the 128 bit atomic loads on arm as long as optimizations are on, but falls back to
5666
// the gcc libatomic implementation (which isn't always equivalent) when optimizations aren't on.
57-
// At build time we have clang pipe the underlying gcc's version info through __GNUC__ and __GNUC_MINOR__
58-
// so that logic works as a fallback when optimizations are off.
5967
// Again, this all works assuming that qthreads is never compiled with a newer libatomic than is available at runtime.
6068
#define QTHREADS_USE_STANDARD_128_BIT_ATOMICS
61-
#elif __GNUC__ >= 13
69+
#elif QTHREADS_GCC_LIB_MAJOR_VERSION >= 13
6270
#if __ARM_ARCH > 8 && __ARM_ARCH != 801 && __ARM_ARCH != 802 && __ARM_ARCH != 803
6371
// gcc only provides lock-free 128 bit atomics in libatomic for armv8.4 and later.
6472
// We want arm 8.4 or later, but there's inconsistency with how to detect arm versions.

src/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
# Copyright (c) 2008 Sandia Corporation
44
#
55

6-
AM_CPPFLAGS = -I$(top_srcdir)/include
6+
# Get the version of gcc that clang (or one of its derivatives) is using for its supporting libraries.
7+
# This will just be empty for gcc and icc.
8+
clang_underlying_gcc_major_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\1/g'
9+
clang_underlying_gcc_minor_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\2/g'
10+
11+
AM_CPPFLAGS = -I$(top_srcdir)/include -DQTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION=$(clang_underlying_gcc_major_version) -DQTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION=$(clang_underlying_gcc_minor_version)
712
AM_CCASFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include -DHAVE_CONFIG_H
813

914
lib_LTLIBRARIES = libqthread.la

test/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ buildall: buildtests buildextra
3737

3838
noinst_HEADERS = argparsing.h
3939

40-
AM_CPPFLAGS = -I$(top_srcdir)/include
40+
# Get the version of gcc that clang (or one of its derivatives) is using for its supporting libraries.
41+
# This will just be empty for gcc and icc.
42+
clang_underlying_gcc_major_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\1/g'
43+
clang_underlying_gcc_minor_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\2/g'
44+
45+
AM_CPPFLAGS = -I$(top_srcdir)/include -DQTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION=$(clang_underlying_gcc_major_version) -DQTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION=$(clang_underlying_gcc_minor_version)
4146
outputdir = $(top_builddir)/src
4247
qthreadlib = $(outputdir)/libqthread.la
4348

test/basics/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ if WANT_SINGLE_WORKER_SCHEDULER
5959
TESTS_ENVIRONMENT += env QT_NUM_SHEPHERDS=2 QT_NUM_WORKERS_PER_SHEPHERD=1
6060
endif
6161

62-
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/
62+
# Get the version of gcc that clang (or one of its derivatives) is using for its supporting libraries.
63+
# This will just be empty for gcc and icc.
64+
clang_underlying_gcc_major_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\1/g'
65+
clang_underlying_gcc_minor_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\2/g'
66+
67+
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/ -DQTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION=$(clang_underlying_gcc_major_version) -DQTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION=$(clang_underlying_gcc_minor_version)
6368
qthreadlib = $(top_builddir)/src/libqthread.la
6469

6570
buildall: $(TESTS)

test/features/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ if WANT_SINGLE_WORKER_SCHEDULER
5454
TESTS_ENVIRONMENT += env QT_NUM_SHEPHERDS=2 QT_NUM_WORKERS_PER_SHEPHERD=1
5555
endif
5656

57-
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/
57+
# Get the version of gcc that clang (or one of its derivatives) is using for its supporting libraries.
58+
# This will just be empty for gcc and icc.
59+
clang_underlying_gcc_major_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\1/g'
60+
clang_underlying_gcc_minor_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\2/g'
61+
62+
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/ -DQTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION=$(clang_underlying_gcc_major_version) -DQTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION=$(clang_underlying_gcc_minor_version)
5863
qthreadlib = $(top_builddir)/src/libqthread.la
5964

6065
buildall: $(TESTS)

test/stress/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ if WANT_SINGLE_WORKER_SCHEDULER
3232
TESTS_ENVIRONMENT += env QT_NUM_SHEPHERDS=2 QT_NUM_WORKERS_PER_SHEPHERD=1
3333
endif
3434

35-
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/
35+
# Get the version of gcc that clang (or one of its derivatives) is using for its supporting libraries.
36+
# This will just be empty for gcc and icc.
37+
clang_underlying_gcc_major_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\1/g'
38+
clang_underlying_gcc_minor_version != clang -v 2>&1 | sed '/Selected GCC installation/!d;s/^.*\/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$$/\2/g'
39+
40+
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/test/ -DQTHREADS_CLANG_UNDERLYING_GCC_MAJOR_VERSION=$(clang_underlying_gcc_major_version) -DQTHREADS_CLANG_UNDERLYING_GCC_MINOR_VERSION=$(clang_underlying_gcc_minor_version)
3641
qthreadlib = $(top_builddir)/src/libqthread.la
3742
utils_rnglib = $(top_builddir)/test/utils/rng/librng.la
3843

0 commit comments

Comments
 (0)