Fix function number datatype from char to uint16_t - #10014
Conversation
| reg->registry.names = (const char*)buffer; | ||
| buffer[0] = 0; // number of functions present in buffer. | ||
| buffer[1] = 0; // end of names list marker. | ||
| buffer[1] = 0; |
There was a problem hiding this comment.
Better to explicitly mention the purpose. Something like
| buffer[1] = 0; | |
| buffer[1] = 0; // note that we combine the first two elements to form a 16-bit function index. |
kparzysz-quic
left a comment
There was a problem hiding this comment.
There are other places in this file that rely on the "one char for function count", that this patch does not update.
The approach of hiding the number of functions in the "names" buffer is dangerous---sooner or later somebody will forget it, or miss some cases, and it will result in hard-to-find bugs. I think it would be better to make it explicit in the data structures, or to create special functions that get/update the function count and get the actual beginning of the function names.
|
@kparzysz-quic thanks for pointing out the missing pieces. For the discussion about putting number in the name buffer, please refer to the discussion: https://discuss.tvm.apache.org/t/byoc-limited-function-numbers-on-crt-runtime/11952 |
|
@kparzysz-quic i agree it can be a bit misleading. if you feel strongly about it, i'm okay with us adopting the more explicit approach. it would waste a few bytes, but we do now provide a more compact solution for the standalone deployment workflow, so perhaps that's okay now. i am curious on your take on my rationale presented in the thread Cody links above. Regardless of the strategy taken around the C struct, it's going to be a bit dangerous since we generate this structure from LLVM and C source codegen, and then try to re-interpret the structure using a different compiler. @A1245967 you'll also need to update the LLVM codegen and C source codegen to generate the proper structure. |
|
I agree that accessing C structures via LLVM codegen may be tricky (due to pointer sizes in particular, and alignment rules), we're already running into a similar issue with enums in |
|
@kparzysz-quic ah i understand. sure, that's totally fine with me. @A1245967 , if you're up for it, would you mind introducing a few helper functions e.g. |
|
@areusch okay, I try to use some helper functions instead of accessing the buffer directly. |
|
@A1245967 that looks pretty good--i think you just need to update the C++ unit tests in |
|
@areusch I have updated the code. Do I need to modify anything else? |
| return return_value; | ||
| } | ||
|
|
||
| uint16_t* TVMFuncRegistry_GetNumFuncs(const TVMFuncRegistry* reg) { return (uint16_t*)reg->names; } |
There was a problem hiding this comment.
for this function, i think we need to impl like this to avoid alignment problems:
| uint16_t* TVMFuncRegistry_GetNumFuncs(const TVMFuncRegistry* reg) { return (uint16_t*)reg->names; } | |
| uint16_t TVMFuncRegistry_GetNumFuncs(const TVMFuncRegistry* reg) { | |
| uint16_t num_funcs; | |
| memcpy(&num_funcs, reg->names, sizeof(num_funcs)); | |
| return num_funcs; | |
| } |
There was a problem hiding this comment.
I think the following line also need to be changed if I choose to return the value instead of the address.
We update the value of num_funcs at this line .
If I choose to return the value in TVMFuncRegistry_GetNumFuncs, I should write a function to update num_funcs in buffer.
Do I need to write a function like TVMFuncRegistry_SetNumFuncs to set the value of num_funcs?
There was a problem hiding this comment.
ah, yeah i think so. good catch.
There was a problem hiding this comment.
@areusch I have implemented the functions TVMFuncRegistry_SetNumFuncs and TVMFuncRegistry_GetNumFuncs.
You can review these functions.
rewrite the modified part to pass lint check Use 2 bytes for func num in fun_registry Fix errors in linter Add the declaration of the helper functions set 2 bytes for func num in func_registry test units pass num_func by value This commit change the datatype of the number of the function from 1 Byte to 2 Bytes. Besides, I use some helper functions to access the number of function and the first function name.
|
@kparzysz-quic merged this now |
|
Hey @A1245967 this PR's last CI run was a bit out of date so it broke things when it was merged with the cd tvm
git fetch origin main
git reset --hard origin/main
curl -L https://github.com/apache/tvm/pull/10014.diff | patch -p1 -N -d .
# then git add, commit as usual) and fix the CI bug we can get it merged back in ASAP |
Hi, @areusch
Here is the bug I mentioned before.
I cast the first two byte to uint16_t to store the function number.