Implement function inlining optimization in SSA
还没有人认领这个 Issue。
评估
调研方向
No source files, entry points, or existing tests are named. Start by locating the SSA optimizer, call-graph handling, and optimization pass ordering, then review how SSA values and multiple returns are represented. Done means conservative inlining handles the listed cases, updates related compiler state, and has tests for each required behavior.
由索引模型根据 Issue 内容生成。
描述
Description
Function inlining eliminates call overhead by replacing function calls with the function body. This optimization is especially valuable for small, frequently-called functions and enables further optimizations.
Current Behavior
int add(int a, int b) { return a + b; }
int compute() {
return add(3, 4); // Function call overhead
}
Expected Behavior
After inlining:
int compute() {
return 3 + 4; // Direct computation, enables constant folding
}
Implementation Requirements
1. Inlining Analysis
- Calculate function size (instruction count)
- Estimate call frequency
- Cost-benefit analysis
- Detect recursive calls
2. SSA Transformation
- Clone function body in SSA form
- Replace parameters with arguments
- Rename SSA variables to avoid conflicts
- Handle return values
3. Integration Points
- Inline before other optimizations
- Update call graph
- Preserve debug information
- Handle multiple return points
4. Heuristics
bool should_inline(func_t *func, call_site_t *site) {
if (func->insn_count > 30) return false; // Too large
if (func->is_recursive) return false; // Recursive
if (site->in_loop) return true; // Hot path
if (func->call_count == 1) return true; // Called once
return func->insn_count < 10; // Small enough
}
Test Cases
// Simple inlining
static int square(int x) { return x * x; }
int test1() {
return square(5); // Should inline to: return 5 * 5;
}
// Multiple returns
int abs(int x) {
if (x < 0) return -x;
return x;
}
int test2(int y) {
return abs(y) + 1; // Should inline with phi node
}
// Loop context (high value)
int sum(int *arr, int n) {
int total = 0;
for (int i = 0; i < n; i++) {
total += get_value(arr, i); // Should inline if small
}
return total;
}
// Don't inline large functions
int complex_function(int x) {
// 100+ instructions
}
// Don't inline recursive
int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n-1); // Don't inline
}
Performance Impact
- 5-15% improvement for call-heavy code
- Enables constant folding and dead code elimination
- Reduces function call overhead
- Improves instruction cache locality
Implementation Strategy
- Add function size calculation
- Implement basic inlining for leaf functions
- Handle multiple returns with phi nodes
- Add heuristics for call sites
- Integrate with existing optimizations
Benefits
- Eliminates function call overhead
- Enables cross-function optimizations
- Improves constant propagation
- Better register allocation
Interaction with Other Optimizations
- Run before: Constant propagation, DCE, LICM
- Enables: Constant folding, better CSE
- Consider: Code size vs speed tradeoff
Notes
- Start with static functions
- Be conservative with size limits
- Profile-guided inlining in future
- 主要语言
- C
- 星标
- 1.4k
- 派生
- 168
- 平均合并
- 2 天 12 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sysprog21/shecc 的其他 Issue
-
enhancement
难度 5/5 一周以上 新手友好度 30/100
-
Import a GitHub Action for benchmarking purposes可能已有人在做 @DrXiao 于 445 天前认领。 未关闭
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 76/100
-
priority:medium type:chore
难度 1/5 1-3 小时 新手友好度 84/100
crazy-goat/php-fpm-ng#1041 ·
维护者通常 1 天内回复
-
Bug Status: Needs Triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Test failure on FreeBSD: #26 unexpected system_clock source可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭low priority rt_lib tests
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复