Skip to content

Commit

Permalink
Give the assume intrinsic a fallback body
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 16, 2024
1 parent ae9d7b0 commit 6a671bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub fn check_intrinsic_type(
}
sym::float_to_int_unchecked => (2, 0, vec![param(0)], param(1)),

sym::assume => (0, 0, vec![tcx.types.bool], Ty::new_unit(tcx)),
sym::assume => (0, 1, vec![tcx.types.bool], Ty::new_unit(tcx)),
sym::likely => (0, 0, vec![tcx.types.bool], tcx.types.bool),
sym::unlikely => (0, 0, vec![tcx.types.bool], tcx.types.bool),

Expand Down
36 changes: 23 additions & 13 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,20 +937,30 @@ extern "rust-intrinsic" {
#[rustc_nounwind]
pub fn unreachable() -> !;

/// Informs the optimizer that a condition is always true.
/// If the condition is false, the behavior is undefined.
///
/// No code is generated for this intrinsic, but the optimizer will try
/// to preserve it (and its condition) between passes, which may interfere
/// with optimization of surrounding code and reduce performance. It should
/// not be used if the invariant can be discovered by the optimizer on its
/// own, or if it does not enable any significant optimizations.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assume", since = "1.77.0")]
#[rustc_nounwind]
pub fn assume(b: bool);
}

/// Informs the optimizer that a condition is always true.
/// If the condition is false, the behavior is undefined.
///
/// No code is generated for this intrinsic, but the optimizer will try
/// to preserve it (and its condition) between passes, which may interfere
/// with optimization of surrounding code and reduce performance. It should
/// not be used if the invariant can be discovered by the optimizer on its
/// own, or if it does not enable any significant optimizations.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assume", since = "1.77.0")]
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
pub const unsafe fn assume(b: bool) {
if !b {
// SAFETY: the caller must guarantee the argument is never `false`
unsafe { unreachable() }
}
}

extern "rust-intrinsic" {
/// Hints to the compiler that branch condition is likely to be true.
/// Returns the value passed to it.
///
Expand Down

0 comments on commit 6a671bd

Please sign in to comment.