Lunderberg opened a new pull request, #17103:
URL: https://github.com/apache/tvm/pull/17103

   Prior to this PR, a TIR PrimFunc could return an `int64`, `float64`, or 
void.  Returning any other type, even if supported by TVM's FFI, would raise an 
exception during `MakePackedAPI`.  With this PR, string literals can be 
returned from TIR PrimFuncs.
   
   Enabling this TIR functionality requires a change to the TVM FFI.  
Previously, a `TVMRetValue` holding a string would allocate a
   `std::string*` and store it to `value_.v_handle`.  This return value depends 
on the C++ runtime, and cannot be constructed on all backends that only require 
the C API.  With this commit, this is instead represented as a `const char*` in 
`value_.v_str`, matching the representation in `TVMArgValue`.  Unlike 
`TVMArgValue`, since p`TVMRetValue` may own its return value, two new member 
variables `void (*f_deleter_)(void*)` and `void* f_deleter_arg_`.  These are 
designed to be easy to call from a C API, and are set to a deleter function 
that will free the allocation (if required), and an argument to be passed into 
the deleter function.
   
   With this new representation, the return value for different use cases would 
be set as follows:
   
   * Return string from C++ PackedFunc.  The backing allocation is made using 
`new std::string(std::move(value))`.  This heap allocation is stored in 
`f_deleter_arg_`, so that `f_deleter_` can then call `delete 
static_cast<std::string*>(arg)`.  Finally, `new_str->data()` is stored as 
`value.v_str`.
   
   * Return pointer to static string from C.  A pointer to the static string is 
stored in `v_str`.  Both `f_deleter_` and `f_deleter_arg_`are left with their 
default value of `nullptr`, indicating that the `TVMRetValue` does not need to 
free any memory on destruction.
   
     This is the return value used by `T.ret(T.StringImm("my_string"))` in a 
TIR PrimFunc.
   
   * Return pointer to heap allocation made in C.  Assuming the allocation is 
made using `malloc()`, a pointer to the heap allocation is stored in both 
`value_.v_str` and `f_deleter_arg_`. The `f_deleter_` is set to `free`, 
indicating that `free(f_deleter_arg_)` should be called when the `TVMRetValue` 
is destructed.
   
     This functionality is possibly within the updated `TVMRetValue` API, but 
is not currently used.
   
   This commit adds unit tests for the return of strings from compiled TIR 
PrimFuncs, when either running the function locally, or over a RPC connection.


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