From d93b53b9e54b96cc0a5dc8bdbb934354571e06a2 Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Wed, 6 Oct 2021 12:03:09 +0100 Subject: [PATCH] newlib: provide missing atomic libcalls Provide emulated atomic load & store libcalls for u8, u16 & u32 integer types. This is required when building with Clang as llvm does not lower these operations to native load / stores, where as gcc does. Provide `sync_lock_test_and_set` atomic implementations for all supported integer types. Closes https://github.com/espressif/esp-idf/issues/7591. Closes https://github.com/espressif/esp-idf/issues/7592. --- components/newlib/stdatomic.c | 64 +++++++++++++++++++++++------ tools/ci/check_copyright_ignore.txt | 1 - 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/components/newlib/stdatomic.c b/components/newlib/stdatomic.c index bbd2d17e80..b54d1b5884 100644 --- a/components/newlib/stdatomic.c +++ b/components/newlib/stdatomic.c @@ -1,16 +1,8 @@ -// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ //replacement for gcc built-in functions @@ -208,6 +200,24 @@ CLANG_DECLARE_ALIAS( __sync_bool_compare_and_swap_ ## n ) } \ CLANG_DECLARE_ALIAS( __sync_val_compare_and_swap_ ## n ) +#define SYNC_LOCK_TEST_AND_SET(n, type) type CLANG_ATOMIC_SUFFIX(__sync_lock_test_and_set_ ## n) (type *ptr, type val, ...) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + type ret = *ptr; \ + *ptr = val; \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} +CLANG_DECLARE_ALIAS( __sync_lock_test_and_set_ ## n ) + +#define SYNC_LOCK_RELEASE(n, type) void CLANG_ATOMIC_SUFFIX(__sync_lock_release_ ## n) (type *ptr, ...) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + *ptr = 0; \ + _ATOMIC_EXIT_CRITICAL(state); \ +} +CLANG_DECLARE_ALIAS( __sync_lock_release_ ## n ) + #if !HAS_ATOMICS_32 @@ -267,6 +277,27 @@ SYNC_VAL_CMP_EXCHANGE(1, uint8_t) SYNC_VAL_CMP_EXCHANGE(2, uint16_t) SYNC_VAL_CMP_EXCHANGE(4, uint32_t) +#ifdef __clang__ + +// LLVM has not implemented native atomic load/stores for riscv targets without the Atomic extension +// therfore we provide libcalls here when building with the clang toolchain. LLVM thread: https://reviews.llvm.org/D47553. +ATOMIC_LOAD(1, uint8_t) +ATOMIC_LOAD(2, uint16_t) +ATOMIC_LOAD(4, uint32_t) +ATOMIC_STORE(1, uint8_t) +ATOMIC_STORE(2, uint16_t) +ATOMIC_STORE(4, uint32_t) + +SYNC_LOCK_TEST_AND_SET(1, uint8_t) +SYNC_LOCK_TEST_AND_SET(2, uint16_t) +SYNC_LOCK_TEST_AND_SET(4, uint32_t) + +SYNC_LOCK_RELEASE(1, uint8_t) +SYNC_LOCK_RELEASE(2, uint16_t) +SYNC_LOCK_RELEASE(4, uint32_t) + +#endif + #endif // !HAS_ATOMICS_32 #if !HAS_ATOMICS_64 @@ -303,4 +334,11 @@ SYNC_BOOL_CMP_EXCHANGE(8, uint64_t) SYNC_VAL_CMP_EXCHANGE(8, uint64_t) +#ifdef __clang__ + +SYNC_LOCK_TEST_AND_SET(8, uint64_t) +SYNC_LOCK_RELEASE(8, uint64_t) + +#endif + #endif // !HAS_ATOMICS_64 diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 51b7be4a10..dd5e640d80 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -2029,7 +2029,6 @@ components/newlib/priv_include/esp_time_impl.h components/newlib/pthread.c components/newlib/random.c components/newlib/reent_init.c -components/newlib/stdatomic.c components/newlib/syscalls.c components/newlib/termios.c components/newlib/test/test_atomic.c