Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1bb848d

Browse files
committed
Re-use cranelift_codegen::ir::Function for every function
Fixes rust-lang#844
1 parent c5a7fca commit 1bb848d

File tree

4 files changed

+17
-29
lines changed

4 files changed

+17
-29
lines changed

src/base.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
2121
.map(|debug_context| FunctionDebugContext::new(debug_context, instance, func_id, &name));
2222

2323
// Make FunctionBuilder
24-
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
25-
func.collect_debug_info();
24+
let context = &mut cx.cached_context;
25+
context.clear();
26+
context.func.name = ExternalName::user(0, func_id.as_u32());
27+
context.func.signature = sig;
28+
context.func.collect_debug_info();
2629
let mut func_ctx = FunctionBuilderContext::new();
27-
let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
30+
let mut bcx = FunctionBuilder::new(&mut context.func, &mut func_ctx);
2831

2932
// Predefine ebb's
3033
let start_ebb = bcx.create_ebb();
@@ -48,7 +51,7 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
4851

4952
clif_comments,
5053
constants_cx: &mut cx.constants_cx,
51-
caches: &mut cx.caches,
54+
vtables: &mut cx.vtables,
5255
source_info_set: indexmap::IndexSet::new(),
5356
};
5457

@@ -69,13 +72,10 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
6972
let local_map = fx.local_map;
7073

7174
#[cfg(debug_assertions)]
72-
crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &func, &clif_comments, None);
75+
crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &context.func, &clif_comments, None);
7376

7477
// Verify function
75-
verify_func(tcx, &clif_comments, &func);
76-
77-
let context = &mut cx.caches.context;
78-
context.func = func;
78+
verify_func(tcx, &clif_comments, &context.func);
7979

8080
// Perform rust specific optimizations
8181
crate::optimize::optimize_function(cx.tcx, instance, context, &mut clif_comments);

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
269269

270270
pub clif_comments: crate::pretty_clif::CommentWriter,
271271
pub constants_cx: &'clif mut crate::constant::ConstantCx,
272-
pub caches: &'clif mut Caches<'tcx>,
272+
pub vtables: &'clif mut HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
273273

274274
pub source_info_set: indexmap::IndexSet<SourceInfo>,
275275
}

src/lib.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ mod prelude {
114114
pub use crate::trap::*;
115115
pub use crate::unimpl::unimpl;
116116
pub use crate::value_and_place::{CPlace, CPlaceInner, CValue};
117-
pub use crate::{Caches, CodegenCx};
117+
pub use crate::CodegenCx;
118118

119119
pub struct PrintOnPanic<F: Fn() -> String>(pub F);
120120
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
@@ -126,25 +126,12 @@ mod prelude {
126126
}
127127
}
128128

129-
pub struct Caches<'tcx> {
130-
pub context: Context,
131-
pub vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
132-
}
133-
134-
impl Default for Caches<'_> {
135-
fn default() -> Self {
136-
Caches {
137-
context: Context::new(),
138-
vtables: HashMap::new(),
139-
}
140-
}
141-
}
142-
143129
pub struct CodegenCx<'clif, 'tcx, B: Backend + 'static> {
144130
tcx: TyCtxt<'tcx>,
145131
module: &'clif mut Module<B>,
146132
constants_cx: ConstantCx,
147-
caches: Caches<'tcx>,
133+
cached_context: Context,
134+
vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
148135
debug_context: Option<&'clif mut DebugContext<'tcx>>,
149136
}
150137

@@ -158,7 +145,8 @@ impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
158145
tcx,
159146
module,
160147
constants_cx: ConstantCx::default(),
161-
caches: Caches::default(),
148+
cached_context: Context::new(),
149+
vtables: HashMap::new(),
162150
debug_context,
163151
}
164152
}

src/vtable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ pub fn get_vtable<'tcx>(
6363
ty: Ty<'tcx>,
6464
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
6565
) -> Value {
66-
let data_id = if let Some(data_id) = fx.caches.vtables.get(&(ty, trait_ref)) {
66+
let data_id = if let Some(data_id) = fx.vtables.get(&(ty, trait_ref)) {
6767
*data_id
6868
} else {
6969
let data_id = build_vtable(fx, ty, trait_ref);
70-
fx.caches.vtables.insert((ty, trait_ref), data_id);
70+
fx.vtables.insert((ty, trait_ref), data_id);
7171
data_id
7272
};
7373

0 commit comments

Comments
 (0)