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

Use GEP inbounds for ZST and DST field offsets #122048

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

erikdesjardins
Copy link
Contributor

@erikdesjardins erikdesjardins commented Mar 5, 2024

ZST field offsets have been non-inbounds since I made this old layout change. Before that, they would have been inbounds due to using struct_gep. Using inbounds for ZSTs likely doesn't matter for performance, but I'd like to remove the special case.

DST field offsets have been non-inbounds since the alignment-aware DST field offset computation was first implemented in 1.6 (back then GEPi() would be used for inbounds), but I don't think there was any reason for it.

Split out from #121577 / #121665.

r? @oli-obk

cc @RalfJung -- is there some weird situation where field offsets can't be inbounds?

Note that it's fine for inbounds offsets to be one-past-the-end, so it's okay even if there's a ZST as the last field in the layout:

The base pointer has an in bounds address of an allocated object, which means that it points into an allocated object, or to its end. (link)

For rust-lang/unsafe-code-guidelines#93, zero-offset GEP is (now) always inbounds:

Note that getelementptr with all-zero indices is always considered to be inbounds, even if the base pointer does not point to an allocated object. (link)

For the former, it's fine for `inbounds` offsets to be one-past-the-end,
so it's okay even if the ZST is the last field in the layout:

> The base pointer has an in bounds address of an allocated object,
> which means that it points into an allocated object, or to its end.

https://llvm.org/docs/LangRef.html#getelementptr-instruction

For the latter, even DST fields must always be inside the layout
(or to its end for ZSTs), so using inbounds is also fine there.
@rustbot rustbot added 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. labels Mar 5, 2024
@the8472
Copy link
Member

the8472 commented Mar 6, 2024

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

bors commented Mar 6, 2024

⌛ Trying commit 8ebd307 with merge 54c6dee...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 6, 2024
Use GEP inbounds for ZST and DST field offsets

ZST field offsets have been non-`inbounds` since I made [this old layout change](https://github.com/rust-lang/rust/pull/73453/files#diff-160634de1c336f2cf325ff95b312777326f1ab29fec9b9b21d5ee9aae215ecf5). Before that, they would have been `inbounds` due to using `struct_gep`. Using `inbounds` for ZSTs likely doesn't matter for performance, but I'd like to remove the special case.

DST field offsets have been non-`inbounds` since the alignment-aware DST field offset computation was first [implemented](erikdesjardins@a2557d4#diff-04fd352da30ca186fe0bb71cc81a503d1eb8a02ca17a3769e1b95981cd20964aR1188) in 1.6 (back then `GEPi()` would be used for `inbounds`), but I don't think there was any reason for it.

Split out from rust-lang#121577 / rust-lang#121665.

r? `@oli-obk`

cc `@RalfJung` -- is there some weird situation where field offsets can't be `inbounds`?

Note that it's fine for `inbounds` offsets to be one-past-the-end, so it's okay even if there's a ZST as the last field in the layout:

> The base pointer has an in bounds address of an allocated object, which means that it points into an allocated object, or to its end. [(link)](https://llvm.org/docs/LangRef.html#getelementptr-instruction)

For rust-lang/unsafe-code-guidelines#93, zero-offset GEP is (now) always `inbounds`:

> Note that getelementptr with all-zero indices is always considered to be inbounds, even if the base pointer does not point to an allocated object. [(link)](https://llvm.org/docs/LangRef.html#getelementptr-instruction)
@bors
Copy link
Contributor

bors commented Mar 6, 2024

☀️ Try build successful - checks-actions
Build commit: 54c6dee (54c6deebacdc7410ee3a306b9bb6491ee7386960)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (54c6dee): comparison URL.

Overall result: ✅ improvements - no 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.

@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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

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)
- - 0
Regressions ❌
(secondary)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 646.18s -> 644.352s (-0.28%)
Artifact size: 175.03 MiB -> 175.06 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 6, 2024
@RalfJung
Copy link
Member

RalfJung commented Mar 6, 2024

This seems fine to me. In fact I was assuming we'd already use inbounds here.

Comment on lines +73 to +84
extern {
pub type Extern;
}

// CHECK-LABEL: @dst_extern
#[no_mangle]
pub fn dst_extern(s: &Dst<Extern>) -> &Extern {
// Computing the alignment of an extern type is currently unsupported and just panics.

// CHECK: call void @{{.+}}panic
&s.z
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is what you expected...

Full IR:

@str.0 = internal unnamed_addr constant [66 x i8] c"attempted to compute the size or alignment of extern type `Extern`"

define align 1 ptr @dst_extern(ptr align 4 %s) unnamed_addr #0 {
start:
; call core::panicking::panic_nounwind
  call void @_ZN4core9panicking14panic_nounwind17h5c216434c597abbdE(ptr align 1 @str.0, i64 66) #2
  %_0 = getelementptr inbounds i8, ptr %s, i64 5
  ret ptr %_0
}

funnily enough it still generates an unreachable GEP.

Copy link
Contributor

Choose a reason for hiding this comment

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

yea, as long as we have a test, whatever the behaviour, that is fine with me. I just want to protect against accidentally changing behaviour here in the future

@oli-obk
Copy link
Contributor

oli-obk commented Mar 7, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Mar 7, 2024

📌 Commit e349900 has been approved by oli-obk

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

bors commented Mar 8, 2024

⌛ Testing commit e349900 with merge 79d2461...

@bors
Copy link
Contributor

bors commented Mar 8, 2024

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 79d2461 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 8, 2024
@bors bors merged commit 79d2461 into rust-lang:master Mar 8, 2024
12 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 8, 2024
@erikdesjardins erikdesjardins deleted the inbounds branch March 8, 2024 05:03
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (79d2461): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 648.175s -> 647.024s (-0.18%)
Artifact size: 172.54 MiB -> 172.57 MiB (0.02%)

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants