llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang @llvm/pr-subscribers-backend-mips Author: Brad Smith (brad0) <details> <summary>Changes</summary> 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. --- Full diff: https://github.com/llvm/llvm-project/pull/246580.diff 3 Files Affected: - (modified) clang/lib/Basic/Targets/Mips.cpp (+8-8) - (modified) clang/lib/Basic/Targets/Mips.h (+14) - (modified) clang/test/Preprocessor/init-mips.c (+27) ``````````diff 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..e6788dbbe18d3e 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,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo { bool OddSpregGiven = false; bool StrictAlign = false; bool FpGiven = false; + bool IsR5900 = false; + bool HasForcedAtomics = false; for (const auto &Feature : Features) { if (Feature == "+single-float") @@ -361,6 +365,10 @@ 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 == "+forced-atomics") + HasForcedAtomics = true; else if (Feature == "+nooddspreg") { NoOddSpreg = true; OddSpregGiven = false; @@ -381,6 +389,12 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo { Features.push_back("+fp64"); } + // Without ll and sc, the backend uses libcalls for atomic operations, + // unless forced-atomics makes native-width atomics lock-free. + HasLLSC = ISALevel != 1 && !IsR5900; + if (!HasLLSC && !HasForcedAtomics) + MaxAtomicInlineWidth = 0; + resetDataLayout(); return true; diff --git a/clang/test/Preprocessor/init-mips.c b/clang/test/Preprocessor/init-mips.c index 78391a73d40c0f..d7c39c4ed8855d 100644 --- a/clang/test/Preprocessor/init-mips.c +++ b/clang/test/Preprocessor/init-mips.c @@ -1935,6 +1935,33 @@ // 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 +// unless forced-atomics is given. +// +// 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 mips1 -target-feature +forced-atomics \ +// RUN: -E -dM -triple=mips-none-none < /dev/null \ +// RUN: | FileCheck -match-full-lines -check-prefix FORCED-ATOMICS %s +// FORCED-ATOMICS:#define __CLANG_ATOMIC_INT_LOCK_FREE 2 +// FORCED-ATOMICS-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 `````````` </details> https://github.com/llvm/llvm-project/pull/246580 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
