Skip to content

Conversation

@guan404ming
Copy link
Member

Why

The GetUsedVars function was defined locally in binding_rewrite.cc with a TODO comment suggesting it should be moved to the analysis module. This refactoring improves code organization by placing the utility function alongside other variable analysis functions.

How

  • Move GetUsedVars implementation to analysis module
  • Add FFI registration and Python wrapper
  • Add parametrized test

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guan404ming, 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 significantly improves code organization by refactoring the GetUsedVars utility. By relocating it to the dedicated analysis module, the function is now centralized with other variable analysis tools, enhancing modularity and maintainability. The change also extends its utility by providing FFI registration and a Python API, ensuring broader accessibility and easier integration into various workflows, all while being backed by new comprehensive tests.

Highlights

  • Function Relocation: The GetUsedVars function has been moved from src/relax/ir/binding_rewrite.cc to the analysis module, specifically implemented in src/relax/analysis/udchain.cc and declared in include/tvm/relax/analysis.h.
  • FFI and Python API Exposure: The GetUsedVars function is now registered for FFI (Foreign Function Interface) in C++ and exposed through a new Python wrapper get_used_vars in python/tvm/relax/analysis/analysis.py, making it accessible from Python.
  • New Test Coverage: A new parametrized test test_get_used_vars has been added to tests/python/relax/test_analysis.py to validate the functionality of the moved and exposed get_used_vars function.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 is a nice refactoring that moves the GetUsedVars function to the analysis module, improving code organization. The changes include adding the FFI registration, a Python wrapper, and a new parametrized test. The implementation is correct and the changes are well-contained. I have one suggestion to slightly optimize the FFI wrapper for better performance.

Comment on lines +128 to +132
ffi::Array<Var> result;
for (const VarNode* var_node : used_vars) {
result.push_back(ffi::GetRef<Var>(var_node));
}
return result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved performance, especially when used_vars is large, consider building a std::vector with pre-reserved capacity and then constructing the ffi::Array from it. This avoids potential reallocations that can occur with repeated push_back calls on ffi::Array.

        std::vector<Var> result_vec;
        result_vec.reserve(used_vars.size());
        for (const VarNode* var_node : used_vars) {
          result_vec.push_back(ffi::GetRef<Var>(var_node));
        }
        return ffi::Array<Var>(std::move(result_vec));

@guan404ming guan404ming force-pushed the refactor-move-getusedvars-to-analysis branch from 2c8e5a2 to 3afa978 Compare January 3, 2026 07:30
@guan404ming guan404ming marked this pull request as ready for review January 3, 2026 11:57
@guan404ming
Copy link
Member Author

cc @mshr-h @tlopex

return _ffi_api.all_vars(expr)


def get_used_vars(expr: Expr) -> List[Var]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the func name might be better to be used_vars() to be consistent with all_vars() and free_vars()? cc @tlopex @cbalint13

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree @guan404ming

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've updated.

@mshr-h mshr-h merged commit 7b9d3d9 into apache:main Jan 5, 2026
14 checks passed
@guan404ming guan404ming deleted the refactor-move-getusedvars-to-analysis branch January 5, 2026 04:32
@guan404ming
Copy link
Member Author

Thanks for all reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants