Skip to content

Commit 829aaf8

Browse files
authored
Rollup merge of #142743 - tshepang:rdg-push, r=jieyouxu
rustc-dev-guide subtree update r? ``@ghost``
2 parents abe7991 + 6ad42bf commit 829aaf8

23 files changed

+252
-96
lines changed

src/doc/rustc-dev-guide/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
14346303d760027e53214e705109a62c0f00b214
1+
d1d8e386c5e84c4ba857f56c3291f73c27e2d62a

src/doc/rustc-dev-guide/src/SUMMARY.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
- [The `rustdoc` test suite](./rustdoc-internals/rustdoc-test-suite.md)
102102
- [The `rustdoc-gui` test suite](./rustdoc-internals/rustdoc-gui-test-suite.md)
103103
- [The `rustdoc-json` test suite](./rustdoc-internals/rustdoc-json-test-suite.md)
104+
- [GPU offload internals](./offload/internals.md)
105+
- [Installation](./offload/installation.md)
104106
- [Autodiff internals](./autodiff/internals.md)
105107
- [Installation](./autodiff/installation.md)
106108
- [How to debug](./autodiff/debugging.md)
@@ -121,8 +123,9 @@
121123
- [Feature gate checking](./feature-gate-ck.md)
122124
- [Lang Items](./lang-items.md)
123125
- [The HIR (High-level IR)](./hir.md)
124-
- [Lowering AST to HIR](./ast-lowering.md)
125-
- [Debugging](./hir-debugging.md)
126+
- [Lowering AST to HIR](./hir/lowering.md)
127+
- [Ambig/Unambig Types and Consts](./hir/ambig-unambig-ty-and-consts.md)
128+
- [Debugging](./hir/debugging.md)
126129
- [The THIR (Typed High-level IR)](./thir.md)
127130
- [The MIR (Mid-level IR)](./mir/index.md)
128131
- [MIR construction](./mir/construction.md)
@@ -181,15 +184,15 @@
181184
- [Significant changes and quirks](./solve/significant-changes.md)
182185
- [`Unsize` and `CoerceUnsized` traits](./traits/unsize.md)
183186
- [Type checking](./type-checking.md)
184-
- [Method Lookup](./method-lookup.md)
187+
- [Method lookup](./method-lookup.md)
185188
- [Variance](./variance.md)
186189
- [Coherence checking](./coherence.md)
187190
- [Opaque types](./opaque-types-type-alias-impl-trait.md)
188191
- [Inference details](./opaque-types-impl-trait-inference.md)
189192
- [Return Position Impl Trait In Trait](./return-position-impl-trait-in-trait.md)
190193
- [Region inference restrictions][opaque-infer]
191194
- [Const condition checking](./effects.md)
192-
- [Pattern and Exhaustiveness Checking](./pat-exhaustive-checking.md)
195+
- [Pattern and exhaustiveness checking](./pat-exhaustive-checking.md)
193196
- [Unsafety checking](./unsafety-checking.md)
194197
- [MIR dataflow](./mir/dataflow.md)
195198
- [Drop elaboration](./mir/drop-elaboration.md)
@@ -209,7 +212,7 @@
209212
- [Closure capture inference](./closure.md)
210213
- [Async closures/"coroutine-closures"](coroutine-closures.md)
211214

212-
# MIR to Binaries
215+
# MIR to binaries
213216

214217
- [Prologue](./part-5-intro.md)
215218
- [MIR optimizations](./mir/optimizations.md)
@@ -218,15 +221,15 @@
218221
- [Interpreter](./const-eval/interpret.md)
219222
- [Monomorphization](./backend/monomorph.md)
220223
- [Lowering MIR](./backend/lowering-mir.md)
221-
- [Code Generation](./backend/codegen.md)
224+
- [Code generation](./backend/codegen.md)
222225
- [Updating LLVM](./backend/updating-llvm.md)
223226
- [Debugging LLVM](./backend/debugging.md)
224227
- [Backend Agnostic Codegen](./backend/backend-agnostic.md)
225-
- [Implicit Caller Location](./backend/implicit-caller-location.md)
226-
- [Libraries and Metadata](./backend/libs-and-metadata.md)
227-
- [Profile-guided Optimization](./profile-guided-optimization.md)
228-
- [LLVM Source-Based Code Coverage](./llvm-coverage-instrumentation.md)
229-
- [Sanitizers Support](./sanitizers.md)
228+
- [Implicit caller location](./backend/implicit-caller-location.md)
229+
- [Libraries and metadata](./backend/libs-and-metadata.md)
230+
- [Profile-guided optimization](./profile-guided-optimization.md)
231+
- [LLVM source-based code coverage](./llvm-coverage-instrumentation.md)
232+
- [Sanitizers support](./sanitizers.md)
230233
- [Debugging support in the Rust compiler](./debugging-support-in-rustc.md)
231234

232235
---

src/doc/rustc-dev-guide/src/backend/implicit-caller-location.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Implicit Caller Location
1+
# Implicit caller location
22

33
<!-- toc -->
44

@@ -8,7 +8,7 @@ adds the [`#[track_caller]`][attr-reference] attribute for functions, the
88
[`caller_location`][intrinsic] intrinsic, and the stabilization-friendly
99
[`core::panic::Location::caller`][wrapper] wrapper.
1010

11-
## Motivating Example
11+
## Motivating example
1212

1313
Take this example program:
1414

@@ -39,7 +39,7 @@ These error messages are achieved through a combination of changes to `panic!` i
3939
of `core::panic::Location::caller` and a number of `#[track_caller]` annotations in the standard
4040
library which propagate caller information.
4141

42-
## Reading Caller Location
42+
## Reading caller location
4343

4444
Previously, `panic!` made use of the `file!()`, `line!()`, and `column!()` macros to construct a
4545
[`Location`] pointing to where the panic occurred. These macros couldn't be given an overridden
@@ -51,7 +51,7 @@ was expanded. This function is itself annotated with `#[track_caller]` and wraps
5151
[`caller_location`][intrinsic] compiler intrinsic implemented by rustc. This intrinsic is easiest
5252
explained in terms of how it works in a `const` context.
5353

54-
## Caller Location in `const`
54+
## Caller location in `const`
5555

5656
There are two main phases to returning the caller location in a const context: walking up the stack
5757
to find the right location and allocating a const value to return.
@@ -138,7 +138,7 @@ fn main() {
138138
}
139139
```
140140

141-
### Dynamic Dispatch
141+
### Dynamic dispatch
142142

143143
In codegen contexts we have to modify the callee ABI to pass this information down the stack, but
144144
the attribute expressly does *not* modify the type of the function. The ABI change must be
@@ -156,7 +156,7 @@ probably the best we can do without modifying fully-stabilized type signatures.
156156
> whether we'll be called in a const context (safe to ignore shim) or in a codegen context (unsafe
157157
> to ignore shim). Even if we did know, the results from const and codegen contexts must agree.
158158
159-
## The Attribute
159+
## The attribute
160160

161161
The `#[track_caller]` attribute is checked alongside other codegen attributes to ensure the
162162
function:

src/doc/rustc-dev-guide/src/backend/libs-and-metadata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Libraries and Metadata
1+
# Libraries and metadata
22

33
When the compiler sees a reference to an external crate, it needs to load some
44
information about that crate. This chapter gives an overview of that process,

src/doc/rustc-dev-guide/src/building/new-target.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ compiler, you can use it instead of the JSON file for both arguments.
174174
## Promoting a target from tier 2 (target) to tier 2 (host)
175175

176176
There are two levels of tier 2 targets:
177-
a) Targets that are only cross-compiled (`rustup target add`)
178-
b) Targets that [have a native toolchain][tier2-native] (`rustup toolchain install`)
177+
- Targets that are only cross-compiled (`rustup target add`)
178+
- Targets that [have a native toolchain][tier2-native] (`rustup toolchain install`)
179179

180180
[tier2-native]: https://doc.rust-lang.org/nightly/rustc/target-tier-policy.html#tier-2-with-host-tools
181181

src/doc/rustc-dev-guide/src/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ To find documentation-related issues, use the [A-docs label].
364364

365365
You can find documentation style guidelines in [RFC 1574].
366366

367-
To build the standard library documentation, use `x doc --stage 0 library --open`.
367+
To build the standard library documentation, use `x doc --stage 1 library --open`.
368368
To build the documentation for a book (e.g. the unstable book), use `x doc src/doc/unstable-book.`
369369
Results should appear in `build/host/doc`, as well as automatically open in your default browser.
370370
See [Building Documentation](./building/compiler-documenting.md#building-documentation) for more

src/doc/rustc-dev-guide/src/diagnostics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ compiler](#linting-early-in-the-compiler).
553553

554554

555555
[AST nodes]: the-parser.md
556-
[AST lowering]: ast-lowering.md
556+
[AST lowering]: ./hir/lowering.md
557557
[HIR nodes]: hir.md
558558
[MIR nodes]: mir/index.md
559559
[macro expansion]: macro-expansion.md

src/doc/rustc-dev-guide/src/hir.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The HIR – "High-Level Intermediate Representation" – is the primary IR used
66
in most of rustc. It is a compiler-friendly representation of the abstract
77
syntax tree (AST) that is generated after parsing, macro expansion, and name
8-
resolution (see [Lowering](./ast-lowering.html) for how the HIR is created).
8+
resolution (see [Lowering](./hir/lowering.md) for how the HIR is created).
99
Many parts of HIR resemble Rust surface syntax quite closely, with
1010
the exception that some of Rust's expression forms have been desugared away.
1111
For example, `for` loops are converted into a `loop` and do not appear in
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Ambig/Unambig Types and Consts
2+
3+
Types and Consts args in the HIR can be in two kinds of positions ambiguous (ambig) or unambiguous (unambig). Ambig positions are where
4+
it would be valid to parse either a type or a const, unambig positions are where only one kind would be valid to
5+
parse.
6+
7+
```rust
8+
fn func<T, const N: usize>(arg: T) {
9+
// ^ Unambig type position
10+
let a: _ = arg;
11+
// ^ Unambig type position
12+
13+
func::<T, N>(arg);
14+
// ^ ^
15+
// ^^^^ Ambig position
16+
17+
let _: [u8; 10];
18+
// ^^ ^^ Unambig const position
19+
// ^^ Unambig type position
20+
}
21+
22+
```
23+
24+
Most types/consts in ambig positions are able to be disambiguated as either a type or const during parsing. Single segment paths are always represented as types in the AST but may get resolved to a const parameter during name resolution, then lowered to a const argument during ast-lowering. The only generic arguments which remain ambiguous after lowering are inferred generic arguments (`_`) in path segments. For example, in `Foo<_>` it is not clear whether the `_` argument is an inferred type argument, or an inferred const argument.
25+
26+
In unambig positions, inferred arguments are represented with [`hir::TyKind::Infer`][ty_infer] or [`hir::ConstArgKind::Infer`][const_infer] depending on whether it is a type or const position respectively.
27+
In ambig positions, inferred arguments are represented with `hir::GenericArg::Infer`.
28+
29+
A naive implementation of this would result in there being potentially 5 places where you might think an inferred type/const could be found in the HIR from looking at the structure of the HIR:
30+
1. In unambig type position as a `hir::TyKind::Infer`
31+
2. In unambig const arg position as a `hir::ConstArgKind::Infer`
32+
3. In an ambig position as a [`GenericArg::Type(TyKind::Infer)`][generic_arg_ty]
33+
4. In an ambig position as a [`GenericArg::Const(ConstArgKind::Infer)`][generic_arg_const]
34+
5. In an ambig position as a [`GenericArg::Infer`][generic_arg_infer]
35+
36+
Note that places 3 and 4 would never actually be possible to encounter as we always lower to `GenericArg::Infer` in generic arg position.
37+
38+
This has a few failure modes:
39+
- People may write visitors which check for `GenericArg::Infer` but forget to check for `hir::TyKind/ConstArgKind::Infer`, only handling infers in ambig positions by accident.
40+
- People may write visitors which check for `hir::TyKind/ConstArgKind::Infer` but forget to check for `GenericArg::Infer`, only handling infers in unambig positions by accident.
41+
- People may write visitors which check for `GenerArg::Type/Const(TyKind/ConstArgKind::Infer)` and `GenerigArg::Infer`, not realising that we never represent inferred types/consts in ambig positions as a `GenericArg::Type/Const`.
42+
- People may write visitors which check for *only* `TyKind::Infer` and not `ConstArgKind::Infer` forgetting that there are also inferred const arguments (and vice versa).
43+
44+
To make writing HIR visitors less error prone when caring about inferred types/consts we have a relatively complex system:
45+
46+
1. We have different types in the compiler for when a type or const is in an unambig or ambig position, `hir::Ty<AmbigArg>` and `hir::Ty<()>`. [`AmbigArg`][ambig_arg] is an uninhabited type which we use in the `Infer` variant of `TyKind` and `ConstArgKind` to selectively "disable" it if we are in an ambig position.
47+
48+
2. The [`visit_ty`][visit_ty] and [`visit_const_arg`][visit_const_arg] methods on HIR visitors only accept the ambig position versions of types/consts. Unambig types/consts are implicitly converted to ambig types/consts during the visiting process, with the `Infer` variant handled by a dedicated [`visit_infer`][visit_infer] method.
49+
50+
This has a number of benefits:
51+
- It's clear that `GenericArg::Type/Const` cannot represent inferred type/const arguments
52+
- Implementors of `visit_ty` and `visit_const_arg` will never encounter inferred types/consts making it impossible to write a visitor that seems to work right but handles edge cases wrong
53+
- The `visit_infer` method handles *all* cases of inferred type/consts in the HIR making it easy for visitors to handle inferred type/consts in one dedicated place and not forget cases
54+
55+
[ty_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.TyKind.html#variant.Infer
56+
[const_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.ConstArgKind.html#variant.Infer
57+
[generic_arg_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Type
58+
[generic_arg_const]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Const
59+
[generic_arg_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Infer
60+
[ambig_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.AmbigArg.html
61+
[visit_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_ty
62+
[visit_const_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_const_arg
63+
[visit_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_infer

src/doc/rustc-dev-guide/src/ast-lowering.md renamed to src/doc/rustc-dev-guide/src/hir/lowering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AST lowering
22

3-
The AST lowering step converts AST to [HIR](hir.html).
3+
The AST lowering step converts AST to [HIR](../hir.md).
44
This means many structures are removed if they are irrelevant
55
for type analysis or similar syntax agnostic analyses. Examples
66
of such structures include but are not limited to

src/doc/rustc-dev-guide/src/llvm-coverage-instrumentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LLVM Source-Based Code Coverage
1+
# LLVM source-based code coverage
22

33
<!-- toc -->
44

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Installation
2+
3+
In the future, `std::offload` should become available in nightly builds for users. For now, everyone still needs to build rustc from source.
4+
5+
## Build instructions
6+
7+
First you need to clone and configure the Rust repository:
8+
```bash
9+
git clone --depth=1 [email protected]:rust-lang/rust.git
10+
cd rust
11+
./configure --enable-llvm-link-shared --release-channel=nightly --enable-llvm-assertions --enable-offload --enable-enzyme --enable-clang --enable-lld --enable-option-checking --enable-ninja --disable-docs
12+
```
13+
14+
Afterwards you can build rustc using:
15+
```bash
16+
./x.py build --stage 1 library
17+
```
18+
19+
Afterwards rustc toolchain link will allow you to use it through cargo:
20+
```
21+
rustup toolchain link offload build/host/stage1
22+
rustup toolchain install nightly # enables -Z unstable-options
23+
```
24+
25+
26+
27+
## Build instruction for LLVM itself
28+
```bash
29+
git clone --depth=1 [email protected]:llvm/llvm-project.git
30+
cd llvm-project
31+
mkdir build
32+
cd build
33+
cmake -G Ninja ../llvm -DLLVM_TARGETS_TO_BUILD="host,AMDGPU,NVPTX" -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_ENABLE_RUNTIMES="offload,openmp" -DLLVM_ENABLE_PLUGINS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=.
34+
ninja
35+
ninja install
36+
```
37+
This gives you a working LLVM build.
38+
39+
40+
## Testing
41+
run
42+
```
43+
./x.py test --stage 1 tests/codegen/gpu_offload
44+
```
45+
46+
## Usage
47+
It is important to use a clang compiler build on the same llvm as rustc. Just calling clang without the full path will likely use your system clang, which probably will be incompatible.
48+
```
49+
/absolute/path/to/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc --edition=2024 --crate-type cdylib src/main.rs --emit=llvm-ir -O -C lto=fat -Cpanic=abort -Zoffload=Enable
50+
/absolute/path/to/rust/build/x86_64-unknown-linux-gnu/llvm/bin/clang++ -fopenmp --offload-arch=native -g -O3 main.ll -o main -save-temps
51+
LIBOMPTARGET_INFO=-1 ./main
52+
```
53+
The first step will generate a `main.ll` file, which has enough instructions to cause the offload runtime to move data to and from a gpu.
54+
The second step will use clang as the compilation driver to compile our IR file down to a working binary. Only a very small Rust subset will work out of the box here, unless
55+
you use features like build-std, which are not covered by this guide. Look at the codegen test to get a feeling for how to write a working example.
56+
In the last step you can run your binary, if all went well you will see a data transfer being reported:
57+
```
58+
omptarget device 0 info: Entering OpenMP data region with being_mapper at unknown:0:0 with 1 arguments:
59+
omptarget device 0 info: tofrom(unknown)[1024]
60+
omptarget device 0 info: Creating new map entry with HstPtrBase=0x00007fffffff9540, HstPtrBegin=0x00007fffffff9540, TgtAllocBegin=0x0000155547200000, TgtPtrBegin=0x0000155547200000, Size=1024, DynRefCount=1, HoldRefCount=0, Name=unknown
61+
omptarget device 0 info: Copying data from host to device, HstPtr=0x00007fffffff9540, TgtPtr=0x0000155547200000, Size=1024, Name=unknown
62+
omptarget device 0 info: OpenMP Host-Device pointer mappings after block at unknown:0:0:
63+
omptarget device 0 info: Host Ptr Target Ptr Size (B) DynRefCount HoldRefCount Declaration
64+
omptarget device 0 info: 0x00007fffffff9540 0x0000155547200000 1024 1 0 unknown at unknown:0:0
65+
// some other output
66+
omptarget device 0 info: Exiting OpenMP data region with end_mapper at unknown:0:0 with 1 arguments:
67+
omptarget device 0 info: tofrom(unknown)[1024]
68+
omptarget device 0 info: Mapping exists with HstPtrBegin=0x00007fffffff9540, TgtPtrBegin=0x0000155547200000, Size=1024, DynRefCount=0 (decremented, delayed deletion), HoldRefCount=0
69+
omptarget device 0 info: Copying data from device to host, TgtPtr=0x0000155547200000, HstPtr=0x00007fffffff9540, Size=1024, Name=unknown
70+
omptarget device 0 info: Removing map entry with HstPtrBegin=0x00007fffffff9540, TgtPtrBegin=0x0000155547200000, Size=1024, Name=unknown
71+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# std::offload
2+
3+
This module is under active development. Once upstream, it should allow Rust developers to run Rust code on GPUs.
4+
We aim to develop a `rusty` GPU programming interface, which is safe, convenient and sufficiently fast by default.
5+
This includes automatic data movement to and from the GPU, in a efficient way. We will (later)
6+
also offer more advanced, possibly unsafe, interfaces which allow a higher degree of control.
7+
8+
The implementation is based on LLVM's "offload" project, which is already used by OpenMP to run Fortran or C++ code on GPUs.
9+
While the project is under development, users will need to call other compilers like clang to finish the compilation process.

src/doc/rustc-dev-guide/src/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ For more details on bootstrapping, see
410410
- Guide: [The HIR](hir.md)
411411
- Guide: [Identifiers in the HIR](hir.md#identifiers-in-the-hir)
412412
- Guide: [The `HIR` Map](hir.md#the-hir-map)
413-
- Guide: [Lowering `AST` to `HIR`](ast-lowering.md)
413+
- Guide: [Lowering `AST` to `HIR`](./hir/lowering.md)
414414
- How to view `HIR` representation for your code `cargo rustc -- -Z unpretty=hir-tree`
415415
- Rustc `HIR` definition: [`rustc_hir`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/index.html)
416416
- Main entry point: **TODO**

src/doc/rustc-dev-guide/src/part-5-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# From MIR to Binaries
1+
# From MIR to binaries
22

33
All of the preceding chapters of this guide have one thing in common:
44
we never generated any executable machine code at all!

src/doc/rustc-dev-guide/src/pat-exhaustive-checking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Pattern and Exhaustiveness Checking
1+
# Pattern and exhaustiveness checking
22

33
In Rust, pattern matching and bindings have a few very helpful properties. The
44
compiler will check that bindings are irrefutable when made and that match arms

0 commit comments

Comments
 (0)