Skip to content

Commit a6afa9e

Browse files
committed
[Driver] Use libatomic for 32-bit SPARC atomics support
Even after D86621 <https://reviews.llvm.org/D86621>, `clang -m32` on Solaris/sparcv9 doesn't inline atomics with 8-byte operands, unlike `gcc`. This leads to many link failures in the testsuite (undefined references to `__atomic_load_8` and `__sync_val_compare_and_swap_8`. Until a proper codegen fix can be implemented, this patch works around the first of those by linking with `-latomic`. Tested on `sparcv9-sun-solaris2.11`. Differential Revision: https://reviews.llvm.org/D118021
1 parent be5734d commit a6afa9e

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
756756
return false;
757757
}
758758

759-
static const char *getAsNeededOption(const ToolChain &TC, bool as_needed) {
759+
const char *tools::getAsNeededOption(const ToolChain &TC, bool as_needed) {
760760
assert(!TC.getTriple().isOSAIX() &&
761761
"AIX linker does not support any form of --as-needed option yet.");
762762

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
117117
bool ForceStaticHostRuntime = false,
118118
bool IsOffloadingHost = false, bool GompNeedsRT = false);
119119

120+
const char *getAsNeededOption(const ToolChain &TC, bool as_needed);
121+
120122
llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);
121123
llvm::opt::Arg *getLastProfileSampleUseArg(const llvm::opt::ArgList &Args);
122124

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
132132
CmdArgs.push_back("-lssp_nonshared");
133133
CmdArgs.push_back("-lssp");
134134
}
135+
// LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
136+
// forcibly link with libatomic as a workaround.
137+
if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) {
138+
CmdArgs.push_back(getAsNeededOption(getToolChain(), true));
139+
CmdArgs.push_back("-latomic");
140+
CmdArgs.push_back(getAsNeededOption(getToolChain(), false));
141+
}
135142
CmdArgs.push_back("-lgcc_s");
136143
CmdArgs.push_back("-lc");
137144
if (!Args.hasArg(options::OPT_shared)) {

clang/test/Driver/solaris-ld.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2"
1818
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/../../.."
1919
// CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/lib"
20+
// CHECK-LD-SPARC32-SAME: "-zignore" "-latomic" "-zrecord"
2021
// CHECK-LD-SPARC32-SAME: "-lgcc_s"
2122
// CHECK-LD-SPARC32-SAME: "-lc"
2223
// CHECK-LD-SPARC32-SAME: "-lgcc"
@@ -40,6 +41,7 @@
4041
// CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/sparcv9"
4142
// CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/../../../sparcv9"
4243
// CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/lib/sparcv9"
44+
// CHECK-LD-SPARC64-NOT: "-latomic"
4345
// CHECK-LD-SPARC64-SAME: "-lgcc_s"
4446
// CHECK-LD-SPARC64-SAME: "-lc"
4547
// CHECK-LD-SPARC64-SAME: "-lgcc"
@@ -63,6 +65,7 @@
6365
// CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4"
6466
// CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/../../.."
6567
// CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/lib"
68+
// CHECK-LD-X32-NOT: "-latomic"
6669
// CHECK-LD-X32-SAME: "-lgcc_s"
6770
// CHECK-LD-X32-SAME: "-lc"
6871
// CHECK-LD-X32-SAME: "-lgcc"
@@ -86,6 +89,7 @@
8689
// CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/amd64"
8790
// CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/../../../amd64"
8891
// CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/lib/amd64"
92+
// CHECK-LD-X64-NOT: "-latomic"
8993
// CHECK-LD-X64-SAME: "-lgcc_s"
9094
// CHECK-LD-X64-SAME: "-lc"
9195
// CHECK-LD-X64-SAME: "-lgcc"

0 commit comments

Comments
 (0)