From a6afa9e6b0d9a08ecc878e62343a2af80ff30435 Mon Sep 17 00:00:00 2001 From: Rainer Orth Date: Thu, 10 Feb 2022 12:40:32 +0100 Subject: [PATCH] [Driver] Use libatomic for 32-bit SPARC atomics support Even after 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 --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 2 +- clang/lib/Driver/ToolChains/CommonArgs.h | 2 ++ clang/lib/Driver/ToolChains/Solaris.cpp | 7 +++++++ clang/test/Driver/solaris-ld.c | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 40364d23c559..71f5bd7cb282 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -756,7 +756,7 @@ static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args, return false; } -static const char *getAsNeededOption(const ToolChain &TC, bool as_needed) { +const char *tools::getAsNeededOption(const ToolChain &TC, bool as_needed) { assert(!TC.getTriple().isOSAIX() && "AIX linker does not support any form of --as-needed option yet."); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h index 646fa76949b7..23012dc247e4 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.h +++ b/clang/lib/Driver/ToolChains/CommonArgs.h @@ -117,6 +117,8 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, bool ForceStaticHostRuntime = false, bool IsOffloadingHost = false, bool GompNeedsRT = false); +const char *getAsNeededOption(const ToolChain &TC, bool as_needed); + llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args); llvm::opt::Arg *getLastProfileSampleUseArg(const llvm::opt::ArgList &Args); diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp index 4d1af094f481..24f18b92dd66 100644 --- a/clang/lib/Driver/ToolChains/Solaris.cpp +++ b/clang/lib/Driver/ToolChains/Solaris.cpp @@ -132,6 +132,13 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-lssp_nonshared"); CmdArgs.push_back("-lssp"); } + // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so + // forcibly link with libatomic as a workaround. + if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) { + CmdArgs.push_back(getAsNeededOption(getToolChain(), true)); + CmdArgs.push_back("-latomic"); + CmdArgs.push_back(getAsNeededOption(getToolChain(), false)); + } CmdArgs.push_back("-lgcc_s"); CmdArgs.push_back("-lc"); if (!Args.hasArg(options::OPT_shared)) { diff --git a/clang/test/Driver/solaris-ld.c b/clang/test/Driver/solaris-ld.c index 59d03c4d8225..d40e5dac2da9 100644 --- a/clang/test/Driver/solaris-ld.c +++ b/clang/test/Driver/solaris-ld.c @@ -17,6 +17,7 @@ // CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2" // CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/../../.." // CHECK-LD-SPARC32-SAME: "-L[[SYSROOT]]/usr/lib" +// CHECK-LD-SPARC32-SAME: "-zignore" "-latomic" "-zrecord" // CHECK-LD-SPARC32-SAME: "-lgcc_s" // CHECK-LD-SPARC32-SAME: "-lc" // CHECK-LD-SPARC32-SAME: "-lgcc" @@ -40,6 +41,7 @@ // CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/sparcv9" // CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/../../../sparcv9" // CHECK-LD-SPARC64-SAME: "-L[[SYSROOT]]/usr/lib/sparcv9" +// CHECK-LD-SPARC64-NOT: "-latomic" // CHECK-LD-SPARC64-SAME: "-lgcc_s" // CHECK-LD-SPARC64-SAME: "-lc" // CHECK-LD-SPARC64-SAME: "-lgcc" @@ -63,6 +65,7 @@ // CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4" // CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/../../.." // CHECK-LD-X32-SAME: "-L[[SYSROOT]]/usr/lib" +// CHECK-LD-X32-NOT: "-latomic" // CHECK-LD-X32-SAME: "-lgcc_s" // CHECK-LD-X32-SAME: "-lc" // CHECK-LD-X32-SAME: "-lgcc" @@ -86,6 +89,7 @@ // CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/amd64" // CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/gcc/4.9/lib/gcc/i386-pc-solaris2.11/4.9.4/../../../amd64" // CHECK-LD-X64-SAME: "-L[[SYSROOT]]/usr/lib/amd64" +// CHECK-LD-X64-NOT: "-latomic" // CHECK-LD-X64-SAME: "-lgcc_s" // CHECK-LD-X64-SAME: "-lc" // CHECK-LD-X64-SAME: "-lgcc"