-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Implement basic
malloc
and free
support on the GPU
This patch adds support for the `malloc` and `free` functions. These currently aren't implemented in-tree so we first add the interface filies. This patch provides the most basic support for a true `malloc` and `free` by using the RPC interface. This is functional, but in the future we will want to implement a more intelligent system and primarily use the RPC interface more as a `brk()` or `sbrk()` interface only called when absolutely necessary. We will need to design an intelligent allocator in the future. The semantics of these memory allocations will need to be checked. I am somewhat iffy on the details. I've heard that HSA can allocate asynchronously which seems to work with my tests at least. CUDA uses an implicit synchronization scheme so we need to use an explicitly separate stream from the one launching the kernel or the default stream. I will need to test the NVPTX case. I would appreciate if anyone more experienced with the implementation details here could chime in for the HSA and CUDA cases. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D151735
- Loading branch information
Showing
13 changed files
with
254 additions
and
21 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,20 @@ | ||
//===-- Implementation header for free --------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <stdlib.h> | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_FREE_H | ||
#define LLVM_LIBC_SRC_STDLIB_FREE_H | ||
|
||
namespace __llvm_libc { | ||
|
||
void free(void *ptr); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_LDIV_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,21 @@ | ||
add_entrypoint_object( | ||
malloc | ||
SRCS | ||
malloc.cpp | ||
HDRS | ||
../malloc.h | ||
DEPENDS | ||
libc.include.stdlib | ||
libc.src.__support.RPC.rpc_client | ||
) | ||
|
||
add_entrypoint_object( | ||
free | ||
SRCS | ||
free.cpp | ||
HDRS | ||
../free.h | ||
DEPENDS | ||
libc.include.stdlib | ||
libc.src.__support.RPC.rpc_client | ||
) |
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,23 @@ | ||
//===-- GPU Implementation of free ----------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdlib/free.h" | ||
#include "src/__support/RPC/rpc_client.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { | ||
rpc::Client::Port port = rpc::client.open<rpc::FREE>(); | ||
port.send([=](rpc::Buffer *buffer) { | ||
buffer->data[0] = reinterpret_cast<uintptr_t>(ptr); | ||
}); | ||
port.close(); | ||
} | ||
|
||
} // namespace __llvm_libc |
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,26 @@ | ||
//===-- GPU Implementation of malloc --------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdlib/malloc.h" | ||
#include "src/__support/RPC/rpc_client.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) { | ||
void *ptr = nullptr; | ||
rpc::Client::Port port = rpc::client.open<rpc::MALLOC>(); | ||
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; }, | ||
[&](rpc::Buffer *buffer) { | ||
ptr = reinterpret_cast<void *>(buffer->data[0]); | ||
}); | ||
port.close(); | ||
return ptr; | ||
} | ||
|
||
} // namespace __llvm_libc |
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,20 @@ | ||
//===-- Implementation header for malloc ------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <stdlib.h> | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_MALLOC_H | ||
#define LLVM_LIBC_SRC_STDLIB_MALLOC_H | ||
|
||
namespace __llvm_libc { | ||
|
||
void *malloc(size_t size); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_LDIV_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
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,19 @@ | ||
//===-- Unittests for malloc ----------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdlib/free.h" | ||
#include "src/stdlib/malloc.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcMallocTest, Allocate) { | ||
int *ptr = reinterpret_cast<int *>(__llvm_libc::malloc(sizeof(int))); | ||
EXPECT_NE(reinterpret_cast<void *>(ptr), static_cast<void *>(nullptr)); | ||
*ptr = 1; | ||
EXPECT_EQ(*ptr, 1); | ||
__llvm_libc::free(ptr); | ||
} |
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
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