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

[WIP] Add optimized_to_const intrinsic #96604

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub struct CodegenCx<'ll, 'tcx> {
pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,

intrinsics: RefCell<FxHashMap<&'static str, (&'ll Type, &'ll Value)>>,
pub is_constant_intrinsics: RefCell<FxHashMap<Ty<'tcx>, (&'ll Type, &'ll Value)>>,

/// A counter that is used for generating local symbol names
local_gen_sym_counter: Cell<usize>,
Expand Down Expand Up @@ -442,6 +443,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
eh_catch_typeinfo: Cell::new(None),
rust_try_fn: Cell::new(None),
intrinsics: Default::default(),
is_constant_intrinsics: Default::default(),
local_gen_sym_counter: Cell::new(0),
renamed_statics: Default::default(),
}
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
return;
}

sym::optimized_to_const => {
let ty = substs.type_at(0);
let ptr = args[0].immediate();
let llty = self.layout_of(ty).llvm_type(self);
let align = self.align_of(ty);
let load = self.load(llty, ptr, align);
is_constant_intrinsic(self, ty, llty, load)
}

_ if name.as_str().starts_with("simd_") => {
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
Ok(llval) => llval,
Expand Down Expand Up @@ -415,6 +424,30 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
}
}

fn is_constant_intrinsic<'ll, 'tcx>(
bx: &mut Builder<'_, 'll, 'tcx>,
ty: Ty<'tcx>,
llty: &'ll Type,
value: &'ll Value,
) -> &'ll Value {
let cached = bx.is_constant_intrinsics.borrow().get(&ty).copied();
let (fn_ty, f) = match cached {
Some(v) => v,
None => {
let fn_ty = bx.type_func(&[llty], bx.type_i1());
let type_id = bx.tcx.type_id_hash(ty);
let f = bx.declare_cfn(
&format!("llvm.is.constant.{}", type_id),
llvm::UnnamedAddr::No,
fn_ty,
);
bx.is_constant_intrinsics.borrow_mut().insert(ty, (fn_ty, f));
(fn_ty, f)
}
};
bx.call(fn_ty, f, &[value], None)
}

fn try_intrinsic<'ll>(
bx: &mut Builder<'_, 'll, '_>,
try_func: &'ll Value,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
self.write_scalar(result, dest)?;
}
sym::optimized_to_const => {
self.write_scalar(Scalar::from_bool(true), dest)?;
}
_ => return Ok(false),
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ symbols! {
opt_out_copy,
optimize,
optimize_attribute,
optimized_to_const,
optin_builtin_traits,
option,
option_env,
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_typeck/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
| sym::type_name
| sym::forget
| sym::black_box
| sym::optimized_to_const
| sym::variant_count => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
Expand Down Expand Up @@ -397,6 +398,13 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {

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

sym::optimized_to_const => {
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0) };
let param_ty =
tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)), param(0));
(1, vec![param_ty], tcx.types.bool)
}

other => {
tcx.sess.emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
return;
Expand Down
6 changes: 6 additions & 0 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,12 @@ extern "rust-intrinsic" {
/// [`std::hint::black_box`]: crate::hint::black_box
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
pub fn black_box<T>(dummy: T) -> T;

/// Determines if the supplied value is a constant or has been folded to a constant during
/// optimization.
#[cfg(not(bootstrap))]
#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
pub fn optimized_to_const<T>(value: &T) -> bool;
}

// Some functions are defined here because they accidentally got made
Expand Down