Skip to content

Add Strength Reduction and Dead Code Elimination to AST Optimizer#4758

Merged
jedel1043 merged 3 commits intoboa-dev:mainfrom
Psykii22:optimize-dce-strength-reduce
Mar 11, 2026
Merged

Add Strength Reduction and Dead Code Elimination to AST Optimizer#4758
jedel1043 merged 3 commits intoboa-dev:mainfrom
Psykii22:optimize-dce-strength-reduce

Conversation

@Psykii22
Copy link
Copy Markdown
Contributor

@Psykii22 Psykii22 commented Feb 28, 2026

This PR adds two new AST optimizer passes:

  • Strength Reduction: Optimizes computationally heavy expressions
    without side effects. Specifically:

    • Eliminates division x / 2 in favor of multiplication x * 0.5.
    • Transforms exponents x ** 2 into x * x.
      Both optimizations use std::mem::replace instead of clone to avoid redundant memory allocations.
  • Dead Code Elimination: Removes unreachable if, while, and for
    branches/loops when the condition is a known boolean literal (after constant folding).
    handles JavaScript hoisting semantics by bailing out if the removed
    body contains var declarations, function declarations, or generator/async variants.
    also, prevents side-effect vulnerabilities in for loops by
    refusing to eliminate loops that contain an initialization statement. like for(let i=doAuth();false;i++)

Also bounds all optimizer pass loops with MAX_PASS_ITERATIONS to prevent
infinite loops.

@Psykii22 Psykii22 requested a review from a team as a code owner February 28, 2026 16:26
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 28, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 49,713 49,713 0
Ignored 2,262 2,262 0
Failed 988 988 0
Panics 0 0 0
Conformance 93.86% 93.86% 0.00%

Tested main commit: 5994303fbdc61f8cfa941a244d98ce6bfbd3ec5b
Tested PR commit: e73970b53e2d2d8c619fe5937828937e17002e1e
Compare commits: 5994303...e73970b

@Psykii22 Psykii22 force-pushed the optimize-dce-strength-reduce branch 2 times, most recently from 430cad8 to 39ce610 Compare February 28, 2026 17:17
@nekevss nekevss added A-Enhancement New feature or request C-AST Issue surrounding the abstract syntax tree labels Mar 1, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 1, 2026

Codecov Report

❌ Patch coverage is 56.00000% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.98%. Comparing base (6ddc2b4) to head (e73970b).
⚠️ Report is 804 commits behind head on main.

Files with missing lines Patch % Lines
...re/engine/src/optimizer/pass/strength_reduction.rs 38.77% 30 Missing ⚠️
...engine/src/optimizer/pass/dead_code_elimination.rs 50.87% 28 Missing ⚠️
core/engine/src/optimizer/mod.rs 81.81% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4758       +/-   ##
===========================================
+ Coverage   47.24%   57.98%   +10.74%     
===========================================
  Files         476      559       +83     
  Lines       46892    61208    +14316     
===========================================
+ Hits        22154    35493    +13339     
- Misses      24738    25715      +977     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Psykii22 Psykii22 force-pushed the optimize-dce-strength-reduce branch 2 times, most recently from c8d63a3 to 7f909f9 Compare March 3, 2026 06:37
Copy link
Copy Markdown
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

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

This is very cool, thank you!

By the way, did you have the opportunity to test if this increases performance in the V8 benchmarks?

@Psykii22 Psykii22 force-pushed the optimize-dce-strength-reduce branch 4 times, most recently from 37a7ca3 to 7f909f9 Compare March 3, 2026 09:05
@Psykii22
Copy link
Copy Markdown
Contributor Author

Psykii22 commented Mar 3, 2026

sir while running the V8 benchmark tests, I encountered a STATUS_STACK_OVERFLOW when executing combined.js. To investigate, I cloned a fresh copy of boa-dev/boa (latest main, no modifications), built it in release mode, and reproduced the same crash.

When running with a larger stack (16 MB thread), all benchmarks completed successfully (Score: 214). also i ran some custom microbenchmarks around the specific patterns introduced by the optimisation passes, and that showed measurable performance improvements to the current boa

@jedel1043
Copy link
Copy Markdown
Member

No worries! You're probably running this on Windows, which has a much smaller stack size by default than Linux. We're kinda working towards fixing that, but than you for testing it anyways!

Copy link
Copy Markdown
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

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

Everything looks good! We can merge this after fixing all the CI warnings

@hansl
Copy link
Copy Markdown
Contributor

hansl commented Mar 4, 2026

Hey, are those test262 still failing? That is a huge red flag that something broke.

Adds two new AST optimizer passes:

- **Strength Reduction**: Optimizes computationally heavy expressions
  without side effects. Specifically:
  - Eliminates division `x / 2` in favor of multiplication `x * 0.5`.
  - Transforms exponents `x ** 2` into `x * x`.
  Both optimizations use `std::mem::replace` instead of clone to avoid redundant memory allocations.

- **Dead Code Elimination**: Removes unreachable `if`, `while`, and `for`
  branches/loops when the condition is a known boolean literal (after constant folding).
  Correctly handles JavaScript hoisting semantics by bailing out if the removed
  body contains `var` declarations, `function` declarations, or generator/async variants.
  Additionally, prevents side-effect vulnerabilities in `for` loops by
  refusing to eliminate loops that contain an initialization statement.

Also bounds all optimizer pass loops with MAX_PASS_ITERATIONS to prevent
infinite loops.

Ref: boa-dev#4694
@Psykii22 Psykii22 force-pushed the optimize-dce-strength-reduce branch from a3bccbd to 74a8be4 Compare March 8, 2026 19:10
@jedel1043 jedel1043 added this pull request to the merge queue Mar 11, 2026
Merged via the queue into boa-dev:main with commit ccc9aef Mar 11, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Enhancement New feature or request C-AST Issue surrounding the abstract syntax tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants