-
-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Offload safe mutable args with Region and PartitioningStrategy
#158076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sa4dUs
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
Sa4dUs:offload-region
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
tests/codegen-llvm/gpu_offload/auxiliary/offload_strategies.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //@ edition: 2024 | ||
|
|
||
| #![feature(gpu_offload)] | ||
| #![feature(offload)] | ||
|
|
||
| use core::offload::{LaunchError, PartitioningStrategy}; | ||
| use core::ptr::NonNull; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Dummy; | ||
|
|
||
| unsafe impl PartitioningStrategy for Dummy { | ||
| type View<'a, T: 'a> = &'a T; | ||
| type ViewMut<'a, T: 'a> = &'a mut T; | ||
|
|
||
| fn index() -> usize { | ||
| 0 | ||
| } | ||
|
|
||
| unsafe fn get<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::View<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| unsafe fn get_mut<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::ViewMut<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| fn check_launch(_len: usize, _grid: [u32; 3], _block: [u32; 3]) -> Result<(), LaunchError> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Linear1D; | ||
|
|
||
| unsafe impl PartitioningStrategy for Linear1D { | ||
| type View<'a, T: 'a> = &'a T; | ||
| type ViewMut<'a, T: 'a> = &'a mut T; | ||
|
|
||
| fn index() -> usize { | ||
| 0 | ||
| } | ||
|
|
||
| unsafe fn get<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::View<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| unsafe fn get_mut<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::ViewMut<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| fn check_launch(_len: usize, grid: [u32; 3], block: [u32; 3]) -> Result<(), LaunchError> { | ||
| if grid[1] == 1 && grid[2] == 1 && block[1] == 1 && block[2] == 1 { | ||
| Ok(()) | ||
| } else { | ||
| Err(LaunchError::new("")) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //@ compile-flags: -Zoffload=Test -Zunstable-options -C opt-level=1 -Clto=fat | ||
| //@ no-prefer-dynamic | ||
| //@ needs-offload | ||
| //@ edition: 2024 | ||
| //@ aux-crate: offload_strategies=offload_strategies.rs | ||
|
|
||
| // This test verifies that a `Region` kernel argument is mapped like a slice. | ||
| #![feature(abi_gpu_kernel)] | ||
| #![feature(core_intrinsics)] | ||
| #![feature(gpu_offload)] | ||
| #![feature(offload)] | ||
| #![feature(rustc_attrs)] | ||
| #![no_main] | ||
|
|
||
| extern crate core; | ||
|
|
||
| use core::offload::Region; | ||
|
|
||
| use offload_strategies::Dummy; | ||
|
|
||
| // CHECK: @anon.[[ID:.*]].0 = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1 | ||
|
|
||
| // CHECK-DAG: @.offload_sizes.[[K:[^ ]*foo]] = private unnamed_addr constant [2 x i64] [i64 0, i64 8] | ||
| // CHECK-DAG: @.offload_maptypes.[[K]].begin = private unnamed_addr constant [2 x i64] [i64 1, i64 768] | ||
| // CHECK-DAG: @.offload_maptypes.[[K]].kernel = private unnamed_addr constant [2 x i64] [i64 32, i64 800] | ||
| // CHECK-DAG: @.offload_maptypes.[[K]].end = private unnamed_addr constant [2 x i64] [i64 2, i64 0] | ||
|
|
||
| // CHECK: define{{( dso_local)?}} void @main() | ||
| // CHECK: %.offload_sizes = alloca [2 x i64], align 8 | ||
| // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr {{.*}} %.offload_sizes, ptr {{.*}} @.offload_sizes.[[K]], i64 16, i1 false) | ||
| // CHECK: store i64 16, ptr %.offload_sizes, align 8 | ||
| // CHECK: call void @__tgt_target_data_begin_mapper(ptr nonnull @anon.[[ID]].1, i64 -1, i32 2, ptr nonnull %.offload_baseptrs, ptr nonnull %.offload_ptrs, ptr nonnull %.offload_sizes, ptr nonnull @.offload_maptypes.[[K]].begin, ptr null, ptr null) | ||
| // CHECK: call i32 @__tgt_target_kernel(ptr nonnull @anon.[[ID]].1, i64 -1, i32 1, i32 1, ptr nonnull @.[[K]].region_id, ptr nonnull %kernel_args) | ||
| // CHECK-NEXT: call void @__tgt_target_data_end_mapper(ptr nonnull @anon.[[ID]].1, i64 -1, i32 2, ptr nonnull %.offload_baseptrs, ptr nonnull %.offload_ptrs, ptr nonnull %.offload_sizes, ptr nonnull @.offload_maptypes.[[K]].end, ptr null, ptr null) | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| fn main() { | ||
| let mut x = [0.0f32; 4]; | ||
| core::offload::offload! { | ||
| kernel = foo, | ||
| args = (Region::<f32, Dummy>::new(&mut x as &mut [f32]),), | ||
| }; | ||
| } | ||
|
|
||
| fn foo(region: Region<'_, f32, Dummy>) { | ||
| unreachable!(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //@ edition: 2024 | ||
|
|
||
| #![feature(gpu_offload)] | ||
| #![feature(offload)] | ||
|
|
||
| use core::offload::{LaunchError, PartitioningStrategy}; | ||
| use core::ptr::NonNull; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Dummy; | ||
|
|
||
| unsafe impl PartitioningStrategy for Dummy { | ||
| type View<'a, T: 'a> = &'a T; | ||
| type ViewMut<'a, T: 'a> = &'a mut T; | ||
|
|
||
| fn index() -> usize { | ||
| 0 | ||
| } | ||
|
|
||
| unsafe fn get<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::View<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| unsafe fn get_mut<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::ViewMut<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| fn check_launch(_len: usize, _grid: [u32; 3], _block: [u32; 3]) -> Result<(), LaunchError> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct Linear1D; | ||
|
|
||
| unsafe impl PartitioningStrategy for Linear1D { | ||
| type View<'a, T: 'a> = &'a T; | ||
| type ViewMut<'a, T: 'a> = &'a mut T; | ||
|
|
||
| fn index() -> usize { | ||
| 0 | ||
| } | ||
|
|
||
| unsafe fn get<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::View<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| unsafe fn get_mut<'a, T>(_ptr: NonNull<T>, _len: usize) -> Option<Self::ViewMut<'a, T>> { | ||
| None | ||
| } | ||
|
|
||
| fn check_launch(_len: usize, grid: [u32; 3], block: [u32; 3]) -> Result<(), LaunchError> { | ||
| if grid[1] == 1 && grid[2] == 1 && block[1] == 1 && block[2] == 1 { | ||
| Ok(()) | ||
| } else { | ||
| Err(LaunchError::new("")) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //@ edition: 2024 | ||
| //@ aux-crate: offload_strategies=offload_strategies.rs | ||
|
|
||
| // This test checks that the borrow checker errors when writing into the data a | ||
| // `Region` was created from while the `Region` is still alive. | ||
|
|
||
| #![feature(gpu_offload)] | ||
| #![feature(offload)] | ||
| #![allow(unused_assignments)] | ||
|
|
||
| use core::offload::Region; | ||
| use offload_strategies::Dummy; | ||
|
|
||
| fn main() { | ||
| let mut x = [0.0f32; 4]; | ||
| let region = Region::<f32, Dummy>::new(&mut x[..]); | ||
|
|
||
| x[0] = 1.0; | ||
| //~^ ERROR cannot assign to `x[_]` because it is borrowed | ||
|
|
||
| let _view = region.get(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| error[E0506]: cannot assign to `x[_]` because it is borrowed | ||
| --> $DIR/region_borrow.rs:18:5 | ||
| | | ||
| LL | let region = Region::<f32, Dummy>::new(&mut x[..]); | ||
| | - `x[_]` is borrowed here | ||
| LL | | ||
| LL | x[0] = 1.0; | ||
| | ^^^^^^^^^^ `x[_]` is assigned to here but it was already borrowed | ||
| ... | ||
| LL | let _view = region.get(); | ||
| | ------ borrow later used here | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0506`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| //@ compile-flags: -Zunstable-options -Zoffload=Device -Clto=fat | ||
| //@ edition: 2024 | ||
| //@ aux-crate: offload_strategies=offload_strategies.rs | ||
|
|
||
| // This test ensures an error is emitted when a `Region` is nested inside another | ||
| // type instead of being passed by value. | ||
|
|
||
| #![feature(core_intrinsics)] | ||
| #![feature(gpu_offload)] | ||
| #![feature(offload)] | ||
|
|
||
| use core::offload::Region; | ||
| use offload_strategies::Dummy; | ||
|
|
||
| fn kernel_shared(_region: &Region<'_, f32, Dummy>) {} | ||
|
|
||
| fn kernel_mut(_region: &mut Region<'_, f32, Dummy>) {} | ||
|
|
||
| fn kernel_nested(_arg: (u32, Region<'_, f32, Dummy>)) {} | ||
|
|
||
| fn main() { | ||
| let mut x = [0.0f32; 4]; | ||
| let region = Region::<f32, Dummy>::new(&mut x[..]); | ||
| core::intrinsics::offload::<_, _, ()>( | ||
| //~^ ERROR offload kernel argument 0 contains a `Region` nested inside another type | ||
| kernel_shared, | ||
| [1, 1, 1], | ||
| [1, 1, 1], | ||
| 0, | ||
| -1, | ||
| (®ion,), | ||
| ); | ||
|
|
||
| let mut y = [0.0f32; 4]; | ||
| let mut region = Region::<f32, Dummy>::new(&mut y[..]); | ||
| core::intrinsics::offload::<_, _, ()>( | ||
| //~^ ERROR offload kernel argument 0 contains a `Region` nested inside another type | ||
| kernel_mut, | ||
| [1, 1, 1], | ||
| [1, 1, 1], | ||
| 0, | ||
| -1, | ||
| (&mut region,), | ||
| ); | ||
|
|
||
| let mut z = [0.0f32; 4]; | ||
| let region = Region::<f32, Dummy>::new(&mut z[..]); | ||
| core::intrinsics::offload::<_, _, ()>( | ||
| //~^ ERROR offload kernel argument 0 contains a `Region` nested inside another type | ||
| kernel_nested, | ||
| [1, 1, 1], | ||
| [1, 1, 1], | ||
| 0, | ||
| -1, | ||
| ((0u32, region),), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| error: offload kernel argument 0 contains a `Region` nested inside another type. Pass the `Region` by value so it can be mapped like a slice | ||
| --> $DIR/region_by_ref.rs:24:5 | ||
| | | ||
| LL | core::intrinsics::offload::<_, _, ()>( | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: offload kernel argument 0 contains a `Region` nested inside another type. Pass the `Region` by value so it can be mapped like a slice | ||
| --> $DIR/region_by_ref.rs:36:5 | ||
| | | ||
| LL | core::intrinsics::offload::<_, _, ()>( | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: offload kernel argument 0 contains a `Region` nested inside another type. Pass the `Region` by value so it can be mapped like a slice | ||
| --> $DIR/region_by_ref.rs:48:5 | ||
| | | ||
| LL | core::intrinsics::offload::<_, _, ()>( | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is only correct by default, not under -Zrandomize-layout, so you should either mark Region as repr(C), or properly look up the order. I'd to the later, just out of principle so we give fewer guarantees to the user.
View changes since the review