Skip to content

【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 6 commits into from
Apr 28, 2025

Conversation

liuruyan
Copy link
Contributor

@liuruyan liuruyan commented Apr 16, 2025

PR Category

CINN

PR Types

Improvements

Description

  1. 对lower生成的lowered_func名字及predicate进行化简,去掉其中无用的条件(true && ..., 1ll < 1ll 等等),增加可读性
  2. 对不可达分支进行删去,减少编译时间,这里存在两种情况:
    a. 之前存 true predicate,那么 true predicate 之后分支将不会执行到
    b. 当前为 false predicate,那么该 predicate 将不会执行到

实现效果:
0size kernel 生成的不可达 host module:
Before this PR:
image
After this PR:
image

正常 host module:
Before this PR:
image
After this PR:
image

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

Pcard-67164

Copy link

paddle-bot bot commented Apr 16, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@liuruyan liuruyan changed the title Simplify predicate 【CINN】Simplify predicate and remove unreachable branch 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);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

这个太长了,单独写个函数吧

@zyfncg zyfncg merged commit 6e0955a into PaddlePaddle:develop Apr 28, 2025
43 of 44 checks passed
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants