-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add control flow branch interface #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| module | ||
|
|
||
| public import Veir.GlobalOpInfo | ||
|
|
||
| /-! | ||
| # ControlFlowInterfaces | ||
|
|
||
| This file provides support for querying which operands are forwarded to a successor | ||
| and mapping those operands to successor block arguments. | ||
| -/ | ||
|
|
||
| namespace Veir | ||
|
|
||
| public section | ||
|
|
||
| /-- The SSA values forwarded from a branch operation to one of its successors. -/ | ||
| structure SuccessorOperands where | ||
| /-- The SSA values forwarded to the successor. -/ | ||
| forwardedOperands : Array ValuePtr | ||
| deriving Inhabited, Repr, DecidableEq | ||
|
|
||
| instance : GetElem SuccessorOperands Nat ValuePtr | ||
| (fun operands blockArgumentIndex => blockArgumentIndex < operands.forwardedOperands.size) where | ||
| getElem := fun operands blockArgumentIndex h => operands.forwardedOperands[blockArgumentIndex]'h | ||
|
|
||
| instance : GetElem? SuccessorOperands Nat ValuePtr | ||
| (fun operands blockArgumentIndex => blockArgumentIndex < operands.forwardedOperands.size) where | ||
| getElem? := fun operands blockArgumentIndex => operands.forwardedOperands[blockArgumentIndex]? | ||
|
|
||
| namespace BranchOpInterface | ||
|
|
||
| /-- | ||
| Return the operands passed to `successorIndex` of a branch operation. | ||
| -/ | ||
| def getSuccessorOperands? | ||
|
math-fehr marked this conversation as resolved.
|
||
| (branchOp : OperationPtr) (successorIndex : Nat) (raw : IRContext OpCode) : | ||
| Option SuccessorOperands := | ||
| let opType := branchOp.getOpType! raw | ||
| match opType with | ||
| | .cf .br | .llvm .br | .riscv_cf .branch => | ||
| some { | ||
| forwardedOperands := branchOp.getOperands! raw | ||
| } | ||
| | _ => do | ||
| -- Determine whether this is a supported conditional branch and how many | ||
| -- fixed operands appear before its successor operand segments. | ||
| let fixedOperandCount ← match opType with | ||
| | .cf .cond_br | ||
| | .llvm .cond_br | ||
| | .riscv_cf .beqz | ||
| | .riscv_cf .bnez => some 1 | ||
| | .riscv_cf .beq | ||
| | .riscv_cf .bne | ||
| | .riscv_cf .blt | ||
| | .riscv_cf .bge | ||
| | .riscv_cf .bltu | ||
| | .riscv_cf .bgeu => some 2 | ||
| | _ => none | ||
| -- TODO: Move the operandSegmentSizes logic to the respective dialects | ||
| -- Read the operand segment metadata from the operation's typed properties. | ||
| let attrs := Properties.toAttrDict opType (branchOp.getProperties! raw opType) | ||
| let some (.denseArrayAttr sizes) := attrs["operandSegmentSizes".toUTF8]? | none | ||
| let segmentSizes := sizes.values | ||
| -- Select the segment corresponding to the requested successor. | ||
| let segmentIndex := fixedOperandCount + successorIndex | ||
| let forwardedCountRaw ← segmentSizes[segmentIndex]? | ||
| let forwardedCount := forwardedCountRaw.toNat | ||
| -- Compute the operation operand index where this successor's forwarded values begin. | ||
| let forwardedStart := fixedOperandCount + | ||
| (segmentSizes.extract fixedOperandCount segmentIndex).foldl | ||
| (init := 0) fun acc value => acc + value.toNat | ||
| -- Return only the operands forwarded to the requested successor. | ||
| some { | ||
| forwardedOperands := (branchOp.getOperands! raw).extract | ||
| forwardedStart (forwardedStart + forwardedCount) | ||
| } | ||
|
|
||
| /-- Return the SSA value forwarded to a successor block argument. -/ | ||
| def getSuccessorOperand? | ||
| (branchOp : OperationPtr) (successorIndex blockArgumentIndex : Nat) | ||
| (raw : IRContext OpCode) : Option ValuePtr := | ||
| getSuccessorOperands? branchOp successorIndex raw >>= fun operands => | ||
| operands[blockArgumentIndex]? | ||
|
|
||
| end BranchOpInterface | ||
|
|
||
| end | ||
|
|
||
| end Veir | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.