Compare commits
13 Commits
222f6caa09
...
fd0fd49df4
| Author | SHA1 | Date |
|---|---|---|
|
|
fd0fd49df4 | |
|
|
4c6d66ccb0 | |
|
|
4233695fea | |
|
|
33eaf36815 | |
|
|
980acc5eee | |
|
|
e0a39188f1 | |
|
|
9d7ba8573d | |
|
|
c963b4ad93 | |
|
|
a02af2f135 | |
|
|
4185dca095 | |
|
|
a2c80e6e23 | |
|
|
7b921990fc | |
|
|
2f266b589e |
|
|
@ -101,6 +101,8 @@
|
|||
- [The `rustdoc` test suite](./rustdoc-internals/rustdoc-test-suite.md)
|
||||
- [The `rustdoc-gui` test suite](./rustdoc-internals/rustdoc-gui-test-suite.md)
|
||||
- [The `rustdoc-json` test suite](./rustdoc-internals/rustdoc-json-test-suite.md)
|
||||
- [GPU offload internals](./offload/internals.md)
|
||||
- [Installation](./offload/installation.md)
|
||||
- [Autodiff internals](./autodiff/internals.md)
|
||||
- [Installation](./autodiff/installation.md)
|
||||
- [How to debug](./autodiff/debugging.md)
|
||||
|
|
@ -121,8 +123,9 @@
|
|||
- [Feature gate checking](./feature-gate-ck.md)
|
||||
- [Lang Items](./lang-items.md)
|
||||
- [The HIR (High-level IR)](./hir.md)
|
||||
- [Lowering AST to HIR](./ast-lowering.md)
|
||||
- [Debugging](./hir-debugging.md)
|
||||
- [Lowering AST to HIR](./hir/lowering.md)
|
||||
- [Ambig/Unambig Types and Consts](./hir/ambig-unambig-ty-and-consts.md)
|
||||
- [Debugging](./hir/debugging.md)
|
||||
- [The THIR (Typed High-level IR)](./thir.md)
|
||||
- [The MIR (Mid-level IR)](./mir/index.md)
|
||||
- [MIR construction](./mir/construction.md)
|
||||
|
|
|
|||
|
|
@ -174,8 +174,8 @@ compiler, you can use it instead of the JSON file for both arguments.
|
|||
## Promoting a target from tier 2 (target) to tier 2 (host)
|
||||
|
||||
There are two levels of tier 2 targets:
|
||||
a) Targets that are only cross-compiled (`rustup target add`)
|
||||
b) Targets that [have a native toolchain][tier2-native] (`rustup toolchain install`)
|
||||
- Targets that are only cross-compiled (`rustup target add`)
|
||||
- Targets that [have a native toolchain][tier2-native] (`rustup toolchain install`)
|
||||
|
||||
[tier2-native]: https://doc.rust-lang.org/nightly/rustc/target-tier-policy.html#tier-2-with-host-tools
|
||||
|
||||
|
|
|
|||
|
|
@ -553,7 +553,7 @@ compiler](#linting-early-in-the-compiler).
|
|||
|
||||
|
||||
[AST nodes]: the-parser.md
|
||||
[AST lowering]: ast-lowering.md
|
||||
[AST lowering]: ./hir/lowering.md
|
||||
[HIR nodes]: hir.md
|
||||
[MIR nodes]: mir/index.md
|
||||
[macro expansion]: macro-expansion.md
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
The HIR – "High-Level Intermediate Representation" – is the primary IR used
|
||||
in most of rustc. It is a compiler-friendly representation of the abstract
|
||||
syntax tree (AST) that is generated after parsing, macro expansion, and name
|
||||
resolution (see [Lowering](./ast-lowering.html) for how the HIR is created).
|
||||
resolution (see [Lowering](./hir/lowering.md) for how the HIR is created).
|
||||
Many parts of HIR resemble Rust surface syntax quite closely, with
|
||||
the exception that some of Rust's expression forms have been desugared away.
|
||||
For example, `for` loops are converted into a `loop` and do not appear in
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
# Ambig/Unambig Types and Consts
|
||||
|
||||
Types and Consts args in the HIR can be in two kinds of positions ambiguous (ambig) or unambiguous (unambig). Ambig positions are where
|
||||
it would be valid to parse either a type or a const, unambig positions are where only one kind would be valid to
|
||||
parse.
|
||||
|
||||
```rust
|
||||
fn func<T, const N: usize>(arg: T) {
|
||||
// ^ Unambig type position
|
||||
let a: _ = arg;
|
||||
// ^ Unambig type position
|
||||
|
||||
func::<T, N>(arg);
|
||||
// ^ ^
|
||||
// ^^^^ Ambig position
|
||||
|
||||
let _: [u8; 10];
|
||||
// ^^ ^^ Unambig const position
|
||||
// ^^ Unambig type position
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
In ambig positions, inferred arguments are represented with `hir::GenericArg::Infer`.
|
||||
|
||||
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:
|
||||
1. In unambig type position as a `hir::TyKind::Infer`
|
||||
2. In unambig const arg position as a `hir::ConstArgKind::Infer`
|
||||
3. In an ambig position as a [`GenericArg::Type(TyKind::Infer)`][generic_arg_ty]
|
||||
4. In an ambig position as a [`GenericArg::Const(ConstArgKind::Infer)`][generic_arg_const]
|
||||
5. In an ambig position as a [`GenericArg::Infer`][generic_arg_infer]
|
||||
|
||||
Note that places 3 and 4 would never actually be possible to encounter as we always lower to `GenericArg::Infer` in generic arg position.
|
||||
|
||||
This has a few failure modes:
|
||||
- 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.
|
||||
- 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.
|
||||
- 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`.
|
||||
- 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).
|
||||
|
||||
To make writing HIR visitors less error prone when caring about inferred types/consts we have a relatively complex system:
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
This has a number of benefits:
|
||||
- It's clear that `GenericArg::Type/Const` cannot represent inferred type/const arguments
|
||||
- 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
|
||||
- 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
|
||||
|
||||
[ty_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.TyKind.html#variant.Infer
|
||||
[const_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.ConstArgKind.html#variant.Infer
|
||||
[generic_arg_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Type
|
||||
[generic_arg_const]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Const
|
||||
[generic_arg_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Infer
|
||||
[ambig_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.AmbigArg.html
|
||||
[visit_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_ty
|
||||
[visit_const_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_const_arg
|
||||
[visit_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_infer
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
# AST lowering
|
||||
|
||||
The AST lowering step converts AST to [HIR](hir.html).
|
||||
The AST lowering step converts AST to [HIR](../hir.md).
|
||||
This means many structures are removed if they are irrelevant
|
||||
for type analysis or similar syntax agnostic analyses. Examples
|
||||
of such structures include but are not limited to
|
||||
|
|
@ -42,20 +42,39 @@ inference variables or other information.
|
|||
The first thing that the probe phase does is to create a series of
|
||||
*steps*. This is done by progressively dereferencing the receiver type
|
||||
until it cannot be deref'd anymore, as well as applying an optional
|
||||
"unsize" step. So if the receiver has type `Rc<Box<[T; 3]>>`, this
|
||||
"unsize" step. This "dereferencing" in fact uses the `Receiver` trait
|
||||
rather than the normal `Deref` trait. There's a blanket implementation
|
||||
of `Receiver` for `T: Deref` so the answer is often the same.
|
||||
|
||||
So if the receiver has type `Rc<Box<[T; 3]>>`, this
|
||||
might yield:
|
||||
|
||||
1. `Rc<Box<[T; 3]>>`
|
||||
2. `Box<[T; 3]>`
|
||||
3. `[T; 3]`
|
||||
4. `[T]`
|
||||
1. `Rc<Box<[T; 3]>>` *
|
||||
2. `Box<[T; 3]>` *
|
||||
3. `[T; 3]` *
|
||||
4. `[T]` *
|
||||
|
||||
Some types might implement `Receiver` but not `Deref`. Imagine that
|
||||
`SmartPtr<T>` does this. If the receiver has type `&Rc<SmartPtr<T>>`
|
||||
the steps would be:
|
||||
|
||||
1. `&Rc<SmartPtr<T>>` *
|
||||
2. `Rc<SmartPtr<T>>` *
|
||||
3. `SmartPtr<T>` *
|
||||
4. `T`
|
||||
|
||||
The first three of those steps, marked with a *, can be reached using
|
||||
`Deref` as well as by `Receiver`. This fact is recorded against each step.
|
||||
|
||||
### Candidate assembly
|
||||
|
||||
We then search along those steps to create a list of *candidates*. A
|
||||
`Candidate` is a method item that might plausibly be the method being
|
||||
invoked. For each candidate, we'll derive a "transformed self type"
|
||||
that takes into account explicit self.
|
||||
We then search along these candidate steps to create a list of
|
||||
*candidates*. A `Candidate` is a method item that might plausibly be the
|
||||
method being invoked. For each candidate, we'll derive a "transformed self
|
||||
type" that takes into account explicit self.
|
||||
|
||||
At this point, we consider the whole list - all the steps reachable via
|
||||
`Receiver`, not just the shorter list reachable via `Deref`.
|
||||
|
||||
Candidates are grouped into two kinds, inherent and extension.
|
||||
|
||||
|
|
@ -97,9 +116,14 @@ might have two candidates:
|
|||
|
||||
### Candidate search
|
||||
|
||||
Finally, to actually pick the method, we will search down the steps,
|
||||
trying to match the receiver type against the candidate types. At
|
||||
each step, we also consider an auto-ref and auto-mut-ref to see whether
|
||||
Finally, to actually pick the method, we will search down the steps again,
|
||||
trying to match the receiver type against the candidate types. This time,
|
||||
we consider only the steps which can be reached via `Deref`, since we
|
||||
actually need to convert the receiver type to match the `self` type.
|
||||
In the examples above, that means we consider only the steps marked with
|
||||
an asterisk.
|
||||
|
||||
At each step, we also consider an auto-ref and auto-mut-ref to see whether
|
||||
that makes any of the candidates match. For each resulting receiver
|
||||
type, we consider inherent candidates before extension candidates.
|
||||
If there are multiple matching candidates in a group, we report an
|
||||
|
|
@ -113,3 +137,68 @@ recursively consider all where-clauses that appear on the impl: if
|
|||
those match (or we cannot rule out that they do), then this is the
|
||||
method we would pick. Otherwise, we would continue down the series of
|
||||
steps.
|
||||
|
||||
### `Deref` vs `Receiver`
|
||||
|
||||
Why have longer and shorter lists here? The use-case is smart pointers.
|
||||
For example:
|
||||
|
||||
```
|
||||
struct Inner;
|
||||
|
||||
// Assume this cannot implement Deref for some reason, e.g. because
|
||||
// we know other code may be accessing T and it's not safe to make
|
||||
// a reference to it
|
||||
struct Ptr<T>;
|
||||
|
||||
impl<T> Receiver for Ptr<T> {
|
||||
type Target = T;
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn method1(self: &Ptr<Self>) {
|
||||
}
|
||||
|
||||
fn method2(&self) {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let ptr = Ptr(Inner);
|
||||
ptr.method1();
|
||||
// ptr.method2();
|
||||
}
|
||||
```
|
||||
|
||||
In this case, the step list for the `method1` call would be:
|
||||
|
||||
1. `Ptr<Inner>` *
|
||||
2. `Inner`
|
||||
|
||||
Because the list of types reached via `Receiver` includes `Inner`, we can
|
||||
look for methods in the `impl Inner` block during candidate search.
|
||||
But, we can't dereference a `&Receiver` to make a `&Inner`, so the picking
|
||||
process won't allow us to call `method2` on a `Ptr<Inner>`.
|
||||
|
||||
### Deshadowing
|
||||
|
||||
Once we've made a pick, code in `pick_all_method` also checks for a couple
|
||||
of cases where one method may shadow another. That is, in the code example
|
||||
above, imagine there also exists:
|
||||
|
||||
```
|
||||
impl Inner {
|
||||
fn method3(self: &Ptr<Self>) {}
|
||||
}
|
||||
|
||||
impl<T> Ptr<T> {
|
||||
fn method3(self) {}
|
||||
}
|
||||
```
|
||||
|
||||
These can both be called using `ptr.method3()`. Without special care, we'd
|
||||
automatically use `Ptr::self` because we pick by value before even looking
|
||||
at by-reference candidates. This could be a problem if the caller previously
|
||||
was using `Inner::method3`: they'd get an unexpected behavior change.
|
||||
So, if we pick a by-value candidate we'll check to see if we might be
|
||||
shadowing a by-value candidate, and error if so. The same applies
|
||||
if a by-mut-ref candidate shadows a by-reference candidate.
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
# Installation
|
||||
|
||||
In the future, `std::offload` should become available in nightly builds for users. For now, everyone still needs to build rustc from source.
|
||||
|
||||
## Build instructions
|
||||
|
||||
First you need to clone and configure the Rust repository:
|
||||
```bash
|
||||
git clone --depth=1 git@github.com:rust-lang/rust.git
|
||||
cd rust
|
||||
./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
|
||||
```
|
||||
|
||||
Afterwards you can build rustc using:
|
||||
```bash
|
||||
./x.py build --stage 1 library
|
||||
```
|
||||
|
||||
Afterwards rustc toolchain link will allow you to use it through cargo:
|
||||
```
|
||||
rustup toolchain link offload build/host/stage1
|
||||
rustup toolchain install nightly # enables -Z unstable-options
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Build instruction for LLVM itself
|
||||
```bash
|
||||
git clone --depth=1 git@github.com:llvm/llvm-project.git
|
||||
cd llvm-project
|
||||
mkdir build
|
||||
cd build
|
||||
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=.
|
||||
ninja
|
||||
ninja install
|
||||
```
|
||||
This gives you a working LLVM build.
|
||||
|
||||
|
||||
## Testing
|
||||
run
|
||||
```
|
||||
./x.py test --stage 1 tests/codegen/gpu_offload
|
||||
```
|
||||
|
||||
## Usage
|
||||
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.
|
||||
```
|
||||
/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
|
||||
/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
|
||||
LIBOMPTARGET_INFO=-1 ./main
|
||||
```
|
||||
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.
|
||||
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
|
||||
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.
|
||||
In the last step you can run your binary, if all went well you will see a data transfer being reported:
|
||||
```
|
||||
omptarget device 0 info: Entering OpenMP data region with being_mapper at unknown:0:0 with 1 arguments:
|
||||
omptarget device 0 info: tofrom(unknown)[1024]
|
||||
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
|
||||
omptarget device 0 info: Copying data from host to device, HstPtr=0x00007fffffff9540, TgtPtr=0x0000155547200000, Size=1024, Name=unknown
|
||||
omptarget device 0 info: OpenMP Host-Device pointer mappings after block at unknown:0:0:
|
||||
omptarget device 0 info: Host Ptr Target Ptr Size (B) DynRefCount HoldRefCount Declaration
|
||||
omptarget device 0 info: 0x00007fffffff9540 0x0000155547200000 1024 1 0 unknown at unknown:0:0
|
||||
// some other output
|
||||
omptarget device 0 info: Exiting OpenMP data region with end_mapper at unknown:0:0 with 1 arguments:
|
||||
omptarget device 0 info: tofrom(unknown)[1024]
|
||||
omptarget device 0 info: Mapping exists with HstPtrBegin=0x00007fffffff9540, TgtPtrBegin=0x0000155547200000, Size=1024, DynRefCount=0 (decremented, delayed deletion), HoldRefCount=0
|
||||
omptarget device 0 info: Copying data from device to host, TgtPtr=0x0000155547200000, HstPtr=0x00007fffffff9540, Size=1024, Name=unknown
|
||||
omptarget device 0 info: Removing map entry with HstPtrBegin=0x00007fffffff9540, TgtPtrBegin=0x0000155547200000, Size=1024, Name=unknown
|
||||
```
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# std::offload
|
||||
|
||||
This module is under active development. Once upstream, it should allow Rust developers to run Rust code on GPUs.
|
||||
We aim to develop a `rusty` GPU programming interface, which is safe, convenient and sufficiently fast by default.
|
||||
This includes automatic data movement to and from the GPU, in a efficient way. We will (later)
|
||||
also offer more advanced, possibly unsafe, interfaces which allow a higher degree of control.
|
||||
|
||||
The implementation is based on LLVM's "offload" project, which is already used by OpenMP to run Fortran or C++ code on GPUs.
|
||||
While the project is under development, users will need to call other compilers like clang to finish the compilation process.
|
||||
|
|
@ -410,7 +410,7 @@ For more details on bootstrapping, see
|
|||
- Guide: [The HIR](hir.md)
|
||||
- Guide: [Identifiers in the HIR](hir.md#identifiers-in-the-hir)
|
||||
- Guide: [The `HIR` Map](hir.md#the-hir-map)
|
||||
- Guide: [Lowering `AST` to `HIR`](ast-lowering.md)
|
||||
- Guide: [Lowering `AST` to `HIR`](./hir/lowering.md)
|
||||
- How to view `HIR` representation for your code `cargo rustc -- -Z unpretty=hir-tree`
|
||||
- Rustc `HIR` definition: [`rustc_hir`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/index.html)
|
||||
- Main entry point: **TODO**
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ This is a guide for how to profile rustc with [perf](https://perf.wiki.kernel.or
|
|||
- Get a clean checkout of rust-lang/master, or whatever it is you want
|
||||
to profile.
|
||||
- Set the following settings in your `bootstrap.toml`:
|
||||
- `debuginfo-level = 1` - enables line debuginfo
|
||||
- `jemalloc = false` - lets you do memory use profiling with valgrind
|
||||
- `rust.debuginfo-level = 1` - enables line debuginfo
|
||||
- `rust.jemalloc = false` - lets you do memory use profiling with valgrind
|
||||
- leave everything else the defaults
|
||||
- Run `./x build` to get a full build
|
||||
- Make a rustup toolchain pointing to that result
|
||||
|
|
|
|||
Loading…
Reference in New Issue