Skip to content

Offload expose device selection - #158032

Open
Sa4dUs wants to merge 3 commits into
rust-lang:mainfrom
Sa4dUs:offload-device-selection
Open

Offload expose device selection#158032
Sa4dUs wants to merge 3 commits into
rust-lang:mainfrom
Sa4dUs:offload-device-selection

Conversation

@Sa4dUs

@Sa4dUs Sa4dUs commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

get amount of available devices with omp_get_num_devices and add arg to the intrinsic for device selection

r? @ZuseZ4

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 17, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 17, 2026
@rust-log-analyzer

This comment has been minimized.

@bjorn3

bjorn3 commented Jun 17, 2026

Copy link
Copy Markdown
Member

Does it actually need to be a rust intrinsic? Does LLVM give omp_get_num_devices any special behavior or is it just a regular runtime call that we could directly call from rust code too?

@ZuseZ4

ZuseZ4 commented Jun 17, 2026

Copy link
Copy Markdown
Member

@bjorn3 Yes, you could call all of our openmp/offload libraries directly, especially here it's easy enough. Some of the other ones we use for data transfer and kernel launches are just a bit more involved to set up, so we do it in the compiler.

The two problems of exposing it are that you'd need to link in the GCC or LLVM libraries yourself, if you build and use the Offload setup, we do it for users. Also cranelift doesn't provide it, with an intrinsic we could have better fallback behaviour.

@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch 2 times, most recently from abcdd8e to d806053 Compare August 13, 2026 15:16
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch from d806053 to 9a535b4 Compare August 13, 2026 15:34
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch from 9a535b4 to c3e2e4c Compare August 13, 2026 16:24
@Sa4dUs
Sa4dUs marked this pull request as ready for review August 13, 2026 18:03
@rustbot

rustbot commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 13, 2026
@ZuseZ4

ZuseZ4 commented Aug 13, 2026

Copy link
Copy Markdown
Member
  1. We should probably land the offload macro first, we're starting to have a lot of arguments which can be annoying without it. Also reduces the churn on tests.
  2. Let's discuss how much effort it would be to directly account for CPU offload.

@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch from c3e2e4c to 2b57373 Compare August 14, 2026 15:04
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Aug 14, 2026
@rustbot

rustbot commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ZuseZ4

ZuseZ4 commented Aug 16, 2026

Copy link
Copy Markdown
Member

The code should work on the technical side, but I feel like exposing the -1 as "any device" to the frontend is a little too low-level.
How about in the macro, we only allow device >= 0 if a user wants to target a specific GPU and use -1 as the fallback, if it's left unspecified by the user?

Also, could you add a check to make sure that if we select GPU X, that this GPU is also available? Right now I often forget to load rocm on a server, run the binary, and have it fail with some obscure llvm error since offload fails (no gpu available) but we prohibit falling back to the CPU. It would be nice to get a more meaningfull error message on the Rust side already.

Comment thread compiler/rustc_codegen_llvm/src/intrinsic.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs Outdated
Comment thread tests/codegen-llvm/gpu_offload/gpu_host.rs Outdated
Comment thread tests/codegen-llvm/gpu_offload/gpu_host.rs Outdated
@ZuseZ4

ZuseZ4 commented Aug 19, 2026

Copy link
Copy Markdown
Member

Left just a nit, other than that, can you add the later cleanup commits into the first three? Otherwise names seem also good now, so lgtm.

@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch from 6f14972 to 9f09134 Compare August 19, 2026 19:15
@Sa4dUs
Sa4dUs force-pushed the offload-device-selection branch from 9f09134 to 3777642 Compare August 19, 2026 19:29
@ZuseZ4

ZuseZ4 commented Aug 19, 2026

Copy link
Copy Markdown
Member

We should later add a way to give users information about the different GPU devices. Servers just have 4/8x identical GPUs, but the majority of end-users will probably have a tiny igpu and a large dgpu.
But for now things like offloading to the CPU seem to be in higher demand, so let's do that first.

@bors r+ rollup

@rust-bors

rust-bors Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 3777642 has been approved by ZuseZ4

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 10. This pull request will be tested once the tree is reopened.

Reason for tree closure: Main branch is broken

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants