Skip to content

Commit a459452

Browse files
nnethercoteLegNeato
authored andcommitted
More guide improvements.
- Use full names for `rustc_codegen_*` crates rather than shortened `cg_*` forms. - Streamline `rustc_codegen_ssa` description. - Minor grammer, punctuation, capitalization fixes. - Fix formatting of some lists.
1 parent 2df1129 commit a459452

File tree

5 files changed

+33
-34
lines changed

5 files changed

+33
-34
lines changed

guide/src/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ things to gain in terms of safety using Rust.
153153
The reasoning for this is the same reasoning as to why you would use CUDA over opengl/vulkan compute shaders:
154154
- CUDA usually outperforms shaders if kernels are written well and launch configurations are optimal.
155155
- CUDA has many useful features such as shared memory, unified memory, graphs, fine grained thread control, streams, the PTX ISA, etc.
156-
- rust-gpu does not perform many optimizations, and with cg_ssa's less than ideal codegen, the optimizations by llvm and libnvvm are needed.
156+
- rust-gpu does not perform many optimizations, and with rustc_codegen_ssa's less than ideal codegen, the optimizations by llvm and libnvvm are needed.
157157
- SPIRV is arguably still not suitable for serious GPU kernel codegen, it is underspecced, complex, and does not mention many things which are needed.
158158
While libnvvm (which uses a well documented subset of LLVM IR) and the PTX ISA are very thoroughly documented/specified.
159159
- rust-gpu is primarily focused on graphical shaders, compute shaders are secondary, which the rust ecosystem needs, but it also

guide/src/nvvm/backends.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ Nowadays, Rustc is almost fully decoupled from LLVM and it is instead generic ov
1515
Rustc instead uses a system of codegen backends that implement traits and then get loaded as dynamically linked libraries.
1616
This allows rust to compile to virtually anything with a surprisingly small amount of work. At the time of writing, there are
1717
five publicly known codegens that exist:
18-
- rustc_codegen_clif, cranelift
18+
- rustc_codegen_cranelift
1919
- rustc_codegen_llvm
2020
- rustc_codegen_gcc
2121
- rustc_codegen_spirv
2222
- rustc_codegen_nvvm, obviously the best codegen ;)
2323

24-
`rustc_codegen_clif` targets the cranelift backend, which is a codegen backend written in rust that is faster than LLVM but does not have many optimizations
24+
`rustc_codegen_cranelift` targets the cranelift backend, which is a codegen backend written in rust that is faster than LLVM but does not have many optimizations
2525
compared to LLVM. `rustc_codegen_llvm` is obvious, it is the backend almost everybody uses which targets LLVM. `rustc_codegen_gcc` targets GCC (GNU Compiler Collection)
2626
which is able to target more exotic targets than LLVM, especially for embedded. `rustc_codegen_spirv` targets the SPIR-V (Standard Portable Intermediate Representation 5)
2727
format, which is a format mostly used for compiling shader languages such as GLSL or WGSL to a standard representation that Vulkan/OpenGL can use, the reasons
@@ -32,16 +32,12 @@ What NVVM IR/libnvvm are has been covered in the [CUDA section](../../cuda/pipel
3232

3333
# rustc_codegen_ssa
3434

35-
Despite its name, `rustc_codegen_ssa` does not actually codegen to anything, it is however the central crate behind every single codegen.
36-
The SSA codegen does most of the hard work in codegen, which is actually codegenning MIR and taking care of managing codegen altogether.
35+
`rustc_codegen_ssa` is the central crate behind every single codegen and does much of the hard work.
36+
It abstracts away the MIR lowering logic so that custom codegens only have to implement some
37+
traits and the SSA codegen does everything else. For example:
38+
- A trait for getting a type like an integer type.
39+
- A trait for optimizing a module.
40+
- A trait for linking everything.
41+
- A trait for declaring a function.
3742

38-
The SSA codegen abstracts away the MIR lowering logic so that custom codegens do not have to implement the time consuming logic of lowering MIR,
39-
they can just implement a bunch of traits and the SSA codegen does everything else.
40-
41-
The SSA codegen is literally just a bunch of traits, for example:
42-
- A trait for getting a type like an integer type
43-
- A trait for optimizing a module
44-
- A trait for linking everything
45-
- A trait for declaring a function
46-
...etc
47-
You will find an SSA codegen trait in almost every single file.
43+
And so on. You will find an SSA codegen trait in almost every file.

guide/src/nvvm/debugging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ which i will add to the project soon.
3434

3535
Miscompilations are rare but annoying. They usually result in one of two things happening:
3636
- CUDA rejecting the PTX as a whole (throwing an InvalidPtx error). This is rare but the most common cause is declaring invalid
37-
extern functions (just grep for `extern` in the ptx file and check if its odd functions that arent cuda syscalls like vprintf, malloc, free, etc).
37+
extern functions (just grep for `extern` in the ptx file and check if it's odd functions that aren't cuda syscalls like vprintf, malloc, free, etc).
3838
- The PTX containing invalid behavior. This is very specific and rare but if you find this, the best way to debug it is:
3939
- Try to get a minimal working example so we don't have to search through megabytes of llvm ir/ptx.
4040
- Use `RUSTFLAGS="--emit=llvm-ir"` and find `crate_name.ll` in `target/nvptx64-nvidia-cuda/<debug/release>/deps/` and attach it in any bug report.

guide/src/nvvm/nvvm.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ dive into each trait.
4242

4343
But first, let's talk about the end of the codegen, it is pretty simple, we do a couple of things:
4444
*after codegen is done and LLVM has been run to optimize each module*
45-
- 1: We gather every llvm bitcode module we created.
46-
- 2: We create a new libnvvm program.
47-
- 3: We add every bitcode module to the libnvvm program.
48-
- 4: We try to find libdevice and add it to the program (see [nvidia docs](https://docs.nvidia.com/cuda/libdevice-users-guide/introduction.html#what-is-libdevice) on what libdevice is).
49-
- 5: We run the verifier on the nvvm program just to check that we did not create any invalid nvvm ir.
50-
- 6: We run the compiler which gives us a final PTX string, hooray!
51-
- 7: Finally, the PTX goes through a small stage where its parsed and function DCE is run to eliminate
52-
Most of the bloat in the file, traditionally this is done by the linker but theres no linker to be found for miles here.
53-
- 8: We write this ptx file to wherever rustc tells us to write the final file.
45+
1. We gather every LLVM bitcode module we created.
46+
2. We create a new libnvvm program.
47+
3. We add every bitcode module to the libnvvm program.
48+
4. We try to find libdevice and add it to the program (see [nvidia
49+
docs](https://docs.nvidia.com/cuda/libdevice-users-guide/introduction.html#what-is-libdevice) on
50+
what libdevice is).
51+
5. We run the verifier on the nvvm program just to check that we did not create any invalid NVVM IR.
52+
6. We run the compiler which gives us a final PTX string, hooray!
53+
7. Finally, the PTX goes through a small stage where its parsed and function DCE is run to
54+
eliminate most of the bloat in the file. Traditionally this is done by the linker but there's no
55+
linker to be found for miles here.
56+
8. We write this PTX file to wherever rustc tells us to write the final file.
5457

5558
We will cover the libnvvm steps in more detail later on.
5659

@@ -71,12 +74,12 @@ rlibs are mysterious files, their origins are mysterious and their contents are
7174
but rlibs often confuse people (including me at first). Rlibs are rustc's way of encoding basically everything it needs to know
7275
about a crate into a file. Rlibs usually contain the following:
7376
- Object files for each CGU.
74-
- LLVM Bitcode.
75-
- a Symbol table.
76-
- metadata:
77-
- rustc version (because things can go kaboom if version mismatches, ABIs are fun amirite)
77+
- LLVM bitcode.
78+
- A symbol table.
79+
- Metadata:
80+
- The rustc version (because things can go kaboom if version mismatches, ABIs are fun amirite)
7881
- A crate hash
79-
- a crate id
80-
- info about the source files
81-
- the exported API, things like macros, traits, etc.
82+
- A crate id
83+
- Info about the source files
84+
- The exported API, things like macros, traits, etc.
8285
- MIR, for things such as generic functions and `#[inline]`d functions (please don't put `#[inline]` on everything, rustc will cry)

guide/src/nvvm/types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Types! who doesn't love types, especially those that cause libnvvm to randomly s
44
Anyways, types are an integral part of the codegen and everything revolves around them and you will see them everywhere.
55

66
`rustc_codegen_ssa` does not actually tell you what your type representation should be, it allows you to decide. For
7-
example, `rust-gpu` represents it as a `SpirvType` enum, while both `cg_llvm` and our codegen represent it as
7+
example, `rust-gpu` represents it as a `SpirvType` enum, while both `rustc_codegen_llvm` and our codegen represent it as
88
opaque llvm types:
99

1010
```rs
@@ -13,7 +13,7 @@ type Type = &'ll llvm::Type;
1313

1414
`llvm::Type` is an opaque type that comes from llvm-c. `'ll` is one of the main lifetimes you will see
1515
throughout the whole codegen, it is used for anything that lasts as long as the current usage of llvm.
16-
LLVM gives you back pointers when you ask for a type or value, some time ago `cg_llvm` fully switched to using
16+
LLVM gives you back pointers when you ask for a type or value, some time ago rustc_codegen_llvm fully switched to using
1717
references over pointers, and we follow in their footsteps.
1818

1919
One important fact about types is that they are opaque, you cannot take a type and ask "is this X struct?",

0 commit comments

Comments
 (0)