Skip to content

Fix: 4 deferred L2 swimlane correctness bugs in a2a3 from #916 review#936

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:pr-fix-l2-swimlane-deferred-bugs
May 31, 2026
Merged

Fix: 4 deferred L2 swimlane correctness bugs in a2a3 from #916 review#936
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
hw-native-sys-bot:pr-fix-l2-swimlane-deferred-bugs

Conversation

@hw-native-sys-bot
Copy link
Copy Markdown
Collaborator

@hw-native-sys-bot hw-native-sys-bot commented May 31, 2026

Summary

Bundled fixes for four issues CodeRabbit flagged during #916 review and agreed to defer. Scope: a2a3 only. Bugs 1 and 2 also exist verbatim in src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp (a5 has the same code shape); a follow-up will bulk-port the same edits across all a5 sites — keeping a5 out of this PR avoids interleaving an a2a3 bugfix with a cross-arch sweep.

# File Bug Trigger
1 src/a2a3/.../scheduler_cold_path.cpp l2_swimlane_aicpu_init_phase uses sched_thread_num_ instead of orch_to_sched_ ? aicpu_thread_num_ : sched_thread_num_ PTO2_ORCH_TO_SCHED=1 + SCHED_PHASES level + aicpu_thread_num_ > sched_thread_num_; under-allocates phase pools and silently drops the tail records
2 src/a2a3/.../scheduler_cold_path.cpp l2_swimlane_level_ not reset when is_l2_swimlane_enabled() == false (no else branch); sched_ctx_.deinit() doesn't clear it either Prior enabled launch's cached level leaks into the >= SCHED_PHASES gates in scheduler_dispatch.cpp:516/627/710/777/804/827, writing into a non-initialized phase pool on the disabled-this-launch run
3 src/a2a3/platform/onboard/aicore/kernel.cpp Rotation slot only set when swimlane enabled; [[block_local]] storage persists across launches Sequence like enabled(valid table) → enabled(NULL table) or enabled → disabled leaves get_l2_swimlane_aicore_rotation() returning the prior launch's freed pointer. Current AICore call sites are themselves flag-gated on this-launch l2_swimlane_enabled, so the disabled-launch path is unreachable through the executors; the unconditional reset is defensive against future call sites without that gate.
4 src/a2a3/.../l2_swimlane_collector.h resolve_entry falls into the AicoreTask branch for any out-of-range entry.kind Defensive — corrupt or future-kind entries silently read a wrong-typed pool

Bug 1's fix mirrors the existing branch in scheduler_dispatch.cpp:489 (dump_tensor_init). Bug 3 mirrors the publish-nullptr branch in sim/aicore/kernel.cpp:122-130 (sim keys only on table pointer; onboard additionally AND-gates on PROFILING_FLAG_L2_SWIMLANE).

Architecture scope

  • a2a3: fixed in this PR
  • a5: not fixed here. Same Bugs 1 and 2 live in src/a5/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp:854-860; will land via a follow-up bulk port that also handles other a5/a2a3 swimlane divergence. Bug 3 doesn't apply to a5 (src/a5/platform/onboard/aicore/kernel.cpp:113-118 already publishes set_aicore_l2_swimlane_ring(nullptr) in the disabled branch). Bug 4 is milder on a5 (binary is_phase check, no AicoreTask branch to fall into).

Testing

  • Existing test_l2_swimlane and test_l2_swimlane_mixed sim tests pass unchanged.
  • All four bugs need cross-launch state or env-gated runtime configs that current swimlane ST infrastructure doesn't model:
    • Bug 1: PTO2_ORCH_TO_SCHED=1 + SCHED_PHASES + a phase-pool-count assertion.
    • Bugs 2/3: enabled→disabled (or enabled-with-valid→enabled-with-null) launch cycling within one process.
    • Bug 4: ready-queue entry corruption (impossible without modifying device code).
  • Test infra additions to model these triggers are deferred to a separate PR rather than expanded inline.

Test plan

  • Build all four variants (a2a3 + a5, onboard + sim)
  • pytest test_l2_swimlane.py --platform a2a3sim --enable-l2-swimlane
  • pytest test_l2_swimlane_mixed.py --platform a2a3sim --enable-l2-swimlane
  • pre-commit run clean on all touched files
  • CI green (onboard + sim, a2a3 + a5)

@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 31, 2026

Warning

Review limit reached

@ChaoWao, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 44 minutes and 31 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 440477de-3696-4905-a0ce-1c725d96191c

📥 Commits

Reviewing files that changed from the base of the PR and between cd60051 and 651d0f4.

📒 Files selected for processing (3)
  • src/a2a3/platform/include/host/l2_swimlane_collector.h
  • src/a2a3/platform/onboard/aicore/kernel.cpp
  • src/a2a3/runtime/tensormap_and_ringbuffer/runtime/scheduler/scheduler_cold_path.cpp

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 and usage tips.

Bundled fixes for issues CodeRabbit flagged during hw-native-sys#916 review and
agreed to defer to a follow-up. All four are tight per-site fixes
without behavior change beyond the affected corner case.

1. scheduler_cold_path::init — phase pool count: pass
   `aicpu_thread_num_` (not `sched_thread_num_`) when
   `orch_to_sched_=true`. When orch merges into sched, phase records
   flow through aicpu-thread-indexed pools; the old code under-allocated
   when `aicpu_thread_num_ > sched_thread_num_`, silently dropping the
   tail. Matches the existing branch in
   `scheduler_dispatch.cpp:489` (dump_tensor_init).

2. scheduler_cold_path::init — reset cached level on disabled runs:
   if `is_l2_swimlane_enabled()` is false this launch, also clear
   `l2_swimlane_level_` to DISABLED. Without this, a stale value from a
   prior enabled launch on the same scheduler instance leaks into the
   `>= SCHED_PHASES` gates in scheduler_dispatch, which then writes
   into a non-initialized phase pool.

3. onboard/aicore/kernel.cpp::KERNEL_ENTRY — always publish the
   rotation slot (nullptr when disabled this launch). `[[block_local]]`
   storage persists across launches on the same loaded binary, so
   without an explicit nullptr publication the stale slot from a prior
   enabled launch survives, and `get_l2_swimlane_aicore_rotation()`
   returns a freed device pointer. Mirrors the sim wrapper's
   always-publish pattern.

4. l2_swimlane_collector.h::resolve_entry — validate `entry.kind`
   before branching. Previously any out-of-range value silently fell
   into the AicoreTask branch and read a wrong-typed pool. The new
   guard logs and returns nullopt for unknown kinds.

Test coverage: existing `test_l2_swimlane` / `test_l2_swimlane_mixed`
sim tests pass unchanged. Bug 1 trigger requires `PTO2_ORCH_TO_SCHED=1`
+ `SCHED_PHASES` level + phase-pool-count assertion, which the current
swimlane test infra doesn't model; bugs 2/3 trigger requires
enabled→disabled launch cycling within one process. These test gaps
are deferred to a separate test-infra PR rather than expanded inline
to keep this fix focused.
@ChaoWao ChaoWao force-pushed the pr-fix-l2-swimlane-deferred-bugs branch from 43586e8 to 651d0f4 Compare May 31, 2026 06:46
@hw-native-sys-bot hw-native-sys-bot changed the title Fix: 4 deferred L2 swimlane correctness bugs from #916 review Fix: 4 deferred L2 swimlane correctness bugs in a2a3 from #916 review May 31, 2026
@ChaoWao ChaoWao merged commit e177d2c into hw-native-sys:main May 31, 2026
15 checks passed
@ChaoWao ChaoWao deleted the pr-fix-l2-swimlane-deferred-bugs branch May 31, 2026 07:16
ChaoWao added a commit that referenced this pull request May 31, 2026
Mechanical backport of two of the four bugs landed for a2a3 in #936.
The a5 file has the identical code shape at the same call site
(scheduler_cold_path.cpp:854-860), so the patch is the same shape:

  1. l2_swimlane_aicpu_init_phase phase pool count: pass
     `aicpu_thread_num_` (not `sched_thread_num_`) when
     `orch_to_sched_=true`. Mirrors the existing precedent in
     `src/a5/.../scheduler_dispatch.cpp:494` (`dump_tensor_init`).
     Without this, `PTO2_ORCH_TO_SCHED=1` + `SCHED_PHASES` level +
     `aicpu_thread_num_ > sched_thread_num_` under-allocates phase
     pools and silently drops the tail records.

  2. Reset `l2_swimlane_level_` to DISABLED when
     `is_l2_swimlane_enabled() == false` this launch. Without this,
     a prior enabled launch's cached level leaks into the
     `>= SCHED_PHASES` gates in scheduler_dispatch (a5
     scheduler_dispatch.cpp:517 reads the same field), writing into
     an uninitialized phase pool on the disabled-this-launch run.

a2a3 Bugs 3 and 4 do NOT apply to a5:
  - Bug 3 (AICore rotation slot stale leak): a5 uses a fixed per-core
    ring (`L2SwimlaneAicoreRing` with dual_issue_slots), not a
    rotated buffer pool, and `src/a5/platform/onboard/aicore/kernel.cpp:113-118`
    already publishes `set_aicore_l2_swimlane_ring(nullptr)` in the
    disabled branch.
  - Bug 4 (resolve_entry invalid kind): a5 `L2SwimlaneBufferKind` is
    a binary `is_phase` flag (only AicpuTask/AicpuPhase, no
    AicoreTask), so the wrong-typed-pool fall-through doesn't exist
    in the same form.

Test plan:
  - Build all four variants (a5 + a2a3, onboard + sim) — clean
  - pytest tests/st/a5/.../dummy_task --platform a5sim — passes
  - pre-commit clean

Co-authored-by: Chao Wang <26245345+ChaoWao@users.noreply.github.com>
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