-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add amdgpu target #134740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add amdgpu target #134740
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
compiler/rustc_target/src/spec/targets/amdgcn_amd_amdhsa.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::spec::{Cc, LinkerFlavor, Lld, PanicStrategy, Target, TargetOptions}; | ||
|
||
pub(crate) fn target() -> Target { | ||
Target { | ||
arch: "amdgpu".into(), | ||
data_layout: "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9".into(), | ||
llvm_target: "amdgcn-amd-amdhsa".into(), | ||
metadata: crate::spec::TargetMetadata { | ||
description: Some("AMD GPU".into()), | ||
tier: Some(3), | ||
host_tools: Some(false), | ||
std: Some(false), | ||
}, | ||
pointer_width: 64, | ||
|
||
options: TargetOptions { | ||
os: "amdhsa".into(), | ||
vendor: "amd".into(), | ||
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes), | ||
linker: Some("rust-lld".into()), | ||
|
||
// There are many CPUs, one for each hardware generation. | ||
// Require to set one explicitly as there is no good default. | ||
need_explicit_cpu: true, | ||
|
||
max_atomic_width: Some(64), | ||
|
||
// Unwinding on GPUs is not useful. | ||
panic_strategy: PanicStrategy::Abort, | ||
|
||
// amdgpu backend does not support libcalls. | ||
no_builtins: true, | ||
simd_types_indirect: false, | ||
|
||
// Allow `cdylib` crate type. | ||
dynamic_linking: true, | ||
Flakebi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
only_cdylib: true, | ||
executables: false, | ||
dll_prefix: "".into(), | ||
dll_suffix: ".elf".into(), | ||
|
||
// The LLVM backend does not support stack canaries for this target | ||
supports_stack_protector: false, | ||
|
||
// Force LTO, object linking does not yet work with amdgpu. | ||
requires_lto: true, | ||
|
||
..Default::default() | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Change this file to make users of the `download-ci-llvm` configuration download | ||
a new version of LLVM from CI, even if the LLVM submodule hasn’t changed. | ||
|
||
Last change is for: https://github.com/rust-lang/rust/pull/129788 | ||
Last change is for: https://github.com/rust-lang/rust/pull/134740 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/doc/rustc/src/platform-support/amdgcn-amd-amdhsa.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# `amdgcn-amd-amdhsa` | ||
|
||
**Tier: 3** | ||
|
||
AMD GPU target for compute/HSA (Heterogeneous System Architecture). | ||
|
||
## Target maintainers | ||
|
||
- [@Flakebi](https://github.com/Flakebi) | ||
|
||
## Requirements | ||
|
||
AMD GPUs can be targeted via cross-compilation. | ||
Supported GPUs depend on the LLVM version that is used by Rust. | ||
In general, most GPUs starting from gfx7 (Sea Islands/CI) are supported as compilation targets, though older GPUs are not supported by the latest host runtime. | ||
Details about supported GPUs can be found in [LLVM’s documentation] and [ROCm documentation]. | ||
|
||
Binaries can be loaded by [HIP] or by the HSA runtime implemented in [ROCR-Runtime]. | ||
The format of binaries is a linked ELF. | ||
|
||
Binaries must be built with no-std. | ||
They can use `core` and `alloc` (`alloc` only if an allocator is supplied). | ||
At least one function needs to use the `"gpu-kernel"` calling convention and should be marked with `no_mangle` for simplicity. | ||
Functions using the `"gpu-kernel"` calling convention are kernel entrypoints and can be used from the host runtime. | ||
|
||
## Building the target | ||
|
||
The target is included in rustc. | ||
|
||
## Building Rust programs | ||
|
||
The amdgpu target supports many hardware generations, which need different binaries. | ||
The generations are exposed as different target-cpus in the backend. | ||
As there are many, Rust does not ship pre-compiled libraries for this target. | ||
Therefore, you have to build your own copy of `core` by using `cargo -Zbuild-std=core` or similar. | ||
|
||
To build a binary, create a no-std library: | ||
```rust,ignore (platform-specific) | ||
// src/lib.rs | ||
#![feature(abi_gpu_kernel)] | ||
#![no_std] | ||
|
||
#[panic_handler] | ||
fn panic(_: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "gpu-kernel" fn kernel(/* Arguments */) { | ||
// Code | ||
} | ||
``` | ||
|
||
Build the library as `cdylib`: | ||
```toml | ||
# Cargo.toml | ||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[profile.dev] | ||
lto = true # LTO must be explicitly enabled for now | ||
[profile.release] | ||
lto = true | ||
``` | ||
|
||
The target-cpu must be from the list [supported by LLVM] (or printed with `rustc --target amdgcn-amd-amdhsa --print target-cpus`). | ||
The GPU version on the current system can be found e.g. with [`rocminfo`]. | ||
|
||
Example `.cargo/config.toml` file to set the target and GPU generation: | ||
```toml | ||
# .cargo/config.toml | ||
[build] | ||
target = "amdgcn-amd-amdhsa" | ||
rustflags = ["-Ctarget-cpu=gfx1100"] | ||
|
||
[unstable] | ||
build-std = ["core"] # Optional: "alloc" | ||
``` | ||
|
||
## Running Rust programs | ||
|
||
To run a binary on an AMD GPU, a host runtime is needed. | ||
On Linux and Windows, [HIP] can be used to load and run binaries. | ||
Example code on how to load a compiled binary and run it is available in [ROCm examples]. | ||
|
||
On Linux, binaries can also run through the HSA runtime as implemented in [ROCR-Runtime]. | ||
|
||
<!-- Mention an allocator once a suitable one exists for amdgpu --> | ||
|
||
<!-- | ||
## Testing | ||
|
||
Does the target support running binaries, or do binaries have varying | ||
expectations that prevent having a standard way to run them? If users can run | ||
binaries, can they do so in some common emulator, or do they need native | ||
hardware? Does the target support running the Rust testsuite? | ||
|
||
--> | ||
|
||
## Additional information | ||
|
||
More information can be found on the [LLVM page for amdgpu]. | ||
|
||
[LLVM’s documentation]: https://llvm.org/docs/AMDGPUUsage.html#processors | ||
[ROCm documentation]: https://rocmdocs.amd.com | ||
[HIP]: https://rocm.docs.amd.com/projects/HIP/ | ||
[ROCR-Runtime]: https://github.com/ROCm/ROCR-Runtime | ||
[supported by LLVM]: https://llvm.org/docs/AMDGPUUsage.html#processors | ||
[LLVM page for amdgpu]: https://llvm.org/docs/AMDGPUUsage.html | ||
[`rocminfo`]: https://github.com/ROCm/rocminfo | ||
[ROCm examples]: https://github.com/ROCm/rocm-examples/tree/ca8ef5b6f1390176616cd1c18fbc98785cbc73f6/HIP-Basic/module_api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//@ assembly-output: emit-asm | ||
// ignore-tidy-linelength | ||
//@ revisions: amdgcn_amd_amdhsa | ||
//@ [amdgcn_amd_amdhsa] compile-flags: --target amdgcn-amd-amdhsa -Ctarget-cpu=gfx900 | ||
//@ [amdgcn_amd_amdhsa] needs-llvm-components: amdgpu | ||
|
||
// Sanity-check that each target can produce assembly code. | ||
|
||
#![feature(no_core, lang_items)] | ||
#![no_std] | ||
#![no_core] | ||
#![crate_type = "lib"] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
pub fn test() -> u8 { | ||
42 | ||
} | ||
|
||
// CHECK: .version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
error: target requires explicitly specifying a cpu with `-C target-cpu` | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//@ revisions: nocpu cpu | ||
//@ no-prefer-dynamic | ||
//@ compile-flags: --crate-type=cdylib --target=amdgcn-amd-amdhsa | ||
//@ needs-llvm-components: amdgpu | ||
//@ needs-rust-lld | ||
//@[nocpu] error-pattern: target requires explicitly specifying a cpu | ||
//@[nocpu] build-fail | ||
//@[cpu] compile-flags: -Ctarget-cpu=gfx900 | ||
//@[cpu] build-pass | ||
|
||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
|
||
#[lang="sized"] | ||
trait Sized {} | ||
|
||
pub fn foo() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.