Skip to content
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

Add MIR Optimization Tests #34715

Merged
merged 1 commit into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ check-stage$(1)-T-$(2)-H-$(3)-exec: \
check-stage$(1)-T-$(2)-H-$(3)-ui-exec \
check-stage$(1)-T-$(2)-H-$(3)-doc-exec \
check-stage$(1)-T-$(2)-H-$(3)-doc-error-index-exec \
check-stage$(1)-T-$(2)-H-$(3)-pretty-exec
check-stage$(1)-T-$(2)-H-$(3)-pretty-exec \
check-stage$(1)-T-$(2)-H-$(3)-mir-opt-exec

ifndef CFG_DISABLE_CODEGEN_TESTS
check-stage$(1)-T-$(2)-H-$(3)-exec: \
Expand Down Expand Up @@ -458,6 +459,7 @@ UI_RS := $(call rwildcard,$(S)src/test/ui/,*.rs) \
$(call rwildcard,$(S)src/test/ui/,*.stdout) \
$(call rwildcard,$(S)src/test/ui/,*.stderr)
RUSTDOCCK_RS := $(call rwildcard,$(S)src/test/rustdoc/,*.rs)
MIR_OPT_RS := $(call rwildcard,$(S)src/test/mir-opt/,*.rs)

RPASS_TESTS := $(RPASS_RS)
RPASS_VALGRIND_TESTS := $(RPASS_VALGRIND_RS)
Expand All @@ -475,6 +477,7 @@ CODEGEN_UNITS_TESTS := $(CODEGEN_UNITS_RS)
INCREMENTAL_TESTS := $(INCREMENTAL_RS)
RMAKE_TESTS := $(RMAKE_RS)
UI_TESTS := $(UI_RS)
MIR_OPT_TESTS := $(MIR_OPT_RS)
RUSTDOCCK_TESTS := $(RUSTDOCCK_RS)

CTEST_SRC_BASE_rpass = run-pass
Expand Down Expand Up @@ -552,6 +555,11 @@ CTEST_BUILD_BASE_ui = ui
CTEST_MODE_ui = ui
CTEST_RUNTOOL_ui = $(CTEST_RUNTOOL)

CTEST_SRC_BASE_mir-opt = mir-opt
CTEST_BUILD_BASE_mir-opt = mir-opt
CTEST_MODE_mir-opt = mir-opt
CTEST_RUNTOOL_mir-opt = $(CTEST_RUNTOOL)

CTEST_SRC_BASE_rustdocck = rustdoc
CTEST_BUILD_BASE_rustdocck = rustdoc
CTEST_MODE_rustdocck = rustdoc
Expand Down Expand Up @@ -684,6 +692,7 @@ CTEST_DEPS_incremental_$(1)-T-$(2)-H-$(3) = $$(INCREMENTAL_TESTS)
CTEST_DEPS_rmake_$(1)-T-$(2)-H-$(3) = $$(RMAKE_TESTS) \
$$(CSREQ$(1)_T_$(3)_H_$(3)) $$(SREQ$(1)_T_$(2)_H_$(3))
CTEST_DEPS_ui_$(1)-T-$(2)-H-$(3) = $$(UI_TESTS)
CTEST_DEPS_mir-opt_$(1)-T-$(2)-H-$(3) = $$(MIR_OPT_TESTS)
CTEST_DEPS_rustdocck_$(1)-T-$(2)-H-$(3) = $$(RUSTDOCCK_TESTS) \
$$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
$(S)src/etc/htmldocck.py
Expand Down Expand Up @@ -755,7 +764,7 @@ endef

CTEST_NAMES = rpass rpass-valgrind rpass-full rfail-full cfail-full rfail cfail pfail \
debuginfo-gdb debuginfo-lldb codegen codegen-units rustdocck incremental \
rmake ui
rmake ui mir-opt

$(foreach host,$(CFG_HOST), \
$(eval $(foreach target,$(CFG_TARGET), \
Expand Down Expand Up @@ -964,6 +973,7 @@ TEST_GROUPS = \
pretty-rfail-full \
pretty-rfail \
pretty-pretty \
mir-opt \
$(NULL)

define DEF_CHECK_FOR_STAGE_AND_TARGET_AND_HOST
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ impl Build {
check::compiletest(self, &compiler, target.target,
"pretty", "run-pass-valgrind");
}
CheckMirOpt { compiler } => {
check::compiletest(self, &compiler, target.target,
"mir-opt", "mir-opt");
}
CheckCodegen { compiler } => {
check::compiletest(self, &compiler, target.target,
"codegen", "codegen");
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ macro_rules! targets {
(check_codegen_units, CheckCodegenUnits { compiler: Compiler<'a> }),
(check_incremental, CheckIncremental { compiler: Compiler<'a> }),
(check_ui, CheckUi { compiler: Compiler<'a> }),
(check_mir_opt, CheckMirOpt { compiler: Compiler<'a> }),
(check_debuginfo, CheckDebuginfo { compiler: Compiler<'a> }),
(check_rustdoc, CheckRustdoc { compiler: Compiler<'a> }),
(check_docs, CheckDocs { compiler: Compiler<'a> }),
Expand Down Expand Up @@ -444,6 +445,7 @@ impl<'a> Step<'a> {
self.check_pretty_rfail_full(compiler),
self.check_rpass_valgrind(compiler),
self.check_rmake(compiler),
self.check_mir_opt(compiler),

// crates
self.check_crate_rustc(compiler),
Expand Down Expand Up @@ -471,6 +473,7 @@ impl<'a> Step<'a> {
Source::CheckTidy { stage } => {
vec![self.tool_tidy(stage)]
}
Source::CheckMirOpt { compiler} |
Source::CheckPrettyRPass { compiler } |
Source::CheckPrettyRFail { compiler } |
Source::CheckRFail { compiler } |
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"set the MIR optimization level (0-3)"),
dump_mir: Option<String> = (None, parse_opt_string,
"dump MIR state at various points in translation"),
dump_mir_dir: Option<String> = (None, parse_opt_string,
"the directory the MIR is dumped into"),
orbit: bool = (false, parse_bool,
"get MIR where it belongs - everywhere; most importantly, in orbit"),
}
Expand Down
9 changes: 8 additions & 1 deletion src/librustc_mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::fmt::Display;
use std::fs;
use std::io::{self, Write};
use syntax::ast::NodeId;
use std::path::{PathBuf, Path};

const INDENT: &'static str = " ";
/// Alignment for lining up comments following MIR statements
Expand Down Expand Up @@ -66,9 +67,15 @@ pub fn dump_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
_ => String::new()
};

let mut file_path = PathBuf::new();
if let Some(ref file_dir) = tcx.sess.opts.debugging_opts.dump_mir_dir {
let p = Path::new(file_dir);
file_path.push(p);
};
let file_name = format!("rustc.node{}{}.{}.{}.mir",
node_id, promotion_id, pass_name, disambiguator);
let _ = fs::File::create(&file_name).and_then(|mut file| {
file_path.push(&file_name);
let _ = fs::File::create(&file_path).and_then(|mut file| {
try!(writeln!(file, "// MIR for `{}`", node_path));
try!(writeln!(file, "// node_id = {}", node_id));
try!(writeln!(file, "// pass_name = {}", pass_name));
Expand Down
44 changes: 44 additions & 0 deletions src/test/mir-opt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
This folder contains tests for MIR optimizations.

The test format is:

```
(arbitrary rust code)
// END RUST SOURCE
// START $file_name_of_some_mir_dump_0
// $expected_line_0
// ...
// $expected_line_N
// END $file_name_of_some_mir_dump_0
// ...
// START $file_name_of_some_mir_dump_N
// $expected_line_0
// ...
// $expected_line_N
// END $file_name_of_some_mir_dump_N
```

All the test information is in comments so the test is runnable.

For each $file_name, compiletest expects [$expected_line_0, ...,
$expected_line_N] to appear in the dumped MIR in order. Currently it allows
other non-matched lines before, after and in-between.

Lines match ignoring whitespace, and the prefix "//" is removed.

It also currently strips trailing comments -- partly because the full file path
in "scope comments" is unpredictable and partly because tidy complains about
the lines being too long.

compiletest handles dumping the MIR before and after every pass for you. The
test writer only has to specify the file names of the dumped files (not the
full path to the file) and what lines to expect. I added an option to rustc
that tells it to dump the mir into some directly (rather then always dumping to
the current directory).

Lines match ignoring whitespace, and the prefix "//" is removed of course.

It also currently strips trailing comments -- partly because the full file path
in "scope comments" is unpredictable and partly because tidy complains about
the lines being too long.

18 changes: 18 additions & 0 deletions src/test/mir-opt/return_an_array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// this tests move up progration, which is not yet implemented

fn foo() -> [u8; 1024] {
let x = [0; 1024];
return x;
}

fn main() { }
27 changes: 27 additions & 0 deletions src/test/mir-opt/simplify_if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
if false {
println!("hello world!");
}
}

// END RUST SOURCE
// START rustc.node4.SimplifyBranches.initial-before.mir
// bb0: {
// if(const false) -> [true: bb1, false: bb2]; // scope 0 at simplify_if.rs:12:5: 14:6
// }
// END rustc.node4.SimplifyBranches.initial-before.mir
// START rustc.node4.SimplifyBranches.initial-after.mir
// bb0: {
// goto -> bb2; // scope 0 at simplify_if.rs:12:5: 14:6
// }
// END rustc.node4.SimplifyBranches.initial-after.mir
3 changes: 3 additions & 0 deletions src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Mode {
Incremental,
RunMake,
Ui,
MirOpt,
}

impl FromStr for Mode {
Expand All @@ -49,6 +50,7 @@ impl FromStr for Mode {
"incremental" => Ok(Incremental),
"run-make" => Ok(RunMake),
"ui" => Ok(Ui),
"mir-opt" => Ok(MirOpt),
_ => Err(()),
}
}
Expand All @@ -71,6 +73,7 @@ impl fmt::Display for Mode {
Incremental => "incremental",
RunMake => "run-make",
Ui => "ui",
MirOpt => "mir-opt",
}, f)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
reqopt("", "stage-id", "the target-stage identifier", "stageN-TARGET"),
reqopt("", "mode", "which sort of compile tests to run",
"(compile-fail|parse-fail|run-fail|run-pass|\
run-pass-valgrind|pretty|debug-info|incremental)"),
run-pass-valgrind|pretty|debug-info|incremental|mir-opt)"),
optflag("", "ignored", "run tests marked as ignored"),
optopt("", "runtool", "supervisor program to run tests under \
(eg. emulator, valgrind)", "PROGRAM"),
Expand Down
Loading