-
Notifications
You must be signed in to change notification settings - Fork 49
[C-API] Introduce TVMFFIHandleInitOnce #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @tqchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust mechanism for thread-safe, one-time initialization and deinitialization of static handles within the TVM FFI environment. This is particularly useful in DSL contexts where ensuring a resource is initialized and deinitialized exactly once, even across multiple threads, is critical, without relying on C++11 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces thread-safe handle initialization and deinitialization functions, TVMFFIEnvHandleInitOnce and TVMFFIEnvHandleDeinitOnce. The intent is valuable, and the inclusion of comprehensive tests is a great step. However, there are critical flaws in the current implementation. TVMFFIEnvHandleInitOnce employs a single global mutex, which will serialize all initializations and create a significant performance bottleneck. TVMFFIEnvHandleDeinitOnce contains a race condition in its lock-free logic that could lead to severe issues like double-frees. I have provided suggestions to fix these critical problems. There is also a minor naming inconsistency that I've pointed out to improve code clarity.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces two new thread-safe helper functions, TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce, to manage the lifecycle of static handles. The implementation correctly uses a double-checked locking pattern for initialization and an atomic exchange for deinitialization, ensuring thread safety. The changes are accompanied by comprehensive single-threaded and multi-threaded tests. My feedback focuses on improving the clarity of function pointer parameter names in the implementation to align with the public C API header, which will enhance code readability and maintainability.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces two new thread-safe helper functions, TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce, for one-time initialization and deinitialization of static handles. The implementation uses a double-checked locking pattern for initialization and an atomic exchange for deinitialization. The changes also include comprehensive unit tests, covering single-threaded and multi-threaded scenarios.
My review focuses on the thread-safety implementation and test quality. I've identified a potential performance issue in TVMFFIHandleInitOnce due to the use of a single global mutex, which could cause contention. I've also suggested an improvement to the multi-threaded tests to make them more robust. Overall, the changes are well-structured and the new functionality is a useful addition.
In DSL settings, sometimes it is helpful to have ability to do thread-safe initialization of static handles. This PR adds two functions to make it easy to do so without stdcpp run_once support.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce to provide thread-safe initialization and deinitialization of static handles, which is particularly useful for DSLs without native support for this. The implementation uses a double-checked locking pattern with a static mutex for initialization and atomic exchange for deinitialization. The changes are accompanied by a comprehensive set of single-threaded and multi-threaded tests.
The implementation is solid, but I have a couple of suggestions for improvement. The TVMFFIHandleInitOnce function uses a single global mutex, which could become a performance bottleneck if many different handles are initialized concurrently. In the tests, global static variables are used to share state with C-style callbacks, which could be problematic if tests are run in parallel. I've left specific comments with suggestions on how to address these points.
In DSL settings, sometimes it is helpful to have ability to do thread-safe initialization of static handles.
This PR adds two functions to make it easy to do so without stdcpp run_once support