Skip to content
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

Add pointer masking convenience functions #96946

Merged
merged 10 commits into from
Aug 28, 2022
Merged

Conversation

WaffleLapkin
Copy link
Member

This PR adds the following public API:

impl<T: ?Sized> *const T {
    fn mask(self, mask: usize) -> *const T;
}

impl<T: ?Sized> *mut T {
    fn mask(self, mask: usize) -> *const T;
}

// mod intrinsics
fn mask<T>(ptr: *const T, mask: usize) -> *const T

This is equivalent to ptr.map_addr(|a| a & mask) but also uses a cool llvm intrinsic.

Proposed in #95643 (comment)

cc @Gankra @scottmcm @RalfJung

r? rust-lang/libs-api

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 11, 2022
@rust-highfive
Copy link
Collaborator

Some changes occured to rustc_codegen_cranelift

cc @bjorn3

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 11, 2022
@WaffleLapkin
Copy link
Member Author

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 11, 2022
@RalfJung
Copy link
Member

Makes sense! Should this be added to the strict provenance feature gate?

Also, if/when this lands it'd be great if you could also implement the new intrinsic for Miri. :)

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin
Copy link
Member Author

Should this be added to the strict provenance feature gate?

I don't really have an opinion on this. While this is mostly useful to mask pointers without touching provenance it may also be used as a shorter version of ptr as uszie & mask (vs ptr.mask(mask)).

Also, if/when this lands it'd be great if you could also implement the new intrinsic for Miri. :)

Sure, I'll take a look into that!

@@ -887,6 +888,9 @@ impl<'ll> CodegenCx<'ll, '_> {
ifn!("llvm.dbg.declare", fn(self.type_metadata(), self.type_metadata()) -> void);
ifn!("llvm.dbg.value", fn(self.type_metadata(), t_i64, self.type_metadata()) -> void);
}

ifn!("llvm.ptrmask", fn(voidp, t_isize) -> voidp);
Copy link
Member Author

Choose a reason for hiding this comment

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

Can someone check that this is valid signature? llvm docs give the following signature:

declare ptrty llvm.ptrmask(ptrty %ptr, intty %mask) readnone speculatable

And the rust intrinsic has the following signature:

fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;

Are all of these compatible with each other?

Copy link
Member

Choose a reason for hiding this comment

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

The LLVM docs confuse me, here. The semantic section says it's possible for the bitwidth of the mask to be different from the pointer size of the target, but that sounds to me like it'd be an overloaded intrinsic, and thus named something like llvm.ptrmask.i64 the same way llvm.cttz.* and friends work. But the name isn't documented the way those ones are...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the name would be llvm.ptrmask.p0i8.i64 etc.

Comment on lines +1200 to +1316
/// Note that, unlike most intrinsics, this is safe to call;
/// it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold
/// any safety invariants.
Copy link
Member Author

Choose a reason for hiding this comment

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

If I understood llvm docs correctly, ptrmask is equivalent to just GEP, not GEP in bounds, so this is safe.

@bors
Copy link
Contributor

bors commented May 19, 2022

☔ The latest upstream changes (presumably #95643) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

this is still waiting on review

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2022
@bors
Copy link
Contributor

bors commented Jul 27, 2022

☔ The latest upstream changes (presumably #99802) made this pull request unmergeable. Please resolve the merge conflicts.

@apiraino
Copy link
Contributor

I think this is now waiting for a feedback from T-libs-api cc @joshtriplett (who was autoassigned)

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2022

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

WaffleLapkin and others added 4 commits August 21, 2022 05:27
Co-authored-by: bjorn3 <17426603+bjorn3@users.noreply.github.com>
I couldn't find where exactly it's documented, but apperantly pointers to void
type are invalid in llvm - void is only allowed as a return type of functions.
@WaffleLapkin
Copy link
Member Author

So, it seems like in the ~1000 commits since I've made this branch llvm was updated bringing opaque pointers with it 😄

cc @nikic, actually -- should it be @llvm.ptrmask.{{ptr|p0isVoid}}.[[WORD]]( to make sure not to fail in the opaque pointers future?

It seems it should have been {{p0|p0i8}} (at least with the new checkout I get p0 and before that I was getting p0i8)

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 21, 2022

📌 Commit ca75312 has been approved by scottmcm

It is now in the queue for this repository.

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 21, 2022
@RalfJung
Copy link
Member

(testing something)

@bors rollup=never

@bors
Copy link
Contributor

bors commented Aug 28, 2022

⌛ Testing commit ca75312 with merge 1e978a3...

@bors
Copy link
Contributor

bors commented Aug 28, 2022

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing 1e978a3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 28, 2022
@bors bors merged commit 1e978a3 into rust-lang:master Aug 28, 2022
@rustbot rustbot added this to the 1.65.0 milestone Aug 28, 2022
@WaffleLapkin WaffleLapkin deleted the ptr_mask branch August 28, 2022 11:39
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1e978a3): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.1% [-1.1%, -1.1%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.2% [3.2%, 3.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.9% [-9.4%, -1.6%] 5
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
3.5% [3.5%, 3.5%] 1
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.5% [3.5%, 3.5%] 1

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

bjorn3 pushed a commit to bjorn3/rust that referenced this pull request Oct 23, 2022
Add pointer masking convenience functions

This PR adds the following public API:
```rust
impl<T: ?Sized> *const T {
    fn mask(self, mask: usize) -> *const T;
}

impl<T: ?Sized> *mut T {
    fn mask(self, mask: usize) -> *const T;
}

// mod intrinsics
fn mask<T>(ptr: *const T, mask: usize) -> *const T
```
This is equivalent to `ptr.map_addr(|a| a & mask)` but also uses a cool llvm intrinsic.

Proposed in rust-lang#95643 (comment)

cc `@Gankra` `@scottmcm` `@RalfJung`

r? rust-lang/libs-api
antoyo pushed a commit to antoyo/rust that referenced this pull request Jun 19, 2023
Add pointer masking convenience functions

This PR adds the following public API:
```rust
impl<T: ?Sized> *const T {
    fn mask(self, mask: usize) -> *const T;
}

impl<T: ?Sized> *mut T {
    fn mask(self, mask: usize) -> *const T;
}

// mod intrinsics
fn mask<T>(ptr: *const T, mask: usize) -> *const T
```
This is equivalent to `ptr.map_addr(|a| a & mask)` but also uses a cool llvm intrinsic.

Proposed in rust-lang#95643 (comment)

cc `@Gankra` `@scottmcm` `@RalfJung`

r? rust-lang/libs-api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet