Skip to content

Comments

refactor(codegen): Consolidate PTO codegen helpers by arity#253

Merged
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
lwDavid:refactor
Feb 24, 2026
Merged

refactor(codegen): Consolidate PTO codegen helpers by arity#253
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
lwDavid:refactor

Conversation

@lwDavid
Copy link
Contributor

@lwDavid lwDavid commented Feb 24, 2026

refactor(codegen): consolidate 910B PTO codegen helpers by arity

Replace 13 operation-specific helper functions (MakeBinaryTileTile,
MakeUnaryTile, MakeBinaryTileScalar, MakeBinaryMatmul, etc.) with 5
generic arity-based helpers (MakeUnary/Binary/Ternary/Quaternary/
QuinaryCodegenPTO), eliminating duplicated logic that differed only
in argument count checks.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lwDavid, 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 refactors the 910B PTO codegen helpers by consolidating numerous specialized functions into a smaller set of generic, arity-based helpers. This change aims to eliminate redundant code and simplify the overall code generation process, making it more modular and easier to maintain. The core idea is to abstract away the common patterns of operations based on the number of arguments they take, rather than their specific functionality.

Highlights

  • Code Consolidation: Thirteen operation-specific PTO codegen helper functions have been replaced by five generic arity-based helpers, significantly reducing code duplication.
  • Improved Maintainability: The refactoring streamlines the codegen logic by centralizing argument count checks and emission patterns into fewer, more general functions.
  • Arity-Based Helpers: New helper functions like MakeUnaryCodegenPTO, MakeBinaryCodegenPTO, MakeTernaryCodegenPTO, MakeQuaternaryCodegenPTO, and MakeQuinaryCodegenPTO now handle operations based on their argument count.
Changelog
  • src/backend/910B_PTO/backend_910b_pto_ops.cpp
    • Replaced 13 specific codegen helper functions (e.g., MakeBinaryTileTileCodegenPTO, MakeUnaryTileCodegenPTO) with 5 generic arity-based functions (MakeUnaryCodegenPTO, MakeBinaryCodegenPTO, MakeTernaryCodegenPTO, MakeQuaternaryCodegenPTO, MakeQuinaryCodegenPTO).
    • Updated all call sites for the removed helper functions to use the new generic arity-based helpers.
    • Modified argument count checks within the new generic helper functions to include the operation name for clearer error messages.
Activity
  • No human activity has been recorded on this pull request yet.
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 24, 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

Refactored PTO code generation in 910B backend by consolidating specialized operation handlers (unary, binary, ternary, etc.) into generalized MakeUnaryCodegenPTO, MakeBinaryCodegenPTO, MakeTernaryCodegenPTO, and related functions, with updated endpoint registrations and improved error messaging.

Changes

Cohort / File(s) Summary
PTO Codegen API Refactoring
src/backend/910B_PTO/backend_910b_pto_ops.cpp
Consolidated specialized codegen functions into generalized variants (Unary, Binary, Ternary, Quaternary, Quinary, TileCmp, TileCvt). Updated all endpoint registrations to use new generalized API. Enhanced error messages with operation names and adjusted argument validation checks to reflect new function signatures.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • Hzfengsy

Poem

🐰 The tiles once danced in specialized ways,
Unary, binary, through countless days,
But now they gather in generalized grace,
One function to rule each operation's case! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'refactor(codegen): Consolidate PTO codegen helpers by arity' clearly and concisely summarizes the main change: consolidating multiple operation-specific helper functions into fewer generic arity-based helpers.
Description check ✅ Passed The description is directly related to the changeset, detailing the replacement of 13 operation-specific helper functions with 5 generic arity-based helpers to eliminate duplicated logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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
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 is a great refactoring that consolidates 13 operation-specific codegen helpers into 5 generic arity-based helpers. This significantly reduces code duplication and improves maintainability. The new generic helpers also provide more informative error messages by including the operation name. The changes are clean and correctly applied across all operator registrations. I have one minor suggestion to improve a confusing error message in the MakeFullCodegenPTO function.

Comment on lines +137 to +138
CHECK(op->args_.size() == 2) << "full op requires 3 arguments."
<< op->args_.size(); // Actually 2 args, two of them are conbined!
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The CHECK message here is incorrect and confusing. It states that 3 arguments are required, but the check is for 2. The appended argument size and the comment add to the confusion. It would be clearer to correct the message and remove the redundant parts.

  CHECK(op->args_.size() == 2) << "full op requires 2 arguments.";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This problem fundamentally stems from the mismatch between the number of parameters accepted by the pl-side operator and the number of parameters actually received by the PTO backend.

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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/backend/910B_PTO/backend_910b_pto_ops.cpp`:
- Around line 133-142: The CHECK in MakeFullCodegenPTO currently asserts
op->args_.size() == 2 but logs "full op requires 3 arguments."; update the
message to reflect the actual expectation (2 arguments) and clarify the
signature (e.g., "full op requires 2 arguments (shape, value), got: ") so the
CHECK for MakeFullCodegenPTO / function MakeFullCodegenPTO and its dynamic_cast
to codegen::PTOCodegen produce an accurate error string when op->args_.size() is
not 2.
- Around line 111-129: Both MakeTileCmpCodegenPTO and MakeTileCvtCodegenPTO use
mode to index cmp_modes.at(mode) and round_modes.at(mode) without validating
mode; add an explicit bounds check before calling .at() (e.g., ensure 0 <= mode
&& mode < cmp_modes.size()/round_modes.size()) and produce a clear CHECK/throw
message identifying the invalid mode and function name, then proceed to build
config_attr and call codegen.Emit with GenerateInsOutsClause as before (also
confirm the attribute concatenation produced by GenerateInsOutsClause matches
expected MLIR syntax).

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81383e1 and 5bf4a2b.

📒 Files selected for processing (1)
  • src/backend/910B_PTO/backend_910b_pto_ops.cpp

Comment on lines +133 to 142
// Helper function for full op
static std::string MakeFullCodegenPTO(const std::string& pto_op_name, const CallPtr& op,
codegen::CodegenBase& codegen_base) {
auto& codegen = dynamic_cast<codegen::PTOCodegen&>(codegen_base);
CHECK(op->args_.size() == 2) << "Binary Axis op requires 2 arguments.";
codegen.Emit(pto_op_name + " " + GenerateInsOutsClause(op, codegen));
CHECK(op->args_.size() == 2) << "full op requires 3 arguments."
<< op->args_.size(); // Actually 2 args, two of them are conbined!
std::string scalar = codegen.GetExprAsCode(op->args_[1]);
std::string dst = codegen.GetCurrentResultTarget();
codegen.Emit(pto_op_name + " " + "ins(" + scalar + ") outs(" + dst + ")");
return "";
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

rg -n -C3 'block\.full' -g '!**/build/**'

Repository: hw-native-sys/pypto

Length of output: 5866


🏁 Script executed:

grep -n "args_\[0\]" src/backend/910B_PTO/backend_910b_pto_ops.cpp

Repository: hw-native-sys/pypto

Length of output: 146


🏁 Script executed:

rg -n 'MakeFullCodegenPTO' -g '!**/build/**'

Repository: hw-native-sys/pypto

Length of output: 313


Fix block.full arg-count message.

The CHECK message says "requires 3 arguments" while enforcing exactly 2. The operation signature is (shape, value), where only the value (args_[1]) is used. Update the message to match the actual requirement:

Suggested fix
-  CHECK(op->args_.size() == 2) << "full op requires 3 arguments."
-                               << op->args_.size();  // Actually 2 args, two of them are conbined!
+  CHECK(op->args_.size() == 2)
+      << "full op requires 2 arguments, got " << op->args_.size();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Helper function for full op
static std::string MakeFullCodegenPTO(const std::string& pto_op_name, const CallPtr& op,
codegen::CodegenBase& codegen_base) {
auto& codegen = dynamic_cast<codegen::PTOCodegen&>(codegen_base);
CHECK(op->args_.size() == 2) << "Binary Axis op requires 2 arguments.";
codegen.Emit(pto_op_name + " " + GenerateInsOutsClause(op, codegen));
CHECK(op->args_.size() == 2) << "full op requires 3 arguments."
<< op->args_.size(); // Actually 2 args, two of them are conbined!
std::string scalar = codegen.GetExprAsCode(op->args_[1]);
std::string dst = codegen.GetCurrentResultTarget();
codegen.Emit(pto_op_name + " " + "ins(" + scalar + ") outs(" + dst + ")");
return "";
// Helper function for full op
static std::string MakeFullCodegenPTO(const std::string& pto_op_name, const CallPtr& op,
codegen::CodegenBase& codegen_base) {
auto& codegen = dynamic_cast<codegen::PTOCodegen&>(codegen_base);
CHECK(op->args_.size() == 2)
<< "full op requires 2 arguments, got " << op->args_.size();
std::string scalar = codegen.GetExprAsCode(op->args_[1]);
std::string dst = codegen.GetCurrentResultTarget();
codegen.Emit(pto_op_name + " " + "ins(" + scalar + ") outs(" + dst + ")");
return "";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/backend/910B_PTO/backend_910b_pto_ops.cpp` around lines 133 - 142, The
CHECK in MakeFullCodegenPTO currently asserts op->args_.size() == 2 but logs
"full op requires 3 arguments."; update the message to reflect the actual
expectation (2 arguments) and clarify the signature (e.g., "full op requires 2
arguments (shape, value), got: ") so the CHECK for MakeFullCodegenPTO / function
MakeFullCodegenPTO and its dynamic_cast to codegen::PTOCodegen produce an
accurate error string when op->args_.size() is not 2.

@lwDavid
Copy link
Contributor Author

lwDavid commented Feb 24, 2026

@Hzfengsy Request review.

@Hzfengsy Hzfengsy merged commit 293990c into hw-native-sys:main Feb 24, 2026
5 checks passed
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