Skip to content

Commit

Permalink
Avoid mir_operand_get_const_val hack for simd_insert and simd_extract
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Sep 15, 2024
1 parent 1e96021 commit 16cee89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
}

/// Used only for intrinsic implementations that need a compile-time constant
///
/// All uses of this function are a bug inside stdarch. [`eval_mir_constant`]
/// should be used everywhere, but for some vendor intrinsics stdarch forgets
/// to wrap the immediate argument in `const {}`, necesitating this hack to get
/// the correct value at compile time instead.
pub(crate) fn mir_operand_get_const_val<'tcx>(
fx: &FunctionCx<'_, '_, 'tcx>,
operand: &Operand<'tcx>,
Expand Down
26 changes: 7 additions & 19 deletions src/intrinsics/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
let val = codegen_operand(fx, &val.node);

// FIXME validate
let idx_const = if let Some(idx_const) =
crate::constant::mir_operand_get_const_val(fx, &idx.node)
{
idx_const
let idx_const = if let Some(idx_const) = idx.node.constant() {
crate::constant::eval_mir_constant(fx, idx_const).0.try_to_scalar_int().unwrap()
} else {
fx.tcx.dcx().span_fatal(span, "Index argument for `simd_insert` is not a constant");
};
Expand Down Expand Up @@ -304,22 +302,12 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
return;
}

let idx_const = if let Some(idx_const) =
crate::constant::mir_operand_get_const_val(fx, &idx.node)
{
idx_const
let idx_const = if let Some(idx_const) = idx.node.constant() {
crate::constant::eval_mir_constant(fx, idx_const).0.try_to_scalar_int().unwrap()
} else {
fx.tcx.dcx().span_warn(span, "Index argument for `simd_extract` is not a constant");
let trap_block = fx.bcx.create_block();
let true_ = fx.bcx.ins().iconst(types::I8, 1);
let ret_block = fx.get_block(target);
fx.bcx.ins().brif(true_, trap_block, &[], ret_block, &[]);
fx.bcx.switch_to_block(trap_block);
crate::trap::trap_unimplemented(
fx,
"Index argument for `simd_extract` is not a constant",
);
return;
fx.tcx
.dcx()
.span_fatal(span, "Index argument for `simd_extract` is not a constant");
};

let idx = idx_const.to_u32();
Expand Down

0 comments on commit 16cee89

Please sign in to comment.