Skip to content

Commit df236a5

Browse files
Centri3NCGThompson
authored andcommitted
Define llvm.is.constant for primitives
1 parent e276dda commit df236a5

File tree

8 files changed

+25
-18
lines changed

8 files changed

+25
-18
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,10 +908,19 @@ impl<'ll> CodegenCx<'ll, '_> {
908908
ifn!("llvm.lifetime.start.p0i8", fn(t_i64, ptr) -> void);
909909
ifn!("llvm.lifetime.end.p0i8", fn(t_i64, ptr) -> void);
910910

911-
// Defining only the `ptr` variant of this overloaded intrinsic means
912-
// we can call this on any type we want (that doesn't ICE), but at the
913-
// slight cost of needing to write `addr_of!` everywhere.
914-
ifn!("llvm.is.constant.ptr", fn(ptr) -> i1);
911+
// FIXME: This is an infinitesimally small portion of the types you can
912+
// pass to this intrinsic, if we can ever lazily register intrinsics we
913+
// should register these when they're used, that way any type can be
914+
// passed.
915+
ifn!("llvm.is.constant.i1", fn(i1) -> i1);
916+
ifn!("llvm.is.constant.i8", fn(t_i8) -> i1);
917+
ifn!("llvm.is.constant.i16", fn(t_i16) -> i1);
918+
ifn!("llvm.is.constant.i32", fn(t_i32) -> i1);
919+
ifn!("llvm.is.constant.i64", fn(t_i64) -> i1);
920+
ifn!("llvm.is.constant.i128", fn(t_i128) -> i1);
921+
ifn!("llvm.is.constant.isize", fn(t_isize) -> i1);
922+
ifn!("llvm.is.constant.f32", fn(t_f32) -> i1);
923+
ifn!("llvm.is.constant.f64", fn(t_f64) -> i1);
915924

916925
ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
917926
ifn!("llvm.eh.typeid.for", fn(ptr) -> t_i32);

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
119119
sym::likely => {
120120
self.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(true)])
121121
}
122-
sym::is_val_statically_known => {
123-
self.call_intrinsic("llvm.is.constant.ptr", &[args[0].immediate()])
124-
}
122+
sym::is_val_statically_known => self.call_intrinsic(
123+
&format!("llvm.is.constant.{:?}", args[0].layout.llvm_type(self.cx)),
124+
&[args[0].immediate()],
125+
),
125126
sym::unlikely => self
126127
.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(false)]),
127128
kw::Try => {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
453453

454454
sym::black_box => (1, vec![param(0)], param(0)),
455455

456-
sym::is_val_statically_known => {
457-
(1, vec![Ty::new_imm_ptr(tcx, param(0))], tcx.types.bool)
458-
}
456+
sym::is_val_statically_known => (1, vec![param(0)], tcx.types.bool),
459457

460458
sym::const_eval_select => (4, vec![param(0), param(1), param(2)], param(3)),
461459

library/core/src/intrinsics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,14 +2546,14 @@ extern "rust-intrinsic" {
25462546
#[rustc_const_unstable(feature = "is_val_statically_known", issue = "none")]
25472547
#[rustc_nounwind]
25482548
#[cfg(not(bootstrap))]
2549-
pub fn is_val_statically_known<T>(arg: *const T) -> bool;
2549+
pub fn is_val_statically_known<T>(arg: T) -> bool;
25502550
}
25512551

25522552
// FIXME: Seems using `unstable` here completely ignores `rustc_allow_const_fn_unstable`
25532553
// and thus compiling stage0 core doesn't work.
25542554
#[rustc_const_stable(feature = "is_val_statically_known", since = "never")]
25552555
#[cfg(bootstrap)]
2556-
pub const unsafe fn is_val_statically_known<T>(_: *const T) -> bool {
2556+
pub const unsafe fn is_val_statically_known<T: ~const crate::marker::Destruct>(_: T) -> bool {
25572557
false
25582558
}
25592559

library/core/src/num/int_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ macro_rules! int_impl {
20912091
#[rustc_allow_const_fn_unstable(is_val_statically_known)]
20922092
pub const fn pow(self, mut exp: u32) -> Self {
20932093
// SAFETY: This path has the same behavior as the other.
2094-
if unsafe { intrinsics::is_val_statically_known(ptr::addr_of!(self)) }
2094+
if unsafe { intrinsics::is_val_statically_known(self) }
20952095
&& self > 0
20962096
&& (self & (self - 1) == 0)
20972097
{

library/core/src/num/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::ascii;
66
use crate::intrinsics;
77
use crate::mem;
88
use crate::ops::{Add, Mul, Sub};
9-
use crate::ptr;
109
use crate::str::FromStr;
1110

1211
// Used because the `?` operator is not allowed in a const context.

library/core/src/num/uint_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ macro_rules! uint_impl {
19871987
// instruction, but we must add a couple more checks for parity with
19881988
// our own `pow`.
19891989
// SAFETY: This path has the same behavior as the other.
1990-
if unsafe { intrinsics::is_val_statically_known(ptr::addr_of!(self)) }
1990+
if unsafe { intrinsics::is_val_statically_known(self) }
19911991
&& self.is_power_of_two()
19921992
{
19931993
let power_used = match self.checked_ilog2() {

tests/codegen/is_compile_time_known.rs renamed to tests/codegen/is_val_statically_known.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// compile-flags: --crate-type=lib
22
#![feature(core_intrinsics)]
33

4-
use std::intrinsics::is_compile_time_known;
4+
use std::intrinsics::is_val_statically_known;
55

66
pub struct A(u32);
77
pub enum B {
@@ -10,7 +10,7 @@ pub enum B {
1010

1111
#[inline]
1212
pub fn tuple_struct(a: A) -> i32 {
13-
if unsafe { is_compile_time_known(a) } { 1 } else { 0 }
13+
if unsafe { is_val_statically_known(a) } { 1 } else { 0 }
1414
}
1515

1616
// CHECK-LABEL: @tuple_struct_true(
@@ -29,7 +29,7 @@ pub fn tuple_struct_false(a: A) -> i32 {
2929

3030
#[inline]
3131
pub fn r#enum(b: B) -> i32 {
32-
if unsafe { is_compile_time_known(b) } { 3 } else { 2 }
32+
if unsafe { is_val_statically_known(b) } { 3 } else { 2 }
3333
}
3434

3535
// CHECK-LABEL: @enum_true(

0 commit comments

Comments
 (0)