Fuse matching product scf.if regions - #484
1sgtpepper wants to merge 92 commits into
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b4213c67d7
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
raghav198
left a comment
There was a problem hiding this comment.
The constrain use is remapped to the branch-local compute value instead of hoisting the read.
I may be misunderstanding, but I think this is not safe when the struct.readm is a constrain op:
%0 = felt.add %a, %b {source = "compute"}
struct.writem %self[@foo] = %0 {source = "compute"}
%foo = struct.readm %self[@foo] {source = "constrain"}
constrain.eq %foo, %c {source = "constrain"}
emits a constraint guaranteeing that the @foo signal equals the result of %c, whereas in the remapped
%0 = felt.add %a, %b {source = "compute"}
struct.writem %self[@foo] = %0 {source = "compute"}
constrain.eq %0, %c {source = "constrain"}
the emitted constraint doesn't refer to a struct signal at all.
|
Thanks for catching this, Raghav. You're right. Remapping the struct.readm result to the branch local compute value can remove the member signal from the emitted constraint. I'll make this fusion case conservative for now and add a regression test so we keep the constraint tied to the member read. |
There was a problem hiding this comment.
Pull request overview
This PR extends the -llzk-fuse-product-loops pass to also fuse matching compute/constrain scf.if regions in struct product programs (Fixes #300), and adds regression tests covering fusable and non-fusable scf.if patterns.
Changes:
- Add
scf.iffusion logic that clones compute + constrain branches into a single fusedscf.ifwhen conditions and structural constraints match. - Refactor pass entry/recursion to fuse both
scf.ifandscf.forcontrol flow via a sharedfuseMatchingRegionControlFlowroutine. - Add lit/FileCheck tests for
scf.iffusion and “do not fuse” guardrails; add an unreleased changelog entry.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/Transforms/LLZKFuseProductLoopsPass.cpp | Implements scf.if fusion and refactors recursion over region control flow. |
| test/Transforms/FuseProductLoops/fuse_if_no_member_read.llzk | Positive test ensuring scf.if fusion occurs in a safe case. |
| test/Transforms/FuseProductLoops/fuse_loop_then_if.llzk | Ensures loop fusion still works and enables nested scf.if fusion after loop fusion. |
| test/Transforms/FuseProductLoops/no_fuse_if_call_after_write.llzk | Negative test: blocks fusion when a call occurs after a mapped write. |
| test/Transforms/FuseProductLoops/no_fuse_if_condition_mismatch.llzk | Negative test: blocks fusion when conditions differ. |
| test/Transforms/FuseProductLoops/no_fuse_if_internal_read.llzk | Negative test: blocks fusion when constrain side reads members inside the scf.if. |
| test/Transforms/FuseProductLoops/no_fuse_if_read_after_write.llzk | Negative test: blocks fusion when a member read appears between the ifs. |
| changelogs/unreleased/fuse-product-if-regions.yaml | Changelog entry for the new scf.if fusion capability. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
I seem to have found some loose ends, will update in a bit. |
|
Pushed a conservative safety guard for moved constrain branches. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
53ba923 to
6719d38
Compare
|
I think I've found some issues with the pr will mark draft for now while I fix them |
bb2fad5 to
583a957
Compare
|
@iangneal I kept the legacy name to avoid breaking existing CLI and pipeline references. Should I open a follow-up issue to rename them, or keep it as-is? |
Go ahead and do the requested renaming in this PR. |
10554fc to
d3f1aca
Compare
|
@tim-hoffman Small non-blocking missed-optimization follow-up related to #619: a same-parent loop in another block can suppress valid same-block fusion. Reproducer · IR output. Result: baseline = 1 fused loop; cross-block = 1 compute + 2 constrain, 0 fused. Should this be tracked under #619? |
# Conflicts: # include/llzk/Transforms/LLZKTransformationPasses.td
Summary
Fuse matching compute/constrain
scf.ifregions in product functions while preserving constrained signal reads, source order, and observable effects. Unsafe or ambiguous pairs remain separate.Related issues
Part of #300. Supports identical SSA conditions and resultless constrain conditionals.
Changes
Testing
nix buildfor.#debugGCC,.#debugClangCov,.#checks.x86_64-linux.llzk-installcheck-debug, and.#checks.x86_64-linux.llzk-installcheck-release;nix --option sandbox false --print-build-logs build .#docs.git-clang-format20.1.8 passed;clang-tidyreported no new LLZK source diagnostics.Submission checklist
approved; otherwise, this does not apply.create-changelogin the Nix development shell creates a template.)AI assistance
Tools used: AI coding assistant
How the tools contributed: Implementation and code review.
How I verified the contribution: Reviewed the final diff and exact-head hosted results.