https://github.com/brad0 updated 
https://github.com/llvm/llvm-project/pull/246580

>From f6271cb4fc1f252956fa49eaba672ba3042440d2 Mon Sep 17 00:00:00 2001
From: Brad Smith <[email protected]>
Date: Tue, 24 Nov 2026 16:43:49 -0500
Subject: [PATCH] [clang][Mips] Don't report lock-free atomics without ll and
 sc

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, and only define the
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros when ll and sc are available.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 clang/lib/Basic/Targets/Mips.cpp    | 16 ++++++++--------
 clang/lib/Basic/Targets/Mips.h      | 10 ++++++++++
 clang/test/Preprocessor/init-mips.c | 20 ++++++++++++++++++++
 3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Basic/Targets/Mips.cpp b/clang/lib/Basic/Targets/Mips.cpp
index c4c2b137f8e5e4..d9803969150e75 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 16ad603dbbb985..c5c16faeccc556 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;
@@ -316,6 +318,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
     bool OddSpregGiven = false;
     bool StrictAlign = false;
     bool FpGiven = false;
+    bool IsR5900 = false;
 
     for (const auto &Feature : Features) {
       if (Feature == "+single-float")
@@ -361,6 +364,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
         IsNoABICalls = true;
       else if (Feature == "+use-indirect-jump-hazard")
         UseIndirectJumpHazard = true;
+      else if (Feature == "+r5900")
+        IsR5900 = true;
       else if (Feature == "+nooddspreg") {
         NoOddSpreg = true;
         OddSpregGiven = false;
@@ -381,6 +386,11 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public 
TargetInfo {
       Features.push_back("+fp64");
     }
 
+    // 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 78391a73d40c0f..f97ca8446840ab 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1935,6 +1935,26 @@
 // MIPS-XXR6:#define __mips_fpr 64
 // MIPS-XXR6:#define __mips_nan2008 1
 
+// 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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to