Skip to content

Commit 1b2f32a

Browse files
convert phi to mu
1 parent 2491ae3 commit 1b2f32a

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

37.9 KB
Loading

docs/DeveloperGuide/DynamaticFeaturesAndOptimizations/FTD/GSAAnalysis.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Consider the following control-flow graph and its corresponding `cf_dyn_transfor
1818

1919
- The argument of bb3 (%7) comes from two mutually exclusive control-flow paths (bb1 or bb2). This corresponds to a γ function.
2020

21-
![CFG](./Figures/if_loop_add_CFG.png)
21+
![if_loop_add_CFG](./Figures/if_loop_add_CFG.png)
2222

2323
```
2424
module {
@@ -96,6 +96,59 @@ For each block in the region:
9696
```
9797
After all ϕ gates are created, the final step is to connect the missing inputs recorded in phisToConnect.
9898

99+
### What is a “missing phi”?
100+
The input of a ϕ gate can itself be another ϕ. This happens when the input comes from a block argument of another block (excluding bb0). In this case, the ϕ input cannot be connected immediately. Instead, it is marked as missing and the necessary information is stored. After all ϕ gates are extracted, these missing inputs are revisited and the connections are reconstructed.
101+
102+
### `isBlockArgAlreadyPresent` and `isValueAlreadyPresent`
103+
These helper functions prevent duplicate inputs from being recorded.
104+
105+
// TODO: explain their implementation details later.
106+
107+
### Why can a value appear multiple times?
108+
// TODo
109+
99110
## Convert ϕ Gates into μ Gates
100111

112+
### Checks
113+
A ϕ gate is classified as a μ gate if the following conditions hold:
114+
115+
1. It is inside a loop.
116+
117+
2. It has at least two operands.
118+
119+
3. It is located in the loop header.
120+
121+
### Input Grouping
122+
123+
Once a candidate μ is identified, its operands (inputs) are divided into two groups:
124+
125+
- **Loop inputs:** values produced inside the same loop as the ϕ.
126+
127+
- **Initial inputs:** values originating from outside the loop.
128+
129+
#### Notice: Inputs from Nested Loops
130+
Blocks only report their innermost loop as the one they belong to. Because of this, inputs from nested loops might be mistakenly recognized as “initial inputs” instead of loop inputs.
131+
132+
*Example:* in the CFG below, an input value from block `bb3` may appear to belong to a different loop than the one containing `bb1` (the ϕ’s loop). To prevent this, the `IsBlockInLoop` function checks whether any parent loop of the input matches the ϕ’s loop.
133+
134+
![gemm_CFG](./Figures/gemm_CFG.png)
135+
136+
### Creating μ Gates
137+
138+
A valid μ gate must have exactly two inputs: one from outside the loop and one from inside the loop. The grouping step above ensures we can identify these two roles.
139+
140+
- If either group is empty → the ϕ cannot be a μ.
141+
142+
- If a group has exactly one member → that value becomes the corresponding μ input (loop or initial).
143+
144+
- If a group has multiple members → an intermediate ϕ is created in the block to merge them. This extra ϕ will later be replaced by a γ (or tree of γs) during the ϕ to γ conversion phase.
145+
146+
### Condition of the μ Gate
147+
148+
The μ gate outputs its initial value during the first iteration of the loop. On subsequent iterations, if the loop continues (i.e., the exit condition is false), it selects the loop-generated value. When the loop finally exits, the initial input will be used again if the loop is re-entered.
149+
150+
Therefore, the condition of a μ gate is defined as the **negation of the loop exit condition**.
151+
152+
#### Note:
153+
The `getLoopExitCondition` function computes the overall exit condition by OR-ing the conditions of all loop exiting blocks. This function relies on `getBlockLoopExitCondition`, which computes the exit condition for a single block.
101154
## Convert ϕ Gates into γ Gates

0 commit comments

Comments
 (0)