Implement Loop-Invariant Code Motion (LICM) optimization
@dragon540 đang làm issue này rồi.
Từ ngày 14/8/2026.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Start with the proposed CFG loop detection, including loop headers, back edges, nesting, and dominance analysis. Then define invariant and safety checks before implementing hoisting to loop preheaders; completion should preserve SSA and exception semantics, support nested loops, and avoid side effects or potentially faulting operations.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
Loop-Invariant Code Motion is a critical optimization that moves computations that don't change within a loop to execute before the loop. This can significantly improve performance for loop-heavy code.
Current Behavior
for (int i = 0; i < n; i++) {
int constant = x * y; // Recomputed every iteration
arr[i] = i + constant;
}
Expected Behavior
The optimizer should detect that x * y is loop-invariant and hoist it:
int constant = x * y; // Moved outside loop
for (int i = 0; i < n; i++) {
arr[i] = i + constant;
}
Implementation Requirements
1. Loop Detection
- Identify natural loops in CFG
- Find loop headers and back edges
- Build loop nesting tree
2. Invariant Analysis
- Identify instructions that don't depend on loop variables
- Check for side effects
- Verify safety of hoisting
3. Code Motion
- Move invariant code to loop preheader
- Preserve SSA form
- Update phi nodes if needed
4. Safety Checks
- Don't hoist potentially faulting operations
- Preserve exception semantics
- Check for aliasing issues
Test Cases
// Basic invariant hoisting
void basic(int *arr, int n, int x, int y) {
for (int i = 0; i < n; i++) {
int inv = x + y; // Should hoist
arr[i] = i * inv;
}
}
// Nested loops
void nested(int *arr, int n, int m) {
for (int i = 0; i < n; i++) {
int outer_inv = n * 2; // Hoist to function entry
for (int j = 0; j < m; j++) {
int inner_inv = i * 3; // Hoist to outer loop
arr[i*m + j] = outer_inv + inner_inv + j;
}
}
}
// Don't hoist side effects
void side_effects(int n) {
for (int i = 0; i < n; i++) {
int x = getchar(); // Don't hoist - side effect
process(x);
}
}
// Don't hoist potential faults
void safe(int *p, int n) {
for (int i = 0; i < n; i++) {
if (p) {
int x = *p; // Don't hoist - might fault if p becomes NULL
use(x);
}
}
}
Performance Impact
- Can reduce loop execution time by 20-50% for computation-heavy loops
- Especially effective for nested loops
- Reduces register pressure inside loops
Priority
MEDIUM - Significant performance improvement for common patterns
Complexity
HARD - Requires loop analysis and careful safety checking
Implementation Strategy
- Implement loop detection in CFG
- Add dominance analysis
- Implement invariant detection
- Add safe hoisting transformation
- Extend to nested loops
Benefits
- Major performance improvement for loops
- Reduces redundant computations
- Enables further optimizations
- Standard optimization in production compilers
Notes
- Start with innermost loops
- Be conservative about safety
- Consider profile-guided decisions
- Measure impact on real code
- Ngôn ngữ chính
- C
- Star
- 1.4k
- Fork
- 168
- Merge trung bình
- 2 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sysprog21/shecc
-
Enhance the rebuild logic of the bootstrapCó thể đã có người làm @DrXiao đã nhận 5 ngày trước. Đang mở
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 38/100
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
-
Import a GitHub Action for benchmarking purposesCó thể đã có người làm @DrXiao đã nhận 443 ngày trước. Đang mở
sysprog21/shecc#236 · 2 bình luận · 1 reaction · 1 người được giao ·
Tất cả issue của sysprog21/shecc
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
polhenarejos/pico-fido#302 ·
-
开发板墙缺少 ESP32_S31_MOSAICOĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
espressif/esp-vision#16 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
libsdl-org/SDL#16577 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 1 ngày
-
halt_error adds a trailing newline for non-string input, contrary to the manualCó thể đã có người làm @Huang-404-Q đã nhận hôm nay. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 2 ngày