3. “Min” Hsu
• Computer Science PhD Candidate in
University of California, Irvine
• Advised by Prof. Michael Franz
about:me
2
4. “Min” Hsu
• Computer Science PhD Candidate in
University of California, Irvine
• Advised by Prof. Michael Franz
• LLVM contributor since 2015
• Code owner of M68k LLVM backend
• Author of book “LLVM Techniques,
Tips and Best Practices” (2021)
about:me
2
5. “Min” Hsu
• Computer Science PhD Candidate in
University of California, Irvine
• Advised by Prof. Michael Franz
• LLVM contributor since 2015
• Code owner of M68k LLVM backend
• Author of book “LLVM Techniques,
Tips and Best Practices” (2021)
• Motorcycle rider and co
ff
ee junkies
about:me
2
45. Instruction selection in LLVM: Running time
LLVM Passes that have the highest running time
21
LLVM Passes
Running Time
0% 10% 20% 30% 40%
DAG->DAG Instruction Selection
Module Veri
fi
er
Live Variable Analysis
Greedy Register Allocator
Live Debug Value Analysis
Prologue / Epilogue Insertion
47. Instruction selection in LLVM: Workflow
22
LLVM IR (Linear) SelectionDAG (DAG)
Target Independent
48. Instruction selection in LLVM: Workflow
22
LLVM IR (Linear) SelectionDAG (DAG) MachineInstr (Linear)
Target Independent Target Speci
fi
c
49. Instruction selection in LLVM: Workflow
22
LLVM IR (Linear) SelectionDAG (DAG) MachineInstr (Linear)
Target Independent Target Speci
fi
c
50. Global Instruction Selection (GlobalISel)
23
LLVM IR (Linear) gMIR (Linear) MachineInstr (Linear)
Target Independent Target Speci
fi
c
51. Global Instruction Selection (GlobalISel)
23
LLVM IR (Linear) gMIR (Linear) MachineInstr (Linear)
Target Independent Target Speci
fi
c
Hybrid
52. Global Instruction Selection (GlobalISel)
24
Credit: Ahmad Bougcha et al. @ LLVM Dev Meeting 2017
• Faster compilation
53. Global Instruction Selection (GlobalISel)
24
Credit: Ahmad Bougcha et al. @ LLVM Dev Meeting 2017
• Faster compilation
• Better codegen
quality
54. Global Instruction Selection (GlobalISel)
24
Credit: Ahmad Bougcha et al. @ LLVM Dev Meeting 2017
• Faster compilation
• Better codegen
quality
• Testing GlobalISel
is much easier.
58. Linker 101
• Linking speed becomes the
bottleneck while building
large-scale projects.
• We barely can parallelize this
process.
26
A.cpp B.cpp C.cpp
Compiler Compiler Compiler
A.o B.o C.o
Linker
Executable
Thread Thread Thread
59. LLD: A (much) faster linker
• Gold was the fastest linker, but
now LLD is (mostly) faster than
gold.
27
Credit: Rui Ueyama @ LLVM Dev Meeting 2017
60. LLD: A (much) faster linker
• Gold was the fastest linker, but
now LLD is (mostly) faster than
gold.
• Secret sauce
27
Credit: Rui Ueyama @ LLVM Dev Meeting 2017
61. LLD: A (much) faster linker
• Gold was the fastest linker, but
now LLD is (mostly) faster than
gold.
• Secret sauce
• Laziness
27
Credit: Rui Ueyama @ LLVM Dev Meeting 2017
62. LLD: A (much) faster linker
• Gold was the fastest linker, but
now LLD is (mostly) faster than
gold.
• Secret sauce
• Laziness
• Certain level of parallelization
• Use
fl
ag -fuse-ld=lld on
GCC and Clang to adopt LLD.
27
Credit: Rui Ueyama @ LLVM Dev Meeting 2017
65. WMO / LTO
29
A.cpp B.cpp C.cpp
Compiler Compiler Compiler
A.ir B.ir C.ir
Linker
Executable
Whole Module / Link Time Optimization
• Perform more aggressive
whole program optimization
during link time.
• Because we can
fi
nally see
the entire program.
66. WMO / LTO
29
A.cpp B.cpp C.cpp
Compiler Compiler Compiler
A.ir B.ir C.ir
Linker
Executable
Whole Module / Link Time Optimization
• Perform more aggressive
whole program optimization
during link time.
• Because we can
fi
nally see
the entire program.
• (Traditionally) It’s still hard to
parallelize WMO / LTO.
Thread Thread Thread
90. Code review with community members
40
Code Review
New Contributor
91. Code review with community members
40
Code Review
New Contributor
?
92. Code review with community members
41
Code Review
New Contributor
?
Why?
93. Code review with community members
41
Code Review
New Contributor
?
• New member does not have
enough “review currency” yet.
Why?
94. Code review with community members
41
Code Review
New Contributor
?
• New member does not have
enough “review currency” yet.
• Patches are less visible.
Why?
95. Code review with community members
41
Code Review
New Contributor
?
• New member does not have
enough “review currency” yet.
• Patches are less visible.
• Most of the core contributors are
busy.
Why?
96. Code review with community members
41
Code Review
New Contributor
?
• New member does not have
enough “review currency” yet.
• Patches are less visible.
• Most of the core contributors are
busy.
• Nearly all of them are working on
LLVM as their day jobs!
Why?
97. Code review with community members
41
Code Review
New Contributor
?
• New member does not have
enough “review currency” yet.
• Patches are less visible.
• Most of the core contributors are
busy.
• Nearly all of them are working on
LLVM as their day jobs!
• Code owners are obligate to review
the patch…but they’re also busy.
Why?
110. What do you think?
50
• What’s your story?
• Project-oriented tutorial
v.s.
A broad overview
• Online forum / mailing list
experiences (for newbie questions)?
• Wiki(-style) book?
113. API stability in a nutshell
53
lib.h
int foo(int x, int y);
MySrc.cpp
int main() {
return foo(94, 87);
}
Version 0.1
114. API stability in a nutshell
54
lib.h
int foo(Point point);
MySrc.cpp
int main() {
return foo(94, 87);
}
Version 0.5
115. API stability in a nutshell
54
lib.h
int foo(Point point);
MySrc.cpp
int main() {
return foo(94, 87);
}
Version 0.5
Compile Error
116. LLVM API stability for downstream users
Some common LLVM downstream use cases
55
117. LLVM API stability for downstream users
Some common LLVM downstream use cases
55
My C++ auto-complete plugin
libclangSema libclangParse
118. LLVM API stability for downstream users
Some common LLVM downstream use cases
55
My C++ auto-complete plugin
libclangSema libclangParse
My Toy compiler
LLVM IR
Optimizer
LLVM X86
Backend
119. LLVM API stability for downstream users
56
LLVM
C++ API
Stability
Policy
120. LLVM API stability for downstream users
56
LLVM
C++ API
Stability
Policy
NONE
121. LLVM API stability for downstream users
56
LLVM
C++ API
Stability
Policy
NONE
• A pain in the a** for downstream users.
• Behaviors of APIs might also
change!
122. LLVM API stability for downstream users
56
LLVM
C++ API
Stability
Policy
NONE
• A pain in the a** for downstream users.
• Behaviors of APIs might also
change!
• The C API is (mostly) stable…
• …But the provided features are lag
(way) behind.
123. LLVM API stability for downstream users
56
LLVM
C++ API
Stability
Policy
NONE
• A pain in the a** for downstream users.
• Behaviors of APIs might also
change!
• The C API is (mostly) stable…
• …But the provided features are lag
(way) behind.
• A design decision made since day 1.
• Trade stability for faster feature
delivery.
124. ABI stability in a nutshell
57
MyExecutable
int foo(Point point);
int main() {
return foo(Point{94, 87});
}
libFoo.so
Version 0.1
125. ABI stability in a nutshell
57
MyExecutable
int foo(Point point);
int main() {
return foo(Point{94, 87});
}
libFoo.so
Version 0.1
Shared Library
126. ABI stability in a nutshell
57
MyExecutable
int foo(Point point);
int main() {
return foo(Point{94, 87});
}
libFoo.so
Version 0.1
Shared Library
Plugin
127. ABI stability in a nutshell
58
MyExecutable
int foo(Point point);
int main() {
return foo(Point{94, 87});
}
libFoo.so
Version 0.5
?
Shared Library
Plugin
128. ABI stability in a nutshell
Name mangling
59
MyExecutable
int foo(Point point); libFoo.so
Version 0.5
?
Old: _Z3foo5Point
129. ABI stability in a nutshell
Name mangling
59
MyExecutable
int foo(Point point); libFoo.so
Version 0.5
?
Old: _Z3foo5Point
New: ?foo@@YAHUPoint@@@Z
130. ABI stability in a nutshell
Type layout
60
MyExecutable
int foo(Point point) {
return point.X + point.Y;
}
libFoo.so
Version 0.5
?
struct Point {
int X;
int Y;
};
Old
131. ABI stability in a nutshell
Type layout
60
MyExecutable
int foo(Point point) {
return point.X + point.Y;
}
libFoo.so
Version 0.5
?
struct Point {
int X;
int Y;
};
Old
struct Point {
int X;
int LMAO;
int Y;
};
New
132. LLVM ABI stability for plugins
All available plugin options in Clang & LLVM
61
Clang LLVM
Preprocessor: Pragma plugin
Sema: Attribute plugin
AST: AST plugin
LLVM IR: Pass plugin (old / new)
133. LLVM ABI stability for plugins
If LLVM Pass plugin has a stable ABI…
62
opt -my-pass
LLVM version X.Y
MyPassPlugin.so
134. LLVM ABI stability for plugins
If LLVM Pass plugin has a stable ABI…
62
opt -my-pass
LLVM version X.Y
MyPassPlugin.so
135. LLVM ABI stability for plugins
If LLVM Pass plugin has a stable ABI…
62
opt -my-pass
LLVM version X.Y
opt -my-pass
LLVM version Y.Z
MyPassPlugin.so
136. LLVM ABI stability for plugins
63
LLVM Plugin
LLVM Plugin
“You have stable
ABI, right?”
“…right?…”
137. LLVM ABI stability for plugins
63
LLVM Plugin
LLVM Plugin
“You have stable
ABI, right?”
“…right?…”
• Again, trading stability for
fl
exibility
• LLVM plugins are pretty powerful.
138. LLVM ABI stability for plugins
63
LLVM Plugin
LLVM Plugin
“You have stable
ABI, right?”
“…right?…”
• Again, trading stability for
fl
exibility
• LLVM plugins are pretty powerful.
• Real world use case: Chromium
139. LLVM ABI stability for plugins
63
LLVM Plugin
LLVM Plugin
“You have stable
ABI, right?”
“…right?…”
• Again, trading stability for
fl
exibility
• LLVM plugins are pretty powerful.
• Real world use case: Chromium
• Using custom clang plugin to enforce
coding styles inside Chromium’s code
base.
• E.g. Catching bad C++ dtor
140. LLVM ABI stability for plugins
63
LLVM Plugin
LLVM Plugin
“You have stable
ABI, right?”
“…right?…”
• Again, trading stability for
fl
exibility
• LLVM plugins are pretty powerful.
• Real world use case: Chromium
• Using custom clang plugin to enforce
coding styles inside Chromium’s code
base.
• E.g. Catching bad C++ dtor
• “Don’t write a clang plugin” @ Chromium
Docs - https://bit.ly/3rIUg0f
172. 77
Frontend
Optimizer
Backend
Compiler + Assembler Process
Assembler
Linker Process
Linker
?
• An assembly
fi
le always comes
from a single source
fi
le
• Avoid round-trip (i.e. read/write
assembly
fi
le) to disk
173. 78
Frontend
Optimizer
Backend
A clang that uses integrated assembler
Assembler
Clang’s integrated assembler
In-memory LLVM IR
In-memory LLVM IR
Textual assembly code
In-memory MCInst objects