-
Notifications
You must be signed in to change notification settings - Fork 5.7k
【CINN】Simplify predicate and remove unreachable branch #72316
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
你的PR提交成功,感谢你对开源项目的贡献! |
zyfncg
reviewed
Apr 27, 2025
Comment on lines
+258
to
+303
auto RemoveUnreachPredicate = [](GroupCompilationContext* context) { | ||
// remove unreachable predicate. | ||
std::vector<ir::Expr> new_predicates; | ||
std::vector<int> new_priorities; | ||
std::vector<ir::LoweredFunc> new_lowered_funcs; | ||
bool has_true_predicate = false; | ||
for (size_t i = 0; i < context->predicates_.size(); ++i) { | ||
if (has_true_predicate) continue; | ||
if (common::IsZero(context->predicates_[i])) continue; | ||
if (common::IsOne(context->predicates_[i])) has_true_predicate = true; | ||
new_predicates.push_back(context->predicates_[i]); | ||
new_priorities.push_back(context->priorities_[i]); | ||
new_lowered_funcs.push_back(context->lowered_funcs_[i]); | ||
} | ||
// CINN does not support returning an empty module now. if all predicates | ||
// are false, we push the first predicate as result. | ||
if (new_predicates.empty() && !context->predicates_.empty()) { | ||
new_predicates.push_back(context->predicates_[0]); | ||
new_priorities.push_back(context->priorities_[0]); | ||
new_lowered_funcs.push_back(context->lowered_funcs_[0]); | ||
} | ||
context->predicates_ = std::move(new_predicates); | ||
context->priorities_ = std::move(new_priorities); | ||
context->lowered_funcs_ = std::move(new_lowered_funcs); | ||
|
||
// remove unreachable CX86 predicate. | ||
std::vector<ir::Expr> new_CX86_predicates; | ||
std::vector<ir::LoweredFunc> new_CX86_lowered_funcs; | ||
bool has_true_CX86_predicate = false; | ||
for (size_t i = 0; i < context->CX86_predicates_.size(); ++i) { | ||
if (has_true_CX86_predicate) continue; | ||
if (common::IsZero(context->CX86_predicates_[i])) continue; | ||
if (common::IsOne(context->CX86_predicates_[i])) | ||
has_true_CX86_predicate = true; | ||
new_CX86_predicates.push_back(context->CX86_predicates_[i]); | ||
new_CX86_lowered_funcs.push_back(context->CX86_lowered_funcs_[i]); | ||
} | ||
// CINN does not support returning an empty module now. if all predicates | ||
// are false, we push the first predicate as result. | ||
if (new_CX86_predicates.empty() && !context->CX86_predicates_.empty()) { | ||
new_CX86_predicates.push_back(context->CX86_predicates_[0]); | ||
new_CX86_lowered_funcs.push_back(context->CX86_lowered_funcs_[0]); | ||
} | ||
context->CX86_predicates_ = std::move(new_CX86_predicates); | ||
context->CX86_lowered_funcs_ = std::move(new_CX86_lowered_funcs); | ||
}; |
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.
这个太长了,单独写个函数吧
zyfncg
approved these changes
Apr 27, 2025
YqGe585
pushed a commit
to YqGe585/Paddle
that referenced
this pull request
May 7, 2025
…72316) * simplify predicate after lowering * remove useless comment * remove useless comment * fix undefine bug * remove unreachable branch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Category
CINN
PR Types
Improvements
Description
a. 之前存 true predicate,那么 true predicate 之后分支将不会执行到
b. 当前为 false predicate,那么该 predicate 将不会执行到
实现效果:


0size kernel 生成的不可达 host module:
Before this PR:
After this PR:
正常 host module:


Before this PR:
After this PR:
Note: 由于历史架构原因,当前 CINN 至少 Lower 后至少保留一个 Lowered_func,因此当全部 predicate 均为 false 时,会保留第一个 predicate 及其对应 lowered_func.
Pcard-67164