Skip to content

fix(hsv): stop hsv2rgb_rainbow leaving AVR's R1 dirty on three paths (#668) - #3808

Merged
zackees merged 1 commit into
masterfrom
fix/issue-668-r1-dirty-leak
Aug 1, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/issue-668-r1-dirty-leak

Conversation

@zackees

@zackees zackees commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

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 the scale8_LEAVING_R1_DIRTY inline 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:

r = scale8_LEAVING_R1_DIRTY( r, satscale);
asm volatile("");  // Fixes jumping red pixel: https://github.com/FastLED/FastLED/pull/943
g = scale8_LEAVING_R1_DIRTY( g, satscale);

This PR fixes a second, independent half of the same hazard that #943 didn't cover.

The bug

scale8*_LEAVING_R1_DIRTY deliberately skips restoring AVR's __zero_reg__ so several calls can share one cleanup_R1(). Its own docs (scale8_avr.h:74, :133) say:

@warning You MUST call cleanup_R1() after using this function!

hsv2rgb_rainbow() has three paths that return without doing so:

# dirtied at why nothing cleans up
1 if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale) sat == 255 skips the saturation block and val == 255 skips the value block
2 same call the sat == 0 branch only assigns r = g = b = 255
3 val = scale8_video_LEAVING_R1_DIRTY(val, val) the val == 0 branch assigns zeros; only the else batches into a cleanup_R1()

Returning with r1 non-zero violates the AVR ABI — the compiler treats r1 as a hard zero and emits code assuming it. Whatever executes next can compute garbage.

That explains every reported symptom, including the confusing ones:

  • Which pixel is wrong depends on hue — hue selects which branch runs, and only some branches leak.
  • It moves under recompilation — the damage depends on what the compiler happens to schedule after the call, which unrelated edits change.
  • -O1 made it go away (@ddesborough) and delay() vs millis() 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 Gscale call — use the cleaning scale8_video(). The _LEAVING_R1_DIRTY variant only pays off when calls are batched before one cleanup; this is a lone call, and covering it otherwise would mean duplicating cleanup_R1() onto three separate exit paths.

For val == 0 — add the missing cleanup_R1(). Here the batching is real (the else branch 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 rebuild
  • bash compile uno — AVR, where this actually matters: builds clean, 4.92 KB flash / 538 B RAM

Host 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

  • Bug Fixes
    • Improved color conversion reliability on AVR-based devices.
    • Ensured internal processor state is properly restored after zero-value color scaling.

…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]>
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: fd78c0b1-651f-474e-9d33-50cc3b5f5771

📥 Commits

Reviewing files that changed from the base of the PR and between 8e58be5 and c93db4e.

📒 Files selected for processing (1)
  • src/hsv2rgb.cpp.hpp

📝 Walkthrough

Walkthrough

hsv2rgb_rainbow now preserves AVR register R1 cleanup when it scales green values or returns after zero-value scaling.

Changes

HSV rainbow AVR cleanup

Layer / File(s) Summary
Clean AVR R1 across scaling paths
src/hsv2rgb.cpp.hpp
Optional green scaling uses scale8_video. The zero-value branch calls cleanup_R1() before returning.

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-668-r1-dirty-leak

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 26df858 into master Aug 1, 2026
10 of 12 checks passed
@zackees
zackees deleted the fix/issue-668-r1-dirty-leak branch August 1, 2026 17:04
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant