Skip to content

feat(verifier): Promote InOut-use discipline to structural property#1039

Merged
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:issue-1028-promote-inout-use-verifier
Apr 16, 2026
Merged

feat(verifier): Promote InOut-use discipline to structural property#1039
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:issue-1028-promote-inout-use-verifier

Conversation

@Hzfengsy
Copy link
Copy Markdown
Member

Summary

  • Adds InOutUseValid as a structural IRProperty and registers a PropertyVerifier for it, so the RFC [RFC] InOut-use discipline as a structural IR invariant #1026 InOut-use discipline is enforced at pipeline start (PassPipeline) and before/after every pass (VerificationInstrument).
  • Splits stmt_dep::CheckInOutUseDiscipline into a non-throwing CollectInOutUseDisciplineDiagnostics entry point that the verifier reuses, and refines the back-edge model so legitimate per-iteration rebinding (loop iter_args and AssignStmt LHSs inside the body) is no longer flagged.
  • Scrubs iter_args from the dead set after each loop so inner-loop state does not leak to the enclosing scope.
  • Migrates affected examples/ and one orchestration test fixture to the new discipline (renaming post-call rebinds such as out_c = self.kernel(..., out_c) -> out_c_ret = ...).

Known limitation

ExpandMixedKernel produces Group-typed function bodies that share the same Out tensor across the AIC and AIV calls without threading SSA versions. The verifier currently skips Group function bodies as a workaround; a follow-up should thread return values through AIC -> Group -> AIV so the discipline covers them.

Test plan

  • cmake --build build --parallel (clean build, no warnings)
  • python -m pytest tests/ut/ -n auto --maxprocesses 8 — 3573 passed, 16 skipped (no regressions)
  • python tests/lint/clang_tidy.py --diff-base HEAD — clean
  • Pre-commit hooks (clang-format, ruff, pyright, markdownlint) — all green

Related Issues

Refs #1028
RFC #1026

Adds InOutUseValid as a structural IRProperty (RFC hw-native-sys#1026), wired into
GetStructuralProperties and GetVerifiedProperties so the discipline is
checked at pipeline start and before/after every pass.

Refines the back-edge model in stmt_dep::CheckInOutUseDiscipline to
exclude vars rebound each iteration (loop iter_args and AssignStmt LHSs
inside the body) from the pre-populated dead set, and scrubs iter_args
from dead_ after each loop so inner-loop state does not leak to enclosing
scopes. Splits the check into a non-throwing CollectInOutUseDiscipline-
Diagnostics entry point that the property verifier reuses.

Group-typed function bodies are skipped pending a follow-up that threads
SSA versions through AIC/Group/AIV calls produced by ExpandMixedKernel.

Migrates examples and one orchestration test fixture to the new
discipline by renaming post-call rebinds (e.g. `out_c = self.kernel(...,
out_c)` -> `out_c_ret = ...`).

Refs hw-native-sys#1028
Copilot AI review requested due to automatic review settings April 15, 2026 11:33
Copy link
Copy Markdown
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 implements the InOut-use discipline (RFC #1026) by adding a new structural property verifier, InOutUseValid. This verifier ensures that variables passed as InOut or Out to user-function calls are not read subsequently within the same scope, enforcing that post-mutation values flow through return slots. The changes include updates to the IR property system, documentation in both English and Chinese, and significant refactoring of example kernels and models to comply with the new discipline. A performance improvement was suggested for the diagnostic collection loop in the new verifier implementation.

Comment thread src/ir/verifier/verify_inout_use.cpp
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Promotes the RFC #1026 InOut-use discipline to a globally enforced structural IR invariant by introducing a new InOutUseValid property verifier, refactoring the existing checker to be reusable for diagnostics collection, and migrating examples/tests/docs to comply.

Changes:

  • Adds IRProperty::InOutUseValid as a structural property, registers its PropertyVerifier, and wires it into builds.
  • Refactors the InOut-use checker to expose a non-throwing diagnostics collector and refines loop/back-edge handling.
  • Updates affected examples, docs, and an orchestration codegen UT fixture to avoid post-call re-reads/rebinds.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ir/verifier/verify_inout_use.cpp New structural property verifier that reuses the dependency-analysis checker and skips Group functions (known limitation).
src/ir/verifier/property_verifier_registry.cpp Registers the new InOutUseValid verifier in the central registry.
src/ir/transforms/ir_property.cpp Adds InOutUseValid to verified + structural property sets and stringification.
include/pypto/ir/transforms/ir_property.h Introduces the IRProperty::InOutUseValid enum value.
include/pypto/ir/verifier/verifier.h Declares the new verifier factory function.
src/ir/transforms/utils/stmt_dependency_analysis.cpp Refactors the checker to return diagnostics; adjusts assignment/loop modeling for the discipline.
include/pypto/ir/transforms/utils/stmt_dependency_analysis.h Exposes the non-throwing diagnostics collection API for reuse by the verifier.
tests/ut/codegen/test_orchestration_codegen.py Updates a fixture to thread post-call values via return variables.
examples/models/01_ffn.py Updates orchestration examples to use returned SSA-style names instead of reusing Out/InOut vars post-call.
examples/models/02_vector_dag.py Same: renames post-call values to respect the discipline across a DAG.
examples/kernels/01_elementwise.py Migrates example to return-slot threading for Out vars.
examples/kernels/02_fused_ops.py Migrates example to return-slot threading for Out vars.
examples/kernels/03_matmul.py Migrates example to return-slot threading for Out vars.
examples/kernels/04_concat.py Migrates example to return-slot threading for Out vars.
examples/kernels/05_activation.py Migrates example to return-slot threading for Out vars.
examples/kernels/06_softmax.py Migrates example to return-slot threading for Out vars.
examples/kernels/07_normalization.py Migrates example to return-slot threading for Out vars.
examples/kernels/08_assemble.py Migrates example to return-slot threading for Out vars.
examples/hello_world.py Migrates example to return-slot threading for Out vars.
docs/en/dev/passes/99-verifier.md Documents InOutUseValid as a structural property and lists it among built-in rules.
docs/zh-cn/dev/passes/99-verifier.md Same documentation update (Chinese).
CMakeLists.txt Adds the new verifier implementation to the build.

Comment thread src/ir/transforms/utils/stmt_dependency_analysis.cpp Outdated
@Hzfengsy Hzfengsy changed the title feat(verifier): Promote InOut-use discipline to structural property (RFC #1026) feat(verifier): Promote InOut-use discipline to structural property Apr 15, 2026
- Drop AssignStmt LHS exclusion from loop back-edge dead set (Copilot):
  the exclusion could hide read-before-rebind dead reads across
  iterations. VisitStmt_(AssignStmt) already revives the var when the
  rebind actually executes, so the simpler "exclude iter_args only"
  rule avoids false negatives without introducing false positives.
- Document why Gemini's make_move_iterator insert suggestion is not
  applicable here — Diagnostic's move-assignment is deleted because
  Span has const members.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 15, 2026

Warning

Rate limit exceeded

@Hzfengsy has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 57 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 15 minutes and 57 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, 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 have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6b9b2687-ca65-4509-9c35-2d7c884912d9

📥 Commits

Reviewing files that changed from the base of the PR and between 34f7715 and f0c90d4.

📒 Files selected for processing (22)
  • CMakeLists.txt
  • docs/en/dev/passes/99-verifier.md
  • docs/zh-cn/dev/passes/99-verifier.md
  • examples/hello_world.py
  • examples/kernels/01_elementwise.py
  • examples/kernels/02_fused_ops.py
  • examples/kernels/03_matmul.py
  • examples/kernels/04_concat.py
  • examples/kernels/05_activation.py
  • examples/kernels/06_softmax.py
  • examples/kernels/07_normalization.py
  • examples/kernels/08_assemble.py
  • examples/models/01_ffn.py
  • examples/models/02_vector_dag.py
  • include/pypto/ir/transforms/ir_property.h
  • include/pypto/ir/transforms/utils/stmt_dependency_analysis.h
  • include/pypto/ir/verifier/verifier.h
  • src/ir/transforms/ir_property.cpp
  • src/ir/transforms/utils/stmt_dependency_analysis.cpp
  • src/ir/verifier/property_verifier_registry.cpp
  • src/ir/verifier/verify_inout_use.cpp
  • tests/ut/codegen/test_orchestration_codegen.py

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.

@lyfne123 lyfne123 merged commit 7c83726 into hw-native-sys:main Apr 16, 2026
14 of 15 checks passed
@Hzfengsy Hzfengsy deleted the issue-1028-promote-inout-use-verifier branch April 16, 2026 03:46
Hzfengsy added a commit to Hzfengsy/pypto that referenced this pull request Apr 16, 2026
Fixes hw-native-sys#1048

PartialUnrollTileLoops no longer tags replicated loops with an
`unroll_replicated` attribute, and ReorderUnrolledIO now canonicalizes
IO order in every multi-stmt SeqStmts rather than only marker-tagged
regions. The reorder is sound everywhere given the InOut-use discipline
structural invariant from PR hw-native-sys#1039; SeqStmts that pre-date the invariant
(under VerificationLevel.NONE) are left alone.

Tail handling: static remainders become a bare SeqStmts flattened into
the outer scope with trailing AssignStmts binding iter_arg return_vars;
dynamic cascade IfStmt branches become bare SeqStmts ending in YieldStmt.
The trip-1 ForStmt wrapper (previously needed only to hang the marker)
is gone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants