about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-02-10 05:18:36 +0000
committerbors <bors@rust-lang.org>2025-02-10 05:18:36 +0000
commitc03c38d5c2368cd2aa0e056dba060b94fc747f4e (patch)
treea1b3efb8bcd3db6efcc8e02ba158e00595175407 /src/doc
parentd9a4a47b8b3dc0bdff83360cea2013200d60d49c (diff)
parent56795fb77a69bc61836537175564455067adada6 (diff)
downloadrust-c03c38d5c2368cd2aa0e056dba060b94fc747f4e.tar.gz
rust-c03c38d5c2368cd2aa0e056dba060b94fc747f4e.zip
Auto merge of #134740 - Flakebi:amdgpu-target, r=workingjubilee
Add amdgpu target

Add amdgpu target to rustc and enable the LLVM target.

Fix compiling `core` with the amdgpu:
The amdgpu backend makes heavy use of different address spaces. This
leads to situations, where a pointer in one addrspace needs to be casted
to a pointer in a different addrspace. `bitcast` is invalid for this
case, `addrspacecast` needs to be used.

Fix compilation failures that created bitcasts for such cases by
creating pointer casts (which creates an `addrspacecast` under the hood)
instead.

MCP: https://github.com/rust-lang/compiler-team/issues/823
Tracking issue: #135024
Kinda related to the original amdgpu tracking issue #51575 (though that one has been closed for a while).
Diffstat (limited to 'src/doc')
-rw-r--r--src/doc/rustc/src/SUMMARY.md1
-rw-r--r--src/doc/rustc/src/platform-support.md2
-rw-r--r--src/doc/rustc/src/platform-support/amdgcn-amd-amdhsa.md111
3 files changed, 114 insertions, 0 deletions
diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md
index 670e4bd1be6..f78d3d4aee8 100644
--- a/src/doc/rustc/src/SUMMARY.md
+++ b/src/doc/rustc/src/SUMMARY.md
@@ -29,6 +29,7 @@
     - [\*-apple-watchos](platform-support/apple-watchos.md)
     - [\*-apple-visionos](platform-support/apple-visionos.md)
     - [aarch64-nintendo-switch-freestanding](platform-support/aarch64-nintendo-switch-freestanding.md)
+    - [amdgcn-amd-amdhsa](platform-support/amdgcn-amd-amdhsa.md)
     - [armeb-unknown-linux-gnueabi](platform-support/armeb-unknown-linux-gnueabi.md)
     - [arm-none-eabi](platform-support/arm-none-eabi.md)
       - [armv4t-none-eabi](platform-support/armv4t-none-eabi.md)
diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md
index 895014b499d..bb89d97a798 100644
--- a/src/doc/rustc/src/platform-support.md
+++ b/src/doc/rustc/src/platform-support.md
@@ -272,6 +272,7 @@ target | std | host | notes
 `aarch64_be-unknown-linux-gnu` | ✓ | ✓ | ARM64 Linux (big-endian)
 `aarch64_be-unknown-linux-gnu_ilp32` | ✓ | ✓ | ARM64 Linux (big-endian, ILP32 ABI)
 [`aarch64_be-unknown-netbsd`](platform-support/netbsd.md) | ✓ | ✓ | ARM64 NetBSD (big-endian)
+[`amdgcn-amd-amdhsa`](platform-support/amdgcn-amd-amdhsa.md) | * |  | `-Ctarget-cpu=gfx...` to specify [the AMD GPU] to compile for
 [`arm64_32-apple-watchos`](platform-support/apple-watchos.md) | ✓ |  | Arm Apple WatchOS 64-bit with 32-bit pointers
 [`arm64e-apple-darwin`](platform-support/arm64e-apple-darwin.md)  | ✓ | ✓ | ARM64e Apple Darwin
 [`arm64e-apple-ios`](platform-support/arm64e-apple-ios.md) | ✓ | | ARM64e Apple iOS
@@ -432,3 +433,4 @@ target | std | host | notes
 [`xtensa-esp32s3-none-elf`](platform-support/xtensa.md) | * |  | Xtensa ESP32-S3
 
 [runs on NVIDIA GPUs]: https://github.com/japaric-archived/nvptx#targets
+[the AMD GPU]: https://llvm.org/docs/AMDGPUUsage.html#processors
diff --git a/src/doc/rustc/src/platform-support/amdgcn-amd-amdhsa.md b/src/doc/rustc/src/platform-support/amdgcn-amd-amdhsa.md
new file mode 100644
index 00000000000..0b2f798e66d
--- /dev/null
+++ b/src/doc/rustc/src/platform-support/amdgcn-amd-amdhsa.md
@@ -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