-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
refactor to remove trans::adt and make rustc::ty::layout authoritative #36151
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
r? @eddyb |
from: &layout::Struct, | ||
size: layout::Size, | ||
variant: Option<ty::VariantDef<'tcx>>, | ||
substs: &Substs<'tcx>)->Struct<'tcx> { |
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.
The arguments should be indented 4 more to the right (to align with tcx: ...
) and there should be spaces around ->
.
8036624
to
f76321e
Compare
I've incorporated the stylistic fixes from @eddyb. For some reason, The I'm going to continue by trying to get I can't continue this until either late this evening or tomorrow. |
@@ -939,10 +939,8 @@ impl<'a, 'gcx, 'tcx> Layout { | |||
} | |||
} | |||
|
|||
if def.variants.len() == 1 { | |||
if def.variants.len() == 1 && hint == attr::ReprAny{ |
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.
nit: missing space after attr::ReprAny
@arielb1 |
@camlorn Look for how we check for size overflows when computing the LLVM types. |
Needs rebase |
@@ -496,6 +496,9 @@ pub struct GlobalCtxt<'tcx> { | |||
/// Cache for layouts computed from types. | |||
pub layout_cache: RefCell<FnvHashMap<Ty<'tcx>, &'tcx Layout>>, | |||
|
|||
//Used to prevent layout from recursing too deeply. |
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.
nit: don't put random mutable parameters on the tcx. Use a LayoutContext
.
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't, because the recursion doesn't go through a dedicated context. You might be able to put it in TargetDataLayout
, I guess, but that's about it.
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.
Space after //
. Also, ///
should be used (signifying doc comments).
1094330
to
0561f6f
Compare
layout::Size::from_bytes(i*element_size.bytes()) | ||
}).collect::<Vec<_>>(); | ||
let contents = build_const_struct(ccx, &offsets[..], vals, true); | ||
C_struct(ccx, &contents[..], true) |
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.
It shouldn't be C_struct
but rather a LLVM vector 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.
It was C_struct
before, as layout::Vector
went to Repr::Univariant
. I'll see if I can figure out how to change this. But I half-suspect it'll cause all the simd tests to start failing (presumably LLVM treats vector types differently and presumably other stuff still assumes it's whatever C_struct
does).
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.
It shouldn't have gone through Repr
, I don't think. Ah, but it's fine because of the way we handle constants, that is, we cast a pointer to a location holding the constant, because enum variants aren't the type of the enum (and in this case, structs instead of vectors).
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.
Are you saying leave it or change it? I follow your explanation, but I'm not sure if you still consider it a problem worthy of being fixed.
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.
It's fine to leave it as it was before, however, it may actually cleaner to use a (LLVM) vector and not generate the Vec
of field types.
1 => Type::array(&Type::i8(cx), align_units), | ||
2 => Type::array(&Type::i16(cx), align_units), | ||
4 => Type::array(&Type::i32(cx), align_units), | ||
8 if machine::llalign_of_min(cx, Type::i64(cx)) == 8 => |
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.
This can use cx.tcx().data_layout.i64_align.abi()
- in fact, look in ty::layout
for how a similar match (for enums) was turned into a more general thing (e.g. there are architectures where i32
doesn't have an alignment of 4).
☔ The latest upstream changes (presumably #36496) made this pull request unmergeable. Please resolve the merge conflicts. |
1d6e735
to
cfadf74
Compare
Don't merge this yet.
Throw your comments in for additional cleanup, and I'll incorporate them. At this point, I'm not sure what all I should try to kill still, or if there's anything else I'm not getting an unused warning about that's not being called. |
} | ||
} | ||
|
||
fn struct_field_ptr<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>, | ||
st: &Struct<'tcx>, val: MaybeSizedValue, | ||
st: &layout::Struct, fields: &Vec<Ty<'tcx>>, val: MaybeSizedValue, |
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.
Creating vectors is expensive. This function should use the layout::Struct
to get the offset of the last field instead.
c2f8e12
to
12ff05f
Compare
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.
Great job! Mostly only formatting nitpicks left.
@@ -328,6 +328,43 @@ pub enum Integer { | |||
} | |||
|
|||
impl Integer { | |||
|
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.
Extra newline.
} | ||
|
||
pub fn to_ty<'a, 'tcx>(&self, tcx: &ty::TyCtxt<'a, 'tcx, 'tcx>, | ||
signed: bool) -> Ty<'tcx> { |
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.
signed
is aligned 4 spaces to the left of where it should be.
@@ -350,6 +387,18 @@ impl Integer { | |||
} | |||
} | |||
|
|||
//Find the smallest integer with the given alignment. |
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.
Space after //
and use ///
.
@@ -1003,6 +1061,16 @@ impl<'a, 'gcx, 'tcx> Layout { | |||
} | |||
} | |||
|
|||
if def.variants.len() == 1 && hint == attr::ReprAny{ |
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.
Missing space before {
. Also, this will never be reached, maybe you meant to put the hint == attr::ReprAny
in the previous if
?
//Given an enum, struct, closure, or tuple, extracts fields. | ||
//treats closures as a struct with one variant. | ||
//`empty_if_no_variants` is a switch to deal with empty enums. | ||
//if true, `variant_index` is disregarded and an empty Vec returned in this case. |
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.
Same: space after //
and make it ///
for doc comments.
General(ity, _) => { | ||
C_integral(ll_inttype(bcx.ccx(), ity), discr.0, true) | ||
layout::RawNullablePointer { .. } | | ||
layout::StructWrappedNullablePointer { .. } => { |
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.
Missing alignment on this line.
for (&val, target_offset) in | ||
vals.iter().zip( | ||
offset_after_field.iter().map(|i| i.bytes()) | ||
) { |
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 put the second iterator in a target_offsets
variable to recover the original look.
@@ -1292,11 +1291,15 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> { | |||
fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |||
-> Vec<MemberDescription> { | |||
let adt = &self.enum_type.ty_adt_def().unwrap(); | |||
let substs = match self.enum_type.sty { | |||
ty::TyAdt(def, ref s) if def.adt_kind() == AdtKind::Enum => s, | |||
ref t @ _ => bug!("{} is not an enum", t) |
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.
Print self.enum_type
instead of the sty
.
@@ -735,7 +733,12 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> { | |||
|
|||
let base = match tr_lvalue.base { | |||
Base::Value(llval) => { | |||
let align = type_of::align_of(self.ccx, ty); | |||
//Fixme: may be wrong for &*(&simd_vec as &fmt::Debug) |
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.
Space after //
and FIXME
.
} | ||
} | ||
|
||
pub fn from_primitive(ccx: &CrateContext, p: layout::Primitive)->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.
Same ->
thing here.
aa83877
to
467454b
Compare
@bors r+ |
📌 Commit 467454b has been approved by |
refactor to remove trans::adt and make rustc::ty::layout authoritative I asked on IRC about optimizing struct layout by reordering fields from most-aligned to least-aligned and somehow ended up getting talked into doing this. The goal here is to make `layout` authoritative and to remove `adt`. The former has been accomplished by reimplementing `represent_type_uncached` and the latter is in progress. @eddyb thought I should make the PR now. My plan is to reserve the actual optimization for a second PR, as this work is useful by itself.
I asked on IRC about optimizing struct layout by reordering fields from most-aligned to least-aligned and somehow ended up getting talked into doing this. The goal here is to make
layout
authoritative and to removeadt
. The former has been accomplished by reimplementingrepresent_type_uncached
and the latter is in progress. @eddyb thought I should make the PR now.My plan is to reserve the actual optimization for a second PR, as this work is useful by itself.