-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Add support for creating wrapper headers for offloading in clang
This is an alternate approach to the patches proposed in D153897 and D153794. Rather than exporting a single header that can be included on the GPU in all circumstances, this patch chooses to instead generate a separate set of headers that only provides the declarations. This can then be used by external tooling to set up what's on the GPU. This leaves room for header hacks for offloading languages without needing to worry about the `libc` implementation. Currently this generates a set of headers that only contain the declarations. These will then be installed to a new clang resource directory called `llvm_libc_wrappers/` which will house the shim code. We can then automaticlaly include this from `clang` when offloading to wrap around the headers while specifying what's on the GPU. Reviewed By: jdoerfert, JonChesterfield Differential Revision: https://reviews.llvm.org/D154036
- Loading branch information
Showing
14 changed files
with
417 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===-- Wrapper for C standard ctype.h declarations on the GPU ------------===// | ||
// | ||
// 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 __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__ | ||
#define __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__ | ||
|
||
#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) | ||
#error "This file is for GPU offloading compilation only" | ||
#endif | ||
|
||
#include_next <ctype.h> | ||
|
||
#if __has_include(<llvm-libc-decls/ctype.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#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("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("tolower") | ||
#pragma push_macro("toupper") | ||
|
||
#undef isalnum | ||
#undef isalpha | ||
#undef iscntrl | ||
#undef isdigit | ||
#undef islower | ||
#undef isgraph | ||
#undef isprint | ||
#undef ispunct | ||
#undef isspace | ||
#undef isupper | ||
#undef isblank | ||
#undef isxdigit | ||
#undef tolower | ||
#undef toupper | ||
|
||
#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("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("tolower") | ||
#pragma pop_macro("toupper") | ||
#endif | ||
|
||
#undef __LIBC_ATTRS | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- Wrapper for C standard inttypes.h declarations on the GPU ---------===// | ||
// | ||
// 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 __CLANG_LLVM_LIBC_WRAPPERS_INTTYPES_H__ | ||
#define __CLANG_LLVM_LIBC_WRAPPERS_INTTYPES_H__ | ||
|
||
#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) | ||
#error "This file is for GPU offloading compilation only" | ||
#endif | ||
|
||
#include_next <inttypes.h> | ||
|
||
#if __has_include(<llvm-libc-decls/inttypes.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#endif | ||
|
||
#pragma omp begin declare target | ||
|
||
#include <llvm-libc-decls/inttypes.h> | ||
|
||
#pragma omp end declare target | ||
|
||
#undef __LIBC_ATTRS | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_INTTYPES_H__ |
6 changes: 6 additions & 0 deletions
6
clang/lib/Headers/llvm_libc_wrappers/llvm-libc-decls/README.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
LLVM libc declarations | ||
====================== | ||
|
||
This directory will be filled by the `libc` project with declarations that are | ||
availible on the device. Each declaration will use the `__LIBC_ATTRS` attribute | ||
to control emission on the device side. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- Wrapper for C standard stdio.h declarations on the GPU ------------===// | ||
// | ||
// 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 __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> | ||
|
||
#if __has_include(<llvm-libc-decls/stdio.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#endif | ||
|
||
#pragma omp begin declare target | ||
|
||
#include <llvm-libc-decls/stdio.h> | ||
|
||
#pragma omp end declare target | ||
|
||
#undef __LIBC_ATTRS | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===-- Wrapper for C standard stdlib.h declarations on the GPU -----------===// | ||
// | ||
// 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 __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__ | ||
#define __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__ | ||
|
||
#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) | ||
#error "This file is for GPU offloading compilation only" | ||
#endif | ||
|
||
#include_next <stdlib.h> | ||
|
||
#if __has_include(<llvm-libc-decls/stdlib.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#endif | ||
|
||
#pragma omp begin declare target | ||
|
||
// The LLVM C library uses this type so we forward declare it. | ||
typedef void (*__atexithandler_t)(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!"); | ||
|
||
#include <llvm-libc-decls/stdlib.h> | ||
|
||
#pragma omp end declare target | ||
|
||
#undef __LIBC_ATTRS | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//===-- Wrapper for C standard string.h declarations on the GPU -----------===// | ||
// | ||
// 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 __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__ | ||
#define __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__ | ||
|
||
#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) | ||
#error "This file is for GPU offloading compilation only" | ||
#endif | ||
|
||
// The GNU headers provide non C-standard headers when in C++ mode. Manually | ||
// undefine it here so that the definitions agree with the C standard for our | ||
// purposes. | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#pragma push_macro("__cplusplus") | ||
#undef __cplusplus | ||
#endif | ||
|
||
#include_next <string.h> | ||
|
||
#pragma pop_macro("__cplusplus") | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#if __has_include(<llvm-libc-decls/string.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#endif | ||
|
||
#pragma omp begin declare target | ||
|
||
#include <llvm-libc-decls/string.h> | ||
|
||
#pragma omp end declare target | ||
|
||
#undef __LIBC_ATTRS | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.