Make more float intrinsics generic - #162395
Conversation
|
Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri
cc @rust-lang/miri
cc @tgross35 Some changes occurred to the CTFE machinery
cc @Amanieu, @folkertdev, @sayantn Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr
cc @rust-lang/miri Any special-casing of Miri in the standard library requires review. cc @rust-lang/miri
cc @bjorn3
cc @rust-lang/wg-const-eval |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | ||
| #[rustc_intrinsic_const_stable_indirect] |
There was a problem hiding this comment.
Noted this required adding #[rustc_const_unstable] to the whole intrinsic,
Why was this needed? I guess it is because the body now needs unstable const features, namely const traits? We should ensure const traits are stable enough before we do that.
(I now notice that you already did the same for fabs. We should have already checked this there then but forgot.)
@BoxyUwU @lcnr @oli-obk are const traits in a state where we can use them internally in const functions that can be called by public monomorphic stable const functions? I think this still allows us to change the syntax and logic for const traits pretty much arbitrarily as long as we keep some way of invoking trait functions in a const fn.
There was a problem hiding this comment.
cc @fee1-dead
This is not the only case of such usage. Equivalent behaviour is here to stay and syntactical changes don't cause bootstrap churn anymore, so to me personally it is fine to add more, especially for intrinsic bodies which can always be replaced by an actual impl again
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | ||
| #[rustc_intrinsic_const_stable_indirect] |
There was a problem hiding this comment.
This combination of attributes is a strange one.
#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_intrinsic_const_stable_indirect]
I don't think I ever intended for this combination to be used. It looks quite unsafe, from a const-stability perspective. We should disallow it and require something like rustc_allow_const_fn_unstable instead.
There was a problem hiding this comment.
fabs seems to be the only intrinsic using this. I am quite uncomfortable with adding more such intrinsics. If possible, we should remove this from fabs as well. Unfortunately I won't have time to work on fixing this hole in our const stability checks any time soon. (Well I can easily add the check that rejects this but probably cannot explore adding alternatives.)
There was a problem hiding this comment.
i admit this is me just trying to understand how to make things compile. very happy to do something else or exploring adding/fixing an attribute
- without
#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")], we get "const function that might be (indirectly) exposed to stable cannot use#[feature(const_trait_impl)]" - without
#[rustc_intrinsic_const_stable_indirect], we get "const function that might be (indirectly) exposed to stable cannot use#[feature(core_intrinsics)]"
There was a problem hiding this comment.
fixed (folded into the relevant commits), there now is rustc_allow_const_fn_unstable; i guess this needs some approval
d78924b to
06625b8
Compare
|
cc @rust-lang/clippy |
06625b8 to
da4599d
Compare
This comment has been minimized.
This comment has been minimized.
ca3f9a9 to
b4275be
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment has been minimized.
This comment has been minimized.
b4275be to
49057d4
Compare
|
Please also update the PR description to reflect which intrinsics are actually being changed here now. |
This comment has been minimized.
This comment has been minimized.
| where | ||
| F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>, | ||
| { | ||
| let x: F = x.to_float()?; |
There was a problem hiding this comment.
By removing this here, now every caller needs to call to_float first... is that really better?
There was a problem hiding this comment.
i found it nicer, because it means outside of SIMD we only do the x: F = x.to_float()? thing in one place, avoiding additional wrappers or calls elsewhere. it does make the SIMD intrinsics slightly bigger but i think it's nicer for the more general case
49057d4 to
2bb83cf
Compare
2bb83cf to
33503c2
Compare
| #[rustc_intrinsic] | ||
| #[rustc_nounwind] | ||
| pub fn powif128(a: f128, x: i32) -> f128; | ||
| pub fn powi<T: bounds::FloatPrimitive>(a: T, x: i32) -> T; |
There was a problem hiding this comment.
Could this intrinsic_dispatch_on_type! calling __powi[sdtk]f2 in the fallback body?
There was a problem hiding this comment.
yes sure :) for f16, powi(x as f32, y) as f16 is correct right?
There was a problem hiding this comment.
note that this requires declaring an extern block for these functions, since we dont have a compiler-builtins equivalent to libm. i'm happy adding a library/core/src/num/imp/builtins.rs for it, if you think it's not too much
There was a problem hiding this comment.
powi has no precision guarantees, so yes. You'll also need to put #[cfg_attr(any(target_arch = "powerpc", target_arch = "powerpc64"), link_name = "__powikf2")] on the __powitf2 extern fn as PowerPC uses slightly different names for f128 intrinsics.
There was a problem hiding this comment.
Adding library/core/src/num/imp/builtins.rs seems fine, e.g. complex number multiplication/division will also need to make direct calls to compiler-builtins.
| @@ -0,0 +1,14 @@ | |||
| //! Bindings to functions provided by `compiler-builtins`. | |||
| //! | |||
| //! These are only ever provided by `compiler-builtins`, which is always linked in. | |||
There was a problem hiding this comment.
This isn't entirely true, compiler-builtins provides an implementation with weak linkage, meaning the implementation from e.g. libgcc_s or LLVM compiler-rt could also end up being used.
There was a problem hiding this comment.
how about this?
//! Bindings to functions provided by `compiler-builtins`.
//!
//! These are weakly provided by `compiler-builtins`, which is always weakly linked in, or
//! possibly by e.g. libgcc_s or compiler-rt.
View all comments
Another (big) step of #153834 + #160989
Makemore remaining float intrinsics generic! Each commit is self-contained.
In order:
ceil,floor,trunc,round,round_ties_even, with const libm fallbacks (using#[rustc_do_not_const_check], see Intrinsics should allow a non-const fallback body if hooked for CTFE #150961, cc @RalfJung @tgross35)sqrt, with libm fallbackspowf, with libm fallbacksfmaandfmuladd, with libm fallbacks (+#[rustc_do_not_const_check]) and a shared fallback, respectively.powi, with no fallback (it already didn't have any)The first commit just removes the f16/f128 fallbacks for
fabs, as mentioned here. I didn't try making the backends use more fallbacks, to make the PR less risky since it's quite large. Happy to do changes there though (cc @folkertdev)copysign,minimum_number_nsz,maximum_number_nsz,minimum,maximumwill be handled separately as they requirerustc_allow_const_fn_unstable: #162414