Skip to content

Solve: treat min/max of booleans as And/Or in SolveForInterval - #9457

Merged
alexreinking merged 1 commit into
halide:mainfrom
wraith1995:fix/solve-min-max-bool
Sep 23, 2026
Merged

alexreinking merged 1 commit into
halide:mainfrom
wraith1995:fix/solve-min-max-bool

Conversation

@wraith1995

Copy link
Copy Markdown
Contributor

Fixes #9456

Checklist

  • Tests added or updated (not required for docs, CI config, or typo fixes)
  • Documentation updated (if public API changed)
  • Python bindings updated (if public API changed)
  • Benchmarks are included here if the change is intended to affect performance
  • Commits include AI attribution where applicable (see Code of Conduct)

solve_for_outer_interval / solve_for_inner_interval mishandled min/max of
boolean operands. Boolean AND/OR of comparisons is represented as min/max by
and_condition_over_domain and by the simplifier, but SolveForInterval implemented
visit(And)/visit(Or)/visit(Not)/visit(Select) and not visit(Min)/visit(Max). So a
negated min-of-bools was not distributed via De Morgan; e.g.

    solve_for_outer_interval(!min(x < 0, x < 2), "x")

returned [2, +inf) instead of [0, +inf) (min(x<0, x<2) == x<0, so !min(...) is
x>=0).

This caused a silent miscompile: trim_no_ops relaxes a loop's no-op condition
over inner loops with and_condition_over_domain (yielding min/max of comparisons)
and trims the loop via solve_for_outer_interval. The wrong interval trimmed a
serial loop to a single iteration, dropping work -- e.g. a wavefront/diamond-tiled
recurrence with a GuardWithIf (likely) split tail over a non-tile-multiple extent
produced incorrect results under parallelism.

Fix: add visit(Min)/visit(Max) to SolveForInterval, rewriting a boolean min to
And and max to Or (mirroring the existing visit(Select)).

Adds regression checks to test/correctness/solve.cpp.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
@abadams

abadams commented Sep 22, 2026

Copy link
Copy Markdown
Member

Change LGTM but I'm suspicious of the comment claiming that these can come from the simplifier. Doesn't the simplifier normalize these to And/Or?

@wraith1995

wraith1995 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Change LGTM but I'm suspicious of the comment claiming that these can come from the simplifier. Doesn't the simplifier normalize these to And/Or?

@abadams It does not; I had claude check this directly.

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.98%. Comparing base (d33fd25) to head (43ff34a).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
src/Solve.cpp 75.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9457      +/-   ##
==========================================
- Coverage   70.21%   69.98%   -0.23%     
==========================================
  Files         261      261              
  Lines       79792    79983     +191     
  Branches    19451    19491      +40     
==========================================
- Hits        56022    55973      -49     
- Misses      17966    18112     +146     
- Partials     5804     5898      +94     

☔ 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.

@abadams

abadams commented Sep 22, 2026

Copy link
Copy Markdown
Member

Maybe we shouldn't even have And/Or and just pretty-print min and max on bools differently

@mcourteaux

Copy link
Copy Markdown
Contributor

@wraith1995 Just to be clear; you'll want to update the comment for this to be merged.

@wraith1995

Copy link
Copy Markdown
Contributor Author

@wraith1995 Just to be clear; you'll want to update the comment for this to be merged.

@mcourteaux I am confused; the comment about the simplifier is accurate because it does indeed not simplify the min/max away.

@abadams I am happy to try that- I suspect it can happen with very few changes to the code base.

@abadams

abadams commented Sep 23, 2026

Copy link
Copy Markdown
Member

The PR is valid as-is, but perhaps the simplifier should normalize Min/Max on bools to And/Or (making the comment inaccurate)? Removing And/Or was just me musing about longer term paths.

@alexreinking
alexreinking merged commit a05f883 into halide:main Sep 23, 2026
27 of 28 checks passed
@wraith1995
wraith1995 deleted the fix/solve-min-max-bool branch September 25, 2026 15:11
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.

SolveForInterval doesn't think about Min and Max of booleans.

4 participants