The original talk was presented in TGSA (http://clubs.uci.edu/tgsa/) Academic Friday, December 6th 2020.
Most of the contents are written in English. Except some Chinese memes.
41. (Some) Sources of Complexities
• Translation
• How do I translate source code into machine binary?
42. (Some) Sources of Complexities
• Translation
• How do I translate source code into machine binary?
• Optimization
• How do I generate machine binaries that run fast?
43. (Some) Sources of Complexities
• Translation
• How do I translate source code into machine binary?
• Optimization
• How do I generate machine binaries that run fast?
• Portability
• How do I support N different programming languages and M
different machines in the same compiler?
44. (Some) Sources of Complexities
• Translation
• How do I translate source code into machine binary?
• Optimization
• How do I generate machine binaries that run fast?
• Portability
• How do I support N different programming languages and M
different machines in the same compiler?
46. Why We Need Compiler Optimizations?
int foo(int x) {
return x + x + x + x +…(1000 times);
}
Source Code
47. Why We Need Compiler Optimizations?
int foo(int x) {
return x + x + x + x +…(1000 times);
}
Source Code
r0 = add x, x
r1 = add r0, x
r2 = add r1, x
r3 = add r2, x
…
result = add r997, x
“Naive” Machine Code
48. Why We Need Compiler Optimizations?
int foo(int x) {
return x + x + x + x +…(1000 times);
}
Source Code
r0 = add x, x
r1 = add r0, x
r2 = add r1, x
r3 = add r2, x
…
result = add r997, x
“Naive” Machine Code
result = mul x, 1000
Optimal Machine Code
49. Why We Need Compiler Optimizations?
int foo(int x) {
return x + x + x + x +…(1000 times);
}
Source Code
r0 = add x, x
r1 = add r0, x
r2 = add r1, x
r3 = add r2, x
…
result = add r997, x
“Naive” Machine Code
result = mul x, 1000
Optimal Machine Code
“IMPOSSIBLE for programmers to write these!”
50. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
51. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
a = 10 * 1000 = 10000
52. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
a = 10 * 1000 = 10000
b = 5 * 1000 = 5000
53. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
a = 10 * 1000 = 10000
b = 5 * 1000 = 5000
a + b = 10000 + 5000 = 15000
54. Why We Need Compiler Optimizations?
int bar() {
return 15000;
}
Expected
55. Why We Need Compiler Optimizations?
int bar() {
return 15000;
}
Expected
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
Compiler w/o
Optimization
56. Why We Need Compiler Optimizations?
int bar() {
return 15000;
}
Expected
int foo(int x) {
return x * 1000;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
Compiler w/o
Optimization
“IMPOSSIBLE for programmers to write these!”
57. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r > 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
58. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r > 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
………
59. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
if(n % 4 != 0) return 0;
return bar() * (n / 4);
}
61. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers.
62. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers. “IMPOSSIBLE!”
63. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers.
• Not every code can be “optimized” by human.
“IMPOSSIBLE!”
64. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r >= 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
65. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r >= 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
Another Number
66. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r >= 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
Another Number
Another Number
67. Why We Need Compiler Optimizations?
int foo(int x) {
return x * 1000;
}
int bar() {
return 15000;
}
int zoo(int n) {
int sum = 0;
if(n % 4 != 0) return 0;
for(int i=0; i < n; i++) {
int e = 1;
for(int r = i % 4; r >= 0; r-=1) {
e *= 2;
}
sum += foo(e);
}
return sum;
}
Another Number
Another Number
?
68. Why We Need Compiler Optimizations?
“IMPOSSIBLE!”
69. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers.
• Not every code can be “optimized” by human!
• Programmers can focus on program logics.
“IMPOSSIBLE!”
70. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers.
• Not every code can be “optimized” by human!
• Programmers can focus on program logics.
• Without putting too many attentions on optimizing code.
“IMPOSSIBLE!”
71. Why We Need Compiler Optimizations?
• Fix stupid code written by programmers.
• Not every code can be “optimized” by human!
• Programmers can focus on program logics.
• Without putting too many attentions on optimizing code.
• Compilers will optimize code for you if applicable.
“IMPOSSIBLE!”
73. Compiler Optimization and More
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
74. Compiler Optimization and More
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
75. Compiler Optimization and More
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
B: Simplify function calls
76. Compiler Optimization and More
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
78. Optimization Order Matters!
int bar() {
return 15000;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
79. Optimization Order Matters!
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
80. Optimization Order Matters!
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
What are the values?
82. Optimization Order Matters!
int bar() {
int a = 0;
for(int I = 0; I < 1000; I++) {
a += 10;
}
int b = 0;
for(int I = 0; I < 1000; I++) {
b += 5;
}
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
83. Optimization Order Matters!
int bar() {
int a = 0;
for(int I = 0; I < 1000; I++) {
a += 10;
}
int b = 0;
for(int I = 0; I < 1000; I++) {
b += 5;
}
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
84. Optimization Order Matters!
int bar() {
int a = 0;
for(int I = 0; I < 1000; I++) {
a += 10;
}
int b = 0;
for(int I = 0; I < 1000; I++) {
b += 5;
}
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
What are the values?
85. Compiler Optimization and More
int foo(int x) {
int sum = 0;
for(int I = 0; I < 8; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
104. Recap: Compiler Optimizations
int foo(int x) {
int sum = 0;
for(int I = 0; I < 1000; I++) {
sum += x;
}
return sum;
}
int bar() {
int a = foo(10);
int b = foo(5);
return a + b;
}
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
106. Multi-Language and Machine
Optimizer
Working on IR
C/C++ to IR
Go to IR
Swift to IR
Intermediate Representation (IR)
IR to X86
IR to ARM
A: Simplify the loop
B: Simplify function calls
C: Simplify constants
(Only ONE version!)
108. The IR in Previous Page
• “Machine Independent”
• Describe the operations instead of telling How to perform them
in different machines.
109. The IR in Previous Page
• “Machine Independent”
• Describe the operations instead of telling How to perform them
in different machines.
• “IR to X86” and “IR to ARM” .etc would translate IR to specific
machine code.
• This process is usually called lowering.
110. The IR in Previous Page
• “Machine Independent”
• Describe the operations instead of telling How to perform them
in different machines.
• “IR to X86” and “IR to ARM” .etc would translate IR to specific
machine code.
• This process is usually called lowering.
• Developers of optimizations don’t need to worry too much about
the target machine.
121. Multiply Rule for Inverse Matrix
A x A-1 = I
Matrix foo(Matrix A) {
return A * inverse(A);
}
A Code Snippet
122. Case 1: IR Specialized for Certain Machine
proc foo(Matrix $a)-> Matrix {
$b = call inverse($a)
$c = mul $a, $b
return $c
}
123. Case 1: IR Specialized for Certain Machine
proc foo(Matrix $a)-> Matrix {
$b = call inverse($a)
$c = mul $a, $b
return $c
}
The optimizer says:
“A matrix multiplied with inverted version of itself”
“That’s easy: $c can be replaced with identity matrix!”
124. Case 2: General IR
int[][] foo(int[][] %a) {
%b = call inverse(%a)
for(…) {
for(…) {
for(…) {…}
}
}
}
125. Case 2: General IR
int[][] foo(int[][] %a) {
%b = call inverse(%a)
for(…) {
for(…) {
for(…) {…}
}
}
}
Matrix Multiplication
126. Case 2: General IR
int[][] foo(int[][] %a) {
%b = call inverse(%a)
for(…) {
for(…) {
for(…) {…}
}
}
}
The optimizer says:
“WTF”
“This is too difficult to optimize”
Matrix Multiplication
130. Optimizations are specialized
for certain language / machine
High engineering cost
Shared large portion of
optimization code base
among languages and machine.
131. Optimizations are specialized
for certain language / machine
High engineering cost
Shared large portion of
optimization code base
among languages and machine.
IR is usually too high-level.
Hard to describe machine
or language-specific optimizations
137. I HopeYou Learned…
• What is Compiler
• Why we need compiler optimizations
• Developers can focus on program logics without worrying too
much on runtime performance
138. I HopeYou Learned…
• What is Compiler
• Why we need compiler optimizations
• Developers can focus on program logics without worrying too
much on runtime performance
• Some details of optimizations I introduced earlier
139. I HopeYou Learned…
• What is Compiler
• Why we need compiler optimizations
• Developers can focus on program logics without worrying too
much on runtime performance
• Some details of optimizations I introduced earlier
• Difficulty of supporting a compiler for different languages and
machines
140. I HopeYou Learned…
• What is Compiler
• Why we need compiler optimizations
• Developers can focus on program logics without worrying too
much on runtime performance
• Some details of optimizations I introduced earlier
• Difficulty of supporting a compiler for different languages and
machines
• Difficulty of compilers to choose a proper abstraction level