✨ Add IndexSwitch operation #1925
Conversation
|
@coderabbitai full review |
✅ Action performedFull review finished. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (13)
📝 WalkthroughSummary by CodeRabbit
WalkthroughAdds ChangesIndex switch support
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant QCProgramBuilder
participant QCToQCO
participant QCOIndexSwitchOp
participant QCOToQC
participant SCFIndexSwitchOp
QCProgramBuilder->>SCFIndexSwitchOp: build index switch
QCToQCO->>QCOIndexSwitchOp: lower switch and regions
QCOToQC->>SCFIndexSwitchOp: recreate converted switch
Possibly related issues
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches✨ Simplify code
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.
Actionable comments posted: 14
🤖 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 `@mlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.h`:
- Around line 1132-1161: Update the Doxygen comment for scfIndexSwitch to
document arg, cases, caseBodies, and defaultBody instead of the copied thenBody
and elseBody parameters. Also make the example lambdas consistently use the
builder instance shown in the invocation.
In `@mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h`:
- Line 1382: Update the parameter documentation for the affected builder method
near the `@param arg` comment to describe `arg` as the index selector/value,
accepting `std::variant<int64_t, Value>`, rather than a boolean condition. Do
not alter the method implementation or the neighboring `qcoIf` documentation.
In `@mlir/lib/Conversion/QCOToQC/QCOToQC.cpp`:
- Around line 903-905: Update the IndexSwitchOp creation in the relevant
conversion pattern to pass adaptor.getArg() instead of op.getArg(), matching the
argument access used by ConvertQCOIfOp while leaving the remaining operands and
switch configuration unchanged.
In `@mlir/lib/Conversion/QCToQCO/QCToQCO.cpp`:
- Around line 1529-1531: Remove the dead commented-out
rewriter.inlineBlockBefore line adjacent to the newBlock operations splice;
leave the active splice implementation unchanged.
In `@mlir/lib/Dialect/QC/Builder/QCProgramBuilder.cpp`:
- Around line 603-635: Update the local buildRegion callback in scfIndexSwitch
to push each newly created switch region onto regionStack before invoking its
body callback and pop it afterward, matching scfFor, scfWhile, and scfIf. Ensure
both case and default bodies execute with their active region tracked so
memrefLoad performs the correct enclosing-region and duplicate-load checks.
In `@mlir/lib/Dialect/QCO/IR/QCOOps.cpp`:
- Around line 197-265: Update the index-switch parser around the case/default
argument handling to preserve target operands: validate the first parsed args
list against result.types, resolve its regionOperands with
parser.resolveOperands, and append them to result.operands exactly once since
targets are shared. Replace both llvm::zip_equal loops with explicit arity
checks that emit parser failure before assigning argument types, while retaining
parsing for subsequent regions.
In `@mlir/lib/Dialect/QCO/IR/SCF/IndexSwitchOp.cpp`:
- Around line 156-179: Update getTiedCaseYieldedValue to validate that
bbArg.getOwner() is specifically getCaseBlock(i), not merely a block nested
under this operation, while retaining the existing operation and index checks.
Update getTiedDefaultYieldedValue to require bbArg.getOwner() ==
getDefaultBlock() before indexing the default yield targets; return nullptr for
arguments from other blocks.
- Around line 45-93: Update the unmatched-case fallback in both
IndexSwitchOp::getRegionInvocationBounds and
IndexSwitchOp::getEntrySuccessorRegions to use the default region index,
getNumRegions() - 1, instead of 0. Preserve matched-case indices and
unknown-operand behavior unchanged.
In `@mlir/lib/Support/IRVerification.cpp`:
- Around line 375-383: Update the qco::CtrlOp branch in the equivalence checker
so the failed compareValueLists condition returns false, matching the behavior
of the preceding qc::CtrlOp branch. Preserve the existing comparisons and allow
execution to continue only when both input controls and input targets are
equivalent.
In `@mlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cpp`:
- Around line 710-712: Align the nested-loop switch fixtures so both QCO and QC
use cases {0, 1, 2} with matching branch behavior for selectors computing iv %
3. Update the NestedForLoopSwitchOp mappings in
mlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cpp:710-712 and
mlir/unittests/Conversion/QCToQCO/test_qc_to_qco.cpp:713-715 after correcting
the fixture definitions.
In `@mlir/unittests/programs/qc_programs.cpp`:
- Around line 1984-2001: Update nestedForLoopSwitchOp so the allocated qubit
register covers every loop index used by the scfFor bound: allocate n qubits
instead of one before calling memrefLoad with iv. Preserve the existing loop
bounds, switch behavior, and measurement flow.
In `@mlir/unittests/programs/qc_programs.h`:
- Around line 918-919: Update the documentation for indexSwitchMultiCase to
state that it creates a circuit using two qubits, matching the implementation’s
size value in qc_programs.cpp.
In `@mlir/unittests/programs/qco_programs.cpp`:
- Around line 3455-3489: Update nestedForLoopSwitchOp so the allocated register
size matches the loop bound, allowing qtensorExtract to safely use every iv from
0 through n - 1. Also align qcoIndexSwitch cases with rem’s actual range {0, 1,
2}, ensuring every remainder selects an intended branch and the default remains
unreachable.
In `@mlir/unittests/programs/qco_programs.h`:
- Around line 1109-1110: Update the documentation for indexSwitchMultiCase in
QCOProgramBuilder to state that the circuit uses two qubits, matching the
implementation’s size constant.
🪄 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: ASSERTIVE
Plan: Pro
Run ID: 40668aa7-ef50-4fe5-a6c6-411d471799c9
📒 Files selected for processing (18)
CHANGELOG.mdmlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.hmlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.hmlir/include/mlir/Dialect/QCO/IR/QCOOps.tdmlir/lib/Conversion/QCOToQC/QCOToQC.cppmlir/lib/Conversion/QCToQCO/QCToQCO.cppmlir/lib/Dialect/QC/Builder/QCProgramBuilder.cppmlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cppmlir/lib/Dialect/QCO/IR/QCOOps.cppmlir/lib/Dialect/QCO/IR/SCF/IfOp.cppmlir/lib/Dialect/QCO/IR/SCF/IndexSwitchOp.cppmlir/lib/Support/IRVerification.cppmlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cppmlir/unittests/Conversion/QCToQCO/test_qc_to_qco.cppmlir/unittests/programs/qc_programs.cppmlir/unittests/programs/qc_programs.hmlir/unittests/programs/qco_programs.cppmlir/unittests/programs/qco_programs.h
|
@coderabbitai review |
✅ Action performedReview finished.
|
Changes
qco.index_switchoperation to TableGen.qco.ifandqco.index_switchinIRVerification.cpp(The conversions use a first-use first-argument approach).Checklist
If PR contains AI-assisted content:
🤖 *AI text below* 🤖(titles are exempt).Assisted-by: [Model Name] via [Tool Name]footer.