fix(codegen): emit tensor phis via tensors dict to avoid NameError (#2180) - #2183
fix(codegen): emit tensor phis via tensors dict to avoid NameError (#2180)#2183georgebisbas wants to merge 3 commits into
Conversation
When ConvertToSSA synthesizes phi `return_vars_` for cross-branch diverging tensor variables, host_orch.py codegen must pre-declare the phi name in the `tensors` dict and emit yield-to-phi assignments that reference tensors-dict names — not bare Python variables. Fixes hw-native-sys#2180. - IfStmt visitor (DistributedCodegen): pre-declares phi variables before `if`, emits branch-specific yield-to-phi `tensors[…] = tensors[…]` assignments, and traces Var→Var aliases (kernel param copies) to the original tensors-dict name. - AssignStmt visitor: aliases `tensor_var = kernel_param` are emitted as `tensors["tensor_var"] = tensors["kernel_param"]` instead of bare Python names. - Unit test validates phi pre-declaration, yield assignments, and absence of bare-name tensor assignments. Co-authored-by: georgebisbas <georgios.bismpas@h-partners.com> Co-authored-by: vloncar <vloncar@users.noreply.github.com>
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughDistributed codegen now preserves tensor aliases through the tensors registry and emits SSA phi declarations and branch assignments for conditional control flow. Tests optionally run SSA conversion and verify generated host orchestration code for cross-branch tensor values. ChangesDistributed tensor phi handling
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ecbf7e1acd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| VisitExpr(src); | ||
| tensor_phi_init = current_expr_value_; |
There was a problem hiding this comment.
Avoid emitting branch calls while probing phi init
When a HOST orchestrator has no tensor formal parameters, this fallback tries to derive a tensor phi initializer by visiting the then-branch assignment RHS before emitting the if. If that RHS is a hierarchy call such as boundary = self.chip_run(tmp) (where tmp is a top-level created tensor), VisitExpr(src) calls EmitCallToWorker, so the generated host_orch.py submits that chip task unconditionally before the branch condition is evaluated, and with no assignment target for the call output. That changes program behavior for no-input host orchestrators with tensor phis; initializer discovery here needs to avoid non-pure expression visitors and only use already-materialized tensor names.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8e0e5e0: both init-discovery paths now guard VisitExpr with ir::As<ir::Var>(src) != nullptr so only Var→Var aliases are visited. For Call RHS the init falls through to the shared tensor_phi_init (function params) or zero placeholder — EmitCallToWorker is never triggered before the if condition.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/codegen/distributed/distributed_codegen.cpp`:
- Around line 869-909: Update the tensor-phi handling in the pre-declaration
loop over op->return_vars_ so each tensor phi derives its initializer from that
phi’s own in-scope source or pre-if tensor, rather than reusing the single
tensor_phi_init selected earlier. Preserve the initializer’s shape and dtype,
and only use the fallback placeholder when no valid source exists for that
specific phi.
In `@tests/ut/codegen/distributed/test_host_orch_distributed.py`:
- Around line 959-1020: Strengthen the then_yield assertion in
test_if_cross_branch_phi_predeclares_and_yields_tensors so it examines only the
generated then-branch body, excluding the pre-if phi declaration. Isolate the
region between the if and else boundaries (or otherwise anchor the match after
the if) and require the tensors-based phi assignment there, so the assertion
fails if then-branch emission regresses.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 6a4b9206-d633-4f0b-a013-142cf86d5458
📒 Files selected for processing (2)
src/codegen/distributed/distributed_codegen.cpptests/ut/codegen/distributed/test_host_orch_distributed.py
…-sys#2183) CodeRabbit review: each tensor-typed phi now derives its pre-declaration initializer from its own yield source instead of a shared init. Test assertion isolates the then-branch body from the pre-if declaration. Pre-commit: suppress pyright reportReturnType for IR-level return 0.
…w-native-sys#2183) When a HOST orchestrator has no tensor formal parameters, the phi init fallback visited the then-branch yield source via VisitExpr. If that source is a hierarchy call, EmitCallToWorker would emit a _submit_chip before the `if` condition, altering program semantics. Only resolve Var→Var aliases now; Call RHS falls through to the zero placeholder. Co-authored-by: georgebisbas <georgios.bismpas@h-partners.com> Co-authored-by: vloncar <vloncar@users.noreply.github.com>
Summary
Fixes #2180: cross-branch phi
NameErrorinhost_orch.pyatprepare()time.When
ConvertToSSAsynthesizes phireturn_vars_for cross-branch diverging tensor variables, the distributed codegen now:tensorsdict before theifblocktensors[...] = tensors[...](not bare Python names)tensors-dict referenceAssignStmts astensors["var"] = tensors["value"]instead of bare Python variable namesTest plan
test_if_cross_branch_phi_predeclares_and_yields_tensorsvalidates:ifblocktensors[...]host_orch.pyconfirmed to produce valid, executable code that no longer raisesNameErrorCo-authored-by: @georgebisbas @vloncar