Skip to content

[libc] Implement 'getenv' on the GPU target #102376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
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
10 changes: 7 additions & 3 deletions libc/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#TODO: Properly select the correct subdirectory.

add_subdirectory(linux)
add_header_library(
app_h
HDRS
app.h
DEPENDS
libc.src.__support.common
)
20 changes: 20 additions & 0 deletions libc/config/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Classes to capture properites of applications -----------*- C++ -*-===//
//
// 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 LLVM_LIBC_CONFIG_APP_H
#define LLVM_LIBC_CONFIG_APP_H

#include "src/__support/macros/properties/architectures.h"

#if defined(LIBC_TARGET_ARCH_IS_GPU)
#include "gpu/app.h"
#elif defined(__linux__)
#include "linux/app.h"
#endif

#endif // LLVM_LIBC_CONFIG_APP_H
28 changes: 28 additions & 0 deletions libc/config/gpu/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Classes to capture properites of GPU applications -------*- C++ -*-===//
//
// 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 LLVM_LIBC_CONFIG_GPU_APP_H
#define LLVM_LIBC_CONFIG_GPU_APP_H

#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"

#include <stdint.h>

namespace LIBC_NAMESPACE_DECL {

// TODO: Move other global values here and export them to the host.
struct DataEnvironment {
uintptr_t *env_ptr;
};

extern DataEnvironment app;

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_CONFIG_GPU_APP_H
1 change: 1 addition & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.strtoull
libc.src.stdlib.at_quick_exit
libc.src.stdlib.quick_exit
libc.src.stdlib.getenv

# TODO: Implement these correctly
libc.src.stdlib.aligned_alloc
Expand Down
7 changes: 0 additions & 7 deletions libc/config/linux/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion libc/src/__support/threads/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ add_object_library(
thread.cpp
DEPENDS
.futex_utils
libc.config.linux.app_h
libc.config.app_h
libc.include.sys_syscall
libc.include.fcntl
libc.src.errno.errno
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/threads/linux/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/__support/threads/thread.h"
#include "config/linux/app.h"
#include "config/app.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/stringstream.h"
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ add_entrypoint_object(
HDRS
getenv.h
DEPENDS
libc.config.linux.app_h
libc.config.app_h
)

add_entrypoint_object(
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/getenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/stdlib/getenv.h"
#include "config/linux/app.h"
#include "config/app.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/auxv/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_entrypoint_object(
libc.src.__support.threads.callonce
libc.src.__support.common
libc.src.errno.errno
libc.config.linux.app_h
libc.config.app_h
libc.src.fcntl.open
libc.src.unistd.read
libc.src.unistd.close
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/auxv/linux/getauxval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/sys/auxv/getauxval.h"
#include "config/linux/app.h"
#include "config/app.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"
Expand Down
2 changes: 1 addition & 1 deletion libc/startup/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function(add_startup_object name)
RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR}
RUNTIME_OUTPUT_NAME ${name}.o)
target_link_options(${fq_target_name}.exe PRIVATE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was GPU startup object named with .exe suffix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a dummy target, needed to be unique and since it's an executable, it's .exe.

"-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
endif()
endfunction()

Expand Down
1 change: 1 addition & 0 deletions libc/startup/gpu/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_startup_object(
SRC
start.cpp
DEPENDS
libc.config.app_h
libc.src.__support.RPC.rpc_client
libc.src.__support.GPU.utils
libc.src.stdlib.exit
Expand Down
5 changes: 5 additions & 0 deletions libc/startup/gpu/amdgpu/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

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

namespace LIBC_NAMESPACE_DECL {

DataEnvironment app;

extern "C" uintptr_t __init_array_start[];
extern "C" uintptr_t __init_array_end[];
extern "C" uintptr_t __fini_array_start[];
Expand All @@ -40,6 +43,8 @@ static void call_fini_array_callbacks() {

extern "C" [[gnu::visibility("protected"), clang::amdgpu_kernel]] void
_begin(int argc, char **argv, char **env) {
__atomic_store_n(&LIBC_NAMESPACE::app.env_ptr,
reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED);
// We want the fini array callbacks to be run after other atexit
// callbacks are run. So, we register them before running the init
// array callbacks as they can potentially register their own atexit
Expand Down
1 change: 1 addition & 0 deletions libc/startup/gpu/nvptx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_startup_object(
SRC
start.cpp
DEPENDS
libc.config.app_h
libc.src.__support.RPC.rpc_client
libc.src.__support.GPU.utils
libc.src.stdlib.exit
Expand Down
6 changes: 6 additions & 0 deletions libc/startup/gpu/nvptx/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

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

namespace LIBC_NAMESPACE_DECL {

DataEnvironment app;

extern "C" {
// Nvidia's 'nvlink' linker does not provide these symbols. We instead need
// to manually create them and update the globals in the loader implememtation.
Expand Down Expand Up @@ -46,6 +49,9 @@ static void call_fini_array_callbacks() {

extern "C" [[gnu::visibility("protected"), clang::nvptx_kernel]] void
_begin(int argc, char **argv, char **env) {
__atomic_store_n(&LIBC_NAMESPACE::app.env_ptr,
reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED);

// We want the fini array callbacks to be run after other atexit
// callbacks are run. So, we register them before running the init
// array callbacks as they can potentially register their own atexit
Expand Down
2 changes: 1 addition & 1 deletion libc/startup/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ add_object_library(
HDRS
do_start.h
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.include.sys_mman
libc.include.sys_syscall
libc.include.llvm-libc-macros.link_macros
Expand Down
4 changes: 2 additions & 2 deletions libc/startup/linux/aarch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_startup_object(
SRC
tls.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
Expand All @@ -18,7 +18,7 @@ add_startup_object(
SRC
start.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
COMPILE_OPTIONS
-fno-omit-frame-pointer
-ffreestanding # To avoid compiler warnings about calling the main function.
Expand Down
2 changes: 1 addition & 1 deletion libc/startup/linux/do_start.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "config/linux/app.h"
#include "config/app.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
4 changes: 2 additions & 2 deletions libc/startup/linux/riscv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_startup_object(
SRC
tls.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
Expand All @@ -18,7 +18,7 @@ add_startup_object(
SRC
start.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.src.__support.macros.attributes
COMPILE_OPTIONS
-fno-omit-frame-pointer
Expand Down
4 changes: 2 additions & 2 deletions libc/startup/linux/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_startup_object(
SRC
tls.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
Expand All @@ -20,7 +20,7 @@ add_startup_object(
SRC
start.cpp
DEPENDS
libc.config.linux.app_h
libc.config.app_h
libc.src.__support.macros.attributes
COMPILE_OPTIONS
-fno-stack-protector
Expand Down
1 change: 0 additions & 1 deletion libc/test/integration/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ add_integration_test(
FRANCE=Paris
GERMANY=Berlin
)

Loading