Skip to content

Add support for generics in offloadand remove no_mangle attribute - #159566

Open
Sa4dUs wants to merge 6 commits into
rust-lang:mainfrom
Sa4dUs:offload-generics
Open

Add support for generics in offloadand remove no_mangle attribute#159566
Sa4dUs wants to merge 6 commits into
rust-lang:mainfrom
Sa4dUs:offload-generics

Conversation

@Sa4dUs

@Sa4dUs Sa4dUs commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

View all comments

implements a 3rd pass where we collect kernel instantiations required by offload to work
also enforces consistent mangling for offload functions between host and device

closes: #150985

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 Jul 19, 2026
@Sa4dUs
Sa4dUs force-pushed the offload-generics branch from 7fefbf7 to a019aea Compare July 19, 2026 14:10
@rust-log-analyzer

This comment has been minimized.

// core/std/allocators/etc. For example symbols used to hook up allocation
// are not considered for export
let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
let is_extern = codegen_fn_attrs.contains_extern_indicator();

@bjorn3 bjorn3 Jul 19, 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.

Perhaps contains_extern_indicator should return true for offload functions?

View changes since the review

Comment thread compiler/rustc_monomorphize/src/collector.rs Outdated
Comment thread compiler/rustc_codegen_ssa/src/base.rs Outdated
@ZuseZ4

ZuseZ4 commented Jul 20, 2026

Copy link
Copy Markdown
Member

Great improvement ( I'll review later).
@Flakebi fyi, I think now we only have the abi checks left to implement for more complex types and offload should cover all the bigger things we talked about?

@rust-bors

This comment has been minimized.

Comment thread compiler/rustc_codegen_ssa/src/back/write.rs Outdated
@Sa4dUs
Sa4dUs force-pushed the offload-generics branch from b40ecd1 to cb87656 Compare July 23, 2026 16:44
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-generics branch from cb87656 to f684946 Compare July 24, 2026 13:07
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs marked this pull request as ready for review July 24, 2026 15:07
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@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 Jul 24, 2026
Comment thread compiler/rustc_monomorphize/src/collector.rs
Comment thread compiler/rustc_monomorphize/src/offload_manifest.rs
Comment thread compiler/rustc_monomorphize/src/offload_manifest.rs Outdated
Comment thread compiler/rustc_monomorphize/src/offload_manifest.rs Outdated
@bjorn3
bjorn3 self-requested a review July 24, 2026 20:49
@Flakebi

Flakebi commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Very nice!
If I read this correctly, there are two important parts here:

  1. Not adding the hash suffix when mangling offload kernel names makes the mangling deterministic
  2. Compilation first compiles the CPU code, which generates a new artifact – a list of function instances of kernels that are referenced. When compiling the GPU code afterwards, this list is added to the monomorphization roots.

On a sidenote: As due to 1., no more hash suffix is added to kernels, we should ensure that we fail compilation if someone tries to compile a program that defines the same mangled kernel name twice.
E.g. by importing two different versions of the same crate that defines the same kernel.
This might already fail to compile but a test for that would be good.

@ZuseZ4 ZuseZ4 left a comment

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.

Just some smaller questions, at the end Oli should probably have another look. But other than cleaning up the old Host, can you add some actual tests for the generic functions?

View changes since this review

.iter()
.any(|o| matches!(o, rustc_session::config::Offload::HostMetadata(_)));
if is_host_metadata {
let crate_items = tcx.hir_crate_items(());

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.

What about trait_items? The impl_items note says it also covers traits, but he owners function on ModuleItems still collects both so they might not be exactly the same?

Comment thread compiler/rustc_session/src/config.rs Outdated
Comment thread compiler/rustc_monomorphize/src/offload_manifest.rs Outdated
@rust-bors

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-generics branch from 2c1b875 to 7860122 Compare August 5, 2026 17:49
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Aug 5, 2026
@rustbot

rustbot commented Aug 5, 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.

@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-generics branch from 7860122 to 3686d5c Compare August 5, 2026 18:43
.emit("obj")
.run();

assert!(object_contains_any_symbol_substring("generic.o", &["6kernelfEB2_"]));

@ZuseZ4 ZuseZ4 Aug 6, 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.

Can you explicitely set the mangling version for that? We just moved to v0 so I don't think it will change soon, but just in case.

View changes since the review

fn kernel(_x: f32) {} //~ ERROR symbol `_RNvC19collision_kernels_a6kernel` is already defined

fn main() {
_RNvC19collision_kernels_a6kernel(0.0);

@ZuseZ4 ZuseZ4 Aug 6, 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.

same here, otherwise this test silently becomes useless in the (tbh probably far) future.

View changes since the review

/// produced by a previous `HostMetadata` pass. An empty manifest
/// means all kernel instantiations are discovered via monomorphization.
Device(String),
/// Second step in the offload pipeline, generates the host code to call kernels.

@ZuseZ4 ZuseZ4 Aug 6, 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.

That's the third step now, right?
And maybe describe Device(String) above as the second step.

View changes since the review

Device,
/// Reads a manifest of required generic kernel instantiations
/// produced by a previous `HostMetadata` pass. An empty manifest
/// means all kernel instantiations are discovered via monomorphization.

@ZuseZ4 ZuseZ4 Aug 6, 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.

The last sentence is imho a little too complicated for people not aware of how gpu compilation works. Can you clarify that it means there are either no generic kernels at all, or that generic functions are only called from non-generic gpu entry points, but never from the Host (cpu), so we don't need to track it?

View changes since the review

}
Offload::Device
}
"Device" => {

@ZuseZ4 ZuseZ4 Aug 6, 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.

This seems risky. Do we throw a good error if there's a generic kernel but a user passes Device without a path? I'd expect he only get's a cryptic error, or?

View changes since the review

/// (which differs between host and device passes).
pub fn mangle_offload_export<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> String {
let instantiating_crate = if is_generic(instance) {
Some(instance.upstream_monomorphization(tcx).unwrap_or(LOCAL_CRATE))

@ZuseZ4 ZuseZ4 Aug 6, 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.

While we might compile a lot of code for the gpu, few functions will be entry points, so I guess it will be ok-ish. Otherwise we can think of smarter mangling in the future.

View changes since the review

@ZuseZ4

ZuseZ4 commented Aug 6, 2026

Copy link
Copy Markdown
Member

Lgtm in general and from the offload side, but let's ping @oli-obk since we talked about it at RustWeek. Is that roughly what you had in mind, or do you see any general issues?

@ZuseZ4

ZuseZ4 commented Aug 6, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 6, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 6, 2026
Add support for generics in `offload`and remove `no_mangle` attribute
@rust-bors

rust-bors Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 427b2a9 (427b2a96dd79c4d6ce4aaca78ba5e82fd45d2b37)
Base parent: 84b36a7 (84b36a78a28a63f134171c670be1932ffa2485f8)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (427b2a9): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
-0.4% [-0.5%, -0.2%] 7
All ❌✅ (primary) -0.4% [-0.4%, -0.4%] 1

Max RSS (memory usage)

Results (primary -5.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-5.5% [-5.5%, -5.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -5.5% [-5.5%, -5.5%] 1

Cycles

Results (primary 3.0%, secondary -7.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.0% [3.0%, 3.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-7.5% [-12.9%, -2.0%] 2
All ❌✅ (primary) 3.0% [3.0%, 3.0%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 457.798s -> 460.645s (0.62%)
Artifact size: 399.35 MiB -> 398.64 MiB (-0.18%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 6, 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-review Status: Awaiting review from the assignee but also interested parties. 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.

std::offload requires mangled names

7 participants