mirror of
https://github.com/Noratrieb/website.git
synced 2026-01-14 17:05:02 +01:00
parent
84f98bb1ff
commit
0262ef8e42
4 changed files with 517 additions and 0 deletions
424
slides/2025-10-10-how-rust-compiles/index.html
Normal file
424
slides/2025-10-10-how-rust-compiles/index.html
Normal file
|
|
@ -0,0 +1,424 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
|
||||
/>
|
||||
|
||||
<title>how rust compiles</title>
|
||||
|
||||
<link rel="stylesheet" href="../dist/reset.css" />
|
||||
<link rel="stylesheet" href="../dist/reveal.css" />
|
||||
<link rel="stylesheet" href="../dist/theme/black.css" />
|
||||
|
||||
<!-- Theme used for syntax highlighted code -->
|
||||
<link rel="stylesheet" href="../plugin/highlight/monokai.css" />
|
||||
<style>
|
||||
* {
|
||||
--r-heading-text-transform: initial;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="reveal">
|
||||
<div class="slides">
|
||||
<section>
|
||||
<h2>how rust compiles</h2>
|
||||
</section>
|
||||
<section style="height: 100%">
|
||||
<div style="display: flex; align-items: flex-start; height: 100%">
|
||||
<details>
|
||||
<summary>the rust compilation model has surprising effects</summary>
|
||||
<iframe
|
||||
height="600"
|
||||
width="800"
|
||||
src="https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=aa4567f7a97dd255dc4f314ae2b5d63c"
|
||||
referrerpolicy="no-referrer"
|
||||
></iframe>
|
||||
</details>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>introduction to me</h2>
|
||||
<div style="display: flex">
|
||||
<div>
|
||||
<div>Noratrieb (she/her)</div>
|
||||
contributing to the compiler since 2021
|
||||
<br />
|
||||
</div>
|
||||
<div>
|
||||
<img src="me.png" height="300" />
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>behind cargo build</h2>
|
||||
<p>cargo vs rustc</p>
|
||||
<div style="display: grid; grid-template-columns: 2fr 1fr 2fr">
|
||||
<pre><code data-trim>
|
||||
$ cargo build
|
||||
</code></pre>
|
||||
<div style="display: flex; align-items: center">cargo</div>
|
||||
<div style="display: flex; flex-direction: column; align-items: flex-start">
|
||||
<div>rustc (clap)</div>
|
||||
<div>rustc (tokio)</div>
|
||||
<div>rustc (tracing)</div>
|
||||
<div>rustc (yourcrate)</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>so you want to compile a crate</h2>
|
||||
</section>
|
||||
<section>
|
||||
<h2>so you want to compile a crate</h2>
|
||||
<p>source code:</p>
|
||||
<pre><code data-trim>
|
||||
#[no_mangle]
|
||||
pub fn add(a: u8, b: u8) -> u8 {
|
||||
a.wrapping_add(b)
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
<section>
|
||||
<h2>so you want to compile a crate</h2>
|
||||
<p>MIR</p>
|
||||
<img src="add-runtime-mir.svg" />
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## so you want to compile a crate
|
||||
LLVM IR
|
||||
|
||||
```
|
||||
; meow::add
|
||||
define noundef i8 @add(i8 noundef %a, i8 noundef %b) unnamed_addr #0 {
|
||||
start:
|
||||
%_0 = add i8 %b, %a
|
||||
ret i8 %_0
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## so you want to compile a crate
|
||||
Assembly
|
||||
|
||||
```
|
||||
<add>:
|
||||
lea (%rsi,%rdi,1),%eax
|
||||
ret
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section>
|
||||
<h2>codegen units</h2>
|
||||
<pre><code>
|
||||
fn main() {}
|
||||
</code></pre>
|
||||
<div class="mermaid">
|
||||
<pre>
|
||||
%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true, 'fontSize': '25px' }}}%%
|
||||
flowchart LR
|
||||
mainmir["main (MIR)"]
|
||||
|
||||
subgraph mycgu1[my CGU 1]
|
||||
mainll["main (LLVM IR)"]
|
||||
end
|
||||
|
||||
mycgu1 --> mycgu1.rcgu.o
|
||||
|
||||
mainmir --> mainll
|
||||
|
||||
mycgu1.rcgu.o --> my_binary
|
||||
std["std (and others)"] --> my_binary
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>codegen units (but more)</h2>
|
||||
<pre><code>
|
||||
fn main() {}
|
||||
fn foo1() {}
|
||||
fn foo2() {}
|
||||
</code></pre>
|
||||
<div class="mermaid">
|
||||
<pre>
|
||||
%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true, 'fontSize': '25px' }}}%%
|
||||
flowchart LR
|
||||
mainmir["main (MIR)"]
|
||||
foo1mir["foo1 (MIR)"]
|
||||
foo2mir["foo2 (MIR)"]
|
||||
|
||||
subgraph mycgu1[my CGU 1]
|
||||
mainll["main (LLVM IR)"]
|
||||
end
|
||||
subgraph mycgu2[my CGU 2]
|
||||
foo1ll["foo1 (LLVM IR)"]
|
||||
foo2ll["foo2 (LLVM IR)"]
|
||||
end
|
||||
|
||||
mycgu1 --> mycgu1.rcgu.o
|
||||
mycgu2 --> mycgu2.rcgu.o
|
||||
|
||||
mainmir --> mainll
|
||||
foo1mir --> foo1ll
|
||||
foo2mir --> foo2ll
|
||||
|
||||
mycgu1.rcgu.o --> my_binary
|
||||
mycgu2.rcgu.o --> my_binary
|
||||
std["std (and others)"] --> my_binary
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>codegen units (cross-crate)</h2>
|
||||
<div style="display: flex; flex-direction: row; gap: 16px">
|
||||
<pre><code>
|
||||
fn add() {}
|
||||
</code></pre>
|
||||
<pre><code>
|
||||
fn main() { math::add() }
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="mermaid">
|
||||
<pre>
|
||||
%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true, 'fontSize': '25px' }}}%%
|
||||
flowchart LR
|
||||
subgraph crate math
|
||||
addmir["add (MIR)"]
|
||||
|
||||
subgraph mathcgu1[math CGU 1]
|
||||
addll["add (LLVM IR)"]
|
||||
end
|
||||
|
||||
addmir --> addll
|
||||
|
||||
mathcgu1 --> mathcgu1.rcgu.o
|
||||
|
||||
mathcgu1.rcgu.o --> libmath.rlib
|
||||
end
|
||||
|
||||
subgraph my crate
|
||||
mainmir["main (MIR)"]
|
||||
|
||||
subgraph mycgu1[my CGU 1]
|
||||
mainll["main (LLVM IR)"]
|
||||
end
|
||||
|
||||
mycgu1 --> mycgu1.rcgu.o
|
||||
|
||||
mainmir --> mainll
|
||||
|
||||
mycgu1.rcgu.o --> my_binary
|
||||
libmath.rlib --> my_binary
|
||||
std["std (and others)"] --> my_binary
|
||||
end
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## generics
|
||||
|
||||
```rust
|
||||
fn add<T: Add>(a: T, b: T) -> T::Output {
|
||||
a + b
|
||||
}
|
||||
|
||||
add(0_u16, 0_u16); // creates add<u16> function
|
||||
add(0_u32, 0_u32); // creates add<u32> function
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section>
|
||||
<h2>instantiating generics</h2>
|
||||
<pre><code data-trim class="language-rust">
|
||||
fn add<T: Add>(a: T, b: T) -> T::Output { a + b }
|
||||
fn main() {
|
||||
add(0_u16, 0_u16);
|
||||
add(0_u32, 0_u32);
|
||||
}
|
||||
</code></pre>
|
||||
<div class="mermaid">
|
||||
<pre>
|
||||
%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true, 'fontSize': '25px' }}}%%
|
||||
flowchart LR
|
||||
addmir["add (MIR)"]
|
||||
useitmir["main (MIR)"]
|
||||
|
||||
subgraph mycgu1[my CGU 1]
|
||||
addu16ll["add_u16 (LLVM IR)"]
|
||||
addu32ll["add_u32 (LLVM IR)"]
|
||||
useitll["main (LLVM IR)"]
|
||||
end
|
||||
|
||||
mycgu1 --> mycgu1.rcgu.o
|
||||
|
||||
addmir -->|instantiate with T=u16| addu16ll
|
||||
addmir -->|instantiate with T=u32| addu32ll
|
||||
useitmir --> useitll
|
||||
|
||||
mycgu1.rcgu.o --> my_binary
|
||||
std["std (and others)"] --> my_binary
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<h2>generics (cross-crate)</h2>
|
||||
<div style="display: flex; flex-direction: row; gap: 16px">
|
||||
<pre><code data-trim class="language-rust">
|
||||
pub fn add<T: Add>(a: T, b: T) -> T::Output {
|
||||
a + b
|
||||
}
|
||||
</code></pre>
|
||||
<pre><code data-trim class="language-rust">
|
||||
fn main() {
|
||||
add(0_u16, 0_u16);
|
||||
add(0_u32, 0_u32);
|
||||
}
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="mermaid">
|
||||
<pre>
|
||||
%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true, 'fontSize': '25px' }}}%%
|
||||
flowchart LR
|
||||
subgraph crate math
|
||||
addmir["add (MIR)"]
|
||||
end
|
||||
|
||||
subgraph my crate
|
||||
mainmir["main (MIR)"]
|
||||
end
|
||||
|
||||
subgraph my crate
|
||||
subgraph mycgu1[my CGU 1]
|
||||
addu16ll["add_u16 (LLVM IR)"]
|
||||
addu32ll["add_u32 (LLVM IR)"]
|
||||
mainll["main (LLVM IR)"]
|
||||
end
|
||||
|
||||
mainmir --> mainll
|
||||
addmir --> addu16ll
|
||||
addmir --> addu32ll
|
||||
mycgu1 --> mycgu1.rcgu.o
|
||||
mycgu1.rcgu.o --> my_binary
|
||||
std["std (and others)"] --> my_binary
|
||||
end
|
||||
</pre>
|
||||
</div>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## instantiation modes
|
||||
|
||||
the two ways to instantiate MIR:
|
||||
|
||||
- in the definition crate (Globally Shared)
|
||||
- in the using crate (Local Copy)
|
||||
- generic functions
|
||||
- `#[inline]` functions
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## inlining
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 1 + 4;
|
||||
println!("{x}");
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## inlining
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 5;
|
||||
println!("{x}");
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## inlining
|
||||
|
||||
```rust
|
||||
fn add(a: u8, b: u8) -> u8 {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = add(1, 4);
|
||||
println!("{x}");
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## inlining
|
||||
|
||||
```rust
|
||||
fn add(a: u8, b: u8) -> u8 {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = {
|
||||
let a = 1;
|
||||
let b = 4;
|
||||
a + b
|
||||
};
|
||||
println!("{x}");
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
<section data-markdown>
|
||||
<textarea data-template>
|
||||
## inlining
|
||||
|
||||
```rust
|
||||
fn add(a: u8, b: u8) -> u8 {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
println!("{x}");
|
||||
}
|
||||
```
|
||||
</textarea>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="../dist/reveal.js"></script>
|
||||
<script src="../plugin/notes/notes.js"></script>
|
||||
<script src="../plugin/markdown/markdown.js"></script>
|
||||
<script src="../plugin/highlight/highlight.js"></script>
|
||||
|
||||
<script src="./reveal.js-mermaid-plugin_11-6-0.js"></script>
|
||||
|
||||
<script>
|
||||
// More info about initialization & config:
|
||||
// - https://revealjs.com/initialization/
|
||||
// - https://revealjs.com/config/
|
||||
Reveal.initialize({
|
||||
hash: true,
|
||||
|
||||
// Learn about plugins: https://revealjs.com/plugins/
|
||||
plugins: [RevealMarkdown, RevealHighlight, RevealNotes, RevealMermaid],
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue