Repository navigation
fix(hsv): stop hsv2rgb_rainbow leaving AVR's R1 dirty on three paths (#668) - #3808
Merged
Merged
Conversation
…668) #668 is the "unexpected red pixel" in fill_rainbow: on AVR one pixel mid-ramp comes out with a wrong channel, the affected pixel moves with the hue, and unrelated edits to the sketch make it appear and disappear. The community diagnosed it correctly as compiler reordering around the scale8_LEAVING_R1_DIRTY inline asm, and #943 fixed the reordering half by inserting asm volatile("") barriers between those calls. This fixes a second, separate half of the same underlying hazard. scale8*_LEAVING_R1_DIRTY skips restoring AVR's __zero_reg__ so that several calls can share one cleanup_R1(); its docs say "You MUST call cleanup_R1() after using this function". hsv2rgb_rainbow has three paths that return without doing so: 1. `if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale)` dirties R1, then `sat == 255` skips the saturation block and `val == 255` skips the value block -- nothing cleans up. 2. Same call, then the `sat == 0` branch, which only assigns 255s. 3. `val = scale8_video_LEAVING_R1_DIRTY(val, val)` dirties R1, then the `val == 0` branch assigns zeros; only the else branch batches into a cleanup_R1(). Leaving r1 non-zero on return violates the AVR ABI -- the compiler assumes r1 is a hard zero -- so whatever runs next can compute garbage. That matches every reported symptom: which pixel is wrong depends on which hue takes which branch, and it moves under recompilation because what lands after the call changes. For the Gscale call, use the cleaning scale8_video(): the DIRTY variant only pays off when calls are batched, and this is a lone call whose cleanup would have to be duplicated onto three exit paths. For val == 0, add the cleanup_R1() that branch was missing. Verified: `bash test --cpp --clean` 359/359, and `bash compile uno` (AVR, where this matters) builds clean at 4.92 KB flash. Refs #668, #943 Co-Authored-By: Claude <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesHSV rainbow AVR cleanup
Estimated code review effort: 2 (Simple) | ~10 minutes ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Refs #668, #943.
Background
#668 is the long-running "unexpected red pixel" in
fill_rainbow()on AVR: one pixel mid-ramp comes out with a wrong channel, the affected pixel tracks the hue, and unrelated edits to the sketch make it appear and disappear. @kriegsman called it early — "this looks like a compiler-level issue" — and @ddesborough traced it to thescale8_LEAVING_R1_DIRTYinline asm, proposing three possible fixes, the third being to stop the compiler rearranging those calls.#943 shipped that third fix, and the barriers are still in the source:
This PR fixes a second, independent half of the same hazard that #943 didn't cover.
The bug
scale8*_LEAVING_R1_DIRTYdeliberately skips restoring AVR's__zero_reg__so several calls can share onecleanup_R1(). Its own docs (scale8_avr.h:74,:133) say:hsv2rgb_rainbow()has three paths that return without doing so:if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale)sat == 255skips the saturation block andval == 255skips the value blocksat == 0branch only assignsr = g = b = 255val = scale8_video_LEAVING_R1_DIRTY(val, val)val == 0branch assigns zeros; only theelsebatches into acleanup_R1()Returning with
r1non-zero violates the AVR ABI — the compiler treatsr1as a hard zero and emits code assuming it. Whatever executes next can compute garbage.That explains every reported symptom, including the confusing ones:
-O1made it go away (@ddesborough) anddelay()vsmillis()changed it (@OneEyeRick) — both change surrounding codegen, not the conversion math.This is also exactly what @kriegsman predicted: "if we're having this issue here, we're probably having it other places too but we just haven't isolated those bugs yet."
The fix
For the
Gscalecall — use the cleaningscale8_video(). The_LEAVING_R1_DIRTYvariant only pays off when calls are batched before one cleanup; this is a lone call, and covering it otherwise would mean duplicatingcleanup_R1()onto three separate exit paths.For
val == 0— add the missingcleanup_R1(). Here the batching is real (theelsebranch chains three more scales), so the branch cleans up itself rather than giving up the optimization.Verification
bash test --cpp --clean— 359/359 on a full clean rebuildbash compile uno— AVR, where this actually matters: builds clean, 4.92 KB flash / 538 B RAMHost tests can't catch this class of bug (
cleanup_R1()is a no-op off AVR), which is why the AVR build is the meaningful check.Credit
@Tim-AQ for the original report and the key observation that the glitch moves with hue; @kriegsman for the delta tables that showed it wasn't a math error; @ddesborough for the root-cause analysis and the three candidate fixes; @reyemxela and @OneEyeRick for independent reproductions that narrowed the trigger conditions.
🤖 Generated with Claude Code
Summary by CodeRabbit