Mousius commented on a change in pull request #8487:
URL: https://github.com/apache/tvm/pull/8487#discussion_r671154648
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -24,33 +24,48 @@
// Check with LIFO checks enabled for stack allocator
#define TVM_CRT_STACK_ALLOCATOR_ENABLE_LIFO_CHECK
+
+/*
+ * Align memory pointer
+ */
+uint32_t align_memory_ptr(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;
+}
+
/*
* Tests allocations are properly aligned when allocated
*/
TEST(AOTMemory, Allocate) {
static uint8_t model_memory[96];
tvm_workspace_t tvm_runtime_workspace;
+ uint8_t* model_memory_ptr = model_memory;
+ uint32_t offset = align_memory_ptr(model_memory_ptr);
Review comment:
Does this come out as non-zero on the test host? I'd propose not
complicating the test logic if alignment is correct in the test environment.
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -114,22 +135,51 @@ TEST(AOTMemory, OverAllocate) {
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 = align_memory_ptr(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 mem_alignment = (uintptr_t)model_memory_ptr %
TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;
+ if (mem_alignment == 0) {
Review comment:
With my comment above about memory alignment in the test host, this
branch may always be `== 0` in which case we can just assume that and keep the
test simple?
##########
File path: tests/crt/aot_memory_test.cc
##########
@@ -24,33 +24,48 @@
// Check with LIFO checks enabled for stack allocator
#define TVM_CRT_STACK_ALLOCATOR_ENABLE_LIFO_CHECK
+
+/*
+ * Align memory pointer
+ */
+uint32_t align_memory_ptr(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;
+}
+
/*
* Tests allocations are properly aligned when allocated
*/
TEST(AOTMemory, Allocate) {
static uint8_t model_memory[96];
tvm_workspace_t tvm_runtime_workspace;
+ uint8_t* model_memory_ptr = model_memory;
Review comment:
I don't think this recast is necessary for most of the tests?
--
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]