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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "LibC.h" |
10 | |
11 | #pragma omp begin declare target device_type(nohost) |
12 | |
13 | namespace impl { |
14 | int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t); |
15 | } |
16 | |
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); |
24 | } |
25 | } // namespace impl |
26 | #pragma omp end declare variant |
27 | |
28 | #pragma omp begin declare variant match(device = {arch(amdgcn)}) |
29 | |
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); |
34 | |
35 | namespace impl { |
36 | int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t Size) { |
37 | return rpc_fprintf(stdout, Format, Arguments, Size); |
38 | } |
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; |
45 | } |
46 | } // namespace impl |
47 | #endif |
48 | #pragma omp end declare variant |
49 | |
50 | extern "C" { |
51 | |
52 | 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); |
55 | |
56 | for (size_t I = 0; I < count; ++I) |
57 | if (L[I] != R[I]) |
58 | return (int)L[I] - (int)R[I]; |
59 | |
60 | return 0; |
61 | } |
62 | |
63 | 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; |
67 | } |
68 | |
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); |
72 | } |
73 | } |
74 | |
75 | #pragma omp end declare target |
76 | |