Skip to content

Commit 971e37f

Browse files
committed
Further Implement is_val_statically_known
1 parent 5a45617 commit 971e37f

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,12 @@ fn codegen_regular_intrinsic_call<'tcx>(
439439

440440
ret.write_cvalue(fx, a);
441441
}
442+
sym::is_val_statically_known => {
443+
intrinsic_args!(fx, args => (_a); intrinsic);
444+
445+
let res = fx.bcx.ins().iconst(types::I8, 0);
446+
ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
447+
}
442448
sym::breakpoint => {
443449
intrinsic_args!(fx, args => (); intrinsic);
444450

compiler/rustc_codegen_gcc/src/context.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
196196

197197
let mut functions = FxHashMap::default();
198198
let builtins = [
199-
"__builtin_unreachable", "abort", "__builtin_expect", "__builtin_add_overflow", "__builtin_mul_overflow",
200-
"__builtin_saddll_overflow", /*"__builtin_sadd_overflow",*/ "__builtin_smulll_overflow", /*"__builtin_smul_overflow",*/
199+
"__builtin_unreachable", "abort", "__builtin_expect", /*"__builtin_expect_with_probability",*/
200+
"__builtin_constant_p", "__builtin_add_overflow", "__builtin_mul_overflow", "__builtin_saddll_overflow",
201+
/*"__builtin_sadd_overflow",*/ "__builtin_smulll_overflow", /*"__builtin_smul_overflow",*/
201202
"__builtin_ssubll_overflow", /*"__builtin_ssub_overflow",*/ "__builtin_sub_overflow", "__builtin_uaddll_overflow",
202203
"__builtin_uadd_overflow", "__builtin_umulll_overflow", "__builtin_umul_overflow", "__builtin_usubll_overflow",
203204
"__builtin_usub_overflow", "sqrtf", "sqrt", "__builtin_powif", "__builtin_powi", "sinf", "sin", "cosf", "cos",
204205
"powf", "pow", "expf", "exp", "exp2f", "exp2", "logf", "log", "log10f", "log10", "log2f", "log2", "fmaf",
205206
"fma", "fabsf", "fabs", "fminf", "fmin", "fmaxf", "fmax", "copysignf", "copysign", "floorf", "floor", "ceilf",
206207
"ceil", "truncf", "trunc", "rintf", "rint", "nearbyintf", "nearbyint", "roundf", "round",
207-
"__builtin_expect_with_probability",
208+
208209
];
209210

210211
for builtin in builtins.iter() {

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
123123
sym::unlikely => {
124124
self.expect(args[0].immediate(), false)
125125
}
126+
sym::is_val_statically_known => {
127+
let a = args[0].immediate();
128+
let builtin = self.context.get_builtin_function("__builtin_constant_p");
129+
let res = self.context.new_call(None, builtin, &[a]);
130+
self.icmp(IntPredicate::IntEQ, res, self.const_i32(0))
131+
}
126132
kw::Try => {
127133
try_intrinsic(
128134
self,

library/core/src/intrinsics.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -2525,15 +2525,30 @@ extern "rust-intrinsic" {
25252525
/// or `false`, and the caller has to ensure sound behavior for both cases.
25262526
/// In other words, the following code has *Undefined Behavior*:
25272527
///
2528-
/// ```rust
2529-
/// if !is_val_statically_known(0) { unreachable_unchecked(); }
2528+
/// ```
2529+
/// #![feature(is_val_statically_known)]
2530+
/// #![feature(core_intrinsics)]
2531+
/// # #![allow(internal_features)]
2532+
/// use std::hint::unreachable_unchecked;
2533+
/// use std::intrinsics::is_val_statically_known;
2534+
///
2535+
/// unsafe {
2536+
/// if !is_val_statically_known(0) { unreachable_unchecked(); }
2537+
/// }
25302538
/// ```
25312539
///
25322540
/// This also means that the following code's behavior is unspecified; it
25332541
/// may panic, or it may not:
25342542
///
2535-
/// ```rust,no_run
2536-
/// assert_eq!(is_val_statically_known(0), black_box(is_val_statically_known(0)))
2543+
/// ```no_run
2544+
/// #![feature(is_val_statically_known)]
2545+
/// #![feature(core_intrinsics)]
2546+
/// # #![allow(internal_features)]
2547+
/// use std::intrinsics::is_val_statically_known;
2548+
///
2549+
/// unsafe {
2550+
/// assert_eq!(is_val_statically_known(0), is_val_statically_known(0));
2551+
/// }
25372552
/// ```
25382553
///
25392554
/// Unsafe code may not rely on `is_val_statically_known` returning any
@@ -2547,15 +2562,14 @@ extern "rust-intrinsic" {
25472562
#[rustc_const_unstable(feature = "is_val_statically_known", issue = "none")]
25482563
#[rustc_nounwind]
25492564
#[cfg(not(bootstrap))]
2550-
pub fn is_val_statically_known<T>(arg: T) -> bool;
2565+
pub fn is_val_statically_known<T: Copy>(arg: T) -> bool;
25512566
}
25522567

25532568
// FIXME: Seems using `unstable` here completely ignores `rustc_allow_const_fn_unstable`
25542569
// and thus compiling stage0 core doesn't work.
2555-
#[rustc_const_stable(feature = "is_val_statically_known", since = "never")]
2570+
#[rustc_const_stable(feature = "is_val_statically_known", since = "0.0.0")]
25562571
#[cfg(bootstrap)]
2557-
pub const unsafe fn is_val_statically_known<T>(t: T) -> bool {
2558-
mem::forget(t);
2572+
pub const unsafe fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
25592573
false
25602574
}
25612575

tests/codegen/is_val_statically_known.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// #[cfg(bootstrap)]
2-
// ignore-stage1
3-
// compile-flags: --crate-type=lib -Zmerge-functions=disabled
1+
// compile-flags: --crate-type=lib -Zmerge-functions=disabled -O
42

53
#![feature(core_intrinsics)]
64

0 commit comments

Comments
 (0)