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

Generate getelementptr instead of inttoptr for ptr::invalid #121242

Closed
wants to merge 1 commit into from

Conversation

joboet
Copy link
Member

@joboet joboet commented Feb 17, 2024

Currently, ptr::invalid generates an inttoptr, which means LLVM doesn't know that the pointer shouldn't have provenance. This PR changes the implementation so that a getelementptr relative to the null pointer is generated, which LLVM knows not to have provenance.

@rustbot
Copy link
Collaborator

rustbot commented Feb 17, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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. labels Feb 17, 2024
@joboet joboet force-pushed the ptr_invalid_codegen branch 3 times, most recently from 49c5206 to 721f991 Compare February 17, 2024 20:27
#[no_mangle]
fn invalid(addr: usize) -> *const () {
// CHECK: start
// CHECK-NEXT: %0 = getelementptr i8, ptr null, i64 %addr
Copy link
Member

@scottmcm scottmcm Feb 18, 2024

Choose a reason for hiding this comment

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

Oh, nice touch. Yeah, today this is inttoptr, so less of that sounds good to me.

EDIT: Doh, you had a better link in the OP 🤦

@scottmcm scottmcm assigned scottmcm and unassigned Mark-Simulacrum Feb 18, 2024
@scottmcm
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Feb 18, 2024

📌 Commit 721f991 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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2024
@joboet
Copy link
Member Author

joboet commented Feb 18, 2024

@bors r-
@rustbot ready

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 18, 2024
@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 Feb 18, 2024
@Noratrieb
Copy link
Member

@bors=scottmcm,Nilstrieb

@scottmcm
Copy link
Member

@bors r=scottmcm,Nilstrieb

@bors
Copy link
Contributor

bors commented Feb 18, 2024

📌 Commit bb399b1 has been approved by scottmcm,Nilstrieb

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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 19, 2024
…e, r=<try>

Lower transmutes from int to pointer type as gep on null

I thought of this while looking at rust-lang#121242

The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
Noratrieb added a commit to Noratrieb/rust that referenced this pull request Feb 20, 2024
…tmcm,Nilstrieb

Generate `getelementptr` instead of `inttoptr` for `ptr::invalid`

Currently, `ptr::invalid` [generates an `inttoptr`](https://godbolt.org/z/3cj15dEG1), which means LLVM doesn't know that the pointer shouldn't have provenance. This PR changes the implementation so that a `getelementptr` relative to the null pointer is generated, which LLVM knows not to have provenance.
@bors
Copy link
Contributor

bors commented Mar 1, 2024

⌛ Testing commit b6d7805 with merge bd7aded...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 1, 2024
Generate `getelementptr` instead of `inttoptr` for `ptr::invalid`

Currently, `ptr::invalid` [generates an `inttoptr`](https://godbolt.org/z/3cj15dEG1), which means LLVM doesn't know that the pointer shouldn't have provenance. This PR changes the implementation so that a `getelementptr` relative to the null pointer is generated, which LLVM knows not to have provenance.
@rust-log-analyzer
Copy link
Collaborator

The job test-various failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [mir-opt] tests/mir-opt/inline/polymorphic_recursion.rs ... ok

failures:

---- [mir-opt] tests/mir-opt/dataflow-const-prop/default_boxed_slice.rs stdout ----
14           scope 3 {
15               debug ptr => _3;
16           }
-           scope 4 (inlined Unique::<[bool; 0]>::dangling) {
-               let mut _5: std::ptr::NonNull<[bool; 0]>;
-               scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
-                   scope 6 {
-                       let _6: *mut [bool; 0];
-                       scope 7 {
-                           debug ptr => _6;
-                           scope 12 (inlined NonNull::<[bool; 0]>::new_unchecked) {
-                               debug ptr => _6;
-                               let mut _8: bool;
-                               let _9: ();
-                               let mut _10: *mut ();
-                               let mut _11: *const [bool; 0];
-                               scope 13 {
-                           }
-                       }
-                       }
-                       scope 8 (inlined dangling_mut::<[bool; 0]>) {
-                           let mut _7: usize;
-                           scope 9 (inlined align_of::<[bool; 0]>) {
-                           }
-                           scope 10 (inlined without_provenance_mut::<[bool; 0]>) {
-                               debug addr => _7;
-                               scope 11 {
-                           }
-                       }
-                   }
-               }
-               }
-           }
47       }
48   
49       bb0: {

50           StorageLive(_1);
51           StorageLive(_2);
52           StorageLive(_3);
-           StorageLive(_9);
54           StorageLive(_4);
-           StorageLive(_5);
-           StorageLive(_6);
-           StorageLive(_7);
- -         _7 = AlignOf([bool; 0]);
- -         _6 = _7 as *mut [bool; 0] (Transmute);
- +         _7 = const 1_usize;
- +         _6 = const {0x1 as *mut [bool; 0]};
-           StorageDead(_7);
-           StorageLive(_10);
-           StorageLive(_11);
-           StorageLive(_8);
-           _8 = cfg!(debug_assertions);
-           switchInt(move _8) -> [0: bb3, otherwise: bb2];
+           _4 = Unique::<[bool; 0]>::dangling() -> [return: bb2, unwind unreachable];
69   
70       bb1: {

73       }
73       }
74   
75       bb2: {
- -         _10 = _6 as *mut () (PtrToPtr);
- -         _9 = NonNull::<T>::new_unchecked::precondition_check(move _10) -> [return: bb3, unwind unreachable];
- +         _10 = const {0x1 as *mut ()};
- +         _9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable];
-   
-       bb3: {
-           StorageDead(_8);
-           StorageDead(_8);
- -         _11 = _6 as *const [bool; 0] (PointerCoercion(MutToConstPointer));
- -         _5 = NonNull::<[bool; 0]> { pointer: _11 };
- +         _11 = const {0x1 as *const [bool; 0]};
- +         _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
-           StorageDead(_11);
-           StorageDead(_10);
-           StorageDead(_6);
- -         _4 = Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
- +         _4 = const Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
-           StorageDead(_5);
- -         _3 = move _4 as std::ptr::Unique<[bool]> (PointerCoercion(Unsize));
- +         _3 = const Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};
+           _3 = move _4 as std::ptr::Unique<[bool]> (PointerCoercion(Unsize));
96           StorageDead(_4);
- -         _2 = Box::<[bool]>(_3, const std::alloc::Global);
- +         _2 = const Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global);
-           StorageDead(_9);
+           _2 = Box::<[bool]>(_3, const std::alloc::Global);
100           StorageDead(_3);
- -         _1 = A { foo: move _2 };
- +         _1 = const A {{ foo: Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC2, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global) }};
+           _1 = A { foo: move _2 };
103           StorageDead(_2);
104           _0 = const ();
105           drop(_1) -> [return: bb1, unwind unreachable];
106       }
- + }
- + 
- + 
- + ALLOC2 (size: 8, align: 4) {
- +     01 00 00 00 00 00 00 00                         │ ........
- + }
- + 
- + ALLOC1 (size: 8, align: 4) {
- +     01 00 00 00 00 00 00 00                         │ ........
- + }
- + 
- + ALLOC0 (size: 8, align: 4) {
- +     01 00 00 00 00 00 00 00                         │ ........
120   
121 

thread '[mir-opt] tests/mir-opt/dataflow-const-prop/default_boxed_slice.rs' panicked at src/tools/compiletest/src/runtest.rs:4141:21:
thread '[mir-opt] tests/mir-opt/dataflow-const-prop/default_boxed_slice.rs' panicked at src/tools/compiletest/src/runtest.rs:4141:21:
Actual MIR output differs from expected MIR output /checkout/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.32bit.panic-abort.diff


failures:
    [mir-opt] tests/mir-opt/dataflow-const-prop/default_boxed_slice.rs

@bors
Copy link
Contributor

bors commented Mar 1, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 1, 2024
@scottmcm
Copy link
Member

scottmcm commented Mar 1, 2024

Looks like mir-opt tests need blessing?
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2024
@joboet
Copy link
Member Author

joboet commented Mar 2, 2024

As without_provenance is much more complicated now, it doesn't get inlined in as many places. Let's see what perf has to say about that...

@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 Mar 2, 2024
@bors
Copy link
Contributor

bors commented Mar 2, 2024

⌛ Trying commit b6d7805 with merge f186eed...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2024
Generate `getelementptr` instead of `inttoptr` for `ptr::invalid`

Currently, `ptr::invalid` [generates an `inttoptr`](https://godbolt.org/z/3cj15dEG1), which means LLVM doesn't know that the pointer shouldn't have provenance. This PR changes the implementation so that a `getelementptr` relative to the null pointer is generated, which LLVM knows not to have provenance.
@RalfJung
Copy link
Member

RalfJung commented Mar 2, 2024

As without_provenance is much more complicated now, it doesn't get inlined in as many places. Let's see what perf has to say about that...

Even with inline(always)?

EDIT: Ah, mir-opt tests. So it's about the MIR inliner, not the LLVM one.

@bors
Copy link
Contributor

bors commented Mar 2, 2024

☀️ Try build successful - checks-actions
Build commit: f186eed (f186eedbfad18048274aca0c85fc40750cf0ba1b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f186eed): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

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

mean range count
Regressions ❌
(primary)
0.6% [0.2%, 1.2%] 21
Regressions ❌
(secondary)
1.1% [0.2%, 1.9%] 5
Improvements ✅
(primary)
-0.9% [-1.6%, -0.2%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [-1.6%, 1.2%] 26

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.

mean range count
Regressions ❌
(primary)
3.0% [0.9%, 6.6%] 5
Regressions ❌
(secondary)
5.2% [5.1%, 5.4%] 2
Improvements ✅
(primary)
-6.1% [-11.0%, -1.2%] 4
Improvements ✅
(secondary)
-2.8% [-2.8%, -2.8%] 1
All ❌✅ (primary) -1.1% [-11.0%, 6.6%] 9

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.

mean range count
Regressions ❌
(primary)
1.0% [1.0%, 1.1%] 2
Regressions ❌
(secondary)
1.3% [1.3%, 1.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.0% [1.0%, 1.1%] 2

Binary size

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.

mean range count
Regressions ❌
(primary)
0.7% [0.0%, 1.7%] 82
Regressions ❌
(secondary)
2.4% [0.1%, 14.7%] 19
Improvements ✅
(primary)
-0.9% [-3.5%, -0.0%] 10
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [-3.5%, 1.7%] 92

Bootstrap: 653.837s -> 655.456s (0.25%)
Artifact size: 175.50 MiB -> 175.51 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 2, 2024
@scottmcm
Copy link
Member

With #121282 looking like it might be able to land, maybe we won't end up needing this one after all?

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 12, 2024
…e, r=scottmcm

Lower transmutes from int to pointer type as gep on null

I thought of this while looking at rust-lang#121242. See that PR's description for why this lowering is preferable.

The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
@joboet
Copy link
Member Author

joboet commented Mar 12, 2024

With #121282 looking like it might be able to land, maybe we won't end up needing this one after all?

Definitely, and when looking at the compile-time regressions this causes, that's a good thing.

@joboet joboet closed this Mar 12, 2024
@joboet joboet deleted the ptr_invalid_codegen branch March 12, 2024 13:23
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Mar 14, 2024
…tmcm

Lower transmutes from int to pointer type as gep on null

I thought of this while looking at rust-lang/rust#121242. See that PR's description for why this lowering is preferable.

The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
…tmcm

Lower transmutes from int to pointer type as gep on null

I thought of this while looking at rust-lang/rust#121242. See that PR's description for why this lowering is preferable.

The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
…tmcm

Lower transmutes from int to pointer type as gep on null

I thought of this while looking at rust-lang/rust#121242. See that PR's description for why this lowering is preferable.

The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.