-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 forever unstable attribute to allow specifying arbitrary scalar ranges #54032
Conversation
src/librustc/middle/region.rs
Outdated
@@ -161,8 +161,7 @@ pub struct BlockRemainder { | |||
|
|||
newtype_index!(FirstStatementIndex | |||
{ | |||
pub idx | |||
MAX = SCOPE_DATA_REMAINDER_MAX | |||
MAX = 0xFFFF_FFFB // the remainder of the scope data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something less magic-value like than this? 0xFFFF_FFFB
is two steps away from the values in SCOPE_DATA_*
. Changing the definition of SCOPE_DATA_*
to be 0xFFFF_FFFE
, ... 0xFFFF_FFFD
would help somewhat, if it is not possible to use arithmetic in the attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SCOPE_DATA_DESTRUCTION
is 0xFFFF_FFFC
, so this is exactly next to it.
Attributes currently only allow literals, no expressions. Though referring to constants might work via queries, there's currently no support for that.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
src/librustc/middle/region.rs
Outdated
const SCOPE_DATA_CALLSITE: u32 = 0xFFFF_FFFE; | ||
const SCOPE_DATA_ARGUMENTS: u32 = 0xFFFF_FFFD; | ||
const SCOPE_DATA_DESTRUCTION: u32 = 0xFFFF_FFFC; | ||
// be sure to add the MAX of FirstStatementIndex if you add more constants here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this done by having an enum with 4 unit variants and a tuple variant around FirstStatementIndex
?
src/librustc/ty/context.rs
Outdated
@@ -1083,6 +1083,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { | |||
interned | |||
} | |||
|
|||
/// Returns a range of the start/end indices specified with the `rustc_layout_scalar_range` | |||
/// attribute. Missing range ends may be denoted by `None` and will just use the max/min of | |||
/// the type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the RangeBound
enum from libstd instead of Option
here.
@oli-obk I think the name should have |
7e185fc
to
95987d0
Compare
2285e76
to
8053f63
Compare
src/librustc/middle/region.rs
Outdated
#[allow(dead_code)] | ||
// only works on stage 1 when the rustc_layout_scalar_valid_range attribute actually exists | ||
#[cfg(not(stage0))] | ||
static ASSERT: () = [()][(mem::size_of::<ScopeData>() != 4) as usize]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put !
around the expression and use ==
instead of !=
. That way, it reads more like an assert.
@@ -82,7 +83,8 @@ macro_rules! newtype_index { | |||
newtype_index!( | |||
// Leave out derives marker so we can use its absence to ensure it comes first | |||
@type [$name] | |||
@max [::std::u32::MAX - 1] | |||
// shave off 256 indices at the end to allow space for packing these indices into enums | |||
@max [0xFFFF_FF00] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhhh, this is clever!
@@ -0,0 +1,5 @@ | |||
// compile-pass | |||
|
|||
static ASSERT: () = [()][(std::mem::size_of::<u32>() != 4) as usize]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd make the same change here (!
around ==
).
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ups forgot to push. Stage 2 tests are passing locally now. |
I'm presuming @bors r=eddyb since all review comments have been addressed |
📌 Commit 79f2cc0 has been approved by |
This comment has been minimized.
This comment has been minimized.
ScopeData::Destruction => write!(fmt, "Destruction({:?})", self.id), | ||
ScopeData::Remainder(fsi) => write!( | ||
fmt, | ||
"Remainder {{ block: {:?}, first_statement_index: {}}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to use the fmt.debug_struct
wrapper, to automate this.
@bors r+ |
📌 Commit a94c166 has been approved by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I was wrong. This is awesome! Nice work @oli-obk! 🎉
Add forever unstable attribute to allow specifying arbitrary scalar ranges r? @eddyb for the first commit and @nikomatsakis for the second one
☀️ Test successful - status-appveyor, status-travis |
Make rustc::middle::region::Scope's fields public This PR makes the following changes to `rustc::middle::region::Scope`: - [x] Makes `region::Scope`'s fields public - [x] Removes the `impl Scope` block with constructors (as per [this comment](#54032 (comment))) - [x] Updates call sites throughout the compiler Closes #54122.
This hurt compile speed by up to 2% across a lot of benchmarks: @oli-obk: any thoughts? |
Previously the unpacking was done manually. Now ruatc generates the relevant enum matching code. Maybe it's not as optimizeable? |
@oli-obk: Here is a Cachegrind diff comparing instruction counts before vs. after. Positive numbers means the instruction counts went up, negative numbers mean they went down.
Any more thoughts about how to fix the regression? |
I'll create some artificial benchmarks to try to repro the issue in isolation. Without knowing which case exactly is slow/verbose and we'd be poking in the dark. |
I think I know the reason for this now. I changed e.g. const FOO_A: u32 = 0xFFFF_FFFF;
const FOO_B: u32 = 0xFFFF_FFFE;
struct Foo { u: u32 } to #[rustc_layout_scalar_range_end(0xFFFF_FF00)]
struct Bar(u32);
enum Foo {
A,
B,
Other(Bar),
} While this will result in pretty much the same layout as before, any derived code on The llvm IR for the second playground link is ca 150 LOC, the first link has 50 LOC I'm opening an issue for this problem, since I can also create an example on stable code (without the unstable attribute) that will result in the same lack of optimitations |
r? @eddyb for the first commit and @nikomatsakis for the second one