Skip to content

feat(codegen): Add PTOAS-aware type annotations and update PTO naming#267

Merged
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
zhangqi-chen:ptoas
Feb 26, 2026
Merged

feat(codegen): Add PTOAS-aware type annotations and update PTO naming#267
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
zhangqi-chen:ptoas

Conversation

@zhangqi-chen
Copy link
Contributor

Emit accurate tile_buf/tensor_view type strings derived from TileType metadata instead of hardcoded defaults. Rename pto.subview to pto.partition_view and update memory space names (ub->vec, ddr->gm, l1->mat, etc.) to match current PTO-ISA specification. Add ins/outs type annotation support to operator codegen callbacks.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @zhangqi-chen, 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 enhances the PTO Codegen by introducing robust type awareness and aligning its output with the latest PTO-ISA specification. The changes ensure that generated MLIR code includes precise type annotations for tensor views and tile buffers, improving the correctness and readability of the intermediate representation. By updating naming conventions and dynamically deriving type information, the codegen becomes more accurate and maintainable, facilitating better integration with the PTO backend.

Highlights

  • PTOAS-aware Type Annotations: Implemented support for emitting accurate type annotations in ins and outs clauses for PTO-ISA MLIR operations, deriving tile_buf and tensor_view types directly from TileType metadata. This enhances type safety and clarity in the generated MLIR.
  • PTO Naming Updates: Renamed pto.subview to pto.partition_view to align with the current PTO-ISA specification. Additionally, memory space names (e.g., ub to vec, ddr to gm, l1 to mat) were updated to match the specification.
  • Improved Type Derivation for Allocations: The pto.alloc_tile operations now derive dtype and dimensions from the associated TileType metadata, moving away from hardcoded defaults and providing more precise type information.
Changelog
  • docs/dev/codegen/00-pto_codegen.md
    • Updated documentation to reflect the renaming of pto.subview to pto.partition_view.
    • Added new points highlighting PTOAS type annotations and derivation of tile_buf/tensor_view types from TileType metadata.
    • Revised memory space mapping table to use new PTO address space names (e.g., ub to vec, ddr to gm).
    • Updated examples of generated MLIR to show !pto.tensor_view<?x?xf32>, !pto.partition_tensor_view, and !pto.tile_buf<loc=vec, ...>.
  • include/pypto/codegen/pto/pto_codegen.h
    • Included <memory> header for std::shared_ptr.
    • Updated class documentation to reflect pto.partition_view generation.
    • Added new public helper methods: GetTensorViewTypeString, GetTileBufTypeString, GetExprTypeAnnotation, and GetCurrentResultTileBufTypeString for type string generation.
    • Introduced memref_to_tile_type_ map to store TileType associated with MemRef pointers.
    • Added current_result_tile_type_ member to track the TileType of the current assignment result.
  • src/backend/910B_PTO/backend_910b_pto_ops.cpp
    • Modified GenerateInsOutsClause to dynamically add type annotations for both ins and outs operands.
    • Updated MakeFullCodegenPTO to include type annotations for scalar and destination operands.
    • Refactored MakeBlockLoadCodegenPTO to use pto.partition_view instead of pto.subview and to derive types dynamically.
    • Refactored MakeBlockStoreCodegenPTO to use pto.partition_view and to derive tile_buf type from the tile variable's TileType.
  • src/codegen/pto/pto_codegen.cpp
    • Included <memory> header.
    • Updated MemorySpaceToMLIR helper function to map PyPTO MemorySpace to new PTO address space names (e.g., ub to vec).
    • Modified MemRefCollectorVisitor to collect and store the TileType associated with each unique MemRef.
    • Cleared memref_to_tile_type_ map at the start of GenerateFunction.
    • Populated memref_to_tile_type_ map after collecting MemRef objects.
    • Updated EmitMakeTensorViews to generate !pto.tensor_view<?x?xf32> instead of fixed dimension types.
    • Modified EmitAllocTiles to use GetTileBufTypeString for generating pto.alloc_tile types, deriving attributes from TileType.
    • Updated VisitStmt_ to store the TileType of the assignment result in current_result_tile_type_.
    • Implemented GetTensorViewTypeString to create type strings like !pto.tensor_view<?x?xf32>.
    • Implemented GetTileBufTypeString to construct !pto.tile_buf type strings with loc, dtype, rows, and cols derived from TileType.
    • Implemented GetExprTypeAnnotation to provide type strings for various expressions (variables, constants).
    • Implemented GetCurrentResultTileBufTypeString to retrieve the type string for the current assignment target.
  • tests/ut/codegen/test_pto_codegen.py
    • Updated assertion for tensor_view type to expect !pto.tensor_view<?x?xf32>.
    • Updated assertion for alloc_tile location to expect loc=vec.
    • Updated test description and assertions for block.load to verify pto.partition_view instead of pto.subview and !pto.partition_tensor_view.
  • tests/ut/codegen/test_pto_codegen_ops.py
    • Updated assertion in validate_kernel_codegen to check for pto.partition_view instead of pto.subview.
Activity
  • No specific activity (comments, reviews, progress updates) was provided in the context for this pull request.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@coderabbitai
Copy link

coderabbitai bot commented Feb 26, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

PTOCodegen class refactored to inherit from CodegenBase with relocated header file and expanded public API. Introduces type-annotated code generation with partition_view semantics for load/store operations. Helper methods added for type annotation and tile buffer type generation. MemRef-to-TileType mapping supports richer type information.

Changes

Cohort / File(s) Summary
Codegen Header Reorganization
docs/dev/codegen/00-pto_codegen.md, include/pypto/codegen/pto/pto_codegen.h
PTOCodegen moved to inherit from CodegenBase; header relocated from include/pypto/codegen/pto_codegen.h to include/pypto/codegen/pto/pto_codegen.h. Public interface expanded with CodegenBase overrides and new type annotation helpers (GetTensorViewTypeString, GetTileBufTypeString, GetExprTypeAnnotation, GetCurrentResultTileBufTypeString).
Backend Operation Type Annotations
src/backend/910B_PTO/backend_910b_pto_ops.cpp
Refactored ins/outs clause generation to include type annotations. Replaced tensor_view/tile_view paths with partition_view/partition_tensor_view model in block.load and block.store codegen. Now emits type-aware operands with tile buffer type information.
Core Codegen Type System
src/codegen/pto/pto_codegen.cpp
Introduced MemRef-to-TileType mapping collection via extended MemRefCollectorVisitor. Implemented new public helper methods for generating type-annotated strings. Updated code generation flow to populate and propagate tile type information during function generation and statement visits.
Test Expectations Update
tests/ut/codegen/test_pto_codegen.py, tests/ut/codegen/test_pto_codegen_ops.py
Updated MLIR expectations to reflect partition_view semantics (replacing subview). Changed tensor view type expectations from fixed dimensions to generic placeholders. Updated buffer location specifier from "loc=ub" to "loc=vec".

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • Hzfengsy

Poem

🐰 A codegen class takes flight,
From tensor views to partitions bright,
With types annotated, types aligned,
The BaseClass heritage enshrined,
Now tiles and buffers understand their place,
In PTO's newly ordered space!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 44.83% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main changes: adding PTOAS-aware type annotations and updating PTO naming conventions. It directly reflects the primary objectives of the changeset.
Description check ✅ Passed The description is well-related to the changeset, covering key aspects: type annotation support, renaming operations, and memory space updates. It provides meaningful context about the changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/codegen/pto/pto_codegen.cpp (1)

500-521: Consider handling 0D and 3+D tile shapes.

The implementation handles 1D and 2D shapes but falls back to defaults (32x32) for 0D or 3+D shapes. If 3D or higher-dimensional tiles are valid in this context, consider adding explicit handling or a log warning.

💡 Suggested enhancement
     if (tile_type->shape_.size() >= 2) {
       if (auto c0 = As<ir::ConstInt>(tile_type->shape_[0])) rows = c0->value_;
       if (auto c1 = As<ir::ConstInt>(tile_type->shape_[1])) cols = c1->value_;
     } else if (tile_type->shape_.size() == 1) {
       if (auto c0 = As<ir::ConstInt>(tile_type->shape_[0])) {
         rows = 1;
         cols = c0->value_;
       }
+    } else if (tile_type->shape_.empty()) {
+      // Scalar tile - use 1x1
+      rows = 1;
+      cols = 1;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/codegen/pto/pto_codegen.cpp` around lines 500 - 521,
PTOCodegen::GetTileBufTypeString currently only handles 1D and 2D
tile_type->shape_ and leaves rows/cols as default 32 for 0D or >=3D shapes;
update the logic in GetTileBufTypeString (and use
memref_to_tile_type_/tile_type->shape_) to explicitly handle 0D by setting
rows=1, cols=1, and handle 3D+ by either flattening higher dims (e.g., set rows
= product(shape[0..n-2]) and cols = shape[n-1]) or emit a diagnostic/log warning
and choose a deterministic mapping, and ensure dtype_str is still set via
GetTypeString(tile_type->dtype_); keep behavior consistent and add a clear
comment explaining the chosen mapping.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/codegen/pto/pto_codegen.cpp`:
- Around line 500-521: PTOCodegen::GetTileBufTypeString currently only handles
1D and 2D tile_type->shape_ and leaves rows/cols as default 32 for 0D or >=3D
shapes; update the logic in GetTileBufTypeString (and use
memref_to_tile_type_/tile_type->shape_) to explicitly handle 0D by setting
rows=1, cols=1, and handle 3D+ by either flattening higher dims (e.g., set rows
= product(shape[0..n-2]) and cols = shape[n-1]) or emit a diagnostic/log warning
and choose a deterministic mapping, and ensure dtype_str is still set via
GetTypeString(tile_type->dtype_); keep behavior consistent and add a clear
comment explaining the chosen mapping.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c4ef34b and 0b21736.

📒 Files selected for processing (6)
  • docs/dev/codegen/00-pto_codegen.md
  • include/pypto/codegen/pto/pto_codegen.h
  • src/backend/910B_PTO/backend_910b_pto_ops.cpp
  • src/codegen/pto/pto_codegen.cpp
  • tests/ut/codegen/test_pto_codegen.py
  • tests/ut/codegen/test_pto_codegen_ops.py

Copy link
Contributor

@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 significantly enhances the PTO codegen by making it more aware of TileType metadata, leading to more accurate MLIR type generation. The renaming of pto.subview to pto.partition_view and the update of memory space names align the codegen with the latest PTO-ISA specification. The addition of type annotations to ins/outs clauses is also a valuable improvement for MLIR readability and correctness. The changes are well-implemented across the documentation, C++ source, and tests. I've included a couple of suggestions to further improve documentation accuracy and make the tile_buf type generation even more metadata-driven.

Emit accurate tile_buf/tensor_view type strings derived from TileType
metadata instead of hardcoded defaults. Extract blayout, slayout,
fractal, and pad from TileView when available, falling back to sensible
defaults. Rename pto.subview to pto.partition_view and update memory
space names (ub->vec, ddr->gm, l1->mat, etc.) to match current PTO-ISA
specification. Add ins/outs type annotation support to operator codegen
callbacks. Update codegen documentation accordingly.
@zhangqi-chen
Copy link
Contributor Author

@Hzfengsy

@Hzfengsy Hzfengsy merged commit 1d89474 into hw-native-sys:main Feb 26, 2026
6 checks passed
@zhangqi-chen zhangqi-chen deleted the ptoas branch February 26, 2026 03:16
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