Skip to content

Conversation

@ZuseZ4
Copy link
Member

@ZuseZ4 ZuseZ4 commented Nov 22, 2025

This automates steps 2+3 (the clang invocations) of the rust offload usage pipeline.
Now all that remains is a clang-linker-invocation after this step.

r? oli-obk

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-rustc-dev-guide Area: rustc-dev-guide 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 Nov 22, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 added the F-gpu_offload `#![feature(gpu_offload)]` label Nov 22, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 mentioned this pull request Nov 23, 2025
5 tasks
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 5992ea5 to 0588363 Compare November 23, 2025 09:01
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Nov 23, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 0588363 to 99348e0 Compare November 23, 2025 09:05
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 99348e0 to b117ff4 Compare November 23, 2025 15:01
@ZuseZ4 ZuseZ4 changed the title Automate offload clangs automate offload, part 2 - clang calls Nov 23, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 mentioned this pull request Nov 24, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from b117ff4 to 3a9d453 Compare November 25, 2025 23:47
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Nov 25, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 89a17be to ebbe811 Compare November 26, 2025 18:35
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 3632e16 to 9b4ad11 Compare November 27, 2025 20:53
@rustbot rustbot added A-CI Area: Our Github Actions CI A-testsuite Area: The testsuite used to check the correctness of rustc T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. labels Nov 27, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 9b4ad11 to b117ff4 Compare November 27, 2025 20:59
@rust-log-analyzer

This comment has been minimized.

let host_out_c = path_to_c_string(device_pathbuf.as_path());

// 2) Finalize host: lib.bc + host.out -> host.offload.o (host TM created in C++)
let llmod2 = llvm::LLVMCloneModule(module.module_llvm.llmod());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My feeling here is that embedding a device artifact into a host module would break incremental compilation, so I create a copy. It's likely not great for compile times, but better correct than fast. Also at least amd is currently using build-std, so compile times are a lost cause anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could do the loading in a query in the future, then it is tracked in incremental just like include_bytes could be


Expected<std::unique_ptr<Module>>
loadHostModuleFromBitcode(LLVMContext &Ctx, StringRef LibBCPath) {
auto MBOrErr = MemoryBuffer::getFile(LibBCPath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have MemoryBuffer, but ModuleBuffer exposed in our RustWrappers. However, they don't seem to be the same. I had some issues exposing MemoryBuffers, especially a constructor for them in Rust, so I left 25 lines of C++.

let variant = match key {
"Host" => {
if let Some(p) = arg {
Offload::Host(p.to_string())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could already do the argument verification here - E.g. is this an absolute path, does it point to a file called host.out, and does the full path with file exist. However, we don't have the error contexts here, so I left them for rustc_cg_llvm, even if that's a bit late.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have to happen later. We may want to do it earlier than the backend at some point. For now this is fine

"clang-21" "-cc1as" "-triple" "x86_64-unknown-linux-gnu" "-filetype" "obj" "-main-file-name" "lib.rs" "-target-cpu" "x86-64" "-mrelocation-model" "pic" "-o" "host.o" "host.s"
"clang-linker-wrapper" "--should-extract=gfx90a" "--device-compiler=amdgcn-amd-amdhsa=-g" "--device-compiler=amdgcn-amd-amdhsa=-save-temps=cwd" "--device-linker=amdgcn-amd-amdhsa=-lompdevice" "--host-triple=x86_64-unknown-linux-gnu" "--save-temps" "--linker-path=/ABSOlUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/lld/bin/ld.lld" "--hash-style=gnu" "--eh-frame-hdr" "-m" "elf_x86_64" "-pie" "-dynamic-linker" "/lib64/ld-linux-x86-64.so.2" "-o" "bare" "/lib/../lib64/Scrt1.o" "/lib/../lib64/crti.o" "/ABSOLUTE_PATH_TO/crtbeginS.o" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/bin/../lib/x86_64-unknown-linux-gnu" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/lib/clang/21/lib/x86_64-unknown-linux-gnu" "-L/lib/../lib64" "-L/usr/lib64" "-L/lib" "-L/usr/lib" "host.o" "-lstdc++" "-lm" "-lomp" "-lomptarget" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/lib" "-lgcc_s" "-lgcc" "-lpthread" "-lc" "-lgcc_s" "-lgcc" "/ABSOLUTE_PATH_TO/crtendS.o" "/lib/../lib64/crtn.o"
"clang-linker-wrapper" "--should-extract=gfx90a" "--device-compiler=amdgcn-amd-amdhsa=-g" "--device-compiler=amdgcn-amd-amdhsa=-save-temps=cwd" "--device-linker=amdgcn-amd-amdhsa=-lompdevice" "--host-triple=x86_64-unknown-linux-gnu" "--save-temps" "--linker-path=/ABSOlUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/lld/bin/ld.lld" "--hash-style=gnu" "--eh-frame-hdr" "-m" "elf_x86_64" "-pie" "-dynamic-linker" "/lib64/ld-linux-x86-64.so.2" "-o" "bare" "/lib/../lib64/Scrt1.o" "/lib/../lib64/crti.o" "/ABSOLUTE_PATH_TO/crtbeginS.o" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/bin/../lib/x86_64-unknown-linux-gnu" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/lib/clang/21/lib/x86_64-unknown-linux-gnu" "-L/lib/../lib64" "-L/usr/lib64" "-L/lib" "-L/usr/lib" "target/<GPU_DIR>/release/host.o" "-lstdc++" "-lm" "-lomp" "-lomptarget" "-L/ABSOLUTE_PATH_TO/rust/build/x86_64-unknown-linux-gnu/llvm/lib" "-lgcc_s" "-lgcc" "-lpthread" "-lc" "-lgcc_s" "-lgcc" "/ABSOLUTE_PATH_TO/crtendS.o" "/lib/../lib64/crtn.o"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't wait to get rid of this one too.

{
let device_pathbuf = PathBuf::from(device_path);
if device_pathbuf.is_relative() {
panic!("Absolute path is needed");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently creating proper errors for all panic/assertions.

@ZuseZ4
Copy link
Member Author

ZuseZ4 commented Dec 9, 2025

r? @oli-obk

I got the c++ code down to 25 lines! Also Rustc does the new writing to file, as you suggested in your last PR.
I left some comments, and I'm just writing a follow-up commit to replace the panics/asserts with proper errors. Done.
Any other thoughts?

@rustbot
Copy link
Collaborator

rustbot commented Dec 9, 2025

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@ZuseZ4 ZuseZ4 marked this pull request as ready for review December 9, 2025 14:30
@rustbot
Copy link
Collaborator

rustbot commented Dec 9, 2025

The rustc-dev-guide subtree was changed. If this PR only touches the dev guide consider submitting a PR directly to rust-lang/rustc-dev-guide otherwise thank you for updating the dev guide with your changes.

cc @BoxyUwU, @jieyouxu, @Kobzol, @tshepang

@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 Dec 9, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 970f1b1 to e83fef0 Compare December 9, 2025 15:11
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from e83fef0 to e01b74f Compare December 9, 2025 15:34
@rust-log-analyzer

This comment has been minimized.

let host_out_c = path_to_c_string(device_pathbuf.as_path());

// 2) Finalize host: lib.bc + host.out -> host.offload.o (host TM created in C++)
let llmod2 = llvm::LLVMCloneModule(module.module_llvm.llmod());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could do the loading in a query in the future, then it is tracked in incremental just like include_bytes could be

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch 2 times, most recently from c02f6ff to 604668f Compare December 9, 2025 17:02
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 604668f to 05e647a Compare December 9, 2025 17:43
@ZuseZ4 ZuseZ4 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 Dec 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2025

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-CI Area: Our Github Actions CI A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-rustc-dev-guide Area: rustc-dev-guide A-testsuite Area: The testsuite used to check the correctness of rustc F-gpu_offload `#![feature(gpu_offload)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants