Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions clang/lib/Headers/llvm_libc_wrappers/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#else
#define __LIBC_ATTRS
#endif

#pragma omp begin declare target

#include <llvm-libc-decls/assert.h>

#pragma omp end declare target
// TODO: Define these for CUDA / HIP.

#undef __LIBC_ATTRS

Expand Down
118 changes: 3 additions & 115 deletions clang/lib/Headers/llvm_libc_wrappers/ctype.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,128 +13,16 @@
#error "This file is for GPU offloading compilation only"
#endif

// The GNU headers like to define 'toupper' and 'tolower' redundantly. This is
// necessary to prevent it from doing that and remapping our implementation.
#if (defined(__NVPTX__) || defined(__AMDGPU__)) && defined(__GLIBC__)
#pragma push_macro("__USE_EXTERN_INLINES")
#undef __USE_EXTERN_INLINES
#endif

#include_next <ctype.h>

#if (defined(__NVPTX__) || defined(__AMDGPU__)) && defined(__GLIBC__)
#pragma pop_macro("__USE_EXTERN_INLINES")
#endif

#if __has_include(<llvm-libc-decls/ctype.h>)

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#else
#define __LIBC_ATTRS
#endif

// The GNU headers like to provide these as macros, we need to undefine them so
// they do not conflict with the following definitions for the GPU.

#pragma push_macro("isalnum")
#pragma push_macro("isalpha")
#pragma push_macro("isascii")
#pragma push_macro("isblank")
#pragma push_macro("iscntrl")
#pragma push_macro("isdigit")
#pragma push_macro("isgraph")
#pragma push_macro("islower")
#pragma push_macro("isprint")
#pragma push_macro("ispunct")
#pragma push_macro("isspace")
#pragma push_macro("isupper")
#pragma push_macro("isxdigit")
#pragma push_macro("toascii")
#pragma push_macro("tolower")
#pragma push_macro("toupper")
#pragma push_macro("isalnum_l")
#pragma push_macro("isalpha_l")
#pragma push_macro("isascii_l")
#pragma push_macro("isblank_l")
#pragma push_macro("iscntrl_l")
#pragma push_macro("isdigit_l")
#pragma push_macro("isgraph_l")
#pragma push_macro("islower_l")
#pragma push_macro("isprint_l")
#pragma push_macro("ispunct_l")
#pragma push_macro("isspace_l")
#pragma push_macro("isupper_l")
#pragma push_macro("isxdigit_l")

#undef isalnum
#undef isalpha
#undef isascii
#undef iscntrl
#undef isdigit
#undef islower
#undef isgraph
#undef isprint
#undef ispunct
#undef isspace
#undef isupper
#undef isblank
#undef isxdigit
#undef toascii
#undef tolower
#undef toupper
#undef isalnum_l
#undef isalpha_l
#undef iscntrl_l
#undef isdigit_l
#undef islower_l
#undef isgraph_l
#undef isprint_l
#undef ispunct_l
#undef isspace_l
#undef isupper_l
#undef isblank_l
#undef isxdigit_l

#pragma omp begin declare target

#include <llvm-libc-decls/ctype.h>

#pragma omp end declare target

// Restore the original macros when compiling on the host.
#if !defined(__NVPTX__) && !defined(__AMDGPU__)
#pragma pop_macro("isalnum")
#pragma pop_macro("isalpha")
#pragma pop_macro("isascii")
#pragma pop_macro("isblank")
#pragma pop_macro("iscntrl")
#pragma pop_macro("isdigit")
#pragma pop_macro("isgraph")
#pragma pop_macro("islower")
#pragma pop_macro("isprint")
#pragma pop_macro("ispunct")
#pragma pop_macro("isspace")
#pragma pop_macro("isupper")
#pragma pop_macro("isxdigit")
#pragma pop_macro("toascii")
#pragma pop_macro("tolower")
#pragma pop_macro("toupper")
#pragma pop_macro("isalnum_l")
#pragma pop_macro("isalpha_l")
#pragma pop_macro("isascii_l")
#pragma pop_macro("isblank_l")
#pragma pop_macro("iscntrl_l")
#pragma pop_macro("isdigit_l")
#pragma pop_macro("isgraph_l")
#pragma pop_macro("islower_l")
#pragma pop_macro("isprint_l")
#pragma pop_macro("ispunct_l")
#pragma pop_macro("isspace_l")
#pragma pop_macro("isupper_l")
#pragma pop_macro("isxdigit_l")
#endif
// TODO: Define these for CUDA / HIP.

#undef __LIBC_ATTRS

#endif

#endif // __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__
8 changes: 3 additions & 5 deletions clang/lib/Headers/llvm_libc_wrappers/inttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#else
#define __LIBC_ATTRS
#endif

#pragma omp begin declare target

#include <llvm-libc-decls/inttypes.h>

#pragma omp end declare target
// TODO: Define these for CUDA / HIP.

#undef __LIBC_ATTRS

Expand Down

This file was deleted.

48 changes: 10 additions & 38 deletions clang/lib/Headers/llvm_libc_wrappers/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,19 @@
//
//===----------------------------------------------------------------------===//

#ifndef __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__
#define __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__

#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__)
#error "This file is for GPU offloading compilation only"
#endif

#include_next <stdio.h>

// In some old versions of glibc, other standard headers sometimes define
// special macros (e.g., __need_FILE) before including stdio.h to cause stdio.h
// to produce special definitions. Future includes of stdio.h when those
// special macros are undefined are expected to produce the normal definitions
// from stdio.h.
//
// We do not apply our include guard (__CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__)
// unconditionally to the above include_next. Otherwise, after an occurrence of
// the first glibc stdio.h use case described above, the include_next would be
// skipped for remaining includes of stdio.h, leaving required symbols
// undefined.
//
// We make the following assumptions to handle all use cases:
//
// 1. If the above include_next produces special glibc definitions, then (a) it
// does not produce the normal definitions that we must intercept below, (b)
// the current file was included from a glibc header that already defined
// __GLIBC__ (usually by including glibc's <features.h>), and (c) the above
// include_next does not define _STDIO_H. In that case, we skip the rest of
// the current file and don't guard against future includes.
// 2. If the above include_next produces the normal stdio.h definitions, then
// either (a) __GLIBC__ is not defined because C headers are from some other
// libc implementation or (b) the above include_next defines _STDIO_H to
// prevent the above include_next from having any effect in the future.
#if !defined(__GLIBC__) || defined(_STDIO_H)

#ifndef __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__
#define __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__

#if __has_include(<llvm-libc-decls/stdio.h>)

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#else
#define __LIBC_ATTRS
#endif

// Some headers provide these as macros. Temporarily undefine them so they do
Expand All @@ -60,21 +34,19 @@

#pragma omp begin declare target

#include <llvm-libc-decls/stdio.h>
__LIBC_ATTRS extern FILE *stderr;
__LIBC_ATTRS extern FILE *stdin;
__LIBC_ATTRS extern FILE *stdout;

#pragma omp end declare target

#undef __LIBC_ATTRS

// Restore the original macros when compiling on the host.
#if !defined(__NVPTX__) && !defined(__AMDGPU__)
#pragma pop_macro("stdout")
#pragma pop_macro("stderr")
#pragma pop_macro("stdin")
#pragma pop_macro("stdout")
#endif

#endif
#undef __LIBC_ATTRS

#endif // __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__

#endif
27 changes: 3 additions & 24 deletions clang/lib/Headers/llvm_libc_wrappers/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,18 @@

#include_next <stdlib.h>

#if __has_include(<llvm-libc-decls/stdlib.h>)

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#else
#define __LIBC_ATTRS
#endif

#pragma omp begin declare target

// The LLVM C library uses these named types so we forward declare them.
typedef void (*__atexithandler_t)(void);
typedef int (*__search_compare_t)(const void *, const void *);
typedef int (*__qsortcompare_t)(const void *, const void *);
typedef int (*__qsortrcompare_t)(const void *, const void *, void *);

// Enforce ABI compatibility with the structs used by the LLVM C library.
_Static_assert(__builtin_offsetof(div_t, quot) == 0, "ABI mismatch!");
_Static_assert(__builtin_offsetof(ldiv_t, quot) == 0, "ABI mismatch!");
_Static_assert(__builtin_offsetof(lldiv_t, quot) == 0, "ABI mismatch!");

#if defined(__GLIBC__) && __cplusplus >= 201103L
#define at_quick_exit atexit
#endif

#include <llvm-libc-decls/stdlib.h>

#if defined(__GLIBC__) && __cplusplus >= 201103L
#undef at_quick_exit
#endif
// TODO: Define these for CUDA / HIP.

#pragma omp end declare target

#undef __LIBC_ATTRS

#endif

#endif // __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__
72 changes: 2 additions & 70 deletions clang/lib/Headers/llvm_libc_wrappers/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,14 @@

#include_next <string.h>

#if __has_include(<llvm-libc-decls/string.h>)

#if defined(__HIP__) || defined(__CUDA__)
#define __LIBC_ATTRS __attribute__((device))
#endif

#pragma omp begin declare target

// The GNU headers provide C++ standard compliant headers when in C++ mode and
// the LLVM libc does not. We need to manually provide the definitions using the
// same prototypes.
#if defined(__cplusplus) && defined(__GLIBC__) && \
defined(__CORRECT_ISO_CPP_STRING_H_PROTO)

#ifndef __LIBC_ATTRS
#define __LIBC_ATTRS
#endif

extern "C" {
void *memccpy(void *__restrict, const void *__restrict, int,
size_t) __LIBC_ATTRS;
int memcmp(const void *, const void *, size_t) __LIBC_ATTRS;
void *memcpy(void *__restrict, const void *__restrict, size_t) __LIBC_ATTRS;
void *memmem(const void *, size_t, const void *, size_t) __LIBC_ATTRS;
void *memmove(void *, const void *, size_t) __LIBC_ATTRS;
void *mempcpy(void *__restrict, const void *__restrict, size_t) __LIBC_ATTRS;
void *memset(void *, int, size_t) __LIBC_ATTRS;
char *stpcpy(char *__restrict, const char *__restrict) __LIBC_ATTRS;
char *stpncpy(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
char *strcat(char *__restrict, const char *__restrict) __LIBC_ATTRS;
int strcmp(const char *, const char *) __LIBC_ATTRS;
int strcoll(const char *, const char *) __LIBC_ATTRS;
char *strcpy(char *__restrict, const char *__restrict) __LIBC_ATTRS;
size_t strcspn(const char *, const char *) __LIBC_ATTRS;
char *strdup(const char *) __LIBC_ATTRS;
size_t strlen(const char *) __LIBC_ATTRS;
char *strncat(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
int strncmp(const char *, const char *, size_t) __LIBC_ATTRS;
char *strncpy(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
char *strndup(const char *, size_t) __LIBC_ATTRS;
size_t strnlen(const char *, size_t) __LIBC_ATTRS;
size_t strspn(const char *, const char *) __LIBC_ATTRS;
char *strtok(char *__restrict, const char *__restrict) __LIBC_ATTRS;
char *strtok_r(char *__restrict, const char *__restrict,
char **__restrict) __LIBC_ATTRS;
size_t strxfrm(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
}

extern "C++" {
char *strstr(char *, const char *) noexcept __LIBC_ATTRS;
const char *strstr(const char *, const char *) noexcept __LIBC_ATTRS;
char *strpbrk(char *, const char *) noexcept __LIBC_ATTRS;
const char *strpbrk(const char *, const char *) noexcept __LIBC_ATTRS;
char *strrchr(char *, int) noexcept __LIBC_ATTRS;
const char *strrchr(const char *, int) noexcept __LIBC_ATTRS;
char *strchr(char *, int) noexcept __LIBC_ATTRS;
const char *strchr(const char *, int) noexcept __LIBC_ATTRS;
char *strchrnul(char *, int) noexcept __LIBC_ATTRS;
const char *strchrnul(const char *, int) noexcept __LIBC_ATTRS;
char *strcasestr(char *, const char *) noexcept __LIBC_ATTRS;
const char *strcasestr(const char *, const char *) noexcept __LIBC_ATTRS;
void *memrchr(void *__s, int __c, size_t __n) noexcept __LIBC_ATTRS;
const void *memrchr(const void *__s, int __c, size_t __n) noexcept __LIBC_ATTRS;
void *memchr(void *__s, int __c, size_t __n) noexcept __LIBC_ATTRS;
const void *memchr(const void *__s, int __c, size_t __n) noexcept __LIBC_ATTRS;
}

#else
#include <llvm-libc-decls/string.h>

#define __LIBC_ATTRS
#endif

#pragma omp end declare target
// TODO: Define these for CUDA / HIP.

#undef __LIBC_ATTRS

#endif

#endif // __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__
Loading
Loading