Read SSA and control-flow graphs
Read SSA and control-flow graphs
Static single assignment (SSA) is an intermediate form in which each value definition has a unique identity. A control-flow graph connects basic blocks (straight-line sequences of operations) by possible execution edges. When two branches join, a phi value chooses the incoming value according to the predecessor edge. Go 1.23 converts its internal IR to SSA, optimizes it, then lowers it for the target. SSA is not Go source syntax.
package main
import ( "fmt")
//go:noinlinefunc choose(n int) int { v := n + 1 if n < 0 { v = -n } return v * 2}func main() { fmt.Println(choose(-3), choose(4))}Save as main.go in an empty scratch directory and run GOSSAFUNC=choose go build -o demo main.go. Open ssa.html locally. The initial form can represent the branch join with a phi; later passes may simplify the graph or use a conditional move. In a teaching sketch, entry defines v1=n+1, the negative block defines v2=-n, and a join selects phi(v1,v2). The names in the actual dump differ. Read incoming edges together with values; phi is not a function executed by Go source.
Expected output:
6 10Loop-carried values
package main
import ( "fmt")
//go:noinlinefunc total(n int) int { s := 0 for i := 0; i < n; i++ { s += i } return s}func main() { fmt.Println(total(0), total(5))}A loop header may use phi values for i and s, selecting initial zero on entry or updated values on the back edge. Run the same command with GOSSAFUNC=total to compare initial and final phases. Compiler dumps can be large and contain source paths: retain them in a scratch directory, not version control. Neither block numbering nor pass ordering is a stable API.
Expected output:
0 10Try it locally
Implement clamp for integers to range 0..10 using two branches. Print results for -2, 4 and 20, then inspect the SSA dump with GOSSAFUNC=clamp. Describe which control-flow paths return each result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compiler pipeline 04: Read SSA and control-flow graphs
Check the lesson objective before moving on.
What does a phi value express?
Why inspect multiple SSA phases?