Skip to content

constant_integer_bounds: bound Select by the union of its values - #9604

Open
alexreinking wants to merge 1 commit into
alexreinking/prefetch-fixesfrom
alexreinking/constant-bounds-select
Open

alexreinking wants to merge 1 commit into
alexreinking/prefetch-fixesfrom
alexreinking/constant-bounds-select

Conversation

@alexreinking

Copy link
Copy Markdown
Member

constant_integer_bounds had no case for Select, so a select fell through to the full bounds of its type. A select always evaluates to one of its two values, so this PR bounds it by the union of the two values' bounds (the condition is ignored). The three-argument Call::if_then_else gets the same treatment; the two-argument form, which has no else value, is unchanged.

This matters for code that decodes small integer codes through selects and then needs constant_integer_bounds to prove that a later integer sum can't overflow. The GGML app (#9511) does this for ternary (tq1_0) weights, and currently works around the gap by replacing each select with a variable bound in a Scope to the union of its values' bounds.

correctness_constant_interval gains test_constant_integer_bounds_of_select(), which fails before this change and passes after it. It covers:

  • a scalar select: select(x > 0, int(u8), -3) → [-3, 255];
  • a nested select;
  • a vector (int16x8) select;
  • a select with one unbounded value (full range);
  • a three-argument if_then_else.

A comment in correctness_struct_type that said bounds_helper had no Select case is updated.

Stacked on #9599.

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)

Tested on macOS arm64 with LLVM 22:

  • ctest -R 'correctness_(bound|bounds|constant_interval|struct_type|bits_known|lossless_cast|intrinsics|interval|many_inlined_selects|multi_way_select|tuple_select|popc_clz_ctz|overflow|simd_op_check_(arm|x86|wasm|hvx))|fuzz_lossless_cast': 39/39 passed.
  • ctest -L correctness, excluding GPU and Hexagon tests: 618 of 621 passed.
    • correctness_cross_compilation failed because the local LLVM build has no Hexagon target.
    • The two auto_schedule_no_* tests failed because the autoscheduler plugin wasn't built; both pass once it is.

@alexreinking
alexreinking added this pull request to stack #9605 October 23, 2026 14:43
@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (alexreinking/prefetch-fixes@d645519). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/ConstantBounds.cpp 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                      Coverage Diff                       @@
##             alexreinking/prefetch-fixes    #9604   +/-   ##
==============================================================
  Coverage                               ?   72.95%           
==============================================================
  Files                                  ?      263           
  Lines                                  ?    82494           
  Branches                               ?    20179           
==============================================================
  Hits                                   ?    60184           
  Misses                                 ?    15914           
  Partials                               ?     6396           

☔ 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
alexreinking force-pushed the alexreinking/constant-bounds-select branch from 205a560 to 9e8a832 Compare October 24, 2026 15:43
@alexreinking
alexreinking removed this pull request from stack #9605 October 24, 2026 15:44
@alexreinking
alexreinking added this pull request to stack #9609 October 24, 2026 15:45
@alexreinking
alexreinking force-pushed the alexreinking/constant-bounds-select branch from 9e8a832 to 01bc902 Compare October 25, 2026 21:05
@alexreinking
alexreinking removed this pull request from stack #9609 October 25, 2026 21:05
@alexreinking
alexreinking changed the base branch from alexreinking/arm-prefetch-line-size to alexreinking/prefetch-fixes October 25, 2026 21:05
@alexreinking
alexreinking added this pull request to stack #9613 October 25, 2026 21:05
@alexreinking
alexreinking force-pushed the alexreinking/constant-bounds-select branch from 01bc902 to beb12bb Compare October 26, 2026 19:58
bounds_helper had no case for Select, so any select fell through to the
full bounds of its type. A select always evaluates to one of its two
values, so the union of their constant bounds is a sound bound; the
condition is ignored. The three-argument form of Call::if_then_else is
handled the same way.

This matters for code that decodes small integer codes via selects (e.g.
ternary weights in quantized GGML kernels) and then needs
constant_integer_bounds to prove that a subsequent integer sum can't
overflow. Such code currently has to work around the missing case by
substituting the selects with variables bound in a Scope.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexreinking
alexreinking force-pushed the alexreinking/constant-bounds-select branch from beb12bb to 045408f Compare October 26, 2026 23:05
@alexreinking
alexreinking removed this pull request from stack #9613 October 28, 2026 03:25
@alexreinking
alexreinking added this pull request to stack #9629 October 28, 2026 03:25

This branch has not been deployed

No deployments
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