Skip to content

Commit

Permalink
Rollup merge of rust-lang#127510 - tgross35:test-float-parse-update, …
Browse files Browse the repository at this point in the history
…r=Mark-Simulacrum

Rewrite `test-float-parse` in Rust

Migrate from the currently broken Rust + Python `test-float-parse` to a Rust implementation. This newer version should be significantly faster (tests execute in parallel with threads, rather than series across multiple processes, which also eliminates the "...the worker processes are leaked and stick around forever" message), and should be significantly easier to extend to the new float types.

Since this is faster and hopefully more stable, we should be able to launch it with `x` and run the faster tests in CI.
  • Loading branch information
compiler-errors authored Jul 20, 2024
2 parents 60684d2 + 7f7ec2d commit 0392b8a
Show file tree
Hide file tree
Showing 36 changed files with 2,509 additions and 555 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2598,12 +2598,76 @@ dependencies = [
"windows-sys 0.48.0",
]

[[package]]
name = "num"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]

[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]

[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]

[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"

[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]

[[package]]
name = "num-iter"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]

[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]

[[package]]
name = "num-traits"
version = "0.2.19"
Expand Down Expand Up @@ -5630,6 +5694,17 @@ dependencies = [
"std",
]

[[package]]
name = "test-float-parse"
version = "0.1.0"
dependencies = [
"indicatif",
"num",
"rand",
"rand_chacha",
"rayon",
]

[[package]]
name = "textwrap"
version = "0.16.1"
Expand Down
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"compiler/rustc",
"library/std",
"library/sysroot",
"src/etc/test-float-parse",
"src/rustdoc-json-types",
"src/tools/build_helper",
"src/tools/cargotest",
Expand Down Expand Up @@ -109,6 +110,18 @@ strip = true
debug = 0
strip = true

# Bigint libraries are slow without optimization, speed up testing
[profile.dev.package.test-float-parse]
opt-level = 3

# Speed up the binary as much as possible
[profile.release.package.test-float-parse]
opt-level = 3
codegen-units = 1
# FIXME: LTO cannot be enabled for binaries in a workspace
# <https://github.com/rust-lang/cargo/issues/9330>
# lto = true

[patch.crates-io]
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on
# here
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/mk/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ check-aux:
$(Q)$(BOOTSTRAP) test --stage 2 \
src/tools/cargo \
src/tools/cargotest \
etc/etc/test-float-parse \
$(BOOTSTRAP_ARGS)
# Run standard library tests in Miri.
$(Q)BOOTSTRAP_SKIP_TARGET_SANITY=1 \
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/build_steps/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ tool_check_step!(CargoMiri, "src/tools/miri/cargo-miri", SourceType::InTree);
tool_check_step!(Rls, "src/tools/rls", SourceType::InTree);
tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
tool_check_step!(MiroptTestTools, "src/tools/miropt-test-tools", SourceType::InTree);
tool_check_step!(TestFloatParse, "src/etc/test-float-parse", SourceType::InTree);

tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);

Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/build_steps/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,5 @@ lint_any!(
Rustfmt, "src/tools/rustfmt", "rustfmt";
RustInstaller, "src/tools/rust-installer", "rust-installer";
Tidy, "src/tools/tidy", "tidy";
TestFloatParse, "src/etc/test-float-parse", "test-float-parse";
);
77 changes: 77 additions & 0 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3505,3 +3505,80 @@ impl Step for CodegenGCC {
cargo.into_cmd().run(builder);
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TestFloatParse {
path: PathBuf,
host: TargetSelection,
}

impl Step for TestFloatParse {
type Output = ();
const ONLY_HOSTS: bool = true;
const DEFAULT: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/etc/test-float-parse")
}

fn make_run(run: RunConfig<'_>) {
for path in run.paths {
let path = path.assert_single_path().path.clone();
run.builder.ensure(Self { path, host: run.target });
}
}

fn run(self, builder: &Builder<'_>) {
let bootstrap_host = builder.config.build;
let compiler = builder.compiler(0, bootstrap_host);
let path = self.path.to_str().unwrap();
let crate_name = self.path.components().last().unwrap().as_os_str().to_str().unwrap();

builder.ensure(compile::Std::new(compiler, self.host));

// Run any unit tests in the crate
let cargo_test = tool::prepare_tool_cargo(
builder,
compiler,
Mode::ToolStd,
bootstrap_host,
"test",
path,
SourceType::InTree,
&[],
);

run_cargo_test(
cargo_test,
&[],
&[],
crate_name,
crate_name,
compiler,
bootstrap_host,
builder,
);

// Run the actual parse tests.
let mut cargo_run = tool::prepare_tool_cargo(
builder,
compiler,
Mode::ToolStd,
bootstrap_host,
"run",
path,
SourceType::InTree,
&[],
);

cargo_run.arg("--");
if builder.config.args().is_empty() {
// By default, exclude tests that take longer than ~1m.
cargo_run.arg("--skip-huge");
} else {
cargo_run.args(builder.config.args());
}

cargo_run.into_cmd().run(builder);
}
}
3 changes: 3 additions & 0 deletions src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ impl<'a> Builder<'a> {
clippy::Rustdoc,
clippy::Rustfmt,
clippy::RustInstaller,
clippy::TestFloatParse,
clippy::Tidy,
),
Kind::Check | Kind::Fix => describe!(
Expand All @@ -840,6 +841,7 @@ impl<'a> Builder<'a> {
check::Rls,
check::Rustfmt,
check::RustAnalyzer,
check::TestFloatParse,
check::Bootstrap,
),
Kind::Test => describe!(
Expand Down Expand Up @@ -901,6 +903,7 @@ impl<'a> Builder<'a> {
test::RustdocJson,
test::HtmlCheck,
test::RustInstaller,
test::TestFloatParse,
// Run bootstrap close to the end as it's unlikely to fail
test::Bootstrap,
// Run run-make last, since these won't pass without make on Windows
Expand Down
9 changes: 5 additions & 4 deletions src/etc/test-float-parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ version = "0.1.0"
edition = "2021"
publish = false

[workspace]
resolver = "1"

[dependencies]
rand = "0.8"
indicatif = { version = "0.17.8", default-features = false }
num = "0.4.3"
rand = "0.8.5"
rand_chacha = "0.3"
rayon = "1"

[lib]
name = "test_float_parse"
55 changes: 55 additions & 0 deletions src/etc/test-float-parse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Float Parsing Tests

These are tests designed to test decimal to float conversions (`dec2flt`) used
by the standard library.

It consistes of a collection of test generators that each generate a set of
patterns intended to test a specific property. In addition, there are exhaustive
tests (for <= `f32`) and fuzzers (for anything that can't be run exhaustively).

The generators work as follows:

- Each generator is a struct that lives somewhere in the `gen` module. Usually
it is generic over a float type.
- These generators must implement `Iterator`, which should return a context type
that can be used to construct a test string (but usually not the string
itself).
- They must also implement the `Generator` trait, which provides a method to
write test context to a string as a test case, as well as some extra metadata.

The split between context generation and string construction is so that we can
reuse string allocations.
- Each generator gets registered once for each float type. Each of these
generators then get their iterator called, and each test case checked against
the float type's parse implementation.

Some generators produce decimal strings, others create bit patterns that need to
be bitcasted to the float type, which then uses its `Display` implementation to
write to a string. For these, float to decimal (`flt2dec`) conversions also get
tested, if unintentionally.

For each test case, the following is done:

- The test string is parsed to the float type using the standard library's
implementation.
- The test string is parsed separately to a `BigRational`, which acts as a
representation with infinite precision.
- The rational value then gets checked that it is within the float's
representable values (absolute value greater than the smallest number to round
to zero, but less less than the first value to round to infinity). If these
limits are exceeded, check that the parsed float reflects that.
- For real nonzero numbers, the parsed float is converted into a rational using
`significand * 2^exponent`. It is then checked against the actual rational
value, and verified to be within half a bit's precision of the parsed value.
Also it is checked that ties round to even.

This is all highly parallelized with `rayon`; test generators can run in
parallel, and their tests get chunked and run in parallel.

There is a simple command line that allows filtering which tests are run,
setting the number of iterations for fuzzing tests, limiting failures, setting
timeouts, etc. See `main.rs` or run with `--help` for options.

Note that when running via `./x`, only tests that take less than a few minutes
are run by default. Navigate to the crate (or pass `-C` to Cargo) and run it
directly to run all tests or pass specific arguments.
Loading

0 comments on commit 0392b8a

Please sign in to comment.