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

rustc: Implement the #[global_allocator] attribute #42727

Merged
merged 1 commit into from
Jul 6, 2017

Conversation

alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented Jun 18, 2017

This PR is an implementation of RFC 1974 which specifies a new method of
defining a global allocator for a program. This obsoletes the old
#![allocator] attribute and also removes support for it.

The new #[global_allocator] attribute solves many issues encountered with the
#![allocator] attribute such as composition and restrictions on the crate
graph itself. The compiler now has much more control over the ABI of the
allocator and how it's implemented, allowing much more freedom in terms of how
this feature is implemented.

cc #27389

@rust-highfive
Copy link
Collaborator

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member Author

Note that this is temporarily rebased on #42313 but will be rebased once that lands. Only the last commit here should be relevant.

r? @sfackler

@rust-highfive rust-highfive assigned sfackler and unassigned brson Jun 18, 2017
self.dealloc(ptr, layout);
}
result
mem::forget(err);
Copy link
Member

Choose a reason for hiding this comment

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

Seems like we need a panic guard for the err, or can just not worry about it if it doesn't have a destructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

An interesting point yeah. I believe, though, that it's actually memory unsafe for a global allocator to panic right now. This is asserted by the compiler and it would also be downright bad if we panicked-when panicking due to an assertion failure.

It may be best to just insert guards here to abort on panic, a global allocator should have no business panicking.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've opted to use ManuallyDrop and added some words to the docs of #![global_allocator]

@Mark-Simulacrum
Copy link
Member

I'm somewhat concerned with the approach taken here as I understand it of just removing the allocator attribute in the same PR as we add global_allocator. Is the transition extremely simple and painless? If so, then perhaps this isn't too big a problem but I personally would prefer that we kept allocator around for at least a little bit (even a half-cycle), though a full cycle would be better, I think.

@alexcrichton
Copy link
Member Author

@Mark-Simulacrum I opted to just remove #[allocator] because keeping both features implemented would be significantly more work than what's implemented here. I suspect there are relatively few allocator crate (beyond the two in-tree) and what is external should be easy to migrate. While nightly is relatively stable I don't think it should be a hard constraint when we would otherwise bend over backwards.

@Mark-Simulacrum
Copy link
Member

Ah, okay. Perhaps we could prepare some instructions on the migration? I assume it's relatively straightforward...

@alexcrichton
Copy link
Member Author

Perhaps we could prepare some instructions on the migration?

Are you thinking of something more than https://github.com/alexcrichton/rust/blob/1389e1c4b713dfc9436c760eb6ffbde806f31c0f/src/doc/unstable-book/src/language-features/global-allocator.md?

@Mark-Simulacrum
Copy link
Member

Yeah, that's good -- I was thinking about having a specific guide for moving to the new trait but no reason to in light of that, I guess. It seems better than what we had before, certainly -- I wasn't able to find documentation for the old attribute quickly.

@carols10cents carols10cents added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 19, 2017
.gitmodules Outdated
@@ -1,6 +1,6 @@
[submodule "src/llvm"]
path = src/llvm
url = https://github.com/rust-lang/llvm.git
url = https://github.com/alexcrichton/llvm.git
Copy link
Member

Choose a reason for hiding this comment

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

Should this be changed back?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I will do that after r+. This is currently necessary to pull in alexcrichton/llvm@fd2b33b

@alexcrichton
Copy link
Member Author

Rebased now that #42313 has landed.

@bors
Copy link
Contributor

bors commented Jun 21, 2017

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

csssuf added a commit to csssuf/rust-uefi that referenced this pull request Jun 21, 2017
The Alloc trait for allocators was added in Rust nightly 2017/06/21
(from rust-lang/rust#42313). With the addition of allowing a global
allocator to be specified (pending in rust-lang/rust#42727) this will
obviate the need for the alloc_uefi crate.
csssuf added a commit to csssuf/rust-uefi that referenced this pull request Jun 21, 2017
The Alloc trait for allocators was added in Rust nightly 2017/06/21
(from rust-lang/rust#42313). With the addition of allowing a global
allocator to be specified (pending in rust-lang/rust#42727) this will
obviate the need for the alloc_uefi crate.
csssuf added a commit to csssuf/rust-uefi that referenced this pull request Jun 21, 2017
The Alloc trait for allocators was added in Rust nightly 2017/06/21
(from rust-lang/rust#42313). With the addition of allowing a global
allocator to be specified (pending in rust-lang/rust#42727) this will
obviate the need for the alloc_uefi crate.
csssuf added a commit to csssuf/rust-uefi that referenced this pull request Jun 21, 2017
The Alloc trait for allocators was added in Rust nightly 2017/06/21
(from rust-lang/rust#42313). With the addition of allowing a global
allocator to be specified (pending in rust-lang/rust#42727) this will
obviate the need for the alloc_uefi crate.
@bors
Copy link
Contributor

bors commented Jun 22, 2017

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

@alexcrichton
Copy link
Member Author

Rebased, ping r? @sfackler

bors added a commit that referenced this pull request Jul 6, 2017
rustc: Implement the #[global_allocator] attribute

This PR is an implementation of [RFC 1974] which specifies a new method of
defining a global allocator for a program. This obsoletes the old
`#![allocator]` attribute and also removes support for it.

[RFC 1974]: rust-lang/rfcs#1974

The new `#[global_allocator]` attribute solves many issues encountered with the
`#![allocator]` attribute such as composition and restrictions on the crate
graph itself. The compiler now has much more control over the ABI of the
allocator and how it's implemented, allowing much more freedom in terms of how
this feature is implemented.

cc #27389
@bors
Copy link
Contributor

bors commented Jul 6, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 1685c92 to master...

@pnkfelix
Copy link
Member

pnkfelix commented Jul 6, 2017

@alexcrichton wrote:

sure but the problem is that there's basically no cross-platform way to actually do that.

That's a good argument against leaving this test in place as it currently stands.

But can't we just make it conditional, so that it only runs on certain platforms (namely, the typical platforms used by compiler hackers)? That would probably satisfy the compiler hackers who once-a-year might otherwise need to create such a program, while keeping the maintenance burden in check?

@pnkfelix
Copy link
Member

pnkfelix commented Jul 6, 2017

I wrote:

But can't we just make it conditional, so that it only runs on certain platforms

in fact, it seems that's exactly what we already do.

So I'd say that test has in fact served its purpose: Documenting the minimal code one needs to get a #[no_core] hello world up and running, a process which the diff here makes clear has certainly changed with this PR...

(But at the same time, I can appreciate that it was a pain for @alexcrichton to actually figure out the necesssary incantation here...)

@alexcrichton
Copy link
Member Author

@pnkfelix ah yeah I'd be fine just adding a bunch of // ignore-* directives to only run it on Linux/OSX x86 where it shoudl be manageable

csssuf added a commit to csssuf/rust-uefi that referenced this pull request Jul 6, 2017
The Alloc trait for allocators was added in Rust nightly 2017/06/21
(from rust-lang/rust#42313). With the addition of allowing a global
allocator to be specified (pending in rust-lang/rust#42727) this will
obviate the need for the alloc_uefi crate.
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Aug 11, 2017
This was forgotten from rust-lang#42727 by accident, but these functions are rarely
called and codegen can be improved in LLVM with the `#[cold]` tag.
bors added a commit that referenced this pull request Aug 13, 2017
Optimize allocation paths in RawVec

Since the `Alloc` trait was introduced (#42313) and it was integrated everywhere (#42727) there's been some slowdowns and regressions that have slipped through. The intention of this PR is to try to tackle at least some of them, but they've been very difficult to quantify up to this point so it probably doesn't solve everything.

This PR primarily targets the `RawVec` type, specifically the `double` function. The codegen for this function is now much closer to what it was before #42313 landed as many runtime checks have been elided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.