areusch commented on a change in pull request #8487:
URL: https://github.com/apache/tvm/pull/8487#discussion_r673276401
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -24,83 +25,118 @@
// Check with LIFO checks enabled for stack allocator
#define TVM_CRT_STACK_ALLOCATOR_ENABLE_LIFO_CHECK
+
+/*!
+ * Align memory pointer.
+ * \return Number of memory offset.
+ */
+uint32_t memory_align(uint8_t** memory_ptr) {
Review comment:
make this a static function and consider calling this `align_pointer`,
also document that `memory_ptr` is modified in the docstring.
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -24,83 +25,118 @@
// Check with LIFO checks enabled for stack allocator
#define TVM_CRT_STACK_ALLOCATOR_ENABLE_LIFO_CHECK
+
+/*!
+ * Align memory pointer.
+ * \return Number of memory offset.
+ */
+uint32_t memory_align(uint8_t** memory_ptr) {
+ uint32_t extra = (uintptr_t)(*memory_ptr) %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ uint32_t offset =
+ (TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - extra) &
(TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1);
+ *memory_ptr += offset;
+ return offset;
+}
+
+/*!
+ * Add misalignment to memory pointer.
+ * \return Number of memory offset.
+ */
+uint32_t memory_add_misalignment(uint8_t** memory_ptr) {
Review comment:
make static and maybe consider calling it `misalign_pointer`? also
docstring
##########
File path: src/runtime/crt/memory/stack_allocator.c
##########
@@ -79,8 +79,14 @@ tvm_crt_error_t StackMemoryManager_Free(tvm_workspace_t*
tvm_runtime_workspace,
tvm_crt_error_t StackMemoryManager_Init(tvm_workspace_t* tvm_runtime_workspace,
uint8_t* g_aot_memory, size_t
workspace_size) {
+ // We need to round up g_aot_memory in case it is not aligned to
+ // TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES.
+ uintptr_t unaligned_mask = TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1;
+ uint32_t offset = TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES -
((uintptr_t)g_aot_memory & unaligned_mask);
Review comment:
it might be easier to understand this computation by subtracting the
passed-in `g_aot_memory` from the newly computed one below.
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -24,83 +25,118 @@
// Check with LIFO checks enabled for stack allocator
#define TVM_CRT_STACK_ALLOCATOR_ENABLE_LIFO_CHECK
+
+/*!
+ * Align memory pointer.
+ * \return Number of memory offset.
+ */
+uint32_t memory_align(uint8_t** memory_ptr) {
+ uint32_t extra = (uintptr_t)(*memory_ptr) %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ uint32_t offset =
+ (TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - extra) &
(TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1);
+ *memory_ptr += offset;
+ return offset;
+}
+
+/*!
+ * Add misalignment to memory pointer.
+ * \return Number of memory offset.
+ */
+uint32_t memory_add_misalignment(uint8_t** memory_ptr) {
+ uint32_t extra = (uintptr_t)(*memory_ptr) %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ if (extra == 0) {
+ *memory_ptr += 1;
+ return 1;
+ }
+ return 0;
+}
+
/*
- * Tests allocations are properly aligned when allocated
+ * Tests allocations are properly aligned when allocated.
*/
TEST(AOTMemory, Allocate) {
- static uint8_t model_memory[96];
+ static uint8_t model_memory[128];
tvm_workspace_t tvm_runtime_workspace;
+ uint8_t* model_memory_ptr = model_memory;
+ uint32_t offset = memory_align(&model_memory_ptr);
+ ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory_ptr,
128 - offset),
+ kTvmErrorNoError);
- ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory, 96),
kTvmErrorNoError);
void* block_one = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_one, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_one, &model_memory[0]);
+ ASSERT_EQ(block_one, &model_memory_ptr[0]);
void* block_two = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 2,
&block_two, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_two, &model_memory[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(block_two, &model_memory_ptr[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
void* two_blocks = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 24,
&two_blocks, 1),
kTvmErrorNoError);
- ASSERT_EQ(two_blocks, &model_memory[32 + 2 *
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(two_blocks, &model_memory_ptr[32 + 2 *
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
void* block_three = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_three, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_three, &model_memory[64 + 3 *
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(block_three, &model_memory_ptr[64 + 3 *
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
}
/*
- * Tests resetting the stack after dealloc
+ * Tests resetting the stack after dealloc.
*/
TEST(AOTMemory, Free) {
static uint8_t model_memory[80];
tvm_workspace_t tvm_runtime_workspace;
- ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory, 80),
kTvmErrorNoError);
+ uint8_t* model_memory_ptr = model_memory;
+ uint32_t offset = memory_align(&model_memory_ptr);
+ ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory_ptr,
80 - offset),
+ kTvmErrorNoError);
void* block_one = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_one, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_one, &model_memory[0]);
+ ASSERT_EQ(block_one, &model_memory_ptr[0]);
void* block_two = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_two, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_two, &model_memory[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(block_two, &model_memory_ptr[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
ASSERT_EQ(kTvmErrorNoError,
StackMemoryManager_Free_Body(&tvm_runtime_workspace, block_two, 1));
void* two_blocks = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 2,
&two_blocks, 1),
kTvmErrorNoError);
- ASSERT_EQ(two_blocks, &model_memory[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(two_blocks, &model_memory_ptr[16 +
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
ASSERT_EQ(kTvmErrorNoError,
StackMemoryManager_Free_Body(&tvm_runtime_workspace, two_blocks, 1));
void* block_three = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_three, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_three, &model_memory[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(block_three, &model_memory_ptr[16 +
STACK_ALLOCATOR_TAG_SIZE_BYTES]);
}
/*
- * Tests we return NULL if we over allocate
+ * Tests we return NULL if we over allocate.
*/
TEST(AOTMemory, OverAllocate) {
static uint8_t model_memory[72];
tvm_workspace_t tvm_runtime_workspace;
- ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory, 80),
kTvmErrorNoError);
+ uint8_t* model_memory_ptr = model_memory;
+ uint32_t offset = memory_align(&model_memory_ptr);
+ ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory_ptr,
80 - offset),
Review comment:
can you replace the `80` with `sizeof(model_memory)` here?
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -109,27 +145,50 @@ TEST(AOTMemory, OverAllocate) {
}
/*
- * Test for out-of-order memory deallocation
+ * Test for out-of-order memory deallocation.
*/
TEST(AOTMemory, FreeOutOfOrder) {
static uint8_t model_memory[80];
tvm_workspace_t tvm_runtime_workspace;
- ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory, 80),
kTvmErrorNoError);
+ uint8_t* model_memory_ptr = model_memory;
+ uint32_t offset = memory_align(&model_memory_ptr);
+ ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory_ptr,
80 - offset),
+ kTvmErrorNoError);
void* block_one = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_one, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_one, &model_memory[0]);
+ ASSERT_EQ(block_one, &model_memory_ptr[0]);
void* block_two = NULL;
ASSERT_EQ(StackMemoryManager_Allocate_Body(&tvm_runtime_workspace, 1,
&block_two, 1),
kTvmErrorNoError);
- ASSERT_EQ(block_two, &model_memory[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
+ ASSERT_EQ(block_two, &model_memory_ptr[16 + STACK_ALLOCATOR_TAG_SIZE_BYTES]);
ASSERT_EQ(StackMemoryManager_Free_Body(&tvm_runtime_workspace, block_one, 1),
kTvmErrorPlatformStackAllocBadFree);
}
+/*
+ * Test for initial memory misalignment.
+ */
+TEST(AOTMemory, InitialMemoryMisAlignment) {
+ static uint8_t model_memory[80];
+ tvm_workspace_t tvm_runtime_workspace;
+ uint8_t* model_memory_ptr = model_memory;
+
+ uint32_t offset = memory_add_misalignment(&model_memory_ptr);
+ ASSERT_EQ(StackMemoryManager_Init(&tvm_runtime_workspace, model_memory_ptr,
80 - offset),
+ kTvmErrorNoError);
+
+ uint32_t next_alloc_offset =
+ (uintptr_t)tvm_runtime_workspace.next_alloc %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ uint32_t workspace_offset =
+ (uintptr_t)tvm_runtime_workspace.workspace %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ ASSERT_EQ(next_alloc_offset, 0);
+ ASSERT_EQ(workspace_offset, 0);
Review comment:
want to explicitly compare a pointer from _Alloc with model_memory_ptr?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]