Skip to content

Commit ddf35af

Browse files
committed
[libc] Change the GPU to use builtin memory functions
Summary: The GPU build is special in the sense that we always know that up-to-date `clang` is always going to be the compiler. This allows us to rely directly on builtins, which allow us to push a lot of this complexity into the backend. Backend implementations are favored on the GPU because it allows us to do a lot more target specific optimizations. This patch changes over the common memory functions to use builtin versions when building for AMDGPU or NVPTX.
1 parent 959e69a commit ddf35af

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Trivial builtin implementations ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BUILTIN_H
10+
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BUILTIN_H
11+
12+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
13+
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
14+
#include "src/string/memory_utils/utils.h" // Ptr, CPtr
15+
16+
#include <stddef.h> // size_t
17+
18+
namespace LIBC_NAMESPACE {
19+
20+
static_assert(LIBC_HAS_BUILTIN(__builtin_memcpy), "Builtin not defined");
21+
static_assert(LIBC_HAS_BUILTIN(__builtin_memset), "Builtin not defined");
22+
static_assert(LIBC_HAS_BUILTIN(__builtin_memmove), "Builtin not defined");
23+
static_assert(LIBC_HAS_BUILTIN(__builtin_bcmp), "Builtin not defined");
24+
25+
[[maybe_unused]] LIBC_INLINE void
26+
inline_memcpy_builtin(Ptr dst, CPtr src, size_t count, size_t offset = 0) {
27+
__builtin_memcpy(dst + offset, src + offset, count);
28+
}
29+
30+
[[maybe_unused]] LIBC_INLINE void inline_memmove_builtin(Ptr dst, CPtr src,
31+
size_t count) {
32+
__builtin_memmove(dst, src, count);
33+
}
34+
35+
[[maybe_unused]] LIBC_INLINE static void
36+
inline_memset_builtin(Ptr dst, uint8_t value, size_t count, size_t offset = 0) {
37+
__builtin_memset(dst + offset, value, count);
38+
}
39+
40+
[[maybe_unused]] LIBC_INLINE int
41+
inline_bcmp_builtin(CPtr p1, CPtr p2, size_t count, size_t offset = 0) {
42+
return __builtin_bcmp(p1 + offset, p2 + offset, count);
43+
}
44+
45+
} // namespace LIBC_NAMESPACE
46+
47+
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BUILTIN_H

libc/src/string/memory_utils/inline_bcmp.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
2424
#include "src/string/memory_utils/riscv/inline_bcmp.h"
2525
#define LIBC_SRC_STRING_MEMORY_UTILS_BCMP inline_bcmp_riscv
26-
#elif defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_GPU)
26+
// FIXME: The NVPTX builtin for `bcmp` currently does not work.
27+
#elif defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_NVPTX)
2728
#include "src/string/memory_utils/generic/byte_per_byte.h"
2829
#define LIBC_SRC_STRING_MEMORY_UTILS_BCMP inline_bcmp_byte_per_byte
30+
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
31+
#include "src/string/memory_utils/generic/builtin.h"
32+
#define LIBC_SRC_STRING_MEMORY_UTILS_BCMP inline_bcmp_builtin
2933
#else
3034
#error "Unsupported architecture"
3135
#endif

libc/src/string/memory_utils/inline_memcpy.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
2929
#include "src/string/memory_utils/riscv/inline_memcpy.h"
3030
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_riscv
31-
#elif defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_GPU)
31+
#elif defined(LIBC_TARGET_ARCH_IS_ARM)
3232
#include "src/string/memory_utils/generic/byte_per_byte.h"
3333
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_byte_per_byte
34+
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
35+
#include "src/string/memory_utils/generic/builtin.h"
36+
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_builtin
3437
#else
3538
#error "Unsupported architecture"
3639
#endif

libc/src/string/memory_utils/inline_memmove.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
2121
#include "src/string/memory_utils/riscv/inline_memmove.h"
2222
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE inline_memmove_riscv
23-
#elif defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_GPU)
23+
#elif defined(LIBC_TARGET_ARCH_IS_ARM)
2424
#include "src/string/memory_utils/generic/byte_per_byte.h"
2525
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE inline_memmove_byte_per_byte
26+
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
27+
#include "src/string/memory_utils/generic/builtin.h"
28+
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMMOVE inline_memmove_builtin
2629
#else
2730
#error "Unsupported architecture"
2831
#endif

libc/src/string/memory_utils/inline_memset.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
2525
#include "src/string/memory_utils/riscv/inline_memset.h"
2626
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_riscv
27-
#elif defined(LIBC_TARGET_ARCH_IS_ARM) || defined(LIBC_TARGET_ARCH_IS_GPU)
27+
#elif defined(LIBC_TARGET_ARCH_IS_ARM)
2828
#include "src/string/memory_utils/generic/byte_per_byte.h"
2929
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_byte_per_byte
30+
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
31+
#include "src/string/memory_utils/generic/builtin.h"
32+
#define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_builtin
3033
#else
3134
#error "Unsupported architecture"
3235
#endif

0 commit comments

Comments
 (0)