[clang] bb3fc7e - [clang][Mips] Don't report lock-free atomics without ll and sc (#246580)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 01:36:53 PST 2026
Author: Brad Smith
Date: 2026-12-01T04:36:47-05:00
New Revision: bb3fc7ed855e7d65df662e35977eddb3c70de325
URL: https://github.com/llvm/llvm-project/commit/bb3fc7ed855e7d65df662e35977eddb3c70de325
DIFF: https://github.com/llvm/llvm-project/commit/bb3fc7ed855e7d65df662e35977eddb3c70de325.diff
LOG: [clang][Mips] Don't report lock-free atomics without ll and sc (#246580)
MIPS I and the R5900 have no ll and sc, so the backend uses libcalls for
their atomic operations, but Clang still set MaxAtomicInlineWidth from
the ABI and reported 32-bit atomics as always lock-free. The R5900 also
defined the __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros.
Set MaxAtomicInlineWidth to 0 there, unless +forced-atomics makes
native-width atomics lock-free, and only define the
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros when ll and sc are available.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Added:
Modified:
clang/lib/Basic/Targets/Mips.cpp
clang/lib/Basic/Targets/Mips.h
clang/test/Preprocessor/init-mips.c
Removed:
################################################################################
diff --git a/clang/lib/Basic/Targets/Mips.cpp b/clang/lib/Basic/Targets/Mips.cpp
index c4c2b137f8e5e..d9803969150e7 100644
--- a/clang/lib/Basic/Targets/Mips.cpp
+++ b/clang/lib/Basic/Targets/Mips.cpp
@@ -211,18 +211,18 @@ void MipsTargetInfo::getTargetDefines(const LangOptions &Opts,
if (StringRef(CPU).starts_with("octeon"))
Builder.defineMacro("__OCTEON__");
- if (ISALevel != 1) {
+ if (HasLLSC) {
Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
- }
- // 32-bit MIPS processors don't have the necessary lld/scd instructions
- // found in 64-bit processors. In the case of O32 on a 64-bit processor,
- // the instructions exist but using them violates the ABI since they
- // require 64-bit GPRs and O32 only supports 32-bit GPRs.
- if (ABI == "n32" || ABI == "n64")
- Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
+ // 32-bit MIPS processors don't have the necessary lld/scd instructions
+ // found in 64-bit processors. In the case of O32 on a 64-bit processor,
+ // the instructions exist but using them violates the ABI since they
+ // require 64-bit GPRs and O32 only supports 32-bit GPRs.
+ if (ABI == "n32" || ABI == "n64")
+ Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
+ }
}
bool MipsTargetInfo::hasFeature(StringRef Feature) const {
diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index 8113e9be34ec1..bb183b825901a 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -42,6 +42,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
// to 6 for MIPS32 and MIPS64 and 0 for the older ISAs.
unsigned ISALevel = 0;
unsigned ISARev = 0;
+ // MIPS I and the R5900 have no ll and sc.
+ bool HasLLSC = true;
protected:
enum FPModeEnum { FPXX, FP32, FP64 } FPMode;
@@ -394,6 +396,11 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
Features.push_back("+nooddspreg");
}
+ // Without ll and sc, the backend uses libcalls for atomic operations.
+ HasLLSC = ISALevel != 1 && !IsR5900;
+ if (!HasLLSC)
+ MaxAtomicInlineWidth = 0;
+
resetDataLayout();
return true;
diff --git a/clang/test/Preprocessor/init-mips.c b/clang/test/Preprocessor/init-mips.c
index 749eb0d56764e..33184b735faf2 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1967,6 +1967,26 @@
// RUN: | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
// SPFPSET-32:#define _MIPS_SPFPSET 32
+// MIPS I and the R5900 have no ll and sc, so atomic operations are libcalls.
+//
+// RUN: %clang_cc1 -target-cpu mips1 \
+// RUN: -E -dM -triple=mips-none-none < /dev/null \
+// RUN: | FileCheck -match-full-lines -check-prefix NO-LLSC %s
+// RUN: %clang_cc1 -target-cpu r5900 \
+// RUN: -E -dM -triple=mipsel-none-none < /dev/null \
+// RUN: | FileCheck -match-full-lines -check-prefix NO-LLSC %s
+// RUN: %clang_cc1 -target-cpu r5900 -target-abi n64 \
+// RUN: -E -dM -triple=mips64el-none-none < /dev/null \
+// RUN: | FileCheck -match-full-lines -check-prefix NO-LLSC %s
+// NO-LLSC:#define __CLANG_ATOMIC_INT_LOCK_FREE 1
+// NO-LLSC-NOT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_{{[0-9]}} 1
+//
+// RUN: %clang_cc1 -target-cpu mips2 \
+// RUN: -E -dM -triple=mips-none-none < /dev/null \
+// RUN: | FileCheck -match-full-lines -check-prefix LLSC %s
+// LLSC:#define __CLANG_ATOMIC_INT_LOCK_FREE 2
+// LLSC:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+
// RUN: %clang_cc1 -target-cpu mips32 \
// RUN: -E -dM -triple=mips-unknown-netbsd -mrelocation-model pic < /dev/null \
// RUN: | FileCheck -match-full-lines -check-prefix MIPS-ABICALLS-NETBSD %s
More information about the cfe-commits
mailing list