Fix infinite recursion in find intrinsics#9215
Open
abadams wants to merge 4 commits into
Open
Conversation
…lement
Buffer::for_each_element previously accepted a callable taking fewer int
arguments than the buffer's dimensionality, silently iterating only over a
reduced-dimensionality slice. A nullary callable (e.g. buffer.fill(rng))
therefore wrote only the origin element. This is an easy mistake to make and
produces no error.
Require the callable to take either a const int *, or exactly as many int
arguments as the buffer has dimensions. This uncovered three real bugs where
buffers were only ever having their first element randomized:
- test/fuzz/lossless_cast.cpp: buf.fill(fuzz)
- test/correctness/interleave_rgb.cpp: fill([]() { return rand(); })
- test/correctness/multiple_scatter.cpp: fill(std::mt19937{0})
All three are fixed to use for_each_value, which passes an element reference
and does not depend on coordinates. The reduced-slice usage in
test/correctness/for_each_element.cpp is rewritten to slice off a channel
first so the callable's arg count matches the iteration domain.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A zero-argument callable passed to fill (for example a random number generator) has no coordinate to consume, so there is no distinction between filling via for_each_element and for_each_value. Dispatch such callables to for_each_value, which evaluates the callable once per element. This makes the natural buffer.fill(rng) idiom fill the whole buffer instead of tripping the for_each_element arg-count assertion. With this, the previously-broken buffer initializations in interleave_rgb, multiple_scatter, and lossless_cast (which only ever randomized the origin element) are fixed by reverting them to their original fill(rng) form. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
strip_widening_cast is meant to return a narrower-or-equal-width equivalent of its argument. When the argument was a narrowing cast (for example int16(int32_expr)), its innermost type was wider than the argument's type, and the consider(inner_t) branch returned an expression wider than the input. Feeding that back into the widening_mul narrowing rewrite re-widened the multiply, so the pass ping-ponged a widening_mul between its int32 and int64 forms forever, overflowing the stack. Only consider the inner type when it is no wider than the argument, i.e. when there is a genuine widening cast to strip. Found by the lossless_cast fuzzer once buffer.fill was fixed to actually randomize its input buffers. Adds a deterministic regression test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
alexreinking
approved these changes
Jul 16, 2026
The for_each_element arg-count assertion rejects a callable taking fewer int arguments than the buffer's dimensionality. These calls iterated a 2D domain over a 3D buffer and handled all channels inside the callable, so slice off the channel dimension first to get a matching 2D domain. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9215 +/- ##
=======================================
Coverage ? 69.73%
=======================================
Files ? 255
Lines ? 78575
Branches ? 18790
=======================================
Hits ? 54796
Misses ? 18177
Partials ? 5602 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The recent change to find intrinsics threw up a segfault on another PR in the lossless cast fuzzer due to infinite recursion. This is what lead me to uncover the fill issue in #9214 . This PR builds on that and actually fixes the infinite recursion in what I think is the intended way.