Skip to content

fix: drop references in dead branches inside live functions before chunk linking - #11059

Merged
IWANABETHATGUY merged 4 commits into
rolldown:mainfrom
elliott-with-the-longest-name-on-github:elliott/fix-dead-function-cross-chunk-imports
Nov 11, 2026
Merged

IWANABETHATGUY merged 4 commits into
rolldown:mainfrom
elliott-with-the-longest-name-on-github:elliott/fix-dead-function-cross-chunk-imports

Conversation

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

Keeping a function currently keeps all of its references, including references inside dead branches. For example, function f() { if (DEV) warn() }, where DEV = false comes from another module, still demands warn. If warn lives in a shared chunk, chunk linking exports it before the minifier removes the call. That export then becomes a DCE root, leaving an unused shared chunk in the output. This is the function-body follow-up described in #11053.

This adds prune_constant_branches after cross-module constant discovery and before statement inclusion. It removes references used exclusively in known-dead branches and records the branch decisions for the finalizer. The finalizer deletes those same branches before rewriting their references, so this works with minify: false too. Removing only the dependencies would leave emitted code referring to symbols that are no longer imported or declared.

Details:

  • Handles if, ternaries, and unreachable right-hand sides of &&, ||, and ??.
  • Preserves condition evaluation, including effects and exceptions, and preserves indirect-call semantics when simplifying expression callees.
  • Revisits only statements referencing known constants, in parallel per module. Synthetic dependencies and symbols with reachable uses stay intact.
  • Conservatively skips statements containing import records, dead if branches with hoisted declarations, and modules containing direct eval. CommonJS property constants, reassigned bindings, and namespace-member conditions are not used to decide branches.

The two-entry diagnostic-helper reproduction now emits only the two entry chunks, with or without minification. Regression tests cover that case, namespace helper references, disabled tree shaking/inlining, and safety cases for effects, exceptions, mutable exports, shadowing, block scope, hoisting, and indirect calls.

Validation: 2,020 integration tests and 105 unit tests passed; Clippy with all features and formatting checks passed. Test262 was excluded because its submodule wasn’t checked out.

@codspeed

codspeed Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 4 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing elliott-with-the-longest-name-on-github:elliott/fix-dead-function-cross-chunk-imports (911e6ff) with main (81a5d5e)

Open in CodSpeed

Footnotes

  1. 10 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. ↩

@IWANABETHATGUY
IWANABETHATGUY force-pushed the elliott/fix-dead-function-cross-chunk-imports branch from d7e1581 to d35a43b Compare November 10, 2026 18:09
@netlify

netlify Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit c0d668c
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6af3cc33b6e205574baa227c

IWANABETHATGUY and others added 2 commits November 11, 2026 02:57
oxc converts an array, object, or regexp literal with the default
`toString`, so `+({ __proto__: { toString: () => '1' } } + '')` evaluates
to `NaN`. The runtime gets `1`. With `minify: false`, nothing else removes
such a branch, so the pass removed a live branch. Use the same
`contains_object_literal` rule as `try_extract_const_literal`.
@IWANABETHATGUY

Copy link
Copy Markdown
Member

I pushed c0d668c to fix one case: with minify: false and an imported EMPTY = '', the pass removed the live branch of if (+({ __proto__: { toString: () => '1' } } + EMPTY)). oxc converts the object with the default toString and gets NaN, but the runtime gets 1. Now the pass skips a condition that contains an array, object, or regexp literal, as try_extract_const_literal does.

@IWANABETHATGUY
IWANABETHATGUY merged commit 165107c into rolldown:main Nov 11, 2026
32 checks passed
@rolldown-guard rolldown-guard Bot mentioned this pull request Nov 11, 2026
shulaoda added a commit that referenced this pull request Nov 11, 2026
## [1.3.2] - 2026-11-11

### 🚀 Features

- magic-string: add a `hires` constructor option (#11643) by @IWANABETHATGUY
- cjs: provide `__dirname` and `__filename` to CommonJS modules in Node ESM output (#10680) by @irvinebroque

### 🐛 Bug Fixes

- strict-execution-order: place re-export targets that no consumer selects (#11634) by @hyfdev
- sourcemap: use the `map` a hook returns with the native MagicString (#11642) by @IWANABETHATGUY
- drop no-op init calls from re-exports and imports (#11632) by @hyfdev
- detect `__esModule` set with `Object.defineProperty(module.exports, ...)` (#11622) by @agarwalla-chirag
- hoist `var` in top-level catch blocks of wrapped modules (#11626) by @hyfdev
- detect top-level `await` in top-level `catch` blocks (#11640) by @hyfdev
- sourcemap: keep columns when a hook returns the native MagicString (#11628) by @anonrig
- make a module without module syntax CommonJS when it is both imported and required (#11633) by @hyfdev
- drop references in dead branches inside live functions before chunk linking (#11059) by @elliott-with-the-longest-name-on-github
- make cross-chunk export names independent of module load order (#10882) by @danieljvdm
- don't treat new URL() references as execution dependencies (#11605) by @shuvroroy
- stop the options passed to hooks from keeping a build's plugins alive (#11623) by @Brooooooklyn
- strict-execution-order: run the declaring module of an inlined constant read through a kept barrel (#11546) by @hyfdev
- hoist `var` in top-level for-in and for-of heads of wrapped modules (#11620) by @hyfdev
- rollup-tests: fail the test case on assertion error in `renderError` (#11621) by @sapphi-red
- preserve rendering errors when `renderError` fails (#11614) by @sapphi-red
- count emitted chunks when rendering relative-path externals (#11611) by @hyfdev
- avoid panic on `{ b: [y = 1] }` destructuring in a wrapped module (#11563) by @IWANABETHATGUY
- treeshaking: tree-shake statements with `instanceof` that a constant makes pure (#11610) by @hyfdev
- dev: don't panic when a partial rebuild adds a relative-path external (#11604) by @hyfdev
- rolldown: fail the build instead of hanging when a module task panics (#11562) by @Brooooooklyn
- rolldown: don't panic in module tasks after the build is dropped (#11557) by @Brooooooklyn
- browser: default `cwd` to `/` (#11554) by @Brooooooklyn

### 🚜 Refactor

- use `.agents/skills` for skill files (#11627) by @camc314

### 📚 Documentation

- perf-kit: show the build config on the page (#11646) by @IWANABETHATGUY

### ⚡ Performance

- trace sequential sourcemap tokens with a bounded cursor (#11545) by @Dunqing
- use raw tokens when collapsing sourcemaps (#11524) by @Dunqing
- magic-string: free the index table of a MagicString returned from transform (#11558) by @Brooooooklyn

### 🧪 Testing

- dev: add exports assignment test (#5654) by @sapphi-red
- rollup-tests: normalize Rollup Error for renderError (#11613) by @sapphi-red
- rolldown: cover async render hooks (#11560) by @Brooooooklyn
- watch: cover the native backend and fix the watch-error CLI test race (#11559) by @Brooooooklyn

### ⚙️ Miscellaneous Tasks

- deps: update dependency vitepress-plugin-group-icons to v1.8.2 (#11647) by @jong-kyung
- deps: update dependency vite-plus to v1.6.0 (#11641) by @renovate[bot]
- skip upstream-only workflows on forks (#11644) by @jong-kyung
- deps: update napi to v3.16.2 (#11639) by @renovate[bot]
- deps: update rollup submodule for tests to v4.68.2 (#11608) by @rolldown-guard[bot]
- rollup-tests: retriage failed tests (#11615) by @sapphi-red
- deps: update test262 submodule for tests (#11609) by @rolldown-guard[bot]
- deps: update npm packages (#11572) by @renovate[bot]
- docs: always use https (#11606) by @TheAlexLichter
- deps: update dependency vite-plus to v1.5.1 (#11591) by @renovate[bot]
- stop building `better-sqlite3` on install (#11602) by @hyfdev
- deps: upgrade oxc to 0.158.0 (#11601) by @camc314
- deps: update dependency rolldown-plugin-dts to ^0.30.0 (#11588) by @renovate[bot]
- deps: update crate-ci/typos action to v1.52.0 (#11590) by @renovate[bot]

### ❤️ New Contributors

* @jong-kyung made their first contribution in [#11647](#11647)
* @irvinebroque made their first contribution in [#10680](#10680)
* @agarwalla-chirag made their first contribution in [#11622](#11622)
* @danieljvdm made their first contribution in [#10882](#10882)

Co-authored-by: shulaoda <[email protected]>
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.

3 participants