Decoding the Black Box: Understanding the Go Compiler
FOSSASIA 2024
@imJenal
Jyotsna Gupta
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Type Checking & Semantic Analysis
Generate Intermediate Representation (IR)
Optimize
IR (SSA)
Source
Code
Input
Read
Source
Code File
Lexer: Convert to Tokens
Parser: Create
AST
Generate Machine Code
Link Machine Code with Libraries
Create Executable Files
Executable Ready
@imJenal
@imJenal
Compiler itself provides logging, debugging and visualization capabilities
@imJenal
$ go build -gcflags=-m=2 # print optimization info, including inlining, escape analysis $ go build -gcflags=-W # print internal parse tree after type checking
$ GOSSAFUNC=Foo go build # generate ssa.html file for func Foo $ go build -gcflags=-S # print assembly $ go tool compile -bench=out.txt x.go # print timing of compiler phases
@imJenal
@imJenal
/internal/syntax :
lexer, parser/internal/types2 : type checking
/internal/types
: compiler types/internal/ir
: compiler AST/internal/noder
: create compiler AST/internal/deadcode
: dead code elimination/internal/inline
: function call inlining/internal/devirtualize
: devirtualization of known interface method calls/internal/escape
: escape analysiscmd/compile/internal/walk
(order of evaluation, desugaring)cmd/compile/internal/ssa
(SSA passes and rules)cmd/compile/internal/ssagen
(converting IR to SSA)
cmd/compile
contains the main packages that form the Go compiler
@imJenal