Skip to content

Offload safe mutable args with Region and PartitioningStrategy - #158076

Open
Sa4dUs wants to merge 4 commits into
rust-lang:mainfrom
Sa4dUs:offload-region
Open

Sa4dUs wants to merge 4 commits into
rust-lang:mainfrom
Sa4dUs:offload-region

Conversation

@Sa4dUs

@Sa4dUs Sa4dUs commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Now Region are lang items and mapped as slices.
needs #156620 to work

r? @ZuseZ4

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 18, 2026
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs
Sa4dUs force-pushed the offload-region branch 2 times, most recently from c075a56 to 2a627bf Compare August 13, 2026 14:05
@rust-log-analyzer

This comment has been minimized.

@Sa4dUs Sa4dUs changed the title offload region Offload safe mutable args with Region and PartitioningStrategy Aug 13, 2026
@Sa4dUs
Sa4dUs marked this pull request as ready for review August 13, 2026 16:21
@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
@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

Comment thread library/core/src/offload/mod.rs Outdated
Comment thread tests/codegen-llvm/gpu_offload/region_host.rs Outdated
Comment thread library/core/src/offload/mod.rs Outdated
@rustbot

This comment has been minimized.

@ZuseZ4

ZuseZ4 commented Sep 5, 2026

Copy link
Copy Markdown
Member

Next round. I think we should actually refuse all cases (for now) where we have a Region within any ADT/struct/slice, etc. not just &Region. Most of them would be transfered incorrectly, and I don't think we gain much. The other direction is something like Region<Vec<f32>, S>, where we also transfer T incorrectly. But to be fair, I think that direction is already unchecked on main. I assume that offloading a vec will still do the wrong thing, so I'm fine if we accept that here as well till we implement a proper handling. But also feel free to already reject it already if you want.

Comment thread compiler/rustc_middle/src/ty/offload_meta.rs
Comment thread library/core/src/offload/mod.rs Outdated
@rustbot

rustbot commented Sep 13, 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 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.

I left another round, mostly about comments and tightening the checks.
I feel like we'll need to restrict get and get_mut a bit further (or at least describe them better), but we can leave that for the last round (and discuss on Wednesday).

Now that it seems closer to the final version, can you add a description to
https://rustc-dev-guide.rust-lang.org/offload/internals.html ?
Probably best as a new subsection, so that we have one about the build pipeline, and one about the safety/region design?

View changes since this review

//@ edition: 2024

#![feature(gpu_offload)]
#![feature(offload)]

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.

Is there a reason why you added another feature instead of reusing the existing one?


/// no-op launch check for kernel arguments that are not [`Region`]s.
#[doc(hidden)]
#[unstable(feature = "offload", issue = "131513")]

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, can you reuse gpu_offload as fueatre?

/// Checks that a kernel using this strategy can be launched with `len`
/// elements on a `grid` by `block` launch configuration.
///
/// This is called automatically before the kernel is launched.

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 make it more explicit (here and in the trait safety note) that this is also soundness-critical?
In summary, this is only allowed to unconditionally return Ok iff. the given strategy can run with arbitrary len+grid+block values. If there are any combinations where an impl would return incorrect results or cause UB, then check_launch must return a LaunchError. We can later ask Ralf or someone unbiased to check that our description is unambiguous.

}

fn contains_nested_offload_region<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
ty.walk().skip(1).any(|arg| arg.as_type().is_some_and(|ty| is_region_ty(tcx, ty)))

@ZuseZ4 ZuseZ4 Sep 15, 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.

Ty::walk skips a few relevant cases:

     2     /// Iterator that walks `self` and any types reachable from
     1     /// `self`, in depth-first order. Note that just walks the types
  2175     /// that appear in `self`, it does not descend into the fields of
     1     /// structs or variants. For example:

E.g.

struct Wrapper<'a> {
    region: Region<'a, f32, Dummy>,
    scalar: f32,
}

$crate::offload!(@device $device),
($({
let __offload_arg = $arg;
(&__offload_arg).__offload_check_launch(__offload_grid, __offload_block);

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.

It seems like I can skip this this test safely by just replacing

offload! {... args = (foo,bar), }

with

let t = (foo,bar);
offload! { ... args = t, }

Just to get started, you can just reject the second case and leave a fixme note in a test and macro for now, we can do something smarter later.

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

4 participants