Skip to content

Commit 1a92cc5

Browse files
authored
[libc] Implement 'getenv' on the GPU target (#102376)
Summary: This patch implements 'getenv'. I was torn on how to implement this, since realistically we only have access to this environment pointer in the "loader" interface. An alternative would be to use an RPC call every time, but I think that's overkill for what this will be used for. A better solution is just to emit a common `DataEnvironment` that contains all of the host visible resources to initialize. Right now this is the `env_ptr`, `clock_freq`, and `rpc_client`. I did this by making the `app.h` interface that Linux uses more general, could possibly move that into a separate patch, but I figured it's easier to see with the usage.
1 parent 8f0c865 commit 1a92cc5

File tree

22 files changed

+84
-26
lines changed

22 files changed

+84
-26
lines changed

libc/config/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
#TODO: Properly select the correct subdirectory.
2-
3-
add_subdirectory(linux)
1+
add_header_library(
2+
app_h
3+
HDRS
4+
app.h
5+
DEPENDS
6+
libc.src.__support.common
7+
)

libc/config/app.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Classes to capture properites of applications -----------*- 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+
#ifndef LLVM_LIBC_CONFIG_APP_H
10+
#define LLVM_LIBC_CONFIG_APP_H
11+
12+
#include "src/__support/macros/properties/architectures.h"
13+
14+
#if defined(LIBC_TARGET_ARCH_IS_GPU)
15+
#include "gpu/app.h"
16+
#elif defined(__linux__)
17+
#include "linux/app.h"
18+
#endif
19+
20+
#endif // LLVM_LIBC_CONFIG_APP_H

libc/config/gpu/app.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Classes to capture properites of GPU applications -------*- 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+
#ifndef LLVM_LIBC_CONFIG_GPU_APP_H
10+
#define LLVM_LIBC_CONFIG_GPU_APP_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/architectures.h"
14+
15+
#include <stdint.h>
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
// TODO: Move other global values here and export them to the host.
20+
struct DataEnvironment {
21+
uintptr_t *env_ptr;
22+
};
23+
24+
extern DataEnvironment app;
25+
26+
} // namespace LIBC_NAMESPACE_DECL
27+
28+
#endif // LLVM_LIBC_CONFIG_GPU_APP_H

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ set(TARGET_LIBC_ENTRYPOINTS
167167
libc.src.stdlib.strtoull
168168
libc.src.stdlib.at_quick_exit
169169
libc.src.stdlib.quick_exit
170+
libc.src.stdlib.getenv
170171

171172
# TODO: Implement these correctly
172173
libc.src.stdlib.aligned_alloc

libc/config/linux/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

libc/src/__support/threads/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ add_object_library(
7777
thread.cpp
7878
DEPENDS
7979
.futex_utils
80-
libc.config.linux.app_h
80+
libc.config.app_h
8181
libc.include.sys_syscall
8282
libc.include.fcntl
8383
libc.src.errno.errno

libc/src/__support/threads/linux/thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/threads/thread.h"
10-
#include "config/linux/app.h"
10+
#include "config/app.h"
1111
#include "src/__support/CPP/atomic.h"
1212
#include "src/__support/CPP/string_view.h"
1313
#include "src/__support/CPP/stringstream.h"

libc/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ add_entrypoint_object(
6262
HDRS
6363
getenv.h
6464
DEPENDS
65-
libc.config.linux.app_h
65+
libc.config.app_h
6666
)
6767

6868
add_entrypoint_object(

libc/src/stdlib/getenv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/getenv.h"
10-
#include "config/linux/app.h"
10+
#include "config/app.h"
1111
#include "src/__support/CPP/string_view.h"
1212
#include "src/__support/common.h"
1313
#include "src/__support/macros/config.h"

libc/src/sys/auxv/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_entrypoint_object(
1111
libc.src.__support.threads.callonce
1212
libc.src.__support.common
1313
libc.src.errno.errno
14-
libc.config.linux.app_h
14+
libc.config.app_h
1515
libc.src.fcntl.open
1616
libc.src.unistd.read
1717
libc.src.unistd.close

libc/src/sys/auxv/linux/getauxval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/sys/auxv/getauxval.h"
10-
#include "config/linux/app.h"
10+
#include "config/app.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
1313
#include "src/errno/libc_errno.h"

libc/startup/gpu/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function(add_startup_object name)
3434
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
3535
RUNTIME_OUTPUT_NAME ${name}.o)
3636
target_link_options(${fq_target_name}.exe PRIVATE
37-
"-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
37+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
3838
endif()
3939
endfunction()
4040

libc/startup/gpu/amdgpu/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_startup_object(
33
SRC
44
start.cpp
55
DEPENDS
6+
libc.config.app_h
67
libc.src.__support.RPC.rpc_client
78
libc.src.__support.GPU.utils
89
libc.src.stdlib.exit

libc/startup/gpu/amdgpu/start.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "config/gpu/app.h"
910
#include "src/__support/GPU/utils.h"
1011
#include "src/__support/RPC/rpc_client.h"
1112
#include "src/__support/macros/config.h"
@@ -16,6 +17,8 @@ extern "C" int main(int argc, char **argv, char **envp);
1617

1718
namespace LIBC_NAMESPACE_DECL {
1819

20+
DataEnvironment app;
21+
1922
extern "C" uintptr_t __init_array_start[];
2023
extern "C" uintptr_t __init_array_end[];
2124
extern "C" uintptr_t __fini_array_start[];
@@ -40,6 +43,8 @@ static void call_fini_array_callbacks() {
4043

4144
extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel]] void
4245
_begin(int argc, char **argv, char **env) {
46+
__atomic_store_n(&LIBC_NAMESPACE::app.env_ptr,
47+
reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED);
4348
// We want the fini array callbacks to be run after other atexit
4449
// callbacks are run. So, we register them before running the init
4550
// array callbacks as they can potentially register their own atexit

libc/startup/gpu/nvptx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_startup_object(
33
SRC
44
start.cpp
55
DEPENDS
6+
libc.config.app_h
67
libc.src.__support.RPC.rpc_client
78
libc.src.__support.GPU.utils
89
libc.src.stdlib.exit

libc/startup/gpu/nvptx/start.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "config/gpu/app.h"
910
#include "src/__support/GPU/utils.h"
1011
#include "src/__support/RPC/rpc_client.h"
1112
#include "src/__support/macros/config.h"
@@ -16,6 +17,8 @@ extern "C" int main(int argc, char **argv, char **envp);
1617

1718
namespace LIBC_NAMESPACE_DECL {
1819

20+
DataEnvironment app;
21+
1922
extern "C" {
2023
// Nvidia's 'nvlink' linker does not provide these symbols. We instead need
2124
// to manually create them and update the globals in the loader implememtation.
@@ -46,6 +49,9 @@ static void call_fini_array_callbacks() {
4649

4750
extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
4851
_begin(int argc, char **argv, char **env) {
52+
__atomic_store_n(&LIBC_NAMESPACE::app.env_ptr,
53+
reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED);
54+
4955
// We want the fini array callbacks to be run after other atexit
5056
// callbacks are run. So, we register them before running the init
5157
// array callbacks as they can potentially register their own atexit

libc/startup/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ add_object_library(
9595
HDRS
9696
do_start.h
9797
DEPENDS
98-
libc.config.linux.app_h
98+
libc.config.app_h
9999
libc.include.sys_mman
100100
libc.include.sys_syscall
101101
libc.include.llvm-libc-macros.link_macros

libc/startup/linux/aarch64/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_startup_object(
33
SRC
44
tls.cpp
55
DEPENDS
6-
libc.config.linux.app_h
6+
libc.config.app_h
77
libc.include.sys_mman
88
libc.include.sys_syscall
99
libc.src.__support.OSUtil.osutil
@@ -18,7 +18,7 @@ add_startup_object(
1818
SRC
1919
start.cpp
2020
DEPENDS
21-
libc.config.linux.app_h
21+
libc.config.app_h
2222
COMPILE_OPTIONS
2323
-fno-omit-frame-pointer
2424
-ffreestanding # To avoid compiler warnings about calling the main function.

libc/startup/linux/do_start.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "config/linux/app.h"
9+
#include "config/app.h"
1010
#include "src/__support/macros/config.h"
1111

1212
namespace LIBC_NAMESPACE_DECL {

libc/startup/linux/riscv/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_startup_object(
33
SRC
44
tls.cpp
55
DEPENDS
6-
libc.config.linux.app_h
6+
libc.config.app_h
77
libc.include.sys_mman
88
libc.include.sys_syscall
99
libc.src.__support.OSUtil.osutil
@@ -18,7 +18,7 @@ add_startup_object(
1818
SRC
1919
start.cpp
2020
DEPENDS
21-
libc.config.linux.app_h
21+
libc.config.app_h
2222
libc.src.__support.macros.attributes
2323
COMPILE_OPTIONS
2424
-fno-omit-frame-pointer

libc/startup/linux/x86_64/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_startup_object(
33
SRC
44
tls.cpp
55
DEPENDS
6-
libc.config.linux.app_h
6+
libc.config.app_h
77
libc.include.sys_mman
88
libc.include.sys_syscall
99
libc.src.__support.OSUtil.osutil
@@ -20,7 +20,7 @@ add_startup_object(
2020
SRC
2121
start.cpp
2222
DEPENDS
23-
libc.config.linux.app_h
23+
libc.config.app_h
2424
libc.src.__support.macros.attributes
2525
COMPILE_OPTIONS
2626
-fno-stack-protector

libc/test/integration/src/stdlib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ add_integration_test(
1313
FRANCE=Paris
1414
GERMANY=Berlin
1515
)
16-

0 commit comments

Comments
 (0)