[Relax] Add Dead Parameter Elimination#18720
[Relax] Add Dead Parameter Elimination#18720shreyasravi320 wants to merge 4 commits intoapache:mainfrom
Conversation
Summary of ChangesHello @shreyasravi320, 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 implements a significant optimization pass for the Relax IR: Dead Parameter Elimination. This pass systematically identifies and removes function parameters that are not utilized within their respective function bodies. By doing so, it streamlines the IR, leading to more efficient code generation and reduced resource consumption. The implementation involves an iterative process of analyzing parameter usage and subsequently rewriting function signatures and all associated call sites. Highlights
🧠 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. Changelog
Activity
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.
Code Review
This pull request introduces a dead parameter elimination pass for Relax, which is a valuable optimization for simplifying IR and improving performance. The implementation is mostly solid, but there are a few areas for improvement regarding correctness, performance, and code style. I've identified a potential bug in how call site struct info is handled, some opportunities for refactoring and performance optimization, and a couple of issues in the new tests. Overall, this is a great addition, and with a few adjustments, it will be even better.
| if (call->struct_info_.defined()) { | ||
| new_call->struct_info_ = call->struct_info_; | ||
| } |
There was a problem hiding this comment.
Manually copying the struct_info_ from the old call to the new call is incorrect. When arguments are removed from a call, the struct_info of the call (which describes the output) may change, especially if the return type depends on the removed arguments. The ExprMutator framework will handle re-inferring the struct_info for the new call node during normalization. Removing this manual assignment will allow the correct struct_info to be inferred.
| PostOrderVisit(func->body, [&](const ObjectRef& obj) { | ||
| if (auto v = obj.as<VarNode>()) { | ||
| Var var = ffi::GetRef<Var>(v); | ||
| for (size_t i = 0; i < func->params.size(); ++i) { | ||
| if (var.same_as(func->params[i])) { | ||
| used[i] = true; | ||
| } | ||
| } | ||
| } | ||
| }); |
There was a problem hiding this comment.
This approach to finding used parameters involves a nested loop within PostOrderVisit, which can be inefficient for functions with large bodies. For each variable encountered, it iterates through all function parameters. This can be optimized by building a map from parameter Var to its index before the visit, and then performing a direct lookup for each variable inside the visitor. This would change the complexity from O(num_vars * num_params) to O(num_vars + num_params).
std::unordered_map<Var, int, ObjectPtrHash, ObjectPtrEqual> param_to_idx;
for (size_t i = 0; i < func->params.size(); ++i) {
param_to_idx[func->params[i]] = i;
}
PostOrderVisit(func->body, [&](const ObjectRef& obj) {
if (auto v = obj.as<VarNode>()) {
Var var = ffi::GetRef<Var>(v);
auto it = param_to_idx.find(var);
if (it != param_to_idx.end()) {
used[it->second] = true;
}
}
});| std::vector<GlobalVar> worklist; | ||
| std::unordered_set<GlobalVar> visited; | ||
| std::function<void(GlobalVar)> dfs = [&](GlobalVar gvar) { | ||
| if (visited.count(gvar)) return; | ||
| visited.insert(gvar); | ||
|
|
||
| if (auto opt_func = mod->Lookup(gvar).as<Function>()) { | ||
| auto func = opt_func.value(); | ||
| PostOrderVisit(func->body, [&](const ObjectRef& obj) { | ||
| if (auto call = obj.as<CallNode>()) { | ||
| if (auto callee_gvar = call->op.as<GlobalVar>()) { | ||
| dfs(callee_gvar.value()); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| worklist.push_back(gvar); | ||
| }; | ||
|
|
||
| for (const auto& [gvar, _] : mod->functions) { | ||
| dfs(gvar); | ||
| } |
There was a problem hiding this comment.
| // S5: remove unused variables again as some arguments may be removed in S3 | ||
| { | ||
| IRModule updates; | ||
| for (const auto& [gvar, base_func] : mod->functions) { | ||
| if (auto opt = base_func.as<Function>()) { | ||
| auto new_func = Downcast<Function>(RemoveAllUnused(opt.value())); | ||
| if (!new_func.same_as(base_func)) { | ||
| updates->Add(gvar, new_func); | ||
| } | ||
| } | ||
| } | ||
| if (updates->functions.size()) { | ||
| mod.CopyOnWrite()->Update(updates); | ||
| } | ||
| } |
There was a problem hiding this comment.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
we do not want parameter change in general since this is the signature |
[Relax] Add Dead Parameter Elimination
This PR adds dead parameter elimination to
src/relax/transform/dead_code_elimination.ccto remove unused parameters from Relax functions.Motivation
Functions often accumulate unused parameters due to:
Eliminating these parameters reduces memory usage, speeds up compilation, and simplifies the IR.
Changes
tests/python/relax/test_transform_dead_param_elimination.pyExample Usage
Before:
After:
TODOs