Skip to content

Commit 1cbaf68

Browse files
author
Kai Luo
committed
[clang][AIX] Add option to control quadword lock free atomics ABI on AIX
We are supporting quadword lock free atomics on AIX. For the situation that users on AIX are using a libatomic that is lock-based for quadword types, we can't enable quadword lock free atomics by default on AIX in case user's new code and existing code accessing the same shared atomic quadword variable, we can't guarentee atomicity. So we need an option to enable quadword lock free atomics on AIX, thus we can build a quadword lock-free libatomic(also for advanced users considering atomic performance critical) for users to make the transition smooth. Reviewed By: shchenz Differential Revision: https://reviews.llvm.org/D127189
1 parent d6e1e0a commit 1cbaf68

File tree

9 files changed

+69
-15
lines changed

9 files changed

+69
-15
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ VALUE_LANGOPT(DoubleSize , 32, 0, "width of double")
195195
VALUE_LANGOPT(LongDoubleSize , 32, 0, "width of long double")
196196
LANGOPT(PPCIEEELongDouble , 1, 0, "use IEEE 754 quadruple-precision for long double")
197197
LANGOPT(EnableAIXExtendedAltivecABI , 1, 0, "__EXTABI__ predefined macro")
198+
LANGOPT(EnableAIXQuadwordAtomicsABI , 1, 0, "Use 16-byte atomic lock free semantics")
198199
COMPATIBLE_VALUE_LANGOPT(PICLevel , 2, 0, "__PIC__ level")
199200
COMPATIBLE_VALUE_LANGOPT(PIE , 1, 0, "is pie")
200201
LANGOPT(ROPI , 1, 0, "Read-only position independence")

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,10 @@ def mabi_EQ_vec_extabi : Flag<["-"], "mabi=vec-extabi">, Group<m_Group>, Flags<[
36793679
MarshallingInfoFlag<LangOpts<"EnableAIXExtendedAltivecABI">>;
36803680
def mabi_EQ_vec_default : Flag<["-"], "mabi=vec-default">, Group<m_Group>, Flags<[CC1Option]>,
36813681
HelpText<"Enable the default Altivec ABI on AIX (AIX only). Uses only volatile vector registers.">;
3682+
def mabi_EQ_quadword_atomics : Flag<["-"], "mabi=quadword-atomics">,
3683+
Group<m_Group>, Flags<[CC1Option]>,
3684+
HelpText<"Enable quadword atomics ABI on AIX (AIX PPC64 only). Uses lqarx/stqcx. instructions.">,
3685+
MarshallingInfoFlag<LangOpts<"EnableAIXQuadwordAtomicsABI">>;
36823686
def mvsx : Flag<["-"], "mvsx">, Group<m_ppc_Features_Group>;
36833687
def mno_vsx : Flag<["-"], "mno-vsx">, Group<m_ppc_Features_Group>;
36843688
def msecure_plt : Flag<["-"], "msecure-plt">, Group<m_ppc_Features_Group>;

clang/lib/Basic/Targets/PPC.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ void PPCTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
849849
? &llvm::APFloat::IEEEquad()
850850
: &llvm::APFloat::PPCDoubleDouble();
851851
Opts.IEEE128 = 1;
852+
if (getTriple().isOSAIX() && Opts.EnableAIXQuadwordAtomicsABI &&
853+
HasQuadwordAtomics)
854+
MaxAtomicInlineWidth = 128;
852855
}
853856

854857
ArrayRef<Builtin::Info> PPCTargetInfo::getTargetBuiltins() const {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5112,6 +5112,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51125112
CmdArgs.push_back("-mabi=vec-default");
51135113
}
51145114

5115+
if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ_quadword_atomics)) {
5116+
if (!Triple.isOSAIX() || Triple.isPPC32())
5117+
D.Diag(diag::err_drv_unsupported_opt_for_target)
5118+
<< A->getSpelling() << RawTriple.str();
5119+
CmdArgs.push_back("-mabi=quadword-atomics");
5120+
}
5121+
51155122
if (Arg *A = Args.getLastArg(options::OPT_mlong_double_128)) {
51165123
// Emit the unsupported option error until the Clang's library integration
51175124
// support for 128-bit long double is available for AIX.

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,12 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
19261926
Opts.EnableAIXExtendedAltivecABI = O.matches(OPT_mabi_EQ_vec_extabi);
19271927
}
19281928

1929+
if (Arg *A = Args.getLastArg(OPT_mabi_EQ_quadword_atomics)) {
1930+
if (!T.isOSAIX() || T.isPPC32())
1931+
Diags.Report(diag::err_drv_unsupported_opt_for_target)
1932+
<< A->getSpelling() << T.str();
1933+
}
1934+
19291935
bool NeedLocTracking = false;
19301936

19311937
if (!Opts.OptRecordFile.empty())

clang/test/CodeGen/PowerPC/quadword-atomics.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// RUN: %clang_cc1 -no-opaque-pointers -Werror -Wno-atomic-alignment -triple powerpc64le-linux-gnu \
2-
// RUN: -target-cpu pwr8 -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC64-PWR8
2+
// RUN: -target-cpu pwr8 -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC64-QUADWORD-ATOMICS
33
// RUN: %clang_cc1 -no-opaque-pointers -Werror -Wno-atomic-alignment -triple powerpc64le-linux-gnu \
44
// RUN: -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC64
55
// RUN: %clang_cc1 -no-opaque-pointers -Werror -Wno-atomic-alignment -triple powerpc64-unknown-aix \
66
// RUN: -target-cpu pwr7 -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC64
7+
// RUN: %clang_cc1 -no-opaque-pointers -Werror -Wno-atomic-alignment -triple powerpc64-unknown-aix \
8+
// RUN: -target-cpu pwr8 -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC64
9+
// RUN: %clang_cc1 -no-opaque-pointers -Werror -Wno-atomic-alignment -triple powerpc64-unknown-aix \
10+
// RUN: -mabi=quadword-atomics -target-cpu pwr8 -emit-llvm -o - %s | FileCheck %s \
11+
// RUN: --check-prefix=PPC64-QUADWORD-ATOMICS
12+
713

814
typedef struct {
915
char x[16];
@@ -13,8 +19,8 @@ typedef _Atomic(Q) AtomicQ;
1319

1420
typedef __int128_t int128_t;
1521

16-
// PPC64-PWR8-LABEL: @test_load(
17-
// PPC64-PWR8: [[TMP3:%.*]] = load atomic i128, i128* [[TMP1:%.*]] acquire, align 16
22+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_load(
23+
// PPC64-QUADWORD-ATOMICS: [[TMP3:%.*]] = load atomic i128, i128* [[TMP1:%.*]] acquire, align 16
1824
//
1925
// PPC64-LABEL: @test_load(
2026
// PPC64: call void @__atomic_load(i64 noundef 16, i8* noundef [[TMP3:%.*]], i8* noundef [[TMP4:%.*]], i32 noundef signext 2)
@@ -24,8 +30,8 @@ Q test_load(AtomicQ *ptr) {
2430
return __c11_atomic_load(ptr, __ATOMIC_ACQUIRE);
2531
}
2632

27-
// PPC64-PWR8-LABEL: @test_store(
28-
// PPC64-PWR8: store atomic i128 [[TMP6:%.*]], i128* [[TMP4:%.*]] release, align 16
33+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_store(
34+
// PPC64-QUADWORD-ATOMICS: store atomic i128 [[TMP6:%.*]], i128* [[TMP4:%.*]] release, align 16
2935
//
3036
// PPC64-LABEL: @test_store(
3137
// PPC64: call void @__atomic_store(i64 noundef 16, i8* noundef [[TMP6:%.*]], i8* noundef [[TMP7:%.*]], i32 noundef signext 3)
@@ -35,8 +41,8 @@ void test_store(Q val, AtomicQ *ptr) {
3541
__c11_atomic_store(ptr, val, __ATOMIC_RELEASE);
3642
}
3743

38-
// PPC64-PWR8-LABEL: @test_add(
39-
// PPC64-PWR8: [[TMP3:%.*]] = atomicrmw add i128* [[TMP0:%.*]], i128 [[TMP2:%.*]] monotonic, align 16
44+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_add(
45+
// PPC64-QUADWORD-ATOMICS: [[TMP3:%.*]] = atomicrmw add i128* [[TMP0:%.*]], i128 [[TMP2:%.*]] monotonic, align 16
4046
//
4147
// PPC64-LABEL: @test_add(
4248
// PPC64: [[CALL:%.*]] = call i128 @__atomic_fetch_add_16(i8* noundef [[TMP2:%.*]], i128 noundef [[TMP3:%.*]], i32 noundef signext 0)
@@ -46,8 +52,8 @@ void test_add(_Atomic(int128_t) *ptr, int128_t x) {
4652
__c11_atomic_fetch_add(ptr, x, __ATOMIC_RELAXED);
4753
}
4854

49-
// PPC64-PWR8-LABEL: @test_xchg(
50-
// PPC64-PWR8: [[TMP8:%.*]] = atomicrmw xchg i128* [[TMP4:%.*]], i128 [[TMP7:%.*]] seq_cst, align 16
55+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_xchg(
56+
// PPC64-QUADWORD-ATOMICS: [[TMP8:%.*]] = atomicrmw xchg i128* [[TMP4:%.*]], i128 [[TMP7:%.*]] seq_cst, align 16
5157
//
5258
// PPC64-LABEL: @test_xchg(
5359
// PPC64: call void @__atomic_exchange(i64 noundef 16, i8* noundef [[TMP7:%.*]], i8* noundef [[TMP8:%.*]], i8* noundef [[TMP9:%.*]], i32 noundef signext 5)
@@ -57,8 +63,8 @@ Q test_xchg(AtomicQ *ptr, Q new) {
5763
return __c11_atomic_exchange(ptr, new, __ATOMIC_SEQ_CST);
5864
}
5965

60-
// PPC64-PWR8-LABEL: @test_cmpxchg(
61-
// PPC64-PWR8: [[TMP10:%.*]] = cmpxchg i128* [[TMP5:%.*]], i128 [[TMP8:%.*]], i128 [[TMP9:%.*]] seq_cst monotonic, align 16
66+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_cmpxchg(
67+
// PPC64-QUADWORD-ATOMICS: [[TMP10:%.*]] = cmpxchg i128* [[TMP5:%.*]], i128 [[TMP8:%.*]], i128 [[TMP9:%.*]] seq_cst monotonic, align 16
6268
//
6369
// PPC64-LABEL: @test_cmpxchg(
6470
// PPC64: [[CALL:%.*]] = call zeroext i1 @__atomic_compare_exchange(i64 noundef 16, i8* noundef [[TMP8:%.*]], i8* noundef [[TMP9:%.*]], i8* noundef [[TMP10:%.*]], i32 noundef signext 5, i32 noundef signext 0)
@@ -68,8 +74,8 @@ int test_cmpxchg(AtomicQ *ptr, Q *cmp, Q new) {
6874
return __c11_atomic_compare_exchange_strong(ptr, cmp, new, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
6975
}
7076

71-
// PPC64-PWR8-LABEL: @test_cmpxchg_weak(
72-
// PPC64-PWR8: [[TMP10:%.*]] = cmpxchg weak i128* [[TMP5:%.*]], i128 [[TMP8:%.*]], i128 [[TMP9:%.*]] seq_cst monotonic, align 16
77+
// PPC64-QUADWORD-ATOMICS-LABEL: @test_cmpxchg_weak(
78+
// PPC64-QUADWORD-ATOMICS: [[TMP10:%.*]] = cmpxchg weak i128* [[TMP5:%.*]], i128 [[TMP8:%.*]], i128 [[TMP9:%.*]] seq_cst monotonic, align 16
7379
//
7480
// PPC64-LABEL: @test_cmpxchg_weak(
7581
// PPC64: [[CALL:%.*]] = call zeroext i1 @__atomic_compare_exchange(i64 noundef 16, i8* noundef [[TMP8:%.*]], i8* noundef [[TMP9:%.*]], i8* noundef [[TMP10:%.*]], i32 noundef signext 5, i32 noundef signext 0)
@@ -79,8 +85,8 @@ int test_cmpxchg_weak(AtomicQ *ptr, Q *cmp, Q new) {
7985
return __c11_atomic_compare_exchange_weak(ptr, cmp, new, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
8086
}
8187

82-
// PPC64-PWR8-LABEL: @is_lock_free(
83-
// PPC64-PWR8: ret i32 1
88+
// PPC64-QUADWORD-ATOMICS-LABEL: @is_lock_free(
89+
// PPC64-QUADWORD-ATOMICS: ret i32 1
8490
//
8591
// PPC64-LABEL: @is_lock_free(
8692
// PPC64: [[CALL:%.*]] = call zeroext i1 @__atomic_is_lock_free(i64 noundef 16, i8* noundef null)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang -### -target powerpc-unknown-aix -S %s 2>&1 | FileCheck %s
2+
// RUN: %clang -### -target powerpc64-unknown-aix -S %s 2>&1 | FileCheck %s
3+
// RUN: %clang -### -target powerpc-unknown-aix -mabi=quadword-atomics -S \
4+
// RUN: %s 2>&1 | FileCheck --check-prefix=CHECK-UNSUPPORTED-TARGET %s
5+
// RUN: %clang -### -target powerpc64-unknown-aix -mabi=quadword-atomics -S \
6+
// RUN: %s 2>&1 | FileCheck %s --check-prefix=CHECK-QUADWORD-ATOMICS
7+
//
8+
// CHECK-UNSUPPORTED-TARGET: unsupported option '-mabi=quadword-atomics' for target 'powerpc-unknown-aix'
9+
// CHECK-NOT: "-mabi=quadword-atomics"
10+
// CHECK-QUADWORD-ATOMICS: "-cc1"
11+
// CHECK-QUADWORD-ATOMICS-SAME: "-mabi=quadword-atomics"

clang/test/Driver/ppc-unsupported.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@
77
// RUN: -c %s 2>&1 | FileCheck %s
88
// RUN: not %clang -target powerpc64le-unknown-linux -msvr4-struct-return \
99
// RUN: -c %s 2>&1 | FileCheck %s
10+
// RUN: not %clang -target powerpc64-unknown-freebsd -mabi=quadword-atomics \
11+
// RUN: -c %s 2>&1 | FileCheck %s
12+
// RUN: not %clang -target powerpc64-unknown-linux -mabi=quadword-atomics \
13+
// RUN: -c %s 2>&1 | FileCheck %s
14+
// RUN: not %clang -target powerpc64le-unknown-linux -mabi=quadword-atomics \
15+
// RUN: -c %s 2>&1 | FileCheck %s
16+
// RUN: not %clang -target powerpc-unknown-unknown -mabi=quadword-atomics \
17+
// RUN: -c %s 2>&1 | FileCheck %s
18+
// RUN: not %clang -target powerpc-unknown-aix -mabi=quadword-atomics \
19+
// RUN: -c %s 2>&1 | FileCheck %s
1020
// CHECK: unsupported option

clang/test/Sema/atomic-ops.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
// RUN: %clang_cc1 %s -verify -fgnuc-version=4.2.1 -ffreestanding \
1111
// RUN: -fsyntax-only -triple=powerpc64le-linux-gnu -std=c11 \
1212
// RUN: -target-cpu pwr8 -DPPC64_PWR8
13+
// RUN: %clang_cc1 %s -verify -fgnuc-version=4.2.1 -ffreestanding \
14+
// RUN: -fsyntax-only -triple=powerpc64-unknown-aix -std=c11 \
15+
// RUN: -target-cpu pwr8
16+
// RUN: %clang_cc1 %s -verify -fgnuc-version=4.2.1 -ffreestanding \
17+
// RUN: -fsyntax-only -triple=powerpc64-unknown-aix -std=c11 \
18+
// RUN: -mabi=quadword-atomics -target-cpu pwr8 -DPPC64_PWR8
1319

1420
// Basic parsing/Sema tests for __c11_atomic_*
1521

0 commit comments

Comments
 (0)