Skip to content

Add FileCheck mode #354

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions guide/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ cargo bisect-rustc --script=./test.sh \
[issue #53157]: https://github.com/rust-lang/rust/issues/53157
[issue #55036]: https://github.com/rust-lang/rust/issues/55036

## Testing with LLVM FileCheck

If you want to investigate a regression in codegen, you can use LLVM's FileCheck. You can write a library containing FileCheck annotations:

```rs
// CHECK-LABEL: @wildcard(
#[no_mangle]
pub fn wildcard(a: u16, b: u16, v: u16) -> u16 {
// CHECK-NOT: br
match (a == v, b == v) {
(true, false) => 0,
(false, true) => u16::MAX,
_ => 1 << 15, // half
}
}
```

To investigate when `br` stopped being emitted, you can use `cargo-bisect-rustc --start 1.70.0 --filecheck src/lib.rs --preserve --regress success`.

By default, this will compile with `cargo rustc -- -Copt-level=3 -Cdebuginfo=0 --emit=llvm-ir=<target dir>/debug/deps/output.ll`.

## Custom bisection messages

*Available from v0.6.9*
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ a date (YYYY-MM-DD), git tag name (e.g. 1.58.0) or git commit SHA."
#[arg(long, help = "Script replacement for `cargo build` command")]
script: Option<PathBuf>,

#[arg(
long,
help = "Run in LLVM FileCheck, using the given path for annotations"
)]
filecheck: Option<PathBuf>,

#[arg(long, help = "Path to LLVM FileCheck")]
filecheck_path: Option<PathBuf>,

#[arg(long, help = "Do not install cargo [default: install cargo]")]
without_cargo: bool,

Expand Down
90 changes: 84 additions & 6 deletions src/toolchains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,24 @@ impl Toolchain {
}
});

let mut cmd = match (script, cfg.args.timeout) {
(Some(script), None) => {
let target_dir = format!("target-{}", self.rustup_name());
// Used in filecheck mode
let llir_path = PathBuf::from(&target_dir)
.join("debug")
.join("deps")
.join("output.ll");

let mut cmd = match (script, cfg.args.timeout, &cfg.args.filecheck) {
(Some(_), _, Some(_)) => {
panic!("incompatbile options `--script` and `--filecheck` used");
}
(Some(script), None, None) => {
let mut cmd = Command::new(script);
cmd.env("RUSTUP_TOOLCHAIN", self.rustup_name());
cmd.args(&cfg.args.command_args);
cmd
}
(None, None) => {
(None, None, None) => {
let mut cmd = Command::new("cargo");
cmd.arg(&format!("+{}", self.rustup_name()));
if cfg.args.command_args.is_empty() {
Expand All @@ -265,15 +275,15 @@ impl Toolchain {
}
cmd
}
(Some(script), Some(timeout)) => {
(Some(script), Some(timeout), None) => {
let mut cmd = Command::new("timeout");
cmd.arg(timeout.to_string());
cmd.arg(script);
cmd.args(&cfg.args.command_args);
cmd.env("RUSTUP_TOOLCHAIN", self.rustup_name());
cmd
}
(None, Some(timeout)) => {
(None, Some(timeout), None) => {
let mut cmd = Command::new("timeout");
cmd.arg(timeout.to_string());
cmd.arg("cargo");
Expand All @@ -285,9 +295,39 @@ impl Toolchain {
}
cmd
}
(None, None, Some(_)) => {
let mut cmd = Command::new("cargo");
cmd.arg(&format!("+{}", self.rustup_name()));
if cfg.args.command_args.is_empty() {
cmd.arg("rustc")
.arg("--")
.arg("-Copt-level=3")
.arg("-Cdebuginfo=0")
.arg(format!("--emit=llvm-ir={}", llir_path.display()));
} else {
cmd.args(&cfg.args.command_args);
}
cmd
}
(None, Some(timeout), Some(_)) => {
let mut cmd = Command::new("timeout");
cmd.arg(timeout.to_string());
cmd.arg("cargo");
cmd.arg(&format!("+{}", self.rustup_name()));
if cfg.args.command_args.is_empty() {
cmd.arg("rustc")
.arg("--")
.arg("-Copt-level=3")
.arg("-Cdebuginfo=0")
.arg(format!("--emit=llvm-ir={}", llir_path.display()));
} else {
cmd.args(&cfg.args.command_args);
}
cmd
}
};
cmd.current_dir(&cfg.args.test_dir);
cmd.env("CARGO_TARGET_DIR", format!("target-{}", self.rustup_name()));
cmd.env("CARGO_TARGET_DIR", &target_dir);
if let Some(target) = &cfg.args.target {
cmd.env("CARGO_BUILD_TARGET", target);
}
Expand Down Expand Up @@ -319,6 +359,44 @@ impl Toolchain {
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
}

let Some(check_file) = &cfg.args.filecheck else {
return output;
};

if !output.status.success() {
return output;
}

let filecheck_path = cfg
.args
.filecheck_path
.clone()
.unwrap_or_else(|| PathBuf::from("FileCheck"));

let mut cmd = Command::new(filecheck_path);
cmd.arg("--input-file")
.arg(
PathBuf::from(target_dir)
.join("debug")
.join("deps")
.join("output.ll"),
)
.arg(check_file);

cmd.stdout(default_stdio());
cmd.stderr(default_stdio());
let output = match cmd.output() {
Ok(output) => output,
Err(err) => {
panic!("thiserror::Errored to run {:?}: {:?}", cmd, err);
}
};
if must_capture_output && emit_output {
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
}

output
}

Expand Down
87 changes: 57 additions & 30 deletions tests/cmd/h.stdout
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure why this changed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files are autogenerated by trycmd when running tests, based on the content of the cli options.

I forgot the details, but in tests/README.md I added some info a while ago

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant I'm not sure specifically why the formatting of the help output changed here - descriptions are now all placed on the following line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some bisection of your patchset. The change that trigger this is the new command line argument

    #[arg(long, help = "Path to LLVM FileCheck")]
    filecheck_path: Option<PathBuf>,

if you remove the underscore from the parameter name (for example to filecheckpath), the help output does not add a newline anymore.

So, the issue is with clap. I quickly skimmed the changelog but couldn't find anything explaining this strange thing. Feel free to investigate further 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it's not the underscore, it's the name length - I think when the size of the longest arg name + the longest help string are such that having all arguments inline would be very long, it's wrapped round to a new line
Repro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ba39c32190f89d5b28976c14b28e58cb
Remove the S from the argument name and it'll be in-line

Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,63 @@ Arguments:
[COMMAND_ARGS]... Arguments to pass to cargo or the file specified by --script during tests

Options:
-a, --alt Download the alt build instead of normal build
--access <ACCESS> How to access Rust git repository [default: github] [possible
values: checkout, github]
--by-commit Bisect via commit artifacts
-c, --component <COMPONENTS> additional components to install
--end <END> Right bound for search (*with* regression). You can use a date
(YYYY-MM-DD), git tag name (e.g. 1.58.0) or git commit SHA.
--force-install Force installation over existing artifacts
-h, --help Print help (see more with '--help')
--host <HOST> Host triple for the compiler [default: [..]]
--install <INSTALL> Install the given artifact
--preserve Preserve the downloaded artifacts
--preserve-target Preserve the target directory used for builds
--prompt Manually evaluate for regression with prompts
--regress <REGRESS> Custom regression definition [default: error] [possible values:
error, success, ice, non-ice, non-error]
--script <SCRIPT> Script replacement for `cargo build` command
--start <START> Left bound for search (*without* regression). You can use a date
(YYYY-MM-DD), git tag name (e.g. 1.58.0) or git commit SHA.
-t, --timeout <TIMEOUT> Assume failure after specified number of seconds (for bisecting
hangs)
--target <TARGET> Cross-compilation target platform
--term-new <TERM_NEW> Text shown when a test does match the condition requested
--term-old <TERM_OLD> Text shown when a test fails to match the condition requested
--test-dir <TEST_DIR> Root directory for tests [default: .]
-v, --verbose...
-V, --version Print version
--with-dev Download rustc-dev [default: no download]
--with-src Download rust-src [default: no download]
--without-cargo Do not install cargo [default: install cargo]
-a, --alt
Download the alt build instead of normal build
--access <ACCESS>
How to access Rust git repository [default: github] [possible values: checkout, github]
--by-commit
Bisect via commit artifacts
-c, --component <COMPONENTS>
additional components to install
--end <END>
Right bound for search (*with* regression). You can use a date (YYYY-MM-DD), git tag name
(e.g. 1.58.0) or git commit SHA.
--filecheck <FILECHECK>
Run in LLVM FileCheck, using the given path for annotations
--filecheck-path <FILECHECK_PATH>
Path to LLVM FileCheck
--force-install
Force installation over existing artifacts
-h, --help
Print help (see more with '--help')
--host <HOST>
Host triple for the compiler [default: [..]]
--install <INSTALL>
Install the given artifact
--preserve
Preserve the downloaded artifacts
--preserve-target
Preserve the target directory used for builds
--prompt
Manually evaluate for regression with prompts
--regress <REGRESS>
Custom regression definition [default: error] [possible values: error, success, ice,
non-ice, non-error]
--script <SCRIPT>
Script replacement for `cargo build` command
--start <START>
Left bound for search (*without* regression). You can use a date (YYYY-MM-DD), git tag
name (e.g. 1.58.0) or git commit SHA.
-t, --timeout <TIMEOUT>
Assume failure after specified number of seconds (for bisecting hangs)
--target <TARGET>
Cross-compilation target platform
--term-new <TERM_NEW>
Text shown when a test does match the condition requested
--term-old <TERM_OLD>
Text shown when a test fails to match the condition requested
--test-dir <TEST_DIR>
Root directory for tests [default: .]
-v, --verbose...

-V, --version
Print version
--with-dev
Download rustc-dev [default: no download]
--with-src
Download rust-src [default: no download]
--without-cargo
Do not install cargo [default: install cargo]

Examples:
Run a fully automatic nightly bisect doing `cargo check`:
Expand Down
6 changes: 6 additions & 0 deletions tests/cmd/help.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Options:
Right bound for search (*with* regression). You can use a date (YYYY-MM-DD), git tag name
(e.g. 1.58.0) or git commit SHA.

--filecheck <FILECHECK>
Run in LLVM FileCheck, using the given path for annotations

--filecheck-path <FILECHECK_PATH>
Path to LLVM FileCheck

--force-install
Force installation over existing artifacts

Expand Down