Skip to content

Commit 6e789d4

Browse files
committed
Auto merge of #57173 - Zoxc:hir-lifetime, r=<try>
[WIP] Allocate HIR on an arena Blocked on #56864
2 parents 729d3f0 + 274b927 commit 6e789d4

File tree

113 files changed

+2589
-4220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+2589
-4220
lines changed

Cargo.lock

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ name = "arena"
4747
version = "0.0.0"
4848
dependencies = [
4949
"rustc_data_structures 0.0.0",
50+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
5051
]
5152

5253
[[package]]
@@ -2081,6 +2082,7 @@ dependencies = [
20812082
"rustc_data_structures 0.0.0",
20822083
"rustc_errors 0.0.0",
20832084
"rustc_fs_util 0.0.0",
2085+
"rustc_macros 0.1.0",
20842086
"rustc_target 0.0.0",
20852087
"scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
20862088
"serialize 0.0.0",
@@ -2328,6 +2330,7 @@ dependencies = [
23282330
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
23292331
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
23302332
"rustc_llvm 0.0.0",
2333+
"rustc_macros 0.1.0",
23312334
]
23322335

23332336
[[package]]
@@ -2499,6 +2502,16 @@ dependencies = [
24992502
"core 0.0.0",
25002503
]
25012504

2505+
[[package]]
2506+
name = "rustc_macros"
2507+
version = "0.1.0"
2508+
dependencies = [
2509+
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
2510+
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
2511+
"syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
2512+
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
2513+
]
2514+
25022515
[[package]]
25032516
name = "rustc_metadata"
25042517
version = "0.0.0"
@@ -2700,6 +2713,7 @@ dependencies = [
27002713
"minifier 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
27012714
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
27022715
"pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
2716+
"rustc_macros 0.1.0",
27032717
"tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
27042718
]
27052719

src/bootstrap/bin/rustc.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,6 @@ fn main() {
258258
}
259259
}
260260

261-
// Force all crates compiled by this compiler to (a) be unstable and (b)
262-
// allow the `rustc_private` feature to link to other unstable crates
263-
// also in the sysroot.
264-
if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
265-
cmd.arg("-Z").arg("force-unstable-if-unmarked");
266-
}
267-
268261
if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
269262
cmd.arg("--remap-path-prefix").arg(&map);
270263
}
@@ -284,6 +277,13 @@ fn main() {
284277
}
285278
}
286279

280+
// Force all crates compiled by this compiler to (a) be unstable and (b)
281+
// allow the `rustc_private` feature to link to other unstable crates
282+
// also in the sysroot.
283+
if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
284+
cmd.arg("-Z").arg("force-unstable-if-unmarked");
285+
}
286+
287287
if env::var_os("RUSTC_PARALLEL_QUERIES").is_some() {
288288
cmd.arg("--cfg").arg("parallel_queries");
289289
}

src/bootstrap/check.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Step for Std {
4242
true);
4343

4444
let libdir = builder.sysroot_libdir(compiler, target);
45-
add_to_sysroot(&builder, &libdir, &libstd_stamp(builder, compiler, target));
45+
add_to_sysroot(&builder, &libdir, &libstd_stamp(builder, compiler, target), None);
4646
}
4747
}
4848

@@ -87,8 +87,14 @@ impl Step for Rustc {
8787
&librustc_stamp(builder, compiler, target),
8888
true);
8989

90+
let stage_out = builder.build.stage_out(compiler, Mode::Rustc);
9091
let libdir = builder.sysroot_libdir(compiler, target);
91-
add_to_sysroot(&builder, &libdir, &librustc_stamp(builder, compiler, target));
92+
add_to_sysroot(
93+
&builder,
94+
&libdir,
95+
&librustc_stamp(builder, compiler, target),
96+
Some(&stage_out),
97+
);
9298
}
9399
}
94100

@@ -175,7 +181,7 @@ impl Step for Test {
175181
true);
176182

177183
let libdir = builder.sysroot_libdir(compiler, target);
178-
add_to_sysroot(builder, &libdir, &libtest_stamp(builder, compiler, target));
184+
add_to_sysroot(builder, &libdir, &libtest_stamp(builder, compiler, target), None);
179185
}
180186
}
181187

@@ -222,7 +228,7 @@ impl Step for Rustdoc {
222228
true);
223229

224230
let libdir = builder.sysroot_libdir(compiler, target);
225-
add_to_sysroot(&builder, &libdir, &rustdoc_stamp(builder, compiler, target));
231+
add_to_sysroot(&builder, &libdir, &rustdoc_stamp(builder, compiler, target), None);
226232
builder.cargo(compiler, Mode::ToolRustc, target, "clean");
227233
}
228234
}

src/bootstrap/compile.rs

+38-9
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Step for StdLink {
211211
target_compiler.host,
212212
target));
213213
let libdir = builder.sysroot_libdir(target_compiler, target);
214-
add_to_sysroot(builder, &libdir, &libstd_stamp(builder, compiler, target));
214+
add_to_sysroot(builder, &libdir, &libstd_stamp(builder, compiler, target), None);
215215

216216
if builder.config.sanitizers && compiler.stage != 0 && target == "x86_64-apple-darwin" {
217217
// The sanitizers are only built in stage1 or above, so the dylibs will
@@ -414,7 +414,7 @@ impl Step for TestLink {
414414
target_compiler.host,
415415
target));
416416
add_to_sysroot(builder, &builder.sysroot_libdir(target_compiler, target),
417-
&libtest_stamp(builder, compiler, target));
417+
&libtest_stamp(builder, compiler, target), None);
418418

419419
builder.cargo(target_compiler, Mode::ToolTest, target, "clean");
420420
}
@@ -574,8 +574,13 @@ impl Step for RustcLink {
574574
&compiler.host,
575575
target_compiler.host,
576576
target));
577-
add_to_sysroot(builder, &builder.sysroot_libdir(target_compiler, target),
578-
&librustc_stamp(builder, compiler, target));
577+
let stage_out = builder.build.stage_out(target_compiler, Mode::Rustc);
578+
add_to_sysroot(
579+
builder,
580+
&builder.sysroot_libdir(target_compiler, target),
581+
&librustc_stamp(builder, compiler, target),
582+
if compiler.stage == 0 { Some(&stage_out) } else { None },
583+
);
579584
builder.cargo(target_compiler, Mode::ToolRustc, target, "clean");
580585
}
581586
}
@@ -982,10 +987,26 @@ impl Step for Assemble {
982987
///
983988
/// For a particular stage this will link the file listed in `stamp` into the
984989
/// `sysroot_dst` provided.
985-
pub fn add_to_sysroot(builder: &Builder, sysroot_dst: &Path, stamp: &Path) {
990+
pub fn add_to_sysroot(
991+
builder: &Builder,
992+
sysroot_dst: &Path,
993+
stamp: &Path,
994+
stage_out: Option<&Path>) {
986995
t!(fs::create_dir_all(&sysroot_dst));
987996
for path in builder.read_stamp_file(stamp) {
988-
builder.copy(&path, &sysroot_dst.join(path.file_name().unwrap()));
997+
let file_dir = path.parent().unwrap() // chop off base name
998+
.parent().unwrap() // chop off `release`
999+
.parent().unwrap(); // chop off `release`
1000+
if stage_out == Some(file_dir) {
1001+
// We are copying a build file. We need to add the build triple to it
1002+
let rustlib_dir = sysroot_dst.parent().unwrap() // chop off `lib`
1003+
.parent().unwrap(); // chop off `$target`
1004+
let build_dir = rustlib_dir.join(builder.build.build).join("lib");
1005+
t!(fs::create_dir_all(&build_dir));
1006+
builder.copy(&path, &build_dir.join(path.file_name().unwrap()));
1007+
} else {
1008+
builder.copy(&path, &sysroot_dst.join(path.file_name().unwrap()));
1009+
}
9891010
}
9901011
}
9911012

@@ -1014,8 +1035,12 @@ pub fn run_cargo(builder: &Builder,
10141035
let mut deps = Vec::new();
10151036
let mut toplevel = Vec::new();
10161037
let ok = stream_cargo(builder, cargo, &mut |msg| {
1017-
let filenames = match msg {
1018-
CargoMessage::CompilerArtifact { filenames, .. } => filenames,
1038+
let (filenames, package_id) = match msg {
1039+
CargoMessage::CompilerArtifact {
1040+
filenames,
1041+
package_id,
1042+
..
1043+
} => (filenames, package_id),
10191044
_ => return,
10201045
};
10211046
for filename in filenames {
@@ -1030,8 +1055,12 @@ pub fn run_cargo(builder: &Builder,
10301055
let filename = Path::new(&*filename);
10311056

10321057
// If this was an output file in the "host dir" we don't actually
1033-
// worry about it, it's not relevant for us.
1058+
// worry about it, it's not relevant for us
10341059
if filename.starts_with(&host_root_dir) {
1060+
// Unless it's a proc macro used in the compiler
1061+
if package_id.starts_with("rustc_macros ") {
1062+
deps.push(filename.to_path_buf());
1063+
}
10351064
continue;
10361065
}
10371066

src/libarena/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ path = "lib.rs"
99
crate-type = ["dylib"]
1010

1111
[dependencies]
12-
rustc_data_structures = { path = "../librustc_data_structures" }
12+
rustc_data_structures = { path = "../librustc_data_structures" }
13+
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }

src/libarena/lib.rs

+54-11
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
#![allow(deprecated)]
2424

2525
extern crate alloc;
26+
extern crate smallvec;
2627
extern crate rustc_data_structures;
2728

2829
use rustc_data_structures::sync::MTLock;
30+
#[allow(unused_imports)]
31+
use smallvec::SmallVec;
2932

3033
use std::cell::{Cell, RefCell};
3134
use std::cmp;
@@ -151,6 +154,27 @@ impl<T> TypedArena<T> {
151154
}
152155
}
153156

157+
#[inline]
158+
fn can_allocate(&self, len: usize) -> bool {
159+
let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
160+
let at_least_bytes = len * mem::size_of::<T>();
161+
available_capacity_bytes >= at_least_bytes
162+
}
163+
164+
#[inline]
165+
fn alloc_raw_slice(&self, len: usize) -> *mut T {
166+
assert!(mem::size_of::<T>() != 0);
167+
assert!(len != 0);
168+
169+
if !self.can_allocate(len) {
170+
self.grow(len);
171+
}
172+
173+
let start_ptr = self.ptr.get();
174+
self.ptr.set(unsafe { start_ptr.add(len) });
175+
start_ptr
176+
}
177+
154178
/// Allocates a slice of objects that are copied into the `TypedArena`, returning a mutable
155179
/// reference to it. Will panic if passed a zero-sized types.
156180
///
@@ -163,21 +187,40 @@ impl<T> TypedArena<T> {
163187
where
164188
T: Copy,
165189
{
166-
assert!(mem::size_of::<T>() != 0);
167-
assert!(slice.len() != 0);
190+
unsafe {
191+
let len = slice.len();
192+
let start_ptr = self.alloc_raw_slice(len);
193+
slice.as_ptr().copy_to_nonoverlapping(start_ptr, len);
194+
slice::from_raw_parts_mut(start_ptr, len)
195+
}
196+
}
168197

169-
let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
170-
let at_least_bytes = slice.len() * mem::size_of::<T>();
171-
if available_capacity_bytes < at_least_bytes {
172-
self.grow(slice.len());
198+
#[allow(unreachable_code)]
199+
pub fn alloc_from_iter<I: IntoIterator<Item=T>>(&self, iter: I) -> &[T] where T: Clone {
200+
assert!(mem::size_of::<T>() != 0);
201+
let vec: Vec<_> = iter.into_iter().collect();
202+
if vec.is_empty() {
203+
return &[]
204+
}
205+
let vec2 = vec.clone();
206+
for a in vec {
207+
self.alloc(a);
208+
}
209+
let result = &vec2[..] as *const [T];
210+
mem::forget(vec2);// FIXME: Remove
211+
unsafe {
212+
return &*result;
173213
}
174214

215+
// Move the content to the arena by copying and them forgetting the content of the SmallVec
175216
unsafe {
176-
let start_ptr = self.ptr.get();
177-
let arena_slice = slice::from_raw_parts_mut(start_ptr, slice.len());
178-
self.ptr.set(start_ptr.add(arena_slice.len()));
179-
arena_slice.copy_from_slice(slice);
180-
arena_slice
217+
let len = vec.len();
218+
let start_ptr = self.alloc_raw_slice(len);
219+
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
220+
mem::forget(vec);// FIXME: Remove
221+
//mem::forget(vec.drain());
222+
223+
slice::from_raw_parts_mut(start_ptr, len)
181224
}
182225
}
183226

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rustc-rayon = "0.1.1"
2222
rustc-rayon-core = "0.1.1"
2323
rustc_apfloat = { path = "../librustc_apfloat" }
2424
rustc_target = { path = "../librustc_target" }
25+
rustc_macros = { path = "../librustc_macros" }
2526
rustc_data_structures = { path = "../librustc_data_structures" }
2627
rustc_errors = { path = "../librustc_errors" }
2728
serialize = { path = "../libserialize" }

0 commit comments

Comments
 (0)