Skip to content
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

Avoid ICE when is_val_statically_known is not of a supported type #120484

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ impl<'ll> CodegenCx<'ll, '_> {
ifn!("llvm.is.constant.isize", fn(t_isize) -> i1);
ifn!("llvm.is.constant.f32", fn(t_f32) -> i1);
ifn!("llvm.is.constant.f64", fn(t_f64) -> i1);
ifn!("llvm.is.constant.ptr", fn(ptr) -> i1);

ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
ifn!("llvm.eh.typeid.for", fn(ptr) -> t_i32);
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,18 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
sym::likely => {
self.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(true)])
}
sym::is_val_statically_known => self.call_intrinsic(
&format!("llvm.is.constant.{:?}", args[0].layout.immediate_llvm_type(self.cx)),
&[args[0].immediate()],
),
sym::is_val_statically_known => {
let intrinsic_type = args[0].layout.immediate_llvm_type(self.cx);
match self.type_kind(intrinsic_type) {
TypeKind::Pointer | TypeKind::Integer | TypeKind::Float | TypeKind::Double => {
self.call_intrinsic(
&format!("llvm.is.constant.{:?}", intrinsic_type),
&[args[0].immediate()],
)
}
_ => self.const_bool(false),
}
}
sym::unlikely => self
.call_intrinsic("llvm.expect.i1", &[args[0].immediate(), self.const_bool(false)]),
kw::Try => {
Expand Down
38 changes: 38 additions & 0 deletions tests/codegen/is_val_statically_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,41 @@ pub fn _bool_false(b: bool) -> i32 {
// CHECK: ret i32 2
_bool(b)
}

#[inline]
pub fn _iref(a: &u8) -> i32 {
if unsafe { is_val_statically_known(a) } { 5 } else { 4 }
}

// CHECK-LABEL: @_iref_borrow(
#[no_mangle]
pub fn _iref_borrow() -> i32 {
// CHECK: ret i32 4
_iref(&0)
}

// CHECK-LABEL: @_iref_arg(
#[no_mangle]
pub fn _iref_arg(a: &u8) -> i32 {
// CHECK: ret i32 4
_iref(a)
}

#[inline]
pub fn _slice_ref(a: &[u8]) -> i32 {
if unsafe { is_val_statically_known(a) } { 7 } else { 6 }
}

// CHECK-LABEL: @_slice_ref_borrow(
#[no_mangle]
pub fn _slice_ref_borrow() -> i32 {
// CHECK: ret i32 6
_slice_ref(&[0;3])
}

// CHECK-LABEL: @_slice_ref_arg(
#[no_mangle]
pub fn _slice_ref_arg(a: &[u8]) -> i32 {
// CHECK: ret i32 6
_slice_ref(a)
}
Comment on lines +49 to +86
Copy link
Contributor

Choose a reason for hiding this comment

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

If it isn't too late, can you add some test cases where the function should return true? Currently, it only returns false for pointers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure 😀

I'm not sure if it would make it into the tree though

Copy link
Member

Choose a reason for hiding this comment

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

Please make a follow-up PR rather than pushing to this one

Loading