Skip to content

Commit

Permalink
Bring back the fix for GCC atomic builtins
Browse files Browse the repository at this point in the history
Commit d53cbfe ("Update to LLVM 14.0.6") removed the patch that
adapts atomic.c to use GCC builtins instead of clang, however its
functionality is still required.
This change brings back this functionality in a cleaner and more
future-proof way, by providing a header that implements clang builtins
in terms of GCC ones.

Signed-off-by: Andrei Tatar <[email protected]>
Reviewed-by: Stefan Jumarea <[email protected]>
Reviewed-by: Maria Sfiraiala <[email protected]>
Reviewed-by: Razvan Deaconescu <[email protected]>
Approved-by: Eduard Vintilă <[email protected]>
Tested-by: Unikraft CI <[email protected]>
GitHub-Closes: #12
  • Loading branch information
andreittr authored and unikraft-bot committed Jun 1, 2023
1 parent ccf1863 commit 55977d5
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.uk
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ CINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib/builtins
CXXINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib
CXXINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib/builtins

LIBCOMPILER_RT_CINCLUDES-$(call have_gcc) += -iquote$(LIBCOMPILER_RT_BASE)/include

################################################################################
# Global flags
################################################################################
Expand Down
106 changes: 106 additions & 0 deletions include/gcc_atomics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Author(s): Andrei Tatar <[email protected]>
*
* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/* GCC and clang have different builtins for atomic operations.
* lib-compiler-rt, being part of LLVM, naturally uses the clang-specific
* __c11_* builtins. This header provides translations between the two, allowing
* compiler-rt to be built with GCC.
*
* Each #define is conditioned by the unavailability of the __c11* builtin,
* for future-proofing if/when GCC implements them.
*/

#ifndef __UKPATCH_GCC_ATOMICS_H__
#define __UKPATCH_GCC_ATOMICS_H__

#ifndef __c11_atomic_compare_exchange_strong
#define __c11_atomic_compare_exchange_strong(object, expected, desired, success_order, fail_order) \
__atomic_compare_exchange_n(object, expected, desired, 0, success_order, fail_order)
#endif

#ifndef __c11_atomic_compare_exchange_weak
#define __c11_atomic_compare_exchange_weak(object, expected, desired, success_order, fail_order) \
__atomic_compare_exchange_n(object, expected, desired, 1, success_order, fail_order)
#endif

#ifndef __c11_atomic_exchange
#define __c11_atomic_exchange(object, desired, order) \
__atomic_exchange_n(object, desired, order)
#endif

#ifndef __c11_atomic_fetch_add
#define __c11_atomic_fetch_add(object, operand, order) \
__atomic_fetch_add(object, operand, order)
#endif

#ifndef __c11_atomic_fetch_and
#define __c11_atomic_fetch_and(object, operand, order) \
__atomic_fetch_and(object, operand, order)
#endif

#ifndef __c11_atomic_fetch_nand
#define __c11_atomic_fetch_nand(object, operand, order) \
__atomic_fetch_and(object, operand, order)
#endif

#ifndef __c11_atomic_fetch_or
#define __c11_atomic_fetch_or(object, operand, order) \
__atomic_fetch_or(object, operand, order)
#endif

#ifndef __c11_atomic_fetch_sub
#define __c11_atomic_fetch_sub(object, operand, order) \
__atomic_fetch_sub(object, operand, order)
#endif

#ifndef __c11_atomic_fetch_xor
#define __c11_atomic_fetch_xor(object, operand, order) \
__atomic_fetch_xor(object, operand, order)
#endif

#ifndef __c11_atomic_load
#define __c11_atomic_load(object, order) \
__atomic_load_n(object, order)
#endif

#ifndef __c11_atomic_store
#define __c11_atomic_store(object, desired, order) \
__atomic_store_n(object, desired, order)
#endif

#ifndef __c11_atomic_thread_fence
#define __c11_atomic_thread_fence(order) \
__atomic_thread_fence(order)
#endif

#endif /* __UKPATCH_GCC_ATOMICS_H__ */
32 changes: 32 additions & 0 deletions patches/0001-Add-header-for-GCC-atomic-builtins.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From 4d39ccc51bac5fb2356d2bf7e29a919e8430c96d Mon Sep 17 00:00:00 2001
From: Andrei Tatar <[email protected]>
Date: Wed, 24 May 2023 13:05:03 +0300
Subject: [PATCH] Add header for GCC atomic builtins

compiler-rt relies on clang's __c11_atomic builtins which GCC does not
have. This patch adds a unikraft header which implements these builtins
in terms of GCC ones.

Signed-off-by: Andrei Tatar <[email protected]>
---
compiler-rt/lib/builtins/atomic.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/compiler-rt/lib/builtins/atomic.c b/compiler-rt/lib/builtins/atomic.c
index 8dc2f4fc0..59667f888 100644
--- a/lib/builtins/atomic.c
+++ b/lib/builtins/atomic.c
@@ -29,6 +29,10 @@

#include "assembly.h"

+#if defined(__GNUC__) && !defined(__clang__)
+#include "gcc_atomics.h"
+#endif /* __GNUC__ && !defined(__clang__) */
+
// We use __builtin_mem* here to avoid dependencies on libc-provided headers.
#define memcpy __builtin_memcpy
#define memcmp __builtin_memcmp
--
2.40.1

0 comments on commit 55977d5

Please sign in to comment.