Skip to content

Perf: precompute dispatch desc and use flat byte offset register encoding#326

Open
zhusy54 wants to merge 2 commits intohw-native-sys:mainfrom
zhusy54:dispatch-payload
Open

Perf: precompute dispatch desc and use flat byte offset register encoding#326
zhusy54 wants to merge 2 commits intohw-native-sys:mainfrom
zhusy54:dispatch-payload

Conversation

@zhusy54
Copy link
Contributor

@zhusy54 zhusy54 commented Mar 18, 2026

Summary

  • Precompute dispatch descriptors at Orchestrator submit time, eliminating per-dispatch address computation in the Scheduler hot path
  • Replace structured (slot_in_ring, ring_id, slot_idx) register encoding with a flat byte offset scheme, removing independent bit-width constraints on ring depth and task window size
  • Add overflow safety check with emergency_shutdown on sentinel collision

Key Changes

  • pto2_dispatch_payload.h: New PTO2DispatchDesc struct with per-slot function_bin_addrs[] + unified args[]; flat byte offset encoding constants (PTO2_REG_OFFSET_SHIFT/MASK/ALIGN_SHIFT); simplified PTO2DispatchInitInfo from {dispatch_bases[4], payload_stride} to {dispatch_base}
  • aicore_executor.cpp: Simplified init to cache single dispatch_base; decode uses offset_field + desc_byte_offset instead of slot_in_ring * payload_stride
  • aicpu_executor.cpp: dispatch_subtask_to_core returns bool (false = fatal overflow); new dispatch_base_ and sm_header_ members; overflow triggers PTO2_ERROR_ENCODING_OVERFLOWemergency_shutdown → clean exit
  • pto_runtime2_types.h: Add PTO2_ERROR_ENCODING_OVERFLOW error code (101); update PTO2TaskPayload docstring
  • pto_orchestrator.cpp/h: Build PTO2DispatchDesc at submit time with per-slot function addresses and unified args layout
  • pto_shared_memory.h, runtime.h, build_config.py: Supporting changes for new dispatch descriptor and profiling infrastructure

Register Encoding

DATA_MAIN_BASE 32-bit:
  [30]    toggle bit (alternates per core)
  [29:2]  offset_field = (desc_byte_offset >> 3) + 1  (28 bits)
  [1:0]   slot_idx (0=AIC, 1=AIV0, 2=AIV1)

Max encodable byte offset ≈ 2GB (vs previous scheme limited by independent bit widths)

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the task dispatch mechanism to improve performance and robustness. By precomputing dispatch descriptors and adopting a more flexible flat byte offset register encoding, the system reduces overhead in the scheduler's critical path and removes previous limitations on task scaling. Additionally, it introduces a crucial safety check to prevent encoding overflows, enhancing system stability.

Highlights

  • Performance Optimization: Dispatch descriptors are now precomputed by the Orchestrator at submit time, eliminating per-dispatch address computation in the Scheduler's hot path.
  • Register Encoding Refactor: The structured register encoding (slot_in_ring, ring_id, slot_idx) has been replaced with a flat byte offset scheme, removing independent bit-width constraints on ring depth and task window size. This new scheme allows for a maximum encodable byte offset of approximately 2GB.
  • Overflow Safety Check: An overflow safety check has been added to detect sentinel collisions in the new register encoding, triggering an emergency_shutdown for graceful error handling.
  • New Dispatch Descriptor Structure: A new PTO2DispatchDesc struct has been introduced, featuring per-slot function_bin_addrs[] and a unified args[] array. PTO2DispatchInitInfo has been simplified to hold only dispatch_base.
  • AICore Executor Updates: The AICore executor now caches a single dispatch_base and decodes dispatch information using offset_field and desc_byte_offset instead of slot_in_ring * payload_stride.
  • AICPU Executor Enhancements: The dispatch_subtask_to_core function now returns a boolean to indicate fatal overflow, and new dispatch_base_ and sm_header_ members have been added to AicpuExecutor.
  • Error Code Addition: A new error code, PTO2_ERROR_ENCODING_OVERFLOW, has been added to pto_runtime2_types.h to specifically handle register encoding overflow scenarios.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant performance optimization by pre-computing dispatch descriptors and adopting a more flexible flat byte offset register encoding. The changes are extensive, touching the core dispatch logic in the orchestrator, scheduler, and AICore. The addition of an overflow safety check is a great improvement for robustness. I've found a critical bug related to potential integer truncation in the new offset calculation logic, which could lead to incorrect task dispatch. I've provided comments with suggestions to fix this issue in both places it occurs.

… encoding

Replace structured (slot_in_ring, ring_id, slot_idx) register encoding with
a flat byte offset scheme. The new layout uses 28 bits for offset_field,
enabling ~2GB of addressable dispatch descriptors and eliminating independent
bit-width constraints on ring depth and task window size.

Add overflow safety check in dispatch_subtask_to_core that triggers
emergency_shutdown with PTO2_ERROR_ENCODING_OVERFLOW on sentinel collision.
Perform the overflow safety check on the full-width uint64_t value
before narrowing to uint32_t, preventing silent truncation from
bypassing the sentinel-collision guard.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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.

1 participant