Skip to content

Avoid exponential simplification when merging conditional boxes - #9661

Merged
derek-gerstmann merged 3 commits into
mainfrom
dg/fix-conditional-box-merge
Nov 4, 2026
Merged

derek-gerstmann merged 3 commits into
mainfrom
dg/fix-conditional-box-merge

Conversation

@derek-gerstmann

@derek-gerstmann derek-gerstmann commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Conditional box unions reference previous min/max expressions multiple times.
Across nested specializations, simplification repeatedly traverses the shared
expression DAG as an expanded tree, causing exponential compile-time growth.

Run expression CSE before simplifying conditional min/max bounds so repeated
expressions remain shared through Let bindings.

The union formulas, used predicates, infinity handling, and unconditional
merge path are unchanged.

Regression tests

  • conditional_box_merge: compiler-level reproducer with semantic checks.
  • bounds_inference_specialized_spans: public-API pipeline reproducer with
    specialization-dependent RDom bounds and an independent pixel oracle.

Both tests have a 60-second timeout.

Validation

The two new regressions and bounds_internal were run on this branch using
Halide's ci-llvm-22 toolchain.

Initial verification against Halide v22.1.0 with LLVM 21.1.8:

  • Both stock reproducers exceeded a 30-second limit.
  • Patched box merging took approximately 0.07–0.09 seconds.
  • Patched public-pipeline compilation took approximately 0.37–0.39 seconds.
  • Thirteen selected upstream bounds/specialization correctness tests passed.

Full upstream, cross-platform, and GPU suites have not been run.

Supersedes #9660 to use the corrected branch name.

Preserve expression sharing with CSE before simplifying conditional bounds, and add standalone regression tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@alexreinking

Copy link
Copy Markdown
Member

Have you tested this PR for performance regressions against the apps? CSE has a way of hiding necessary structure from the simplifier.

We really should have a bounds inference fuzzer...

@derek-gerstmann

Copy link
Copy Markdown
Contributor Author

Have you tested this PR for performance regressions against the apps? CSE has a way of hiding necessary structure from the simplifier.

Yes, that's what triggered this ... generators in 22.0.0-dev444 that used to take 10-30sec started timing out after 1000's of seconds. Runtime with this fix was the same before the regression got introduced, but testing has only been on my own apps.

@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.01%. Comparing base (dd9d531) to head (6805c39).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9661      +/-   ##
==========================================
+ Coverage   72.91%   73.01%   +0.10%     
==========================================
  Files         266      266              
  Lines       83341    83345       +4     
  Branches    20468    20470       +2     
==========================================
+ Hits        60765    60855      +90     
+ Misses      16156    16129      -27     
+ Partials     6420     6361      -59     

☔ View full report in Codecov by Harness.
📢 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.

@alexreinking

Copy link
Copy Markdown
Member

Runtime with this fix was the same before the regression got introduced, but testing has only been on my own apps.

Just to confirm: that's the runtime performance of the app, not compilation time, right?

@derek-gerstmann

Copy link
Copy Markdown
Contributor Author

Runtime with this fix was the same before the regression got introduced, but testing has only been on my own apps.

Just to confirm: that's the runtime performance of the app, not compilation time, right?

Correct. I couldn't find a workaround outside of the compiler that didn't introduce a significant runtime performance regression. This PR avoids the compilation time, without impacting the original runtime performance for my specific test cases.

@derek-gerstmann
derek-gerstmann merged commit 8ac8ab4 into main Nov 4, 2026
27 of 28 checks passed
@alexreinking
alexreinking deleted the dg/fix-conditional-box-merge branch November 4, 2026 18:44
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