Skip to content
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

【CINN】Add pass to fold consecutive IfThenElse. #70142

Merged
merged 25 commits into from
Jan 2, 2025

Conversation

liuruyan
Copy link
Contributor

@liuruyan liuruyan commented Dec 11, 2024

PR Category

CINN

PR Types

Performance

Description

  1. Add a method for change div and mod sequence in IndexExpr
  2. Fold consecutive IfThenElse stmts if their conditions can be simplified. For example:
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 case1: All if can be simplified.
 if ((((((256 * j) + ((1024 * i) + k)) / 56) / 56) == 0)) {
   if ((((((256 * j) + ((1024 * i) + k)) / 56) % 56) == 0)) {
     if (((((256 * j) + ((1024 * i) + k)) % 56) == 0)) {
       int32 a = 1
     }
   }
 }
 can be simplified to:
 if (((((i * 1024ll) + k) + (j * 256ll)) == 0)) {
   int32 a = 1
 }
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 case2: All if can be simplified and the inner one has false branch.
 if ((((((256 * j) + ((1024 * i) + k)) / 56) / 56) == 0)) {
   if ((((((256 * j) + ((1024 * i) + k)) / 56) % 56) == 0)) {
     if (((((256 * j) + ((1024 * i) + k)) % 56) == 0)) {
       int32 a = 1
       int32 b = 1
     } else {
       int32 c = 1
     }
   }
 }
 can be simplified to:
 if (((((i * 1024ll) + k) + (j * 256ll)) == 0)) {
   int32 a = 1
   int32 b = 1
 } else {
   int32 c = 1
 }
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 case3: The inner one can not be simplified.
 if ((((((256 * j) + ((1024 * i) + k)) / 56) / 56) == 0)) {
   if ((((((256 * j) + ((1024 * i) + k)) / 56) % 56) == 0)) {
     if (((((256 * j) + ((1024 * i) + k)) % 56) == 0)) {
       if (l <= 0)) {
         int32 a = 1
       }
     }
   }
 }
 can be simplified to:
 if (((((i * 1024ll) + k) + (j * 256ll)) == 0)) {
   if (l <= 0)) {
     int32 a = 1
   }
 }
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

Pcard-67164

Copy link

paddle-bot bot commented Dec 11, 2024

你的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 add if fold pass. 【CINN】Add pass to fold consecutive IfThenElse. Dec 11, 2024
return true;
}

bool IsInnerIfWithEqCond(const StmtRef& stmt) {
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么叫InnerIf?函数实现看上去和inner没有多少关联?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

函数内部确实没有判断是否为 inner, 只是调用处限制了 inner,命名存在不妥之处,已修改

ir::IndexExpr expr(0);
int32_t min_len = INT32_MAX;

VLOG(6) << "-------------cond_vec start--------------";
Copy link
Contributor

Choose a reason for hiding this comment

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

这是调试log吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是预留的调试信息,之后方便调试

Comment on lines +69 to +71
if (eq_lhs.is_index())
cond_vec->push_back(common::ChangeSeqOfDivMod(
ir::ir_utils::IRCopy(eq_lhs).as_index().Normalize()));
Copy link
Contributor

Choose a reason for hiding this comment

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

if加{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines +91 to +95
if (v.node_type() == ir::IrNodeTy::Div) {
expr = expr + v * v.operand(1);
} else {
expr = expr + v;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

加一些注释说明这里处理的原理

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@zyfncg zyfncg merged commit 2608e60 into PaddlePaddle:develop Jan 2, 2025
30 checks passed
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