Skip to content

Commit 24b7442

Browse files
committed
---
yaml --- r: 151315 b: refs/heads/try2 c: d943b0f h: refs/heads/master i: 151313: f7b1e22 151311: 9171acb v: v3
1 parent 4f18737 commit 24b7442

File tree

4 files changed

+116
-24
lines changed

4 files changed

+116
-24
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 055cbdeee07cae7e8c1a253d4a6f154f140ba821
8+
refs/heads/try2: d943b0fee8cc0f747eaf2c0f0ae6e7bc35284ca8
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustc/middle/trans/adt.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,26 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
779779
}
780780
}
781781

782+
/**
783+
* Compute struct field offsets relative to struct begin.
784+
*/
785+
fn compute_struct_field_offsets(ccx: &CrateContext, st: &Struct) -> Vec<u64> {
786+
let mut offsets = vec!();
787+
788+
let mut offset = 0;
789+
for &ty in st.fields.iter() {
790+
let llty = type_of::sizing_type_of(ccx, ty);
791+
if !st.packed {
792+
let type_align = machine::llalign_of_min(ccx, llty) as u64;
793+
offset = roundup(offset, type_align);
794+
}
795+
offsets.push(offset);
796+
offset += machine::llsize_of_alloc(ccx, llty) as u64;
797+
}
798+
assert_eq!(st.fields.len(), offsets.len());
799+
offsets
800+
}
801+
782802
/**
783803
* Building structs is a little complicated, because we might need to
784804
* insert padding if a field's value is less aligned than its type.
@@ -793,26 +813,32 @@ fn build_const_struct(ccx: &CrateContext, st: &Struct, vals: &[ValueRef])
793813
-> Vec<ValueRef> {
794814
assert_eq!(vals.len(), st.fields.len());
795815

816+
let target_offsets = compute_struct_field_offsets(ccx, st);
817+
818+
// offset of current value
796819
let mut offset = 0;
797820
let mut cfields = Vec::new();
798-
for (i, &ty) in st.fields.iter().enumerate() {
799-
let llty = type_of::sizing_type_of(ccx, ty);
800-
let type_align = machine::llalign_of_min(ccx, llty)
801-
/*bad*/as u64;
802-
let val_align = machine::llalign_of_min(ccx, val_ty(vals[i]))
803-
/*bad*/as u64;
804-
let target_offset = roundup(offset, type_align);
805-
offset = roundup(offset, val_align);
821+
for (&val, &target_offset) in vals.iter().zip(target_offsets.iter()) {
822+
if !st.packed {
823+
let val_align = machine::llalign_of_min(ccx, val_ty(val))
824+
/*bad*/as u64;
825+
offset = roundup(offset, val_align);
826+
}
806827
if offset != target_offset {
807828
cfields.push(padding(ccx, target_offset - offset));
808829
offset = target_offset;
809830
}
810-
assert!(!is_undef(vals[i]));
811-
cfields.push(vals[i]);
812-
offset += machine::llsize_of_alloc(ccx, llty) as u64
831+
assert!(!is_undef(val));
832+
cfields.push(val);
833+
offset += machine::llsize_of_alloc(ccx, val_ty(val)) as u64;
834+
}
835+
836+
assert!(offset <= st.size);
837+
if offset != st.size {
838+
cfields.push(padding(ccx, st.size - offset));
813839
}
814840

815-
return cfields;
841+
cfields
816842
}
817843

818844
fn padding(ccx: &CrateContext, size: u64) -> ValueRef {

branches/try2/src/librustc/middle/trans/base.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv,
186186
}
187187
}
188188
// `~` pointer return values never alias because ownership is transferred
189-
// FIXME #6750 ~Trait cannot be directly marked as
190-
// noalias because the actual object pointer is nested.
191189
ty::ty_uniq(..) // | ty::ty_trait(_, _, ty::UniqTraitStore, _, _)
192190
=> {
193191
unsafe {
@@ -258,23 +256,25 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
258256
let llarg = unsafe { llvm::LLVMGetParam(llfn, (offset + i) as c_uint) };
259257
match ty::get(arg_ty).sty {
260258
// `~` pointer parameters never alias because ownership is transferred
261-
// FIXME #6750 ~Trait cannot be directly marked as
262-
// noalias because the actual object pointer is nested.
263-
ty::ty_uniq(..) | // ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
264-
ty::ty_closure(~ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
259+
ty::ty_uniq(..) => {
265260
unsafe {
266261
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
267262
}
268-
},
269-
// When a reference in an argument has no named lifetime, it's
270-
// impossible for that reference to escape this function(ie, be
271-
// returned).
263+
}
264+
// `&mut` pointer parameters never alias other parameters, or mutable global data
265+
ty::ty_rptr(_, mt) if mt.mutbl == ast::MutMutable => {
266+
unsafe {
267+
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
268+
}
269+
}
270+
// When a reference in an argument has no named lifetime, it's impossible for that
271+
// reference to escape this function (returned or stored beyond the call by a closure).
272272
ty::ty_rptr(ReLateBound(_, BrAnon(_)), _) => {
273273
debug!("marking argument of {} as nocapture because of anonymous lifetime", name);
274274
unsafe {
275275
llvm::LLVMAddAttribute(llarg, lib::llvm::NoCaptureAttribute as c_uint);
276276
}
277-
},
277+
}
278278
_ => {
279279
// For non-immediate arguments the callee gets its own copy of
280280
// the value on the stack, so there are no aliases
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
// Issue #13186
13+
14+
// For simplicity of explanations assuming code is compiled for x86_64
15+
// Linux ABI.
16+
17+
// Size of TestOption<u64> is 16, and alignment of TestOption<u64> is 8.
18+
// Size of u8 is 1, and alignment of u8 is 1.
19+
// So size of Request is 24, and alignment of Request must be 8:
20+
// the maximum alignment of its fields.
21+
// Last 7 bytes of Request struct are not occupied by any fields.
22+
23+
24+
enum TestOption<T> {
25+
TestNone,
26+
TestSome(T),
27+
}
28+
29+
pub struct Request {
30+
foo: TestOption<u64>,
31+
bar: u8,
32+
}
33+
34+
fn default_instance() -> &'static Request {
35+
static instance: Request = Request {
36+
// LLVM does not allow to specify alignment of expressions, thus
37+
// alignment of `foo` in constant is 1, not 8.
38+
foo: TestNone,
39+
bar: 17,
40+
// Space after last field is not occupied by any data, but it is
41+
// reserved to make struct aligned properly. If compiler does
42+
// not insert padding after last field when emitting constant,
43+
// size of struct may be not equal to size of struct, and
44+
// compiler crashes in internal assertion check.
45+
};
46+
&'static instance
47+
}
48+
49+
fn non_default_instance() -> &'static Request {
50+
static instance: Request = Request {
51+
foo: TestSome(0x1020304050607080),
52+
bar: 19,
53+
};
54+
&'static instance
55+
}
56+
57+
pub fn main() {
58+
match default_instance() {
59+
&Request { foo: TestNone, bar: 17 } => {},
60+
_ => fail!(),
61+
};
62+
match non_default_instance() {
63+
&Request { foo: TestSome(0x1020304050607080), bar: 19 } => {},
64+
_ => fail!(),
65+
};
66+
}

0 commit comments

Comments
 (0)