areusch commented on a change in pull request #8487:
URL: https://github.com/apache/tvm/pull/8487#discussion_r671512084



##########
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:
       yeah i'd suggest changing `memory_add_misalignment` to accept a pointer 
and return the mutated pointer.

##########
File path: src/runtime/crt/memory/stack_allocator.c
##########
@@ -79,8 +79,13 @@ 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) {
-  tvm_runtime_workspace->next_alloc = g_aot_memory;
-  tvm_runtime_workspace->workspace = g_aot_memory;
-  tvm_runtime_workspace->workspace_size = workspace_size;
+  // Calculate g_aot_memory misalignment and offset
+  uint32_t extra = (uintptr_t)g_aot_memory % TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES;

Review comment:
       i think you could simplify this as:
   ```
   uintptr_t unaligned_mask = TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1;
   g_aot_memory = ((uintptr_t) g_aot_memory) + unaligned_mask) & 
~unaligned_mask;
   ```
   
   also can you update the comment to explain we need to round up g_aot_memory 
in case it is not aligned to `TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES`?




-- 
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]


Reply via email to