Skip to content

offload: automate manual clang-linker-wrapper step - #162309

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
sgasho:offload-clang-linker-wrapper
Sep 8, 2026
Merged

offload: automate manual clang-linker-wrapper step#162309
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
sgasho:offload-clang-linker-wrapper

Conversation

@sgasho

@sgasho sgasho commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

automate manual clang-linker-wrapper step from https://rustc-dev-guide.rust-lang.org/offload/usage.html
extract bitcode from device.bin and then wraps it into the host module.
needs some refactoring.

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 Sep 4, 2026
Comment thread compiler/rustc_codegen_llvm/src/diagnostics.rs Outdated
);

if sess.opts.unstable_opts.offload.iter().any(|o| matches!(o, config::Offload::Host(_))) {
cmd.link_dylib_by_name("omptarget", false, true);

@ZuseZ4 ZuseZ4 Sep 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks to your last PR, we should ship these in our sysroot. Is it guaranteed to be on our default search path? If not, we might want to add an rpath or -L, or?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, looking at walk_native_lib_search_dirs, it is on our default linker search path, so we don't need an additional -L, but we need an rpath for runtime. I also checked this on a UofT server.

@sgasho

sgasho commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

I'm looking into this issue I encountered while testing on a gpu server

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ ./target/x86_64-unknown-linux-gnu/release/sample-offload 
PluginInterface error: Failure to load binary image 0x62597fffacf0 on device 0: "jit compile failure while processing binary image" failure to jit IR image: Unable to find target for this triple (no targets are registered)
omptarget error: Failed to load image "a provided binary image is malformed" failed to load binary 0x62597fffacf0
omptarget fatal error 0: "the plugin backend is in an invalid or unsupported state" failed to load images on device '0'
Aborted                    ./target/x86_64-unknown-linux-gnu/release/sample-offload

update:
fixing by bringin more logic from ClangLinerWrapper.cpp...

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Sep 6, 2026
@sgasho

sgasho commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Succeeded on UofT server, though we still need -rpath for now

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ RUSTFLAGS="-Ctarget-cpu=sm_86 --emit=llvm-bc,llvm-ir -Zoffload=Device -Csave-temps -Zunstable-options" \
cargo +offload-local build -Zunstable-options -r -v \
  --target nvptx64-nvidia-cuda -Zbuild-std=core
    (omitted...)
    Finished `release` profile [optimized] target(s) in 40.58s

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ RUSTFLAGS="-C embed-bitcode=yes" \
cargo +offload-local rustc \
  --release \
  --bin sample-offload \
  --target x86_64-unknown-linux-gnu \
  -- \
  -C lto=fat \
  -Zunstable-options \
  "-Zoffload=Host=$PWD/target/nvptx64-nvidia-cuda/release/build/sample-offload/839aa6816b5baeb2/out/device.bin" \
  -C "link-arg=-Wl,-rpath,$(rustc +offload-local --print target-libdir)"
info: `cargo` is unavailable for the active toolchain
info: falling back to "/u/shotasugano/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"
   Compiling libc v0.2.189
   Compiling sample-offload v0.1.0 (/w/333/shotasugano/sample-offload)
    Finished `release` profile [optimized] target(s) in 8.87s

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ ./target/x86_64-unknown-linux-gnu/release/sample-offload 
all checks passedshotasugano@gpunode1:/w/333/shotasugano/sample-offload$ 

@sgasho

sgasho commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Succeeded without rpath

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ RUSTFLAGS="-Ctarget-cpu=sm_86 --emit=llvm-bc,llvm-ir -Zoffload=Device -Csave-temps -Zunstable-options" \
cargo +offload-local build -Zunstable-options -r -v \
  --target nvptx64-nvidia-cuda -Zbuild-std=core
    (omitted...)
    Finished `release` profile [optimized] target(s) in 40.91s

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ RUSTFLAGS="-C embed-bitcode=yes" \
cargo +offload-local rustc \
  --release \
  --bin sample-offload \
  --target x86_64-unknown-linux-gnu \
  -- \
  -C lto=fat \
  -Zunstable-options \
  "-Zoffload=Host=$PWD/target/nvptx64-nvidia-cuda/release/build/sample-offload/839aa6816b5baeb2/out/device.bin"
info: `cargo` is unavailable for the active toolchain
info: falling back to "/u/shotasugano/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"
   Compiling libc v0.2.189
   Compiling sample-offload v0.1.0 (/w/333/shotasugano/sample-offload)
    Finished `release` profile [optimized] target(s) in 2.41s

shotasugano@gpunode1:/w/333/shotasugano/sample-offload$ ./target/x86_64-unknown-linux-gnu/release/sample-offload 
all checks passedshotasugano@gpunode1:/w/333/shotasugano/sample-offload$ 

@ZuseZ4

ZuseZ4 commented Sep 6, 2026

Copy link
Copy Markdown
Member

Exciting, let me test it on some AMD server as well then.

@sgasho

sgasho commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!
Also, I think we'd better test this via tarball from a bors try before merging since I modified dist.rs

@ZuseZ4

ZuseZ4 commented Sep 6, 2026

Copy link
Copy Markdown
Member

I'll look a little closer later, but this adds clang and a few other binaries, right? My hope was that we could directly call the underlying APIs without adding more binaries. During compilation of nvptx64 I don't think we can get around relying on ptxas, but other than that we should already have the APIs there to compile everything ahead of time.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the offload-clang-linker-wrapper branch from d659d8c to ff680e2 Compare September 7, 2026 17:49
@sgasho

sgasho commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

I confirmed that the sample offload code compiled successfully on the UofT gpu server with the latest commit, without adding extra binaries.

@sgasho
sgasho marked this pull request as ready for review September 7, 2026 18:16
@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 Sep 7, 2026
@ZuseZ4

ZuseZ4 commented Sep 8, 2026

Copy link
Copy Markdown
Member

@bors r+ rollup p=1 unblocks offload in nightly

@rust-bors

rust-bors Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ff680e2 has been approved by ZuseZ4

It is now in the queue for this repository.

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

Reason for tree closure: spurious failures

@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 Sep 8, 2026
@ZuseZ4

ZuseZ4 commented Sep 8, 2026

Copy link
Copy Markdown
Member

there are a few more things to clean up, but looks good for now. Let's see if we can land it today, so that tomorrows nightly during rustconf already has support.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 8, 2026
…r, r=ZuseZ4

offload: automate manual clang-linker-wrapper step

automate manual clang-linker-wrapper step from https://rustc-dev-guide.rust-lang.org/offload/usage.html
extract bitcode from device.bin and then wraps it into the host module.
needs some refactoring.

r? @ZuseZ4
rust-bors Bot pushed a commit that referenced this pull request Sep 8, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #162309 (offload: automate manual clang-linker-wrapper step)
 - #160505 (delegation: supporting inherent impls)
 - #160712 (windows-gnullvm: always link libunwind statically)
 - #161423 (trait_selection: Keep type-op region constraints in borrowck)
 - #162461 (limit the api of `fold_predicate` and `visit_predicate`)
 - #162475 (Fix unsoundness bug on next trait solver for dyn const generics placeholder)
@rust-bors
rust-bors Bot merged commit 16f306c into rust-lang:main Sep 8, 2026
13 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 8, 2026
Rollup merge of #162309 - sgasho:offload-clang-linker-wrapper, r=ZuseZ4

offload: automate manual clang-linker-wrapper step

automate manual clang-linker-wrapper step from https://rustc-dev-guide.rust-lang.org/offload/usage.html
extract bitcode from device.bin and then wraps it into the host module.
needs some refactoring.

r? @ZuseZ4
@rustbot rustbot added this to the 1.100.0 milestone Sep 8, 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. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants