Skip to content

feat(minifier): remove unreachable code after terminating statements#24441

Merged
graphite-app[bot] merged 1 commit into
mainfrom
minifier-unreachable-after-terminating-statements
Jul 13, 2026
Merged

feat(minifier): remove unreachable code after terminating statements#24441
graphite-app[bot] merged 1 commit into
mainfrom
minifier-unreachable-after-terminating-statements

Conversation

@Dunqing

@Dunqing Dunqing commented Jul 13, 2026

Copy link
Copy Markdown
Member

Folding a define-replaced branch can leave if (true) { ... return fn; } as a bare block: the lexical declaration inside pins the block (flattening it could collide with sibling bindings), and the statements after it survived even though the block always returns. rolldown's per-module tree-shake hits this shape and ships the dead branch (rolldown/rolldown#10184).

minimize_statements only flagged dead control flow on direct jump statements, each arm setting the flag by hand. The driver now flips the flag whenever the statement it just emitted never completes normally: a direct jump, a block ending in a jump, an if/else where both branches jump, or a try where every path jumps. That single check subsumes the per-arm writes, so the &mut bool threaded through the return/throw handlers is gone. The removal reuses the existing dead-zone machinery, so var hoisting and kept function declarations behave as before, and both DCE and full-minify modes are covered.

A minimized dead zone keeps hoisting survivors after the jump — function declarations and the initializer-less var stub KeepVar re-emits — so the block predicate skips those from the back instead of testing only the literal last statement; nothing after an aborting statement ever runs, so they cannot change how the block completes.

The helper is conservative on purpose: labeled statements never count (a: { break a; } completes normally), and loops and switches are left to their specific folds. Terser removes the same tails; esbuild keeps them. The leftover { ... } block wrapper is also kept — flattening a block whose lexical declarations may collide with siblings is a separate rewrite, and terser and esbuild keep the braces too.

Example

__SSR__ defined to true (rolldown treeshake, minify: false):

export function test1() {
  if (true) {
    const fn = () => console.log("ssr");
    fn.stop = fn;
    return fn;
  }
  const fn = () => console.log("client");
  fn.stop = fn;
  return fn;
}

Before, the unreachable client tail stayed in the output. After:

export function test1() {
	{
		const fn = () => console.log("ssr");
		fn.stop = fn;
		return fn;
	}
}

Verified with the minifier unit suite, test262/babel minifier conformance (no changes), DCE --twice idempotency, and just minsize (antd gzip 455.51 kB -> 455.50 kB, iteration counts unchanged).

Fixes rolldown/rolldown#10184.

Implemented with AI assistance (Claude Code); design, review, and verification driven by the author per the repo AI-usage policy.

@github-actions github-actions Bot added the A-minifier Area - Minifier label Jul 13, 2026

Dunqing commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Dunqing Dunqing added the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 13, 2026
@oxc-guard

oxc-guard Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

@oxc-guard oxc-guard Bot removed the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 13, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 52 untouched benchmarks
⏩ 19 skipped benchmarks1


Comparing minifier-unreachable-after-terminating-statements (6cd22be) with main (cfed680)

Open in CodSpeed

Footnotes

  1. 19 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing
Dunqing marked this pull request as ready for review July 13, 2026 08:00
@Dunqing
Dunqing requested a review from overlookmotel as a code owner July 13, 2026 08:00
@Dunqing
Dunqing force-pushed the minifier-unreachable-after-terminating-statements branch from 17f80bf to 6cd22be Compare July 13, 2026 08:10
@Dunqing
Dunqing marked this pull request as draft July 13, 2026 08:11
@Dunqing Dunqing added the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 13, 2026
@oxc-guard oxc-guard Bot removed the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 13, 2026
@Dunqing
Dunqing marked this pull request as ready for review July 13, 2026 08:42
@Dunqing

Dunqing commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

I've reviewed the code myself and understand it.

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Jul 13, 2026

Dunqing commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

Merge activity

…24441)

Folding a define-replaced branch can leave `if (true) { ... return fn; }` as a bare block: the lexical declaration inside pins the block (flattening it could collide with sibling bindings), and the statements after it survived even though the block always returns. rolldown's per-module tree-shake hits this shape and ships the dead branch (rolldown/rolldown#10184).

`minimize_statements` only flagged dead control flow on direct jump statements, each arm setting the flag by hand. The driver now flips the flag whenever the statement it just emitted never completes normally: a direct jump, a block ending in a jump, an if/else where both branches jump, or a try where every path jumps. That single check subsumes the per-arm writes, so the `&mut bool` threaded through the return/throw handlers is gone. The removal reuses the existing dead-zone machinery, so `var` hoisting and kept function declarations behave as before, and both DCE and full-minify modes are covered.

A minimized dead zone keeps hoisting survivors after the jump — `function` declarations and the initializer-less `var` stub KeepVar re-emits — so the block predicate skips those from the back instead of testing only the literal last statement; nothing after an aborting statement ever runs, so they cannot change how the block completes.

The helper is conservative on purpose: labeled statements never count (`a: { break a; }` completes normally), and loops and switches are left to their specific folds. Terser removes the same tails; esbuild keeps them. The leftover `{ ... }` block wrapper is also kept — flattening a block whose lexical declarations may collide with siblings is a separate rewrite, and terser and esbuild keep the braces too.

## Example

`__SSR__` defined to `true` (rolldown treeshake, `minify: false`):

```js
export function test1() {
  if (true) {
    const fn = () => console.log("ssr");
    fn.stop = fn;
    return fn;
  }
  const fn = () => console.log("client");
  fn.stop = fn;
  return fn;
}
```

Before, the unreachable client tail stayed in the output. After:

```js
export function test1() {
	{
		const fn = () => console.log("ssr");
		fn.stop = fn;
		return fn;
	}
}
```

Verified with the minifier unit suite, test262/babel minifier conformance (no changes), DCE `--twice` idempotency, and `just minsize` (antd gzip 455.51 kB -> 455.50 kB, iteration counts unchanged).

Fixes rolldown/rolldown#10184.

Implemented with AI assistance (Claude Code); design, review, and verification driven by the author per the repo AI-usage policy.
@graphite-app
graphite-app Bot force-pushed the minifier-unreachable-after-terminating-statements branch from 6cd22be to 616bfa2 Compare July 13, 2026 08:44
@graphite-app
graphite-app Bot merged commit 616bfa2 into main Jul 13, 2026
29 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jul 13, 2026
@graphite-app
graphite-app Bot deleted the minifier-unreachable-after-terminating-statements branch July 13, 2026 08:48
Boshen added a commit that referenced this pull request Jul 14, 2026
### 🚀 Features

- 616bfa2 minifier: Remove unreachable code after terminating statements
(#24441) (Dunqing)
- ddab89a data_structures: Add `likely` and `unlikely` functions
(#24368) (overlookmotel)
- a3a39f9 react_compiler: Implement enableEmitHookGuards codegen
(#24329) (Boshen)
- b79eef7 minifier: Apply De Morgan's law to negated comparison chains
in jump guards and loop tests (#24279) (Dunqing)
- 34ff7b4 minifier: Drop write-only property assignments to unused local
bindings by default (#24112) (Dunqing)
- 1b829d8 semantic: Record const enums in EnumData (#24268) (Dunqing)
- ba0944c semantic: Add `Scoping::set_symbol_span` (#24221) (camc314)

### 🐛 Bug Fixes

- 7d33363 minifier: Preserve guaranteed throws from class heritage
evaluation (#24349) (Dunqing)
- 058a62f semantic: Track ambient contexts in `SemanticBuilder` (#24327)
(camc314)
- 721eb0b transformer/decorator: Scope accessor class binding (#24330)
(camc314)
- 1ebdce3 semantic: Allow reserved keywords in ambient declaration types
(#24325) (camc314)
- 460176a track-memory-allocations: Exclude arena chunks from Sys allocs
(#24292) (Dunqing)
- af4922b transformer: Clear lowered namespace redeclarations (#24300)
(camc314)
- ffd2765 semantic: Mark declared computed `MethodDefinition`s as type
references (#24296) (camc314)
- f17514b isolated-declarations: Emit const readonly fields as types
(#24288) (camc314)
- 40f769d minifier: Make `__proto__` write tracking execution-order
independent (#24280) (Dunqing)
- 6371fed transformer: Remove stale enum member bindings (#24272)
(camc314)
- f05dfab transformer: Correct symbol flags for lowered namespaces
(#24271) (Dunqing)
- 84eeb55 transformer: Correct symbol flags for lowered enums (#24269)
(Dunqing)
- c3057da transformer: Preserve generated class binding spans (#24220)
(camc314)
- 8260096 transformer: Correct span for lowered namespace symbol
(#24222) (camc314)
- 42d00d3 semantic: Mark declared class heritage as type references
(#24237) (camc314)
- 588d997 semantic: Mark TS `PropertyDefinition`s computed fields as
type references (#24233) (camc314)
- 9b95632 semantic: Mark computed method keys in `TSMethodSignature`s as
type references (#24232) (camc314)

### ⚡ Performance

- 5b26643 transformer_plugins: Dispatch global defines by trailing name
(#23666) (Boshen)
- dce0f29 react_compiler: Replace all compiled functions in a single AST
walk (#24403) (Boshen)
- f85f0d8 ast: Delegate inherited enum variants in clone_in and estree
derives (#23555) (Boshen)
- 3ff0234 allocator: Remove `unwrap` from `ReplaceWith` (#24365)
(overlookmotel)
- ab22e80 transformer: Fix Rust 1.97 performance regression (#24354)
(camc314)
- b47585c parser: Use `ReplaceWith` instead of `TakeIn` (#24018)
(overlookmotel)
- b227a06 minifier: Use `ReplaceWith` instead of `TakeIn` (#24017)
(overlookmotel)

Co-authored-by: Boshen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Defining compile-time environment variables and inadequate code cleanup

1 participant