Skip to content

Commit 308c7ca

Browse files
authored
Merge pull request #265 from oli-obk/optimize_prime
Also test optimized MIR
2 parents 9710ff4 + f2d0101 commit 308c7ca

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

src/bin/miri.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,7 @@ fn main() {
199199
args.push(sysroot_flag);
200200
args.push(find_sysroot());
201201
}
202-
// we run the optimization passes inside miri
203-
// if we ran them twice we'd get funny failures due to borrowck ElaborateDrops only working on
204-
// unoptimized MIR
205-
// FIXME: add an after-mir-passes hook to rustc driver
206-
args.push("-Zmir-opt-level=0".to_owned());
202+
207203
// for auxilary builds in unit tests
208204
args.push("-Zalways-encode-mir".to_owned());
209205

src/step.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
8787

8888
match *dest_layout {
8989
Layout::General { discr, .. } => {
90-
// FIXME: I (oli-obk) think we need to check the
91-
// `dest_ty` for the variant's discriminant and write
92-
// instead of the variant index
93-
// We don't have any tests actually going through these lines
94-
let discr_ty = discr.to_ty(&self.tcx, false);
95-
let discr_lval = self.lvalue_field(dest, 0, dest_ty, discr_ty)?;
96-
97-
self.write_value(Value::ByVal(PrimVal::Bytes(variant_index as u128)), discr_lval, discr_ty)?;
90+
let discr_size = discr.size().bytes();
91+
let dest_ptr = self.force_allocation(dest)?.to_ptr()?;
92+
self.memory.write_uint(dest_ptr, variant_index as u128, discr_size)?
9893
}
9994

10095
Layout::RawNullablePointer { nndiscr, .. } => {
@@ -103,6 +98,17 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
10398
}
10499
}
105100

101+
Layout::StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
102+
if variant_index as u64 != nndiscr {
103+
let (offset, ty) = self.nonnull_offset_and_ty(dest_ty, nndiscr, discrfield)?;
104+
let nonnull = self.force_allocation(dest)?.to_ptr()?.offset(offset.bytes(), self.memory.layout)?;
105+
trace!("struct wrapped nullable pointer type: {}", ty);
106+
// only the pointer part of a fat pointer is used for this space optimization
107+
let discr_size = self.type_size(ty)?.expect("bad StructWrappedNullablePointer discrfield");
108+
self.memory.write_uint(nonnull, 0, discr_size)?;
109+
}
110+
},
111+
106112
_ => bug!("SetDiscriminant on {} represented as {:#?}", dest_ty, dest_layout),
107113
}
108114
}

tests/compiletest.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
#![feature(slice_concat_ext)]
2+
13
extern crate compiletest_rs as compiletest;
24

5+
use std::slice::SliceConcatExt;
36
use std::path::{PathBuf, Path};
47
use std::io::Write;
58

@@ -41,22 +44,34 @@ fn run_pass(path: &str) {
4144
compiletest::run_tests(&config);
4245
}
4346

44-
fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool) {
45-
eprintln!("## Running run-pass tests in {} against miri for target {}", path, target);
47+
fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
48+
let opt_str = if opt {
49+
" with optimizations"
50+
} else {
51+
""
52+
};
53+
eprintln!("## Running run-pass tests in {} against miri for target {}{}", path, target, opt_str);
4654
let mut config = compiletest::default_config();
4755
config.mode = "mir-opt".parse().expect("Invalid mode");
4856
config.src_base = PathBuf::from(path);
4957
config.target = target.to_owned();
5058
config.host = host.to_owned();
5159
config.rustc_path = PathBuf::from("target/debug/miri");
60+
let mut flags = Vec::new();
5261
if fullmir {
5362
if host != target {
5463
// skip fullmir on nonhost
5564
return;
5665
}
5766
let sysroot = Path::new(&std::env::var("HOME").unwrap()).join(".xargo").join("HOST");
58-
config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
67+
flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
68+
}
69+
if opt {
70+
flags.push("-Zmir-opt-level=3".to_owned());
71+
} else {
72+
flags.push("-Zmir-opt-level=0".to_owned());
5973
}
74+
config.target_rustcflags = Some(flags.join(" "));
6075
// don't actually execute the final binary, it might be for other targets and we only care
6176
// about running miri, not the binary.
6277
config.runtool = Some("echo \"\" || ".to_owned());
@@ -113,10 +128,12 @@ fn run_pass_miri() {
113128
let sysroot = get_sysroot();
114129
let host = get_host();
115130

116-
for_all_targets(&sysroot, |target| {
117-
miri_pass("tests/run-pass", &target, &host, false);
118-
});
119-
miri_pass("tests/run-pass-fullmir", &host, &host, true);
131+
for &opt in [false, true].iter() {
132+
for_all_targets(&sysroot, |target| {
133+
miri_pass("tests/run-pass", &target, &host, false, opt);
134+
});
135+
miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
136+
}
120137
}
121138

122139
#[test]

tests/run-pass-fullmir/integer-ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// FIXME: remove the next line once https://github.com/rust-lang/rust/issues/43359 is fixed
12+
// compile-flags: -Zmir-opt-level=0
13+
1114
use std::i32;
1215

1316
pub fn main() {

tests/run-pass/cast-rfc0401-vtable-kinds.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
12+
// FIXME: remove the next line when https://github.com/rust-lang/rust/issues/43358 is resolved
13+
// compile-flags: -Zmir-opt-level=0
14+
1115
// Check that you can cast between different pointers to trait objects
1216
// whose vtable have the same kind (both lengths, or both trait pointers).
1317

0 commit comments

Comments
 (0)