[libc] Efficiently implement 'realloc' for AMDGPU devices (#145960)
Summary: Now that we have `malloc` we can implement `realloc` efficiently. This uses the known chunk sizes to avoid unnecessary allocations. We just return nullptr for NVPTX. I'd remove the list for the entrypoint but then the libc++ code would stop working. When someone writes the NVPTX support this will be trivial.
This commit is contained in:
@@ -17,6 +17,21 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
|
||||
--blocks 1024
|
||||
)
|
||||
|
||||
add_integration_test(
|
||||
realloc
|
||||
SUITE
|
||||
stdlib-gpu-integration-tests
|
||||
SRCS
|
||||
realloc.cpp
|
||||
DEPENDS
|
||||
libc.src.stdlib.malloc
|
||||
libc.src.stdlib.free
|
||||
libc.src.stdlib.realloc
|
||||
LOADER_ARGS
|
||||
--threads 256
|
||||
--blocks 1024
|
||||
)
|
||||
|
||||
add_integration_test(
|
||||
malloc_stress
|
||||
SUITE
|
||||
|
||||
44
libc/test/integration/src/stdlib/gpu/realloc.cpp
Normal file
44
libc/test/integration/src/stdlib/gpu/realloc.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "test/IntegrationTest/test.h"
|
||||
|
||||
#include "src/__support/GPU/utils.h"
|
||||
#include "src/stdlib/free.h"
|
||||
#include "src/stdlib/malloc.h"
|
||||
#include "src/stdlib/realloc.h"
|
||||
|
||||
using namespace LIBC_NAMESPACE;
|
||||
|
||||
TEST_MAIN(int, char **, char **) {
|
||||
// realloc(nullptr, size) is equivalent to malloc.
|
||||
int *alloc = reinterpret_cast<int *>(LIBC_NAMESPACE::realloc(nullptr, 32));
|
||||
EXPECT_NE(alloc, nullptr);
|
||||
*alloc = 42;
|
||||
EXPECT_EQ(*alloc, 42);
|
||||
|
||||
// realloc to same size returns the same pointer.
|
||||
void *same = LIBC_NAMESPACE::realloc(alloc, 32);
|
||||
EXPECT_EQ(same, alloc);
|
||||
EXPECT_EQ(reinterpret_cast<int *>(same)[0], 42);
|
||||
|
||||
// realloc to smaller size returns same pointer.
|
||||
void *smaller = LIBC_NAMESPACE::realloc(same, 16);
|
||||
EXPECT_EQ(smaller, alloc);
|
||||
EXPECT_EQ(reinterpret_cast<int *>(smaller)[0], 42);
|
||||
|
||||
// realloc to larger size returns new pointer and preserves contents.
|
||||
int *larger = reinterpret_cast<int *>(LIBC_NAMESPACE::realloc(smaller, 128));
|
||||
EXPECT_NE(larger, nullptr);
|
||||
EXPECT_EQ(larger[0], 42);
|
||||
|
||||
// realloc works when called with a divergent size.
|
||||
int *div = reinterpret_cast<int *>(
|
||||
LIBC_NAMESPACE::malloc((gpu::get_thread_id() + 1) * 16));
|
||||
EXPECT_NE(div, nullptr);
|
||||
div[0] = static_cast<int>(gpu::get_thread_id());
|
||||
int *div_realloc = reinterpret_cast<int *>(
|
||||
LIBC_NAMESPACE::realloc(div, ((gpu::get_thread_id() + 1) * 32)));
|
||||
EXPECT_NE(div_realloc, nullptr);
|
||||
EXPECT_EQ(div_realloc[0], static_cast<int>(gpu::get_thread_id()));
|
||||
LIBC_NAMESPACE::free(div_realloc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user