Skip to content

Commit 09b7a43

Browse files
Mostly intermediate representations and compiler book reviews
1 parent 2f1a48b commit 09b7a43

10 files changed

+682
-45
lines changed

_sources/compiler-books.rst.txt

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,50 @@ I have 3 editions of these.
1515
These books are criticised today because of the excessive focus on lexical analysis and parsing techniques.
1616
While this is true, they do cover various aspects of a compiler backend such as intermediate representations and
1717
optimization techniques including peephole optimization, data flow analysis, register allocation etc.
18-
I found the description of the lattice in a data flow analysis quite accessible.
18+
I found the description of the lattice in a data flow analysis quite accessible.
1919

2020
The 2nd edition adopts a more mathematical presentation style, whereas the earlier editions present
2121
algorithms using pseudo code. I think the 1986 edition is the best.
2222

23+
The dragon books are a bit dated in that newer techniques such as Static Single Assignment or Graph
24+
Coloring Register Allocation etc. are not covered in any detail. I would even say that these books are not
25+
useful if your goal is to work with SSA IR.
26+
2327
For a different take on 2nd edition see `Review of the second addition of the "Dragon Book" <https://gcc.gnu.org/wiki/Review_of_the_second_addition_of_the_Dragon_Book.>`_.
2428

2529
Engineering a Compiler, 2nd Ed. Cooper & Torczon. 2012.
2630
=======================================================
2731
This is a more modern version of the Dragon book. It is less focused on the lexical analysis / parsing
28-
phases, and covers the later phases of a compiler in more detail. Exposition is similar to the Dragon book, i.e. mostly describes
29-
techniques conceptually, with some high level algorithm descriptions, but like the Dragon book, does not
30-
go into detailed descriptions of algorithms.
32+
phases, and covers the later phases of a compiler in more detail. Exposition is similar to the Dragon book, i.e. describes
33+
techniques conceptually, and some algorithms are described in more detail using a form of pseudo code.
34+
35+
Defines an intermediate language called ILOC, but this IR does not have support for function calls.
36+
37+
In practice, I found this book helpful when implementing the Dominator algorithm and SSA transformation. However, it left out
38+
important parts in its coverage of SSA which meant that the algorithms as described do not work. For instance:
39+
40+
* The SSA construction algorithm inserts Phis in blocks where the original variable is dead (semi-pruned SSA). This then
41+
causes the renaming phase to fail as there is no available definition of the variable.
42+
* Liveness analysis does not cover SSA form and does not handle phis correctly.
43+
* Exiting out of SSA is described conceptually but the algorithms are not described in detail.
44+
45+
In practice though it is easy to recommend Engineering a Compiler over the Dragon books.
3146

3247
Both this and the Dragon books describe ahead of time compilers and cover topics that are suited for procedural languages
3348
such as C or traditional Pascal or Fortran. They cover both front-end and back-end techniques; however, on the front-end
34-
side, interesting topics such as Object Orientation, Closures, Generics,
35-
or Semantic analysis of more complex languages such as Java are not covered.
49+
side, interesting topics such as Object Orientation, Closures, Generics, Semantic analysis of more complex languages
50+
such as Java are not covered.
3651

3752
Modern Compiler Implementation in C. Appel. 1998. (Tiger book)
3853
==============================================================
3954
This book takes a hands on tutorial like approach to describing how to implement both the front-end and back-end
4055
of a compiler, using a toy language called Tiger as an example. Algorithms are described in pseudo code.
41-
If I had to choose between the Dragon book, Engineering a compiler, and this book, I would pick this one.
56+
If I had to choose from the Dragon book, Engineering a compiler, and this book, I would pick this one and
57+
Engineering a Compiler.
58+
59+
It covers a lot of techniques, and usually presents algorithms in pseudo code form. I consulted this book
60+
when implementing SSA and SCCP, but the descriptions were not sufficiently comprehensive so that I had to
61+
consult other material too.
4262

4363
This book covers functional languages, closures, as well as Object Oriented languages such as Java. Type inference is
4464
covered too.
@@ -47,49 +67,69 @@ Crafting a Compiler. Fischer, LeBlanc, Cytron. 2010.
4767
====================================================
4868
The last couple of chapters are the most interesting - these focus on code generation and program optimization.
4969

50-
The 2nd edition of the book (with Cytron as co author) has a description of Static Single assignment that is
51-
perhaps the most complete in all the books I cover here. The 1st edition describes data flow analysis in more
52-
detail.
70+
The 2nd edition of the book (with Cytron as co author) has a description of Static Single assignment. However the
71+
description is based on a statement level IR, rather than one that uses Basic Blocks. Also, the algorithm for exiting
72+
SSA is not described.
73+
74+
The 1st edition describes data flow analysis in more detail, but does not cover SSA.
5375

5476
Apart from the final two chapters, the rest of the book is about parsing and semantic analysis.
5577

5678
Building an Optimizing Compiler. Bob Morgan. 1998.
5779
==================================================
5880
I have the kindle edition which is very poor and hard to read. I wish I had a paper copy.
5981

60-
This book is almost completely about the backend of the compiler.
82+
This book is almost completely about the backend of the compiler. I consulted the description of SCCP and
83+
based my implementation at least in part on the descriptions. In particular I found some discussion about how to
84+
exploit local knowledge in conditional branches to handle null checks, which was useful, and not discussed in
85+
other books.
6186

6287
Advanced Compiler Design & Implementation. Muchnick. 1997.
6388
==========================================================
64-
I have the kindle edition, which is very poor and hard to read.
89+
I have the kindle edition, which is very poor quality and hard to read.
6590

66-
This book is also mostly about the backend of a compiler, focusing on optimization.
91+
This book is mostly about the backend of a compiler, focusing on optimization.
6792

6893
My impression is that this book describes many algorithms in detail. But when I tried to implement one of the
6994
simpler algorithms (18.1 Unreachable Code Elimination) I found that the description left out a
70-
part (No_Path) of the algorithm.
95+
part (No_Path) of the algorithm.
7196

72-
This book describes the idea of multiple levels of intermediate representation, HIR, MIR and LIR.
97+
Introduces the idea of multiple levels of intermediate representation, HIR, MIR and LIR.
7398
I guess this has influenced many compiler implementations.
7499

75-
Its coverage of SSA is rudimentary - I guess it was written when SSA was still very new.
100+
Its coverage of SSA is rudimentary - I guess it was written when SSA was still very new. Hence if you are
101+
working with SSA IR then you will need to consult other material.
102+
103+
The Graph Coloring register allocation algorithm is presented in detail and is based on the paper by
104+
Preston Briggs.
76105

77106
This book has a reputation of containing many errors, although I assume the latest printings have the errors
78-
fixed.
107+
fixed.
79108

80109
Despite its faults, it is a must have book if you want to learn about compiler construction.
81110

82111
Retargetable C Compiler, A: Design and Implementation. Hanson & Fraser. 1995.
83112
=============================================================================
84-
Describes a production C compiler. Detailed dsecription of the actual compiler code.
113+
Describes a production C compiler. Contains detailed walkthrough of the actual compiler code.
85114

86-
Weak on theoretical aspects, and limited by features of the compiler being described.
115+
Weak on theoretical aspects, and limited by features of the compiler being described. The compiler
116+
implementation is a single pass code generator, hence its optimizing capabilities are limited.
117+
There is no coverage of data flow analysis or SSA as these weren't used by the implementation.
118+
119+
In short this describes an old school C compiler that generates code fast, but lacks optimizations.
87120

88121
Program Flow Analysis: Theory and Applications. Editors Muchnick, Jones. 1981.
89122
==============================================================================
90123
Collection of essays on program analysis, by various authors. This is pre-SSA, hence a bit
91124
dated.
92125

126+
SSA-Based Compiler Design - various authors
127+
===========================================
128+
An online version of this book is available `here <https://pfalcon.github.io/ssabook/latest/book-full.pdf>`_.
129+
This book is a collection of articles on various topics related to SSA. As such it presents more
130+
recent knowledge regarding SSA construction, optimizations based on SSA, and finally destruction and
131+
register allocation. I will have more to say about this book as I use it.
132+
93133
Other Book Reviews
94134
==================
95135
* `List of compiler books <https://gcc.gnu.org/wiki/ListOfCompilerBooks>`_

_sources/index.rst.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ Basic Front-End techniques
6161
Basic Back-end techniques
6262
=========================
6363

64-
* Stack based vs register based Intermediate Representation
65-
* Control flow graphs and Basic Blocks
66-
* Bytecode VM with simple garbage collection
64+
.. toctree::
65+
:maxdepth: 1
66+
:caption: Backend Basics
67+
68+
intermediate-representations
6769

6870
Basic Optimization techniques
6971
=============================
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
============================
2+
Intermediate Representations
3+
============================
4+
5+
An input program in the source language may go through many intermediate representations within
6+
a compiler before it is in a form ready for execution.
7+
8+
One of the first such intermediate representations that we have seen is the
9+
the Abstract Syntax Tree (AST), which is mainly concerned with the grammar of the source language.
10+
11+
From the AST, we generate a different kind of intermediate representation, one that is more amenable
12+
to the manipulations required during optimization and execution. There are many such representations; we will
13+
limit ourselves to the following.
14+
15+
* Stack based IR
16+
* Register based IR
17+
* Sea of Nodes IR
18+
19+
Stack-Based IR
20+
==============
21+
22+
The stack based IR encodes stack operations as part of the intermediate representation. Lets look at a simple
23+
example::
24+
25+
func foo(n: Int)->Int {
26+
return n+1;
27+
}
28+
29+
Produces::
30+
31+
L0:
32+
load 0
33+
pushi 1
34+
addi
35+
jump L1
36+
L1:
37+
38+
The stack based IR is so called because many of the intructions in the IR push and pop values to/from an evaluation stack at
39+
runtime. Above for example, we have the following instructions:
40+
41+
* ``load 0`` - this pushes the value of the input parameter ``n`` to the stack. The ``0`` here identifies the location of the variable ``n``.
42+
* ``pushi 1`` - pushes the constant ``1`` to the stack.
43+
* ``addi`` - pops the two topmost values on the stack, and computes the sum and pushes this to the stack
44+
45+
So at the end of the program we are left with the sum of ``n+1`` on the stack, and this forms the return
46+
value of the function.
47+
48+
In this IR, control flow can be represented either using labels and branching instructions, or by grouping
49+
instructions into basic blocks, and linking basic blocks through jump instructions. These two approaches are
50+
equivalent, you can think of a label as indicating the start of a basic block, and a jump as ending
51+
a basic block.
52+
53+
The idea is that inside a basic block, instructions executed linearly one after the other.
54+
Each basic block ends with a branching instruction, something like a goto or a conditional jump.
55+
56+
Here is a simple example of input source code and the IR you might see::
57+
58+
func foo()->Int
59+
{
60+
return 1 == 1 && 2 == 2
61+
}
62+
63+
This results in IR that may look like this::
64+
65+
L0:
66+
pushi 1
67+
pushi 1
68+
eq
69+
cbr L2 L3
70+
L2:
71+
pushi 2
72+
pushi 2
73+
eq
74+
jump L4
75+
L3:
76+
pushi 0
77+
jump L4
78+
L4:
79+
jump L1
80+
L1:
81+
82+
Each basic block begins with a label, which is just the unique name of the block.
83+
84+
* The ``jump`` instruction transfers control from a basic block to another.
85+
* The ``cbr`` instruction is the conditional branch. It consumes the top most value from the stack,
86+
and if this value is true, then control is transferred to the first block, else to the second block.
87+
* The ``eq`` instruction pops the topmost two values from the stack, and replaces them with integer value
88+
``1`` or ``0``.
89+
90+
Advantages
91+
----------
92+
* The IR is compact to represent in stored form as most instructions do not take have operands.
93+
This is a reason why many languages choose to encode their compiled code in
94+
this form. Examples are Java, C#, Web Assembly.
95+
* The IR can be executed easily by an Interpreter.
96+
* Relatively easy to generate IR from an AST.
97+
98+
Disadvantages
99+
-------------
100+
* Not easy to implement optimizations.
101+
* Harder to analyze the IR, although there are methods available to do so.
102+
103+
Examples
104+
--------
105+
* `Example implementation in EeZee Programming Language <https://github.com/CompilerProgramming/ez-lang/tree/main/stackvm>`_.
106+
* `Java Specifications <https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-6.html>`_.
107+
* `Web Assembly Specifications <https://webassembly.github.io/spec/core/syntax/instructions.html>`_.
108+
109+
Register Based IR or Three-Address IR
110+
=====================================
111+
112+
This intermediate representation uses named slots called virtual registers in the instruction when referencing
113+
values. Lets look at the same example we saw above::
114+
115+
func foo(n: Int)->Int {
116+
return n+1;
117+
}
118+
119+
Produces::
120+
121+
L0:
122+
%t1 = n+1
123+
ret %t1
124+
goto L1
125+
L1:
126+
127+
The instructions above are as follows:
128+
129+
* ``%t1 = n+1`` - is a typical three-address instruction of the form ``result = value1 operator value2``. The name ``%t1``
130+
refers to a temporary, whereas ``n`` refers to the input argument ``n``.
131+
* ``ret %t1`` - is the return instruction, in this instance it references the temporary.
132+
133+
The virtual registers in the IR are so called because they do not map to real registers in the target physical machine.
134+
Instead these are just named slots in the abstract machine responsible for executing the IR. Typically, the abstract machine
135+
will assign each virtual register a unique location in its stack frame. So we still end up using the function's
136+
stack frame, but the IR references locations within the stack frame via these virtual names, rather than implicitly
137+
through push and pop instructions. During optimization some of the virtual registers will end up in real hardware registers.
138+
139+
Control flow is represented the same way as for the stack IR. Revisiting the same source example from above, we get following
140+
IR::
141+
142+
L0:
143+
%t0 = 1==1
144+
if %t0 goto L2 else goto L3
145+
L2:
146+
%t0 = 2==2
147+
goto L4
148+
L3:
149+
%t0 = 0
150+
goto L4
151+
L4:
152+
ret %t0
153+
goto L1
154+
L1:
155+
156+
157+
Advantages
158+
----------
159+
* Readability: the flow of values is easier to trace, whereas with a stack IR you need to conceptualize a stack somewhere,
160+
and track values being pushed and popped.
161+
* The IR can be executed easily by an Interpreter.
162+
* Most optimization algorithms can be applied to this form of IR.
163+
* The IR can represent Static Single Assignment (SSA) in a natural way.
164+
165+
Disadvantages
166+
-------------
167+
* Each instruction has operands, hence representing the IR in serialized form takes more space.
168+
* Harder to generate the IR during compilation.
169+
170+
Examples
171+
--------
172+
* `Example basic register IR in EeZee Programming Language <https://github.com/CompilerProgramming/ez-lang/tree/main/registervm>`_.
173+
* `Example register IR including SSA form and optimizations in EeZee Programming Language <https://github.com/CompilerProgramming/ez-lang/tree/main/optvm>`_.
174+
* `LLVM instruction set <https://llvm.org/docs/LangRef.html#instruction-reference>`_.
175+
* `Android Dalvik IR <https://source.android.com/docs/core/runtime/dex-format>`_.
176+
177+
Sea of Nodes IR
178+
===============
179+
The final example we will look at is known as the Sea of Nodes IR.
180+
181+
It is quite different from the IRs we described above.
182+
183+
The key features of this IR are:
184+
185+
* Instructions are NOT organized into Basic Blocks - instead, intructions form a graph, where
186+
each instruction has as its inputs the definitions it uses.
187+
* Instructions that produce data values are not directly bound to a Basic Block, instead they "float" around,
188+
the order being defined purely in terms of the dependencies between the instructions.
189+
* Control flow is also represented in the same way, and control flows between control flow
190+
instructions. Dependencies between data instructions and control intructions occur at few well
191+
defined places.
192+
* The IR as described above cannot be readily executed, because to execute the IR, the instructions
193+
must be scheduled; you can think of this as a process that puts the instructions into a traditional
194+
Basic Block IR as described earlier.
195+
196+
Describing Sea of Nodes IR is quite involved. For now, I direct you to the `Simple project <https://github.com/SeaOfNodes/Simple/tree/main>`_; this
197+
is an ongoing effort to explain the Sea of Nodes IR representation and how to implement it.
198+
199+
Beyond how the IR is represented, the main benefits of the Sea of Nodes IR are that:
200+
201+
* It is an SSA IR
202+
* Various optimizations such as peephole optimizations, value numbering and common subexpressions elimination,
203+
dead code elimitation, occur as the IR is built.
204+
* This makes the SoN IR suitable for quick optimizations, suitable for Just-In-Time (JIT) compilers.

_sources/learning-resources.rst.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Learning Resources
55
Courses
66
=======
77

8+
COMP 512: Advanced Compiler Construction - Rice University, K. Cooper
9+
---------------------------------------------------------------------
10+
* `COMP 512 Lectures <https://www.clear.rice.edu/comp512/Lectures/>`_. Nice bibliography of important papers related to optimization.
11+
812
CS 6120: Advanced Compilers: The Self-Guided Online Course
913
----------------------------------------------------------
1014

0 commit comments

Comments
 (0)