Skip to content

Commit

Permalink
Auto merge of rust-lang#116924 - fmease:rollup-aznzvf8, r=fmease
Browse files Browse the repository at this point in the history
Rollup of 4 pull requests

Successful merges:

 - rust-lang#116037 (Add `-Zstack-protector` test for Windows targets)
 - rust-lang#116132 (Make TCP connect handle EINTR correctly)
 - rust-lang#116810 (Add FileCheck annotations to mir-opt tests.)
 - rust-lang#116896 (Only check in a single place if a pass is enabled.)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 19, 2023
2 parents 36b61e5 + b6f4522 commit 9679ae0
Show file tree
Hide file tree
Showing 362 changed files with 2,338 additions and 609 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
let is_fn_like = tcx.def_kind(def).is_fn_like();
if is_fn_like {
// Do not compute the mir call graph without said call graph actually being used.
if inline::Inline.is_enabled(&tcx.sess) {
if pm::should_run_pass(tcx, &inline::Inline) {
tcx.ensure_with_value().mir_inliner_callees(ty::InstanceDef::Item(def.to_def_id()));
}
}
Expand Down
33 changes: 21 additions & 12 deletions compiler/rustc_mir_transform/src/pass_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ pub fn run_passes<'tcx>(
run_passes_inner(tcx, body, passes, phase_change, true);
}

pub fn should_run_pass<'tcx, P>(tcx: TyCtxt<'tcx>, pass: &P) -> bool
where
P: MirPass<'tcx> + ?Sized,
{
let name = pass.name();

let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
let overridden =
overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(|(_name, polarity)| {
trace!(
pass = %name,
"{} as requested by flag",
if *polarity { "Running" } else { "Not running" },
);
*polarity
});
overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess))
}

fn run_passes_inner<'tcx>(
tcx: TyCtxt<'tcx>,
body: &mut Body<'tcx>,
Expand All @@ -100,19 +119,9 @@ fn run_passes_inner<'tcx>(
for pass in passes {
let name = pass.name();

let overridden = overridden_passes.iter().rev().find(|(s, _)| s == &*name).map(
|(_name, polarity)| {
trace!(
pass = %name,
"{} as requested by flag",
if *polarity { "Running" } else { "Not running" },
);
*polarity
},
);
if !overridden.unwrap_or_else(|| pass.is_enabled(&tcx.sess)) {
if !should_run_pass(tcx, *pass) {
continue;
}
};

let dump_enabled = pass.is_mir_dump_enabled();

Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/hermit/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl Socket {
unimplemented!()
}

pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
let (addr, len) = addr.into_inner();
cvt_r(|| unsafe { netc::connect(self.as_raw_fd(), addr.as_ptr(), len) })?;
Ok(())
}

pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
self.set_nonblocking(true)?;
let r = unsafe {
Expand Down
11 changes: 7 additions & 4 deletions library/std/src/sys/solid/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,15 @@ impl Socket {
}
}

pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
let (addr, len) = addr.into_inner();
cvt(unsafe { netc::connect(self.0.raw(), addr.as_ptr(), len) })?;
Ok(())
}

pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
self.set_nonblocking(true)?;
let r = unsafe {
let (addr, len) = addr.into_inner();
cvt(netc::connect(self.0.raw(), addr.as_ptr(), len))
};
let r = self.connect(addr);
self.set_nonblocking(false)?;

match r {
Expand Down
17 changes: 17 additions & 0 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::net::{Shutdown, SocketAddr};
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::str;
use crate::sys::fd::FileDesc;
use crate::sys::unix::IsMinusOne;
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::time::{Duration, Instant};
Expand Down Expand Up @@ -140,6 +141,22 @@ impl Socket {
unimplemented!()
}

pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
let (addr, len) = addr.into_inner();
loop {
let result = unsafe { libc::connect(self.as_raw_fd(), addr.as_ptr(), len) };
if result.is_minus_one() {
let err = crate::sys::os::errno();
match err {
libc::EINTR => continue,
libc::EISCONN => return Ok(()),
_ => return Err(io::Error::from_raw_os_error(err)),
}
}
return Ok(());
}
}

pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
self.set_nonblocking(true)?;
let r = unsafe {
Expand Down
12 changes: 7 additions & 5 deletions library/std/src/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ impl Socket {
}
}

pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
let (addr, len) = addr.into_inner();
let result = unsafe { c::connect(self.as_raw(), addr.as_ptr(), len) };
cvt(result).map(drop)
}

pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
self.set_nonblocking(true)?;
let result = {
let (addr, len) = addr.into_inner();
let result = unsafe { c::connect(self.as_raw(), addr.as_ptr(), len) };
cvt(result).map(drop)
};
let result = self.connect(addr);
self.set_nonblocking(false)?;

match result {
Expand Down
4 changes: 1 addition & 3 deletions library/std/src/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ impl TcpStream {
init();

let sock = Socket::new(addr, c::SOCK_STREAM)?;

let (addr, len) = addr.into_inner();
cvt_r(|| unsafe { c::connect(sock.as_raw(), addr.as_ptr(), len) })?;
sock.connect(addr)?;
Ok(TcpStream { inner: sock })
}

Expand Down
65 changes: 26 additions & 39 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::json;
use crate::read2::{read2_abbreviated, Truncated};
use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
use crate::ColorConfig;
use miropt_test_tools::{files_for_miropt_test, MiroptTest, MiroptTestFile};
use regex::{Captures, Regex};
use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};

Expand Down Expand Up @@ -229,6 +230,7 @@ enum Emit {
None,
Metadata,
LlvmIr,
Mir,
Asm,
LinkArgsAsm,
}
Expand Down Expand Up @@ -2506,6 +2508,9 @@ impl<'test> TestCx<'test> {
Emit::LlvmIr => {
rustc.args(&["--emit", "llvm-ir"]);
}
Emit::Mir => {
rustc.args(&["--emit", "mir"]);
}
Emit::Asm => {
rustc.args(&["--emit", "asm"]);
}
Expand Down Expand Up @@ -3984,11 +3989,17 @@ impl<'test> TestCx<'test> {
fn run_mir_opt_test(&self) {
let pm = self.pass_mode();
let should_run = self.should_run(pm);
let emit_metadata = self.should_emit_metadata(pm);
let passes = self.get_passes();

let proc_res = self.compile_test_with_passes(should_run, emit_metadata, passes);
self.check_mir_dump();
let mut test_info = files_for_miropt_test(
&self.testpaths.file,
self.config.get_pointer_width(),
self.config.target_cfg().panic.for_miropt_test_tools(),
);

let passes = std::mem::take(&mut test_info.passes);

let proc_res = self.compile_test_with_passes(should_run, Emit::Mir, passes);
self.check_mir_dump(test_info);
if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
}
Expand All @@ -4002,37 +4013,12 @@ impl<'test> TestCx<'test> {
}
}

fn get_passes(&self) -> Vec<String> {
let files = miropt_test_tools::files_for_miropt_test(
&self.testpaths.file,
self.config.get_pointer_width(),
self.config.target_cfg().panic.for_miropt_test_tools(),
);

let mut out = Vec::new();

for miropt_test_tools::MiroptTestFiles {
from_file: _,
to_file: _,
expected_file: _,
passes,
} in files
{
out.extend(passes);
}
out
}

fn check_mir_dump(&self) {
fn check_mir_dump(&self, test_info: MiroptTest) {
let test_dir = self.testpaths.file.parent().unwrap();
let test_crate =
self.testpaths.file.file_stem().unwrap().to_str().unwrap().replace("-", "_");

let suffix = miropt_test_tools::output_file_suffix(
&self.testpaths.file,
self.config.get_pointer_width(),
self.config.target_cfg().panic.for_miropt_test_tools(),
);
let MiroptTest { run_filecheck, suffix, files, passes: _ } = test_info;

if self.config.bless {
for e in
Expand All @@ -4047,14 +4033,7 @@ impl<'test> TestCx<'test> {
}
}

let files = miropt_test_tools::files_for_miropt_test(
&self.testpaths.file,
self.config.get_pointer_width(),
self.config.target_cfg().panic.for_miropt_test_tools(),
);
for miropt_test_tools::MiroptTestFiles { from_file, to_file, expected_file, passes: _ } in
files
{
for MiroptTestFile { from_file, to_file, expected_file } in files {
let dumped_string = if let Some(after) = to_file {
self.diff_mir_files(from_file.into(), after.into())
} else {
Expand Down Expand Up @@ -4095,6 +4074,14 @@ impl<'test> TestCx<'test> {
}
}
}

if run_filecheck {
let output_path = self.output_base_name().with_extension("mir");
let proc_res = self.verify_with_filecheck(&output_path);
if !proc_res.status.success() {
self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
}
}
}

fn diff_mir_files(&self, before: PathBuf, after: PathBuf) -> String {
Expand Down
27 changes: 17 additions & 10 deletions src/tools/miropt-test-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::fs;
use std::path::Path;

pub struct MiroptTestFiles {
pub struct MiroptTestFile {
pub expected_file: std::path::PathBuf,
pub from_file: String,
pub to_file: Option<String>,
}

pub struct MiroptTest {
pub run_filecheck: bool,
pub suffix: String,
pub files: Vec<MiroptTestFile>,
/// Vec of passes under test to be dumped
pub passes: Vec<String>,
}
Expand All @@ -14,11 +20,7 @@ pub enum PanicStrategy {
Abort,
}

pub fn output_file_suffix(
testfile: &Path,
bit_width: u32,
panic_strategy: PanicStrategy,
) -> String {
fn output_file_suffix(testfile: &Path, bit_width: u32, panic_strategy: PanicStrategy) -> String {
let mut each_bit_width = false;
let mut each_panic_strategy = false;
for line in fs::read_to_string(testfile).unwrap().lines() {
Expand Down Expand Up @@ -47,16 +49,22 @@ pub fn files_for_miropt_test(
testfile: &std::path::Path,
bit_width: u32,
panic_strategy: PanicStrategy,
) -> Vec<MiroptTestFiles> {
) -> MiroptTest {
let mut out = Vec::new();
let test_file_contents = fs::read_to_string(&testfile).unwrap();

let test_dir = testfile.parent().unwrap();
let test_crate = testfile.file_stem().unwrap().to_str().unwrap().replace('-', "_");

let suffix = output_file_suffix(testfile, bit_width, panic_strategy);
let mut run_filecheck = true;
let mut passes = Vec::new();

for l in test_file_contents.lines() {
if l.starts_with("// skip-filecheck") {
run_filecheck = false;
continue;
}
if l.starts_with("// EMIT_MIR ") {
let test_name = l.trim_start_matches("// EMIT_MIR ").trim();
let mut test_names = test_name.split(' ');
Expand All @@ -65,7 +73,6 @@ pub fn files_for_miropt_test(
let mut expected_file;
let from_file;
let to_file;
let mut passes = Vec::new();

if test_name.ends_with(".diff") {
let trimmed = test_name.trim_end_matches(".diff");
Expand Down Expand Up @@ -114,9 +121,9 @@ pub fn files_for_miropt_test(
}
let expected_file = test_dir.join(expected_file);

out.push(MiroptTestFiles { expected_file, from_file, to_file, passes });
out.push(MiroptTestFile { expected_file, from_file, to_file });
}
}

out
MiroptTest { run_filecheck, suffix, files: out, passes }
}
3 changes: 2 additions & 1 deletion src/tools/tidy/src/mir_opt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn check_unused_files(path: &Path, bless: bool, bad: &mut bool) {
for file in rs_files {
for bw in [32, 64] {
for ps in [PanicStrategy::Unwind, PanicStrategy::Abort] {
for output_file in miropt_test_tools::files_for_miropt_test(&file, bw, ps) {
let mir_opt_test = miropt_test_tools::files_for_miropt_test(&file, bw, ps);
for output_file in mir_opt_test.files {
output_files.remove(&output_file.expected_file);
}
}
Expand Down
Loading

0 comments on commit 9679ae0

Please sign in to comment.