Skip to content

Optimize DST field access #80200

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

Merged
merged 1 commit into from
Jan 7, 2021
Merged

Optimize DST field access #80200

merged 1 commit into from
Jan 7, 2021

Conversation

mahkoh
Copy link
Contributor

@mahkoh mahkoh commented Dec 19, 2020

For

struct X<T: ?Sized>(T)
struct Y<T: ?Sized>(u8, T)

the offset of the unsized field is

0
mem::align_of_val(&self.1)

respectively. This patch changes the expression used to compute these
offsets so that the optimizer can perform this optimization.

Consider

fn f(x: &X<dyn Any>) -> &dyn Any {
    &x.0
}

Before:

test:
	movq	%rsi, %rdx
	movq	16(%rsi), %rax
	leaq	-1(%rax), %rcx
	negq	%rax
	andq	%rcx, %rax
	addq	%rdi, %rax
	retq

After:

test:
	movq	%rsi, %rdx
	movq	%rdi, %rax
	retq

For

    struct X<T: ?Sized>(T)
    struct Y<T: ?Sized>(u8, T)

the offset of the unsized field is

    0
    mem::align_of_val(&self.1)

respectively. This patch changes the expression used to compute these
offsets so that the optimizer can perform this optimization.

Consider

```rust
fn test(x: &X<dyn Any>) -> &dyn Any {
    &x.0
}
```

Before:

```asm
test:
	movq	%rsi, %rdx
	movq	16(%rsi), %rax
	leaq	-1(%rax), %rcx
	negq	%rax
	andq	%rcx, %rax
	addq	%rdi, %rax
	retq
```

After:

```asm
test:
	movq	%rsi, %rdx
	movq	%rdi, %rax
	retq
```
@rust-highfive
Copy link
Contributor

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 19, 2020
@davidtwco
Copy link
Member

r? @nagisa

@rust-highfive rust-highfive assigned nagisa and unassigned davidtwco Dec 21, 2020
@nagisa
Copy link
Member

nagisa commented Jan 7, 2021

I'm sorry I missed this for so long – fell through cracks in my mail! Will take a look tomorrow.

@nagisa
Copy link
Member

nagisa commented Jan 7, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 7, 2021

📌 Commit be15114 has been approved by nagisa

@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 Jan 7, 2021
@nagisa
Copy link
Member

nagisa commented Jan 7, 2021

@bors rollup=never

@bors
Copy link
Collaborator

bors commented Jan 7, 2021

⌛ Testing commit be15114 with merge dfdfaa1...

@bors
Copy link
Collaborator

bors commented Jan 7, 2021

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing dfdfaa1 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 7, 2021
@bors bors merged commit dfdfaa1 into rust-lang:master Jan 7, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 7, 2021
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants