r/Compilers • u/Majestic-Lack2528 • May 24 '26
Phi to block parameters
Is there a way to convert phi nodes to basic block parameters?
21
Upvotes
r/Compilers • u/Majestic-Lack2528 • May 24 '26
Is there a way to convert phi nodes to basic block parameters?
10
u/josef May 24 '26
Read "SSA is functional programming" by Andrew Appel. He shows that SSA basic blocks can be reformulated a functions where the phi nodes are modelled as function parameters instead.
7
u/SwedishFindecanor May 24 '26 edited May 26 '26
If you view the parameters as laid out in a matrix, the phi-functions are rows and the block parameters are columns. Phi-functions and block parameters are just different ways to reason about the same thing.
<=>
In my compiler, I actually put block/phi parameters into a single "PhiMatrix" data structure, and have each phi-node and jmp-node have a pair of a pointer and an index into it (index = row or column, respectively). I still keep separate nodes for phi-nodes, and also for incoming function parameters and returns from functions because that allows me the convenience of referencing values by their ops.
3
u/ImpactCertain3395 May 24 '26
I’m not entirely sure about what you mean, but one of the MIR pass creates COPY and bb to handle this. Maybe that’s what you need?
1
u/augustty1 May 28 '26
Andrew Appel, son of Kenneth Appel (How proves the four colors thoerem in graph theory), wrote a paper that models the basic blocks of SSA form as functions, Phi nodes are fundamentally equivalent to function parameters.
www.cs.princeton.edu/~appel/papers/ssafun.pdf
Another source I can suggest is the MLIR Rationale, block arguments vs PHI nodes.
https://mlir.llvm.org/docs/Rationale/Rationale/#block-arguments-vs-phi-nodes
For instance, the traditonal LLVM IR format:
The format with block parameters MLIR/SIL:
You can find more about SSA form and phi nodes in this course taught at my university: https://homepages.dcc.ufmg.br/~fernando/classes/dcc888/ementa/
0
u/MasonWheeler May 24 '26
What do you mean by "basic block parameters"? Typically you refer to functions as having parameters, not basic blocks.