Skip to content

Dead Code Elimination OptimizerAdd new DCE optimization#44

Open
Pshimaf-Git wants to merge 3 commits into
ixionlang:mainfrom
Pshimaf-Git:feature/dce
Open

Dead Code Elimination OptimizerAdd new DCE optimization#44
Pshimaf-Git wants to merge 3 commits into
ixionlang:mainfrom
Pshimaf-Git:feature/dce

Conversation

@Pshimaf-Git

@Pshimaf-Git Pshimaf-Git commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

General

Adds a dead code elimination (DCE) pass to the optimizer pipeline. Runs after constant folding to remove unreachable code.

Changes

  • optimizer/DeadCodeEliminationVisitor.kt -- New visitor that eliminates dead code after terminating statements (return, break, continue, throw) and constant-folded branches (if (true) / if (false) / while (false) / for i : [])
  • optimizer/Optimizer.kt -- Pipeline manager -- ConstantFolding -> DCE
  • api/IxApi.kt -- Wired optimizer into compilation flow

Currently, Semantic Analyzer find and report unreachable code too, but in these case it is no find it:

if (true) {
    return 1
}
println("dead code") 

DCE find it and successesfully replace these code with:

return 1

Closed issue

These PR close issue: #41

Summary by CodeRabbit

Summary

  • New Features

    • Dead code elimination is now automatically applied during compilation, removing unreachable code.
  • Improvements

    • Compilation optimization is more efficient by running unified optimization passes across all files.
    • Boolean conditionals and loops with constant values are automatically simplified, reducing generated output size (applies across JVM, Java, and Go compilation).

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eabfebb9-5ad6-4a2b-afc6-7351254f4bd0

📥 Commits

Reviewing files that changed from the base of the PR and between 1b9e9e3 and 1e9c32f.

📒 Files selected for processing (1)
  • src/main/kotlin/com/kingmang/ixion/optimizer/DeadCodeEliminationVisitor.kt
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/kotlin/com/kingmang/ixion/optimizer/DeadCodeEliminationVisitor.kt

📝 Walkthrough

Walkthrough

Adds DeadCodeEliminationVisitor, an AST-rewriting visitor that eliminates unreachable statements after returns, folds constant boolean control flow, and removes loops over empty list literals. A new Optimizer class sequences ConstantFoldingVisitor followed by DeadCodeEliminationVisitor over all files. IxApi replaces per-file ConstantFoldingVisitor calls with a single Optimizer invocation.

Changes

Dead Code Elimination & Optimizer Pipeline

Layer / File(s) Summary
DeadCodeEliminationVisitor core logic
src/main/kotlin/com/kingmang/ixion/optimizer/DeadCodeEliminationVisitor.kt
Implements the full AST-rewriting visitor: eliminates statements after return in blocks, folds if (true)/if (false), removes while (false) and for loops over empty lists, and recursively rewrites all other statement and expression node types while copying semantic metadata fields.
Optimizer pipeline and IxApi wiring
src/main/kotlin/com/kingmang/ixion/optimizer/Optimizer.kt, src/main/kotlin/com/kingmang/ixion/api/IxApi.kt
Optimizer sequences ConstantFoldingVisitor then DeadCodeEliminationVisitor over compilationSet.values; IxApi replaces three per-file ConstantFoldingVisitor loops in compile, compileToJava, and compileToGo with a single Optimizer().optimize(compilationSet!!) call.

Sequence Diagram

sequenceDiagram
    participant IxApi
    participant Optimizer
    participant ConstantFoldingVisitor
    participant DeadCodeEliminationVisitor

    rect rgba(70, 130, 180, 0.5)
        Note over IxApi,Optimizer: compile / compileToJava / compileToGo
        IxApi->>Optimizer: optimize(compilationSet)
    end
    loop each IxFile
        Optimizer->>ConstantFoldingVisitor: optimize(IxFile)
        ConstantFoldingVisitor-->>Optimizer: mutated AST
        Optimizer->>DeadCodeEliminationVisitor: optimize(IxFile)
        DeadCodeEliminationVisitor-->>Optimizer: pruned AST
    end
    Optimizer-->>IxApi: all files optimized
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

  • Dead Code Elimination #41: This PR directly implements the dead code elimination feature tracked in that issue, introducing DeadCodeEliminationVisitor with constant control-flow folding and unreachable-code removal, integrated as a second pass after constant folding.

Poem

🐇 Hop, hop through the AST I go,
Snipping branches with if (false) flow,
Loops on empty lists? Gone in a bound!
Dead code after return? No longer found.
The optimizer runs clean, pass by pass—
Only live code shall come to pass! 🌿

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The PR title 'Dead Code Elimination OptimizerAdd new DCE optimization' is vague and poorly formatted with missing spaces and unclear phrasing. Revise the title to be clear and concise, such as 'Add dead code elimination optimization pass' or 'Implement DCE optimizer pass'.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/kotlin/com/kingmang/ixion/optimizer/DeadCodeEliminationVisitor.kt`:
- Around line 294-301: The isTerminatingStatement function incorrectly returns
false for any BlockStatement with more than one statement, preventing it from
recognizing blocks that end with a return statement (like { prelude(); return 1
}). Instead of checking if the block has exactly one statement and returning
false otherwise, the function should extract and evaluate only the LAST
statement in the block to determine if the block itself is terminating. Modify
the logic to filter the non-null statements, take the last one from that
filtered list, and assign it to current for the next iteration, allowing the
function to properly identify terminating blocks regardless of how many
statements precede the final return statement.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ac5f3944-dd57-46a8-a5ca-0dc8f5649f09

📥 Commits

Reviewing files that changed from the base of the PR and between cea5b50 and 1b9e9e3.

📒 Files selected for processing (3)
  • src/main/kotlin/com/kingmang/ixion/api/IxApi.kt
  • src/main/kotlin/com/kingmang/ixion/optimizer/DeadCodeEliminationVisitor.kt
  • src/main/kotlin/com/kingmang/ixion/optimizer/Optimizer.kt

@Pshimaf-Git Pshimaf-Git marked this pull request as draft June 22, 2026 19:11
@Pshimaf-Git Pshimaf-Git marked this pull request as ready for review June 22, 2026 19:11
@Pshimaf-Git Pshimaf-Git marked this pull request as draft June 22, 2026 19:11
@Pshimaf-Git Pshimaf-Git marked this pull request as ready for review June 22, 2026 19:24
@k1ngmang

Copy link
Copy Markdown
Member

could you write tests?

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