Skip to content

feat(ir): add CtrlFlowTransform pass to eliminate break/continue from loops#494

Open
YunjiQin wants to merge 6 commits intohw-native-sys:mainfrom
YunjiQin:issue-448-pto-break-continue
Open

feat(ir): add CtrlFlowTransform pass to eliminate break/continue from loops#494
YunjiQin wants to merge 6 commits intohw-native-sys:mainfrom
YunjiQin:issue-448-pto-break-continue

Conversation

@YunjiQin
Copy link
Contributor

Summary

  • Add CtrlFlowTransform pass that rewrites break and continue statements into flag-based control flow using iter_args, enabling SSA conversion for programs with early exits
  • Introduce StructuredCtrlFlow IRProperty and its property verifier to track and validate structured control flow invariants
  • Full cross-layer implementation: C++ pass + verifier, Python bindings, type stubs
  • Per-pass documentation (EN/ZH-CN) and renumbered all pass docs to match pipeline execution order (see pass-doc-ordering.md)

Key Changes

  • New pass: src/ir/transforms/ctrl_flow_transform_pass.cpp — transforms break/continue into boolean flags with conditional yields
  • New verifier: src/ir/verifier/verify_structured_ctrl_flow_pass.cpp — validates no break/continue remains post-transform
  • Pass pipeline: Inserted as 2nd pass (after UnrollLoops, before ConvertToSSA)
  • Docs renumbering: All pass docs shifted +1 to accommodate new pass at position 02

Testing

  • Unit tests in tests/ut/ir/transforms/test_ctrl_flow_transform.py covering break, continue, nested loops, and combined cases
  • System tests extended in tests/st/runtime/test_ctrl_flow.py
  • Pass manager ordering test updated in tests/ut/ir/transforms/test_pass_manager.py

Related Issues

Fixes #448

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a new CtrlFlowTransform pass that rewrites BreakStmt/ContinueStmt into structured control flow, plus IR property, verifier, PTO codegen expression visitors, Python bindings, docs, and unit/integration tests to exercise and validate the transformation and downstream codegen.

Changes

Cohort / File(s) Summary
Control Flow Transform Pass
src/ir/transforms/ctrl_flow_transform_pass.cpp
Implements the CtrlFlowTransform pass: two-phase elimination (continue→if/else, break→for→while with __break_N flag), applies only to InCore-type functions, preserves iter_args yields, handles nested/combined cases.
Verifier Infrastructure
src/ir/verifier/verify_structured_ctrl_flow_pass.cpp, src/ir/verifier/property_verifier_registry.cpp
Adds StructuredCtrlFlow property verifier that errors on remaining BreakStmt/ContinueStmt in InCore functions and registers it in the verifier registry.
IR Property & Pass Declarations
include/pypto/ir/transforms/ir_property.h, src/ir/transforms/ir_property.cpp, include/pypto/ir/transforms/pass_properties.h, include/pypto/ir/transforms/passes.h, include/pypto/ir/verifier/verifier.h
Adds IRProperty::StructuredCtrlFlow, string mapping, pass properties kCtrlFlowTransformProperties, Pass CtrlFlowTransform() declaration, and CreateStructuredCtrlFlowPropertyVerifier() declaration.
PTO Codegen (header & impl)
include/pypto/codegen/pto/pto_codegen.h, src/codegen/pto/pto_codegen.cpp
Adds 15 new expression VisitExpr_ overrides (logical, bitwise, floats, min/max, unary ops) and changes boolean emission for i1 to "1"/"0"; integrates visitors into PTO emission paths.
Build Files
CMakeLists.txt
Registers new sources: src/ir/transforms/ctrl_flow_transform_pass.cpp and src/ir/verifier/verify_structured_ctrl_flow_pass.cpp into PYPTO_SOURCES.
Python Bindings & Pass Manager
python/bindings/modules/passes.cpp, python/pypto/ir/pass_manager.py, python/pypto/pypto_core/passes.pyi
Exposes IRProperty.StructuredCtrlFlow and ctrl_flow_transform() binding; registers CtrlFlowTransform in default strategy (after UnrollLoops, before ConvertToSSA); updates public API exports.
Documentation (EN)
.claude/rules/pass-doc-ordering.md, docs/en/dev/passes/00-pass_manager.md, docs/en/dev/passes/01-unroll_loops.md, docs/en/dev/passes/02-ctrl_flow_transform.md, docs/en/dev/language/00-python_syntax.md
Inserts new 02-ctrl_flow_transform.md, renumbers/reorders pass docs to execution order, updates pipeline diagrams and references (SplitChunkedLoops index fix).
Documentation (ZH-CN)
docs/zh-cn/dev/passes/00-pass_manager.md, docs/zh-cn/dev/passes/01-unroll_loops.md, docs/zh-cn/dev/passes/02-ctrl_flow_transform.md, docs/zh-cn/dev/language/00-python_syntax.md
Chinese translations/parallel updates for the new pass, pipeline ordering, and examples.
Unit Tests
tests/ut/ir/transforms/test_ctrl_flow_transform.py, tests/ut/ir/transforms/test_pass_manager.py
Adds thorough unit tests exercising continue/break elimination, nested loops, while-loop cases, end-to-end SSA conversion, and updates pass-manager ordering expectations.
Integration / Runtime Tests
tests/st/runtime/test_ctrl_flow.py
Adds PTO runtime tests for for-loop break/continue/both scenarios and PTO-backed test methods.

Sequence Diagram

sequenceDiagram
    participant User
    participant PassManager
    participant UnrollLoops
    participant CtrlFlowTransform
    participant Verifier
    participant ConvertToSSA
    participant PTOCodeGen

    User->>PassManager: compile(program)
    PassManager->>UnrollLoops: apply()
    UnrollLoops-->>PassManager: IR (may contain break/continue)
    PassManager->>CtrlFlowTransform: apply()
    Note over CtrlFlowTransform: Phase 1: eliminate continue\nPhase 2: eliminate break (for→while w/ __break flag)
    CtrlFlowTransform-->>PassManager: IR (StructuredCtrlFlow)
    PassManager->>Verifier: verify(StructuredCtrlFlow)
    Verifier-->>PassManager: valid / errors
    PassManager->>ConvertToSSA: apply()
    ConvertToSSA-->>PassManager: IR (SSA)
    PassManager->>PTOCodeGen: emit(IR)
    Note over PTOCodeGen: uses new VisitExpr_ handlers and bool emission changes
    PTOCodeGen-->>User: generated artifact
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested reviewers

  • Hzfengsy
  • lyfne123

Poem

🐰 I hop through loops and tidy the trail,
Turning breaks and continues into a tale.
Flags and whiles now dance in line,
SSA smiles — the flow's refined.
Hooray — structured paths, neat and hale!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.84% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'feat(ir): add CtrlFlowTransform pass to eliminate break/continue from loops' directly and clearly describes the main change—a new pass to transform break/continue statements into structured control flow.
Description check ✅ Passed The PR description comprehensively covers the changes made: new pass implementation, verifier introduction, pipeline insertion, documentation updates, and test additions. It is clearly related to the changeset.
Linked Issues check ✅ Passed The PR addresses issue #448 by implementing a CtrlFlowTransform pass that eliminates break/continue through structured control flow transformation, enabling SSA conversion and resolving the codegen backend issue where break/continue nodes were not handled properly.
Out of Scope Changes check ✅ Passed All changes are scoped to the CtrlFlowTransform pass implementation, documentation, verifier, and test infrastructure. Minor changes to PTOCodegen visitor methods for additional IR operations are necessary supporting infrastructure for the broader pass system.

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

📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

You can disable poems in the walkthrough.

Disable the reviews.poem setting to disable the poems in the walkthrough.

@gemini-code-assist
Copy link
Contributor

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 enhances the compiler's intermediate representation (IR) by introducing a new pass that transforms unstructured control flow statements (break and continue) into structured equivalents. This transformation is vital for improving the robustness and applicability of subsequent optimization passes, such as Static Single Assignment (SSA) conversion, which operate more effectively on structured code. The changes span core compiler logic, Python bindings, documentation, and extensive testing to ensure correctness and integration into the existing compilation pipeline.

Highlights

  • New Control Flow Transformation Pass: Introduced CtrlFlowTransform pass to rewrite break and continue statements into flag-based control flow using iter_args, enabling SSA conversion for programs with early exits.
  • Structured Control Flow Verification: Added StructuredCtrlFlow IRProperty and a corresponding verifier to track and validate structured control flow invariants, ensuring no break or continue statements remain after transformation.
  • Comprehensive Implementation: Provided a full cross-layer implementation including the C++ pass and verifier, Python bindings, and type stubs.
  • Documentation and Pipeline Updates: Updated pass documentation in both English and Chinese, renumbered all pass documents to match the new pipeline execution order, and inserted the new pass as the second step in the default pass pipeline (after UnrollLoops, before ConvertToSSA).
  • Expanded Testing: Added new unit tests for the CtrlFlowTransform pass covering various break, continue, nested loop, and combined scenarios, and extended system tests to validate runtime behavior of structured control flow.
Changelog
  • .claude/rules/pass-doc-ordering.md
    • Updated the pass ordering table to include the new CtrlFlowTransform pass at position 02 and renumbered subsequent passes accordingly.
  • CMakeLists.txt
    • Added src/ir/transforms/ctrl_flow_transform_pass.cpp to the PYPTO_SOURCES list.
    • Added src/ir/verifier/verify_structured_ctrl_flow_pass.cpp to the PYPTO_SOURCES list.
  • docs/en/dev/language/00-python_syntax.md
    • Updated the internal link to the SplitChunkedLoops Pass documentation to reflect its new numbering.
  • docs/en/dev/passes/00-pass_manager.md
    • Added CtrlFlowTransform to the pass properties table, indicating it produces StructuredCtrlFlow.
  • docs/en/dev/passes/01-unroll_loops.md
    • Updated the pipeline position description to include CtrlFlowTransform before ConvertToSSA.
  • docs/en/dev/passes/02-convert_to_ssa.md
    • Renamed to docs/en/dev/passes/03-convert_to_ssa.md.
  • docs/en/dev/passes/02-ctrl_flow_transform.md
    • Added new documentation for the CtrlFlowTransform pass.
  • docs/en/dev/passes/03-flatten_call_expr.md
    • Renamed to docs/en/dev/passes/04-flatten_call_expr.md.
  • docs/en/dev/passes/04-split_chunked_loops.md
    • Renamed to docs/en/dev/passes/05-split_chunked_loops.md.
  • docs/en/dev/passes/05-interchange_chunk_loops.md
    • Renamed to docs/en/dev/passes/06-interchange_chunk_loops.md.
  • docs/en/dev/passes/06-outline_incore_scopes.md
    • Renamed to docs/en/dev/passes/07-outline_incore_scopes.md.
  • docs/en/dev/passes/07-outline_cluster_scopes.md
    • Renamed to docs/en/dev/passes/08-outline_cluster_scopes.md.
  • docs/en/dev/passes/09-flatten_tile_nd_to_2d.md
    • Renamed to docs/en/dev/passes/10-flatten_tile_nd_to_2d.md.
  • docs/en/dev/passes/10-expand_mixed_kernel.md
    • Renamed to docs/en/dev/passes/11-expand_mixed_kernel.md.
  • docs/en/dev/passes/11-init_memref.md
    • Renamed to docs/en/dev/passes/12-init_memref.md.
  • docs/en/dev/passes/12-basic_memory_reuse.md
    • Renamed to docs/en/dev/passes/13-basic_memory_reuse.md.
  • docs/en/dev/passes/13-insert_sync.md
    • Renamed to docs/en/dev/passes/14-insert_sync.md.
  • docs/en/dev/passes/14-allocate_memory_addr.md
    • Renamed to docs/en/dev/passes/15-allocate_memory_addr.md.
  • docs/en/dev/passes/15-utility_passes.md
    • Renamed to docs/en/dev/passes/16-utility_passes.md.
  • docs/zh-cn/dev/language/00-python_syntax.md
    • Updated the internal link to the SplitChunkedLoops Pass documentation to reflect its new numbering.
  • docs/zh-cn/dev/passes/00-pass_manager.md
    • Added CtrlFlowTransform to the pass properties table, indicating it produces StructuredCtrlFlow.
  • docs/zh-cn/dev/passes/01-unroll_loops.md
    • Updated the pipeline position description to include CtrlFlowTransform before ConvertToSSA.
  • docs/zh-cn/dev/passes/02-convert_to_ssa.md
    • Renamed to docs/zh-cn/dev/passes/03-convert_to_ssa.md.
  • docs/zh-cn/dev/passes/02-ctrl_flow_transform.md
    • Added new documentation for the CtrlFlowTransform pass.
  • docs/zh-cn/dev/passes/03-flatten_call_expr.md
    • Renamed to docs/zh-cn/dev/passes/04-flatten_call_expr.md.
  • docs/zh-cn/dev/passes/04-split_chunked_loops.md
    • Renamed to docs/zh-cn/dev/passes/05-split_chunked_loops.md.
  • docs/zh-cn/dev/passes/05-interchange_chunk_loops.md
    • Renamed to docs/zh-cn/dev/passes/06-interchange_chunk_loops.md.
  • docs/zh-cn/dev/passes/06-outline_incore_scopes.md
    • Renamed to docs/zh-cn/dev/passes/07-outline_incore_scopes.md.
  • docs/zh-cn/dev/passes/07-outline_cluster_scopes.md
    • Renamed to docs/zh-cn/dev/passes/08-outline_cluster_scopes.md.
  • docs/zh-cn/dev/passes/09-flatten_tile_nd_to_2d.md
    • Renamed to docs/zh-cn/dev/passes/10-flatten_tile_nd_to_2d.md.
  • docs/zh-cn/dev/passes/10-expand_mixed_kernel.md
    • Renamed to docs/zh-cn/dev/passes/11-expand_mixed_kernel.md.
  • docs/zh-cn/dev/passes/11-init_memref.md
    • Renamed to docs/zh-cn/dev/passes/12-init_memref.md.
  • docs/zh-cn/dev/passes/12-basic_memory_reuse.md
    • Renamed to docs/zh-cn/dev/passes/13-basic_memory_reuse.md.
  • docs/zh-cn/dev/passes/13-insert_sync.md
    • Renamed to docs/zh-cn/dev/passes/14-insert_sync.md.
  • docs/zh-cn/dev/passes/14-allocate_memory_addr.md
    • Renamed to docs/zh-cn/dev/passes/15-allocate_memory_addr.md.
  • docs/zh-cn/dev/passes/15-utility_passes.md
    • Renamed to docs/zh-cn/dev/passes/16-utility_passes.md.
  • include/pypto/ir/transforms/ir_property.h
    • Added StructuredCtrlFlow to the IRProperty enum.
  • include/pypto/ir/transforms/pass_properties.h
    • Added kCtrlFlowTransformProperties with StructuredCtrlFlow as a produced property.
  • include/pypto/ir/transforms/passes.h
    • Declared the CtrlFlowTransform pass function.
  • include/pypto/ir/verifier/verifier.h
    • Declared the CreateStructuredCtrlFlowPropertyVerifier factory function.
  • python/bindings/modules/passes.cpp
    • Bound StructuredCtrlFlow to the Python IRProperty enum.
    • Added a Python binding for the ctrl_flow_transform pass.
  • python/pypto/ir/pass_manager.py
    • Inserted CtrlFlowTransform into the default optimization strategy of the pass manager.
  • python/pypto/pypto_core/passes.pyi
    • Added StructuredCtrlFlow to the IRProperty enum in the type stubs.
    • Declared the ctrl_flow_transform function in the type stubs.
  • src/ir/transforms/ctrl_flow_transform_pass.cpp
    • Added the C++ implementation of the CtrlFlowTransform pass.
  • src/ir/transforms/ir_property.cpp
    • Added string conversion for the StructuredCtrlFlow property.
  • src/ir/verifier/property_verifier_registry.cpp
    • Registered the StructuredCtrlFlowPropertyVerifier.
  • src/ir/verifier/verify_structured_ctrl_flow_pass.cpp
    • Added the C++ implementation of the StructuredCtrlFlow property verifier.
  • tests/st/runtime/test_ctrl_flow.py
    • Updated the module docstring to reflect support for break/continue.
    • Added new system tests for for loops with break, continue, and combined break and continue statements.
  • tests/ut/ir/transforms/test_ctrl_flow_transform.py
    • Added new unit tests for the CtrlFlowTransform pass, covering various scenarios for break and continue elimination.
  • tests/ut/ir/transforms/test_pass_manager.py
    • Updated the pass manager test to reflect the new pass ordering and count.
Activity
  • YunjiQin created the pull request to add a CtrlFlowTransform pass.
  • YunjiQin implemented the C++ pass and verifier for CtrlFlowTransform.
  • YunjiQin added Python bindings and type stubs for the new pass and property.
  • YunjiQin updated the pass pipeline to include CtrlFlowTransform.
  • YunjiQin renumbered and updated pass documentation in both English and Chinese.
  • YunjiQin developed unit tests for the CtrlFlowTransform pass.
  • YunjiQin extended system tests to cover break and continue control flow 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.

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.

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 introduces a significant feature: the CtrlFlowTransform pass, which eliminates break and continue statements from loops by converting them into structured control flow. This is a crucial step for enabling more of Python's control flow constructs in the compiler. The implementation is comprehensive, spanning the C++ pass itself, a corresponding verifier, Python bindings, documentation, and a solid suite of unit and system tests. My review identified two high-severity issues: a potential data type bug in the C++ implementation where INT32 is hardcoded for loop variable increments, and the omission of this new pass from the CCE optimization strategy. Addressing these will ensure the feature is both correct and complete.

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: 4

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

Inline comments:
In @.claude/rules/pass-doc-ordering.md:
- Around line 24-31: The pass-order table is missing InferTileMemorySpace and
ResolveTransposeLayout and incorrectly marks ExpandMixedKernel as not in any
strategy; update the table so that the sequence and numbering match the actual
Default pipeline (as asserted in tests/ut/ir/transforms/test_pass_manager.py) by
inserting entries for InferTileMemorySpace and ResolveTransposeLayout in the
correct positions and changing the ExpandMixedKernel entry to indicate it is
part of the Default strategy, ensuring the pass names (InferTileMemorySpace,
ResolveTransposeLayout, ExpandMixedKernel) and subsequent pass numbers are
adjusted accordingly.

In `@docs/en/dev/passes/00-pass_manager.md`:
- Around line 62-64: The docs erroneously list a non-existent IR property
FlattenedSingleStmt as invalidated by the ConvertToSSA and FlattenCallExpr
passes; update the table rows for ConvertToSSA and FlattenCallExpr to remove
FlattenedSingleStmt and instead list only NormalizedStmtStructure (matching
kConvertToSSAProperties and kFlattenCallExprProperties in pass_properties.h and
the IRProperty enum defined in ir_property.h). Ensure the table entries for
those passes match the actual invalidation sets used by kConvertToSSAProperties
and kFlattenCallExprProperties.

In `@include/pypto/ir/verifier/verifier.h`:
- Around line 183-184: Update the comment for the StructuredCtrlFlow
PropertyVerifier to accurately describe its scope: state that it verifies that
no BreakStmt or ContinueStmt remains in function bodies of InCore-type functions
only, and that it intentionally skips Host and Orchestration function types;
reference the verifier class/name StructuredCtrlFlow and the checked constructs
BreakStmt and ContinueStmt so readers understand the narrowed contract.

In `@src/codegen/pto/pto_codegen.cpp`:
- Around line 1156-1172: The BitShiftRightPtr, MinPtr, and MaxPtr visitor
implementations always emit signed MLIR ops; update PTOCodegen::VisitExpr_(const
ir::BitShiftRightPtr&), PTOCodegen::VisitExpr_(const ir::MinPtr&), and
PTOCodegen::VisitExpr_(const ir::MaxPtr&) to inspect the operand scalar dtype
(use ir::GetScalarDtype(op->a) or equivalent and DataType::IsUnsignedInt()) and
choose the unsigned op names ("arith.shrui", "arith.minui", "arith.maxui") when
the dtype is unsigned, otherwise keep the signed variants ("arith.shrsi",
"arith.minsi", "arith.maxsi"); use GetTypeString()/existing helpers to build the
call into VisitBinaryArithExpr accordingly. Also note similar unsigned-vs-signed
fixes will be needed for FloatDiv/RemInt in a follow-up.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4f39ac44-8818-4e4b-baa5-7fab3154c30c

📥 Commits

Reviewing files that changed from the base of the PR and between 47ff5fa and 95638b8.

📒 Files selected for processing (52)
  • .claude/rules/pass-doc-ordering.md
  • CMakeLists.txt
  • docs/en/dev/language/00-python_syntax.md
  • docs/en/dev/passes/00-pass_manager.md
  • docs/en/dev/passes/01-unroll_loops.md
  • docs/en/dev/passes/02-ctrl_flow_transform.md
  • docs/en/dev/passes/03-convert_to_ssa.md
  • docs/en/dev/passes/04-flatten_call_expr.md
  • docs/en/dev/passes/05-split_chunked_loops.md
  • docs/en/dev/passes/06-interchange_chunk_loops.md
  • docs/en/dev/passes/07-outline_incore_scopes.md
  • docs/en/dev/passes/08-outline_cluster_scopes.md
  • docs/en/dev/passes/10-flatten_tile_nd_to_2d.md
  • docs/en/dev/passes/11-expand_mixed_kernel.md
  • docs/en/dev/passes/12-init_memref.md
  • docs/en/dev/passes/13-basic_memory_reuse.md
  • docs/en/dev/passes/14-insert_sync.md
  • docs/en/dev/passes/15-allocate_memory_addr.md
  • docs/en/dev/passes/16-utility_passes.md
  • docs/zh-cn/dev/language/00-python_syntax.md
  • docs/zh-cn/dev/passes/00-pass_manager.md
  • docs/zh-cn/dev/passes/01-unroll_loops.md
  • docs/zh-cn/dev/passes/02-ctrl_flow_transform.md
  • docs/zh-cn/dev/passes/03-convert_to_ssa.md
  • docs/zh-cn/dev/passes/04-flatten_call_expr.md
  • docs/zh-cn/dev/passes/05-split_chunked_loops.md
  • docs/zh-cn/dev/passes/06-interchange_chunk_loops.md
  • docs/zh-cn/dev/passes/07-outline_incore_scopes.md
  • docs/zh-cn/dev/passes/08-outline_cluster_scopes.md
  • docs/zh-cn/dev/passes/10-flatten_tile_nd_to_2d.md
  • docs/zh-cn/dev/passes/11-expand_mixed_kernel.md
  • docs/zh-cn/dev/passes/12-init_memref.md
  • docs/zh-cn/dev/passes/13-basic_memory_reuse.md
  • docs/zh-cn/dev/passes/14-insert_sync.md
  • docs/zh-cn/dev/passes/15-allocate_memory_addr.md
  • docs/zh-cn/dev/passes/16-utility_passes.md
  • include/pypto/codegen/pto/pto_codegen.h
  • include/pypto/ir/transforms/ir_property.h
  • include/pypto/ir/transforms/pass_properties.h
  • include/pypto/ir/transforms/passes.h
  • include/pypto/ir/verifier/verifier.h
  • python/bindings/modules/passes.cpp
  • python/pypto/ir/pass_manager.py
  • python/pypto/pypto_core/passes.pyi
  • src/codegen/pto/pto_codegen.cpp
  • src/ir/transforms/ctrl_flow_transform_pass.cpp
  • src/ir/transforms/ir_property.cpp
  • src/ir/verifier/property_verifier_registry.cpp
  • src/ir/verifier/verify_structured_ctrl_flow_pass.cpp
  • tests/st/runtime/test_ctrl_flow.py
  • tests/ut/ir/transforms/test_ctrl_flow_transform.py
  • tests/ut/ir/transforms/test_pass_manager.py

YunjiQin added a commit to YunjiQin/pypto that referenced this pull request Mar 16, 2026
- Update pass-doc-ordering table to match actual Default pipeline
  (add InferTileMemorySpace, ResolveTransposeLayout; fix ExpandMixedKernel)
- Remove non-existent FlattenedSingleStmt from pass property docs
- Fix FlattenCallExpr required/produced properties to match code
- Narrow StructuredCtrlFlow verifier comment to reflect InCore-only scope
- Use unsigned MLIR ops (shrui/divui/minui/maxui) for unsigned int types
  in PTO codegen
@YunjiQin YunjiQin force-pushed the issue-448-pto-break-continue branch from 95638b8 to 1f9611a Compare March 16, 2026 12:55
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
python/pypto/ir/pass_manager.py (1)

72-91: ⚠️ Potential issue | 🔴 Critical

CCE strategy should include CtrlFlowTransform to handle break/continue statements.

The CCE strategy omits CtrlFlowTransform, but still runs UnrollLoops first. While UnrollLoops only expands unroll-loops and does not generate break/continue, user code can still contain regular loops (pl.range) with break/continue statements. Without CtrlFlowTransform, these statements would pass through to CCE codegen unchanged. However, CCECodegen has no visitor methods for BreakStmt or ContinueStmt, causing codegen to fail. The Default strategy avoids this by including CtrlFlowTransform after UnrollLoops to eliminate all break/continue before downstream passes.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/pypto/ir/pass_manager.py` around lines 72 - 91, The CCE optimization
list in OptimizationStrategy.CCE is missing the CtrlFlowTransform step which
removes break/continue before codegen; add an entry for ("CtrlFlowTransform",
lambda: passes.ctrl_flow_transform()) immediately after the existing
("UnrollLoops", lambda: passes.unroll_loops()) entry so that CtrlFlowTransform
runs before downstream passes (ensuring CCECodegen won't see
BreakStmt/ContinueStmt).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@python/pypto/ir/pass_manager.py`:
- Around line 72-91: The CCE optimization list in OptimizationStrategy.CCE is
missing the CtrlFlowTransform step which removes break/continue before codegen;
add an entry for ("CtrlFlowTransform", lambda: passes.ctrl_flow_transform())
immediately after the existing ("UnrollLoops", lambda: passes.unroll_loops())
entry so that CtrlFlowTransform runs before downstream passes (ensuring
CCECodegen won't see BreakStmt/ContinueStmt).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 90d68965-1ff8-4b39-a0f3-2a71f5c4f252

📥 Commits

Reviewing files that changed from the base of the PR and between 95638b8 and 1f9611a.

📒 Files selected for processing (52)
  • .claude/rules/pass-doc-ordering.md
  • CMakeLists.txt
  • docs/en/dev/language/00-python_syntax.md
  • docs/en/dev/passes/00-pass_manager.md
  • docs/en/dev/passes/01-unroll_loops.md
  • docs/en/dev/passes/02-ctrl_flow_transform.md
  • docs/en/dev/passes/03-convert_to_ssa.md
  • docs/en/dev/passes/04-flatten_call_expr.md
  • docs/en/dev/passes/05-split_chunked_loops.md
  • docs/en/dev/passes/06-interchange_chunk_loops.md
  • docs/en/dev/passes/07-outline_incore_scopes.md
  • docs/en/dev/passes/08-outline_cluster_scopes.md
  • docs/en/dev/passes/10-flatten_tile_nd_to_2d.md
  • docs/en/dev/passes/11-expand_mixed_kernel.md
  • docs/en/dev/passes/12-init_memref.md
  • docs/en/dev/passes/13-basic_memory_reuse.md
  • docs/en/dev/passes/14-insert_sync.md
  • docs/en/dev/passes/15-allocate_memory_addr.md
  • docs/en/dev/passes/16-utility_passes.md
  • docs/zh-cn/dev/language/00-python_syntax.md
  • docs/zh-cn/dev/passes/00-pass_manager.md
  • docs/zh-cn/dev/passes/01-unroll_loops.md
  • docs/zh-cn/dev/passes/02-ctrl_flow_transform.md
  • docs/zh-cn/dev/passes/03-convert_to_ssa.md
  • docs/zh-cn/dev/passes/04-flatten_call_expr.md
  • docs/zh-cn/dev/passes/05-split_chunked_loops.md
  • docs/zh-cn/dev/passes/06-interchange_chunk_loops.md
  • docs/zh-cn/dev/passes/07-outline_incore_scopes.md
  • docs/zh-cn/dev/passes/08-outline_cluster_scopes.md
  • docs/zh-cn/dev/passes/10-flatten_tile_nd_to_2d.md
  • docs/zh-cn/dev/passes/11-expand_mixed_kernel.md
  • docs/zh-cn/dev/passes/12-init_memref.md
  • docs/zh-cn/dev/passes/13-basic_memory_reuse.md
  • docs/zh-cn/dev/passes/14-insert_sync.md
  • docs/zh-cn/dev/passes/15-allocate_memory_addr.md
  • docs/zh-cn/dev/passes/16-utility_passes.md
  • include/pypto/codegen/pto/pto_codegen.h
  • include/pypto/ir/transforms/ir_property.h
  • include/pypto/ir/transforms/pass_properties.h
  • include/pypto/ir/transforms/passes.h
  • include/pypto/ir/verifier/verifier.h
  • python/bindings/modules/passes.cpp
  • python/pypto/ir/pass_manager.py
  • python/pypto/pypto_core/passes.pyi
  • src/codegen/pto/pto_codegen.cpp
  • src/ir/transforms/ctrl_flow_transform_pass.cpp
  • src/ir/transforms/ir_property.cpp
  • src/ir/verifier/property_verifier_registry.cpp
  • src/ir/verifier/verify_structured_ctrl_flow_pass.cpp
  • tests/st/runtime/test_ctrl_flow.py
  • tests/ut/ir/transforms/test_ctrl_flow_transform.py
  • tests/ut/ir/transforms/test_pass_manager.py
✅ Files skipped from review due to trivial changes (1)
  • docs/zh-cn/dev/passes/02-ctrl_flow_transform.md
🚧 Files skipped from review as they are similar to previous changes (10)
  • CMakeLists.txt
  • docs/en/dev/passes/01-unroll_loops.md
  • docs/zh-cn/dev/language/00-python_syntax.md
  • src/ir/verifier/property_verifier_registry.cpp
  • include/pypto/codegen/pto/pto_codegen.h
  • tests/ut/ir/transforms/test_pass_manager.py
  • python/pypto/pypto_core/passes.pyi
  • include/pypto/ir/transforms/ir_property.h
  • src/ir/transforms/ir_property.cpp
  • docs/en/dev/language/00-python_syntax.md

YunjiQin added a commit to YunjiQin/pypto that referenced this pull request Mar 18, 2026
- Update pass-doc-ordering table to match actual Default pipeline
  (add InferTileMemorySpace, ResolveTransposeLayout; fix ExpandMixedKernel)
- Remove non-existent FlattenedSingleStmt from pass property docs
- Fix FlattenCallExpr required/produced properties to match code
- Narrow StructuredCtrlFlow verifier comment to reflect InCore-only scope
- Use unsigned MLIR ops (shrui/divui/minui/maxui) for unsigned int types
  in PTO codegen
@YunjiQin YunjiQin force-pushed the issue-448-pto-break-continue branch from 1f9611a to 352318b Compare March 18, 2026 02:25
… loops

Transform break and continue statements into flag-based control flow
using iter_args, enabling SSA conversion for programs with early exits.
The pass rewrites loops to use boolean flags and conditional yields,
preserving semantics while producing structured control flow.

Includes StructuredCtrlFlow IRProperty, property verifier, full cross-layer
bindings (C++/Python/stubs), per-pass documentation (EN/ZH-CN), and
renumbered pass docs to match pipeline execution order.
…ions

Skip break/continue elimination for Host/Orchestration functions since
they support native control flow. Also fix ConstBool codegen to emit
MLIR-compatible integer literals and clean up duplicate includes.
Add expression visitors for 15 operators missing from PTO codegen:
- Logical: And, Or, Xor
- Bitwise: BitAnd, BitOr, BitXor, BitShiftLeft, BitShiftRight
- Other binary: FloatDiv, Min, Max
- Unary: Not, Neg, Abs, BitNot

Maps to corresponding MLIR arith/math dialect operations with
int/float dispatch where applicable.
- Update pass-doc-ordering table to match actual Default pipeline
  (add InferTileMemorySpace, ResolveTransposeLayout; fix ExpandMixedKernel)
- Remove non-existent FlattenedSingleStmt from pass property docs
- Fix FlattenCallExpr required/produced properties to match code
- Narrow StructuredCtrlFlow verifier comment to reflect InCore-only scope
- Use unsigned MLIR ops (shrui/divui/minui/maxui) for unsigned int types
  in PTO codegen
Add <cstddef>, <optional>, and kind_traits.h to satisfy
misc-include-cleaner checks.
Rewrite CtrlFlowTransform pass to handle break and continue in SSA-form
IR using phi-node-based yield value resolution. Supports nested ifs,
multiple escapes per loop, combined break+continue, and while loops.
Add 48 comprehensive tests covering all edge cases.
@YunjiQin YunjiQin force-pushed the issue-448-pto-break-continue branch from 352318b to b2a4416 Compare March 18, 2026 07:39
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.

[Bug] Codegen: PTO and CCE backends do not handle BreakStmt/ContinueStmt

1 participant