Skip to content

Commit 0b96bce

Browse files
rorthtstellar
authored andcommitted
[Driver] Use libatomic for 32-bit SPARC atomics support on Linux
This is the Linux/sparc64 equivalent to D118021 <https://reviews.llvm.org/D118021>, necessary to provide an external implementation of atomics on 32-bit SPARC which LLVM cannot inline even with `-mcpu=v9` or an equivalent default. Tested on `sparc64-unknown-linux-gnu`. Differential Revision: https://reviews.llvm.org/D130569 (cherry picked from commit 9b1897b)
1 parent 12e4e97 commit 0b96bce

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
631631

632632
AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
633633

634+
// LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
635+
// forcibly link with libatomic as a workaround.
636+
// TODO: Issue #41880 and D118021.
637+
if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) {
638+
CmdArgs.push_back("--push-state");
639+
CmdArgs.push_back("--as-needed");
640+
CmdArgs.push_back("-latomic");
641+
CmdArgs.push_back("--pop-state");
642+
}
643+
634644
if (WantPthread && !isAndroid)
635645
CmdArgs.push_back("-lpthread");
636646

clang/test/Driver/linux-ld.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@
10071007
// CHECK-SPARCV8: "{{.*}}ld{{(.exe)?}}"
10081008
// CHECK-SPARCV8: "-m" "elf32_sparc"
10091009
// CHECK-SPARCV8: "-dynamic-linker" "{{(/usr/sparc-unknown-linux-gnu)?}}/lib/ld-linux.so.2"
1010+
// CHECK-SPARCV8: "--push-state" "--as-needed" "-latomic" "--pop-state"
10101011
//
10111012
// RUN: %clang -### %s -no-pie 2>&1 \
10121013
// RUN: --target=sparcel-unknown-linux-gnu \
@@ -1021,6 +1022,7 @@
10211022
// CHECK-SPARCV9: "{{.*}}ld{{(.exe)?}}"
10221023
// CHECK-SPARCV9: "-m" "elf64_sparc"
10231024
// CHECK-SPARCV9: "-dynamic-linker" "{{(/usr/sparcv9-unknown-linux-gnu)?}}/lib{{(64)?}}/ld-linux.so.2"
1025+
// CHECK-SPARCV9-NOT: "-latomic"
10241026

10251027
// Test linker invocation on Android.
10261028
// RUN: %clang -### %s -no-pie 2>&1 \

0 commit comments

Comments
 (0)