Skip to content

Commit

Permalink
[libc] Add compiler, builtin and feature detection
Browse files Browse the repository at this point in the history
This is a first step to support GCC. This patch adds support for builtin and feature detection.

Differential Revision: https://reviews.llvm.org/D139712
  • Loading branch information
gchatelet committed Dec 13, 2022
1 parent f354716 commit f52ca09
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 35 deletions.
25 changes: 20 additions & 5 deletions libc/src/__support/CMakeLists.txt
Expand Up @@ -13,12 +13,17 @@ add_header_library(
)

add_header_library(
builtin_wrappers
compiler_features
HDRS
builtin_wrappers.h
compiler_features.h
)

add_header_library(
sanitizer
HDRS
sanitizer.h
DEPENDS
.named_pair
libc.src.__support.CPP.type_traits
libc.src.__support.compiler_features
)

add_header_library(
Expand All @@ -27,7 +32,17 @@ add_header_library(
architectures.h
common.h
endian.h
sanitizer.h
)

add_header_library(
builtin_wrappers
HDRS
builtin_wrappers.h
DEPENDS
.named_pair
libc.src.__support.common
libc.src.__support.compiler_features
libc.src.__support.CPP.type_traits
)

add_header_library(
Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Expand Up @@ -8,6 +8,8 @@ add_header_library(
bit
HDRS
bit.h
DEPENDS
libc.src.__support.compiler_features
)

add_header_library(
Expand Down
10 changes: 4 additions & 6 deletions libc/src/__support/CPP/bit.h
Expand Up @@ -9,19 +9,17 @@
#ifndef LLVM_LIBC_SUPPORT_CPP_BIT_H
#define LLVM_LIBC_SUPPORT_CPP_BIT_H

#include "src/__support/compiler_features.h"

namespace __llvm_libc::cpp {

#if defined __has_builtin
#if __has_builtin(__builtin_bit_cast)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_bit_cast)
#define LLVM_LIBC_HAS_BUILTIN_BIT_CAST
#endif
#endif

#if defined __has_builtin
#if __has_builtin(__builtin_memcpy_inline)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
#endif
#endif

// This function guarantees the bitcast to be optimized away by the compiler for
// GCC >= 8 and Clang >= 6.
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/FPUtil/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@ add_header_library(
DEPENDS
libc.include.fenv
libc.src.__support.common
libc.src.__support.compiler_features
)

add_header_library(
Expand Down
9 changes: 5 additions & 4 deletions libc/src/__support/builtin_wrappers.h
Expand Up @@ -12,6 +12,7 @@

#include "named_pair.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/compiler_features.h"

namespace __llvm_libc {

Expand Down Expand Up @@ -80,7 +81,7 @@ add_with_carry(T a, T b, T carry_in) {
return {sum, carry_out};
}

#if __has_builtin(__builtin_addc)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_addc)
// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins

template <>
Expand Down Expand Up @@ -128,7 +129,7 @@ add_with_carry<unsigned long long>(unsigned long long a, unsigned long long b,
return result;
}

#endif // __has_builtin(__builtin_addc)
#endif // LLVM_LIBC_HAS_BUILTIN(__builtin_addc)

// Subtract with borrow
DEFINE_NAMED_PAIR_TEMPLATE(DiffBorrow, diff, borrow);
Expand All @@ -143,7 +144,7 @@ sub_with_borrow(T a, T b, T borrow_in) {
return {diff, borrow_out};
}

#if __has_builtin(__builtin_subc)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_subc)
// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins

template <>
Expand Down Expand Up @@ -191,7 +192,7 @@ sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
return result;
}

#endif // __has_builtin(__builtin_subc)
#endif // LLVM_LIBC_HAS_BUILTIN(__builtin_subc)

} // namespace __llvm_libc

Expand Down
41 changes: 41 additions & 0 deletions libc/src/__support/compiler_features.h
@@ -0,0 +1,41 @@
//===-- Compile time compiler detection -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SUPPORT_COMPILER_FEATURES_H
#define LLVM_LIBC_SUPPORT_COMPILER_FEATURES_H

#if defined(__clang__)
#define LLVM_LIBC_COMPILER_CLANG
#endif

#if defined(__GNUC__) && !defined(__clang__)
#define LLVM_LIBC_COMPILER_GCC
#endif

#if defined(_MSC_VER)
#define LLVM_LIBC_COMPILER_MSC
#endif

// Compiler builtin-detection.
// clang.llvm.org/docs/LanguageExtensions.html#has-builtin
#if defined(LLVM_LIBC_COMPILER_CLANG) || \
(defined(LLVM_LIBC_COMPILER_GCC) && (__GNUC__ >= 10))
#define LLVM_LIBC_HAS_BUILTIN(BUILTIN) __has_builtin(BUILTIN)
#else
#define LLVM_LIBC_HAS_BUILTIN(BUILTIN) 0
#endif

// Compiler feature-detection.
// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
#if defined(LLVM_LIBC_COMPILER_CLANG)
#define LLVM_LIBC_HAS_FEATURE(FEATURE) __has_feature(FEATURE)
#else
#define LLVM_LIBC_HAS_FEATURE(FEATURE) 0
#endif

#endif // LLVM_LIBC_SUPPORT_COMPILER_FEATURES_H
12 changes: 4 additions & 8 deletions libc/src/__support/sanitizer.h
Expand Up @@ -9,11 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_SANITIZER_H
#define LLVM_LIBC_SRC_SUPPORT_SANITIZER_H

#ifdef __has_feature
#define LLVM_LIBC_HAVE_FEATURE(f) __has_feature(f)
#else
#define LLVM_LIBC_HAVE_FEATURE(f) 0
#endif
#include "src/__support/compiler_features.h"

// MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
// a compiler instrumentation module and a run-time library.
Expand All @@ -25,7 +21,7 @@
#define LLVM_LIBC_HAVE_MEMORY_SANITIZER 1
#elif defined(__SANITIZE_MEMORY__)
#define LLVM_LIBC_HAVE_MEMORY_SANITIZER 1
#elif !defined(__native_client__) && LLVM_LIBC_HAVE_FEATURE(memory_sanitizer)
#elif !defined(__native_client__) && LLVM_LIBC_HAS_FEATURE(memory_sanitizer)
#define LLVM_LIBC_HAVE_MEMORY_SANITIZER 1
#endif

Expand All @@ -38,14 +34,14 @@
#define LLVM_LIBC_HAVE_ADDRESS_SANITIZER 1
#elif defined(__SANITIZE_ADDRESS__)
#define LLVM_LIBC_HAVE_ADDRESS_SANITIZER 1
#elif LLVM_LIBC_HAVE_FEATURE(address_sanitizer)
#elif LLVM_LIBC_HAS_FEATURE(address_sanitizer)
#define LLVM_LIBC_HAVE_ADDRESS_SANITIZER 1
#endif

// HWAddressSanitizer (HWASan) is a fast, low memory overhead error detector.
#ifdef LLVM_LIBC_HAVE_HWADDRESS_SANITIZER
#error "LLVM_LIBC_HAVE_HWADDRESS_SANITIZER cannot be directly set."
#elif LLVM_LIBC_HAVE_FEATURE(hwaddress_sanitizer)
#elif LLVM_LIBC_HAS_FEATURE(hwaddress_sanitizer)
#define LLVM_LIBC_HAVE_HWADDRESS_SANITIZER 1
#endif

Expand Down
1 change: 1 addition & 0 deletions libc/src/string/memory_utils/CMakeLists.txt
Expand Up @@ -15,6 +15,7 @@ add_header_library(
utils.h
DEPS
libc.src.__support.common
libc.src.__support.compiler_features
libc.src.__support.CPP.bit
libc.src.__support.CPP.cstddef
libc.src.__support.CPP.type_traits
Expand Down
9 changes: 3 additions & 6 deletions libc/src/string/memory_utils/utils.h
Expand Up @@ -12,6 +12,7 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/cstddef.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/compiler_features.h"

#include <stddef.h> // size_t
#include <stdint.h> // intptr_t / uintptr_t
Expand Down Expand Up @@ -80,17 +81,13 @@ template <size_t alignment, typename T> static T *assume_aligned(T *ptr) {
return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, alignment));
}

#if defined __has_builtin
#if __has_builtin(__builtin_memcpy_inline)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
#endif
#endif

#if defined __has_builtin
#if __has_builtin(__builtin_memset_inline)
#if LLVM_LIBC_HAS_BUILTIN(__builtin_memset_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMSET_INLINE
#endif
#endif

// Performs a constant count copy.
template <size_t Size>
Expand Down
1 change: 0 additions & 1 deletion libc/src/string/stpcpy.cpp
Expand Up @@ -11,7 +11,6 @@
#include "src/string/string_utils.h"

#include "src/__support/common.h"
#include "src/__support/sanitizer.h"

namespace __llvm_libc {

Expand Down
5 changes: 3 additions & 2 deletions libc/test/src/string/memory_utils/CMakeLists.txt
Expand Up @@ -9,8 +9,9 @@ add_libc_unittest(
${LIBC_COMPILE_OPTIONS_NATIVE}
-ffreestanding
DEPENDS
libc.src.string.memory_utils.memory_utils
libc.src.__support.compiler_features
libc.src.__support.CPP.array
libc.src.__support.CPP.span
libc.src.__support.CPP.cstddef
libc.src.__support.CPP.span
libc.src.string.memory_utils.memory_utils
)
3 changes: 2 additions & 1 deletion libc/test/src/string/memory_utils/memory_check_utils.h
Expand Up @@ -10,13 +10,14 @@
#define LIBC_TEST_SRC_STRING_MEMORY_UTILS_MEMORY_CHECK_UTILS_H

#include "src/__support/CPP/span.h"
#include "src/__support/compiler_features.h"
#include "src/string/memory_utils/utils.h"
#include <assert.h> // assert
#include <stddef.h> // size_t
#include <stdint.h> // uintxx_t
#include <stdlib.h> // malloc/free

#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#if LLVM_LIBC_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#include <sanitizer/asan_interface.h>
#define ASAN_POISON_MEMORY_REGION(addr, size) \
__asan_poison_memory_region((addr), (size))
Expand Down
20 changes: 18 additions & 2 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Expand Up @@ -60,13 +60,23 @@ cc_library(

############################## Support libraries #############################

libc_support_library(
name = "__support_compiler_features",
hdrs = ["src/__support/compiler_features.h"],
)

libc_support_library(
name = "__support_sanitizer",
hdrs = ["src/__support/sanitizer.h"],
deps = [":__support_compiler_features"],
)

libc_support_library(
name = "__support_common",
hdrs = [
"src/__support/architectures.h",
"src/__support/common.h",
"src/__support/endian.h",
"src/__support/sanitizer.h",
],
)

Expand All @@ -79,7 +89,10 @@ libc_support_library(
libc_support_library(
name = "__support_cpp_bit",
hdrs = ["src/__support/CPP/bit.h"],
deps = [":libc_root"],
deps = [
":__support_compiler_features",
":libc_root",
],
)

libc_support_library(
Expand Down Expand Up @@ -255,6 +268,7 @@ libc_support_library(
name = "__support_builtin_wrappers",
hdrs = ["src/__support/builtin_wrappers.h"],
deps = [
":__support_common",
":__support_cpp_type_traits",
":__support_named_pair",
":libc_root",
Expand Down Expand Up @@ -294,7 +308,9 @@ libc_support_library(
],
deps = [
":__support_common",
":__support_compiler_features",
":__support_fputil_fp_bits",
":__support_sanitizer",
":libc_root",
],
)
Expand Down

0 comments on commit f52ca09

Please sign in to comment.