Skip to content

Commit 167c7f2

Browse files
committed
Don't force static refs to const memory
1 parent c4acc78 commit 167c7f2

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

src/constant.rs

+45-40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_mir::interpret::{
1111
StackPopCleanup, StackPopInfo,
1212
};
1313

14+
use cranelift_codegen::ir::GlobalValue;
1415
use cranelift_module::*;
1516

1617
use crate::prelude::*;
@@ -47,7 +48,10 @@ fn codegen_static_ref<'tcx>(
4748
) -> CPlace<'tcx> {
4849
let linkage = crate::linkage::get_static_ref_linkage(fx.tcx, def_id);
4950
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, linkage);
50-
cplace_for_dataid(fx, layout, data_id)
51+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
52+
#[cfg(debug_assertions)]
53+
fx.add_entity_comment(local_data_id, format!("{:?}", def_id));
54+
cplace_for_dataid(fx, layout, local_data_id)
5155
}
5256

5357
pub fn trans_constant<'tcx>(
@@ -119,53 +123,52 @@ pub fn trans_const_value<'tcx>(
119123
layout,
120124
);
121125
}
122-
123126
let const_val = match const_.val {
124127
ConstKind::Value(const_val) => const_val,
125128
_ => unreachable!("Const {:?} should have been evaluated", const_),
126129
};
127130

128131
match const_val {
129132
ConstValue::Scalar(x) => {
130-
let scalar = match layout.abi {
131-
layout::Abi::Scalar(ref x) => x,
132-
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout),
133-
};
133+
if fx.clif_type(layout.ty).is_none() {
134+
return trans_const_place(fx, const_).to_cvalue(fx);
135+
}
134136

135-
match ty.kind {
136-
ty::Bool | ty::Uint(_) => {
137-
let bits = const_.val.try_to_bits(layout.size).unwrap_or_else(|| {
138-
panic!("{:?}\n{:?}", const_, layout);
139-
});
140-
CValue::const_val(fx, layout, bits)
137+
match x {
138+
Scalar::Raw { data, size } => {
139+
assert_eq!(u64::from(size), layout.size.bytes());
140+
return CValue::const_val(fx, layout, data);
141141
}
142-
ty::Int(_) => {
143-
let bits = const_.val.try_to_bits(layout.size).unwrap();
144-
CValue::const_val(
145-
fx,
146-
layout,
147-
rustc::mir::interpret::sign_extend(bits, layout.size),
148-
)
149-
}
150-
ty::Float(fty) => {
151-
let bits = const_.val.try_to_bits(layout.size).unwrap();
152-
let val = match fty {
153-
FloatTy::F32 => fx
154-
.bcx
155-
.ins()
156-
.f32const(Ieee32::with_bits(u32::try_from(bits).unwrap())),
157-
FloatTy::F64 => fx
158-
.bcx
159-
.ins()
160-
.f64const(Ieee64::with_bits(u64::try_from(bits).unwrap())),
142+
Scalar::Ptr(ptr) => {
143+
let alloc_kind = fx.tcx.alloc_map.lock().get(ptr.alloc_id);
144+
let base_addr = match alloc_kind {
145+
Some(GlobalAlloc::Memory(alloc)) => {
146+
fx.constants_cx.todo.insert(TodoItem::Alloc(ptr.alloc_id));
147+
let data_id = data_id_for_alloc_id(fx.module, ptr.alloc_id, alloc.align);
148+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
149+
#[cfg(debug_assertions)]
150+
fx.add_entity_comment(local_data_id, format!("{:?}", ptr.alloc_id));
151+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
152+
}
153+
Some(GlobalAlloc::Function(instance)) => {
154+
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
155+
let local_func_id = fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
156+
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
157+
}
158+
Some(GlobalAlloc::Static(def_id)) => {
159+
assert!(fx.tcx.is_static(def_id));
160+
let linkage = crate::linkage::get_static_ref_linkage(fx.tcx, def_id);
161+
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, linkage);
162+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
163+
#[cfg(debug_assertions)]
164+
fx.add_entity_comment(local_data_id, format!("{:?}", def_id));
165+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
166+
}
167+
None => bug!("missing allocation {:?}", ptr.alloc_id),
161168
};
162-
CValue::by_val(val, layout)
169+
let val = fx.bcx.ins().iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap());
170+
return CValue::by_val(val, layout);
163171
}
164-
ty::FnDef(_def_id, _substs) => CValue::by_ref(
165-
crate::pointer::Pointer::const_addr(fx, fx.pointer_type.bytes() as i64),
166-
layout,
167-
),
168-
_ => trans_const_place(fx, const_).to_cvalue(fx),
169172
}
170173
}
171174
ConstValue::ByRef { alloc, offset } => {
@@ -228,7 +231,10 @@ fn trans_const_place<'tcx>(
228231
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
229232
fx.constants_cx.todo.insert(TodoItem::Alloc(alloc_id));
230233
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
231-
cplace_for_dataid(fx, fx.layout_of(const_.ty), data_id)
234+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
235+
#[cfg(debug_assertions)]
236+
fx.add_entity_comment(local_data_id, format!("{:?}", alloc_id));
237+
cplace_for_dataid(fx, fx.layout_of(const_.ty), local_data_id)
232238
}
233239

234240
fn data_id_for_alloc_id<B: Backend>(
@@ -305,9 +311,8 @@ fn data_id_for_static(
305311
fn cplace_for_dataid<'tcx>(
306312
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
307313
layout: TyLayout<'tcx>,
308-
data_id: DataId,
314+
local_data_id: GlobalValue,
309315
) -> CPlace<'tcx> {
310-
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
311316
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
312317
assert!(!layout.is_unsized(), "unsized statics aren't supported");
313318
CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)

0 commit comments

Comments
 (0)