pub(crate) fn const_val(
        fx: &mut FunctionCx<'_, '_, 'tcx>,
        layout: TyAndLayout<'tcx>,
        const_val: ty::ScalarInt,
    ) -> CValue<'tcx> {
        assert_eq!(const_val.size(), layout.size, "{:#?}: {:?}", const_val, layout);
        use cranelift_codegen::ir::immediates::{Ieee16, Ieee32, Ieee64, Ieee128};

        let clif_ty = fx.clif_type(layout.ty).unwrap();

        let val = match layout.ty.kind() {
            ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
                let const_val = const_val.to_bits(layout.size);
                let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64);
                let msb = fx.bcx.ins().iconst(types::I64, (const_val >> 64) as u64 as i64);
                fx.bcx.ins().iconcat(lsb, msb)
            }
            ty::Bool
            | ty::Char
            | ty::Uint(_)
            | ty::Int(_)
            | ty::Ref(..)
            | ty::RawPtr(..)
            | ty::FnPtr(..) => {
                let raw_val = const_val.size().truncate(const_val.to_bits(layout.size));
                fx.bcx.ins().iconst(clif_ty, raw_val as i64)
            }
            ty::Float(FloatTy::F16) => {
                fx.bcx.ins().f16const(Ieee16::with_bits(u16::try_from(const_val).unwrap()))
            }
            ty::Float(FloatTy::F32) => {
                fx.bcx.ins().f32const(Ieee32::with_bits(u32::try_from(const_val).unwrap()))
            }
            ty::Float(FloatTy::F64) => {
                fx.bcx.ins().f64const(Ieee64::with_bits(u64::try_from(const_val).unwrap()))
            }
            ty::Float(FloatTy::F128) => {
                let value = fx
                    .bcx
                    .func
                    .dfg
                    .constants
                    .insert(Ieee128::with_bits(u128::try_from(const_val).unwrap()).into());
                fx.bcx.ins().f128const(value)
            }
            _ => panic!(
                "CValue::const_val for non bool/char/float/integer/pointer type {:?} is not allowed",
                layout.ty
            ),
        };

        CValue::by_val(val, layout)
    }
If `ty` is signed, `const_val` must already be sign extended.
src/value_and_place.rs