areusch commented on a change in pull request #7785: URL: https://github.com/apache/tvm/pull/7785#discussion_r615046675
########## File path: src/runtime/crt/memory/stack_allocator.c ########## @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// LINT_C_FILE + +#include <tvm/runtime/crt/stack_allocator.h> + +void* StackMemoryManager_Allocate(tvm_workspace_t* tvm_runtime_workspace, int32_t nbytes) { + uint32_t offset_bytes = (~nbytes + 1) & (TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1); Review comment: one way to do my proposal below this is to store a "tag" word either before or after the region being returned here, then validate the tag in Free(). For instance: ``` *((uint32_t*) next_alloc) = 0xabcd1234 ^ nbytes; next_alloc += 4; ``` then in free, check that the tag word is correct: ``` uint32_t tag = *(((uint32_t*) ptr) - 1); CHECK_EQ(tag, (((uintptr_t) tvm_runtime_workspace->next_alloc) - ((uintptr_t) ptr)) ^ 0xabcd1234, "tag did not match"); ``` ########## File path: tests/python/relay/test_backend_graph_executor.py ########## @@ -133,7 +133,7 @@ def test_plan_memory(): storage_ids = set() device_types = set() for k, v in smap.items(): - assert len(v) == 2 + assert len(v) == 3 Review comment: here you're expanding the length assertion, but below, there are some rudimentary asserts on v[0] and v[1]. can you also add some assert on v[2]? ########## File path: src/runtime/crt/memory/stack_allocator.c ########## @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// LINT_C_FILE + +#include <tvm/runtime/crt/stack_allocator.h> + +void* StackMemoryManager_Allocate(tvm_workspace_t* tvm_runtime_workspace, int32_t nbytes) { + uint32_t offset_bytes = (~nbytes + 1) & (TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES - 1); + uint8_t* current_alloc = tvm_runtime_workspace->next_alloc; + uint8_t* next_alloc = tvm_runtime_workspace->next_alloc + nbytes + offset_bytes; + uint8_t* workspace_end = tvm_runtime_workspace->workspace + tvm_runtime_workspace->workspace_size; + + if (next_alloc > workspace_end) { + return NULL; + } + + tvm_runtime_workspace->next_alloc = next_alloc; + return current_alloc; +} + +tvm_crt_error_t StackMemoryManager_Free(tvm_workspace_t* tvm_runtime_workspace, void* ptr) { + tvm_runtime_workspace->next_alloc = ptr; Review comment: I think this API depends on an e.g. FIFO ordering in calls. Specifically: ``` Allocate(100, &a); Allocate(200, &b); Free(&b); Free(&a); ``` is correct, while: ``` Allocate(100, &a); Allocate(200, &b); Free(&a); ``` is not. It's pretty easy to screw this up, so can we add a sanity-check here to assert that indeed we are calling Free in the correct order? -- 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. For queries about this service, please contact Infrastructure at: [email protected]
