Bump version to 19.1.0-rc3
[llvm-project.git] / offload / DeviceRTL / src / LibC.cpp
blob4bca5d29643fe943b579707068c307a67c4e8c22
1 //===------- LibC.cpp - Simple implementation of libc functions --- C++ ---===//
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 //===----------------------------------------------------------------------===//
9 #include "LibC.h"
11 #pragma omp begin declare target device_type(nohost)
13 namespace impl {
14 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t);
17 #pragma omp begin declare variant match( \
18 device = {arch(nvptx, nvptx64)}, \
19 implementation = {extension(match_any)})
20 extern "C" int32_t vprintf(const char *, void *);
21 namespace impl {
22 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
23 return vprintf(Format, Arguments);
25 } // namespace impl
26 #pragma omp end declare variant
28 #pragma omp begin declare variant match(device = {arch(amdgcn)})
30 #ifdef OMPTARGET_HAS_LIBC
31 // TODO: Remove this handling once we have varargs support.
32 extern "C" struct FILE *stdout;
33 extern "C" int32_t rpc_fprintf(FILE *, const char *, void *, uint64_t);
35 namespace impl {
36 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
37 return rpc_fprintf(stdout, Format, Arguments, Size);
39 } // namespace impl
40 #else
41 // We do not have a vprintf implementation for AMD GPU so we use a stub.
42 namespace impl {
43 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
44 return -1;
46 } // namespace impl
47 #endif
48 #pragma omp end declare variant
50 extern "C" {
52 [[gnu::weak]] int memcmp(const void *lhs, const void *rhs, size_t count) {
53 auto *L = reinterpret_cast<const unsigned char *>(lhs);
54 auto *R = reinterpret_cast<const unsigned char *>(rhs);
56 for (size_t I = 0; I < count; ++I)
57 if (L[I] != R[I])
58 return (int)L[I] - (int)R[I];
60 return 0;
63 [[gnu::weak]] void memset(void *dst, int C, size_t count) {
64 auto *dstc = reinterpret_cast<char *>(dst);
65 for (size_t I = 0; I < count; ++I)
66 dstc[I] = C;
69 /// printf() calls are rewritten by CGGPUBuiltin to __llvm_omp_vprintf
70 int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
71 return impl::omp_vprintf(Format, Arguments, Size);
75 #pragma omp end declare target