Skip to content

Replace in-tree rustc_apfloat with the new version of the crate #113843

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

Merged
merged 5 commits into from
Jul 26, 2023
Merged
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
5 changes: 0 additions & 5 deletions .reuse/dep5
Original file line number Diff line number Diff line change
@@ -38,11 +38,6 @@ Files: compiler/*
Copyright: The Rust Project Developers (see https://thanks.rust-lang.org)
License: MIT or Apache-2.0

Files: compiler/rustc_apfloat/*
Copyright: LLVM APFloat authors
The Rust Project Developers (see https://thanks.rust-lang.org)
License: NCSA AND (MIT OR Apache-2.0)

Files: compiler/rustc_codegen_cranelift/src/cranelift_native.rs
Copyright: The Cranelift Project Developers
The Rust Project Developers (see https://thanks.rust-lang.org)
8 changes: 5 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
@@ -3135,7 +3135,9 @@ dependencies = [

[[package]]
name = "rustc_apfloat"
version = "0.0.0"
version = "0.2.0+llvm-462a31f5a5ab"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "465187772033a5ee566f69fe008df03628fce549a0899aae76f0a0c2e34696be"
dependencies = [
"bitflags 1.3.2",
"smallvec",
@@ -4725,9 +4727,9 @@ dependencies = [

[[package]]
name = "smallvec"
version = "1.10.0"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"

[[package]]
name = "snap"
15 changes: 0 additions & 15 deletions LICENSES/NCSA.txt

This file was deleted.

8 changes: 0 additions & 8 deletions compiler/rustc_apfloat/Cargo.toml

This file was deleted.

2,757 changes: 0 additions & 2,757 deletions compiler/rustc_apfloat/src/ieee.rs

This file was deleted.

695 changes: 0 additions & 695 deletions compiler/rustc_apfloat/src/lib.rs

This file was deleted.

434 changes: 0 additions & 434 deletions compiler/rustc_apfloat/src/ppc.rs

This file was deleted.

3,301 changes: 0 additions & 3,301 deletions compiler/rustc_apfloat/tests/ieee.rs

This file was deleted.

530 changes: 0 additions & 530 deletions compiler/rustc_apfloat/tests/ppc.rs

This file was deleted.

2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
tracing = "0.1"
either = "1"
rustc_apfloat = { path = "../rustc_apfloat" }
rustc_apfloat = "0.2.0"
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
rustc_data_structures = { path = "../rustc_data_structures" }
2 changes: 1 addition & 1 deletion compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ gsgdt = "0.1.2"
field-offset = "0.3.5"
measureme = "10.0.0"
polonius-engine = "0.13.0"
rustc_apfloat = { path = "../rustc_apfloat" }
rustc_apfloat = "0.2.0"
rustc_arena = { path = "../rustc_arena" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ rustc_arena = { path = "../rustc_arena" }
tracing = "0.1"
either = "1"
rustc_middle = { path = "../rustc_middle" }
rustc_apfloat = { path = "../rustc_apfloat" }
rustc_apfloat = "0.2.0"
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
rustc_errors = { path = "../rustc_errors" }
10 changes: 0 additions & 10 deletions src/tools/miri/src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
@@ -914,16 +914,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let x = this.read_scalar(x)?.to_f64()?;
let exp = this.read_scalar(exp)?.to_i32()?;

// Saturating cast to i16. Even those are outside the valid exponent range so
Copy link
Member

Choose a reason for hiding this comment

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

I am trying to figure out why this was originally here so that I can justify why its being removed now.

I got to this commit: 0743ed6
and now I'm stuck. Why were we clamping ldexp exponent to i16, and why don't we need to keep doing so now? I'm assuming its a weakness in the old APFloat API that has since been addressed in the new rustc_apfloat, but can you confirm that @wesleywiser ?

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI this is from Miri PR rust-lang/miri#902

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm assuming its a weakness in the old APFloat API that has since been addressed in the new rustc_apfloat, but can you confirm that @wesleywiser ?

Yes, exactly. The old APFloat API took ldexp as an i16 but the new crate takes i32 which matches what the code originally did before calling into APFloat. There's additional discussion why i16 is sufficient for this parameter in the discussion here rust-lang/miri#902 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

I see. So in theory this could have stayed as-is, but since it was an artifact of a prior limitation, we're removing it. RIght?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I could have just added an .into() to do the i16 -> i32 conversion but since we're actually doing a i32 -> i16 -> i32 conversion, it seemed cleaner to just remove the conversions entirely.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, thanks for the nice cleanup. :)

Copy link
Member

Choose a reason for hiding this comment

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

I can explain this one, it's llvm/llvm-project@8606d01 [APFloat] Enlarge ExponentType to 32bit integer from late 2019 - previously, miri was stuck with an i32 and rustc_apfloat wanted i16, but they now both agree on i32.

// `scalbn` below will do its over/underflow handling.
let exp = if exp > i32::from(i16::MAX) {
i16::MAX
} else if exp < i32::from(i16::MIN) {
i16::MIN
} else {
exp.try_into().unwrap()
};

let res = x.scalbn(exp);
this.write_scalar(Scalar::from_f64(res), dest)?;
}
2 changes: 2 additions & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ const EXCEPTIONS: &[(&str, &str)] = &[
("instant", "BSD-3-Clause"), // rustc_driver/tracing-subscriber/parking_lot
("mdbook", "MPL-2.0"), // mdbook
("openssl", "Apache-2.0"), // opt-dist
("rustc_apfloat", "Apache-2.0 WITH LLVM-exception"), // rustc (license is the same as LLVM uses)
("ryu", "Apache-2.0 OR BSL-1.0"), // cargo/... (because of serde)
("self_cell", "Apache-2.0"), // rustc (fluent translations)
("snap", "BSD-3-Clause"), // rustc
@@ -224,6 +225,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"rustc-hash",
"rustc-rayon",
"rustc-rayon-core",
"rustc_apfloat",
"rustc_version",
"rustix",
"ruzstd", // via object in thorin-dwp
4 changes: 0 additions & 4 deletions src/tools/tidy/src/style.rs
Original file line number Diff line number Diff line change
@@ -302,10 +302,6 @@ pub fn check(path: &Path, bad: &mut bool) {
return;
}
}
// apfloat shouldn't be changed because of license problems
if is_in(file, "compiler", "rustc_apfloat") {
return;
}
let mut skip_cr = contains_ignore_directive(can_contain, &contents, "cr");
let mut skip_undocumented_unsafe =
contains_ignore_directive(can_contain, &contents, "undocumented-unsafe");
9 changes: 9 additions & 0 deletions tests/ui/const_prop/apfloat-f64-roundtrip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-pass
// compile-flags: -O -Zmir-opt-level=3 -Cno-prepopulate-passes
// min-llvm-version: 16.0 (requires APFloat fixes in LLVM)
Copy link
Member

Choose a reason for hiding this comment

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

It is still a mystery to me why with -Cno-prepopulate-passes this require apfloat fixes. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm not sure either. Investigating more into LLVM at this point didn't seem worthwhile to me if it's been fixed in LLVM 16 but I could be convinced otherwise 🙂

Perhaps the issue is that LLVM 14's bitcode reader fails to roundtrip the float?

Copy link
Member

Choose a reason for hiding this comment

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

LLVM constant-folding is not a pass, but rather happens when instructions are attempted to be created.

There are passes, however, that do non-trivial simplifications, and propagate constants from e.g. loads of constant globals (though that might be in constant-folding, too? unsure), which re-triggers the constant-folding logic that first happened when the instructions were built, to account for their new inputs (you can almost think of it as "replace all uses of X with Y" simply rebuilding all instructions using X, and then the constructors of those instructions see Y now, which lets them try constant-folding again, and maybe get farther this time).

In fewer words: attempting to add a runtime LLVM Instruction to a BasicBlock may result in a Constant being produced instead (I don't know if there's a way to bypass this).

Copy link
Member

Choose a reason for hiding this comment

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

Small addendum: you may see something different when testing calls (of either LLVM intrinsics or libm functions it knows about): IIRC, those are handled by a pass, it's only the native LLVM IR operations that get constant-folded as they're built.


// Regression test for a broken MIR optimization (issue #113407).
pub fn main() {
let f = f64::from_bits(0x19873cc2) as f32;
assert_eq!(f.to_bits(), 0);
}
15 changes: 15 additions & 0 deletions tests/ui/const_prop/apfloat-remainder-regression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass
// compile-flags: -O -Zmir-opt-level=3 -Cno-prepopulate-passes

// Regression test for a broken MIR optimization (issue #102403).
pub fn f() -> f64 {
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
}

pub fn g() -> f64 {
-1.0 % -1.0
}

pub fn main() {
assert_eq!(f().signum(), g().signum());
}
15 changes: 15 additions & 0 deletions tests/ui/numbers-arithmetic/apfloat-modulo-wrong.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass
// check-run-results
// regression test for issue #109567

fn f() -> f64 {
std::hint::black_box(-1.0) % std::hint::black_box(-1.0)
}
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be basically the same as tests/ui/const_prop/apfloat-remainder-regression.rs? Is it worth having both?

Copy link
Member Author

Choose a reason for hiding this comment

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

I figured since there are two different issues, it was probably worth having two tests, but I really don't feel strongly either way.


const G: f64 = -1.0 % -1.0;

pub fn main() {
assert_eq!(-1, G.signum() as i32);
assert_eq!((-0.0_f64).to_bits(), G.to_bits());
assert_eq!(f().signum(), G.signum());
}
9 changes: 0 additions & 9 deletions triagebot.toml
Original file line number Diff line number Diff line change
@@ -318,14 +318,6 @@ changelog-branch = "master"

[shortcut]


[mentions."compiler/rustc_apfloat"]
message = """
Changes rustc_apfloat. rustc_apfloat is currently in limbo and you almost \
certainly don't want to change it (see #55993).
"""
cc = ["@eddyb"]

[mentions."compiler/rustc_codegen_cranelift"]
cc = ["@bjorn3"]

@@ -609,7 +601,6 @@ style-team = [
"/Cargo.lock" = ["@Mark-Simulacrum"]
"/Cargo.toml" = ["@Mark-Simulacrum"]
"/compiler" = ["compiler"]
"/compiler/rustc_apfloat" = ["@eddyb"]
"/compiler/rustc_ast" = ["compiler", "parser"]
"/compiler/rustc_ast_lowering" = ["compiler", "ast_lowering"]
"/compiler/rustc_hir_analysis" = ["compiler", "types"]