Skip to content

Commit ef3a112

Browse files
committed
Optimize exception value storage
1 parent 62a8655 commit ef3a112

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

Diff for: src/abi/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
651651
fx.bcx.switch_to_block(pre_cleanup_block);
652652
fx.bcx.set_cold_block(pre_cleanup_block);
653653
let exception_ptr = fx.bcx.append_block_param(pre_cleanup_block, fx.pointer_type);
654-
fx.exception_slot.store(fx, exception_ptr, MemFlags::trusted());
654+
fx.bcx.def_var(fx.exception_slot, exception_ptr);
655655
let cleanup_block = fx.get_block(cleanup);
656656
fx.bcx.ins().jump(cleanup_block, &[]);
657657

@@ -854,7 +854,7 @@ pub(crate) fn codegen_drop<'tcx>(
854854
fx.bcx.set_cold_block(pre_cleanup_block);
855855
let exception_ptr =
856856
fx.bcx.append_block_param(pre_cleanup_block, fx.pointer_type);
857-
fx.exception_slot.store(fx, exception_ptr, MemFlags::trusted());
857+
fx.bcx.def_var(fx.exception_slot, exception_ptr);
858858
let cleanup_block = fx.get_block(cleanup);
859859
fx.bcx.ins().jump(cleanup_block, &[]);
860860
}
@@ -934,7 +934,7 @@ pub(crate) fn codegen_drop<'tcx>(
934934
fx.bcx.set_cold_block(pre_cleanup_block);
935935
let exception_ptr =
936936
fx.bcx.append_block_param(pre_cleanup_block, fx.pointer_type);
937-
fx.exception_slot.store(fx, exception_ptr, MemFlags::trusted());
937+
fx.bcx.def_var(fx.exception_slot, exception_ptr);
938938
let cleanup_block = fx.get_block(cleanup);
939939
fx.bcx.ins().jump(cleanup_block, &[]);
940940
}
@@ -997,7 +997,7 @@ pub(crate) fn codegen_drop<'tcx>(
997997
fx.bcx.set_cold_block(pre_cleanup_block);
998998
let exception_ptr =
999999
fx.bcx.append_block_param(pre_cleanup_block, fx.pointer_type);
1000-
fx.exception_slot.store(fx, exception_ptr, MemFlags::trusted());
1000+
fx.bcx.def_var(fx.exception_slot, exception_ptr);
10011001
let cleanup_block = fx.get_block(cleanup);
10021002
fx.bcx.ins().jump(cleanup_block, &[]);
10031003
}

Diff for: src/base.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use cranelift_codegen::CodegenError;
44
use cranelift_codegen::ir::{BlockArg, ExceptionTableData, ExceptionTag, UserFuncName};
5-
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
5+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
66
use cranelift_module::ModuleError;
77
use rustc_ast::InlineAsmOptions;
88
use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization;
@@ -110,22 +110,16 @@ pub(crate) fn codegen_fn<'tcx>(
110110
// Make FunctionCx
111111
let target_config = module.target_config();
112112
let pointer_type = target_config.pointer_type();
113-
let mut clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance, fn_abi);
113+
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance, fn_abi);
114114

115115
let func_debug_cx = if let Some(debug_context) = &mut cx.debug_context {
116116
Some(debug_context.define_function(tcx, type_dbg, instance, fn_abi, &symbol_name, mir.span))
117117
} else {
118118
None
119119
};
120120

121-
let exception_slot = bcx.func.create_sized_stack_slot(StackSlotData {
122-
kind: StackSlotKind::ExplicitSlot,
123-
size: pointer_type.bytes(),
124-
align_shift: 4,
125-
});
126-
if clif_comments.enabled() {
127-
clif_comments.add_comment(exception_slot, "exception slot");
128-
}
121+
let exception_slot = Variable::from_u32(0);
122+
bcx.declare_var(exception_slot, pointer_type);
129123

130124
let mut fx = FunctionCx {
131125
cx,
@@ -145,10 +139,10 @@ pub(crate) fn codegen_fn<'tcx>(
145139
block_map,
146140
local_map: IndexVec::with_capacity(mir.local_decls.len()),
147141
caller_location: None, // set by `codegen_fn_prelude`
148-
exception_slot: Pointer::stack_slot(exception_slot),
142+
exception_slot,
149143

150144
clif_comments,
151-
next_ssa_var: 0,
145+
next_ssa_var: 1, // var0 is used for the exception slot
152146
};
153147

154148
tcx.prof.generic_activity("codegen clif ir").run(|| codegen_fn_body(&mut fx, start_block));
@@ -575,8 +569,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
575569
codegen_unwind_terminate(fx, source_info, *reason);
576570
}
577571
TerminatorKind::UnwindResume => {
578-
let exception_ptr =
579-
fx.exception_slot.load(fx, fx.pointer_type, MemFlags::trusted());
572+
let exception_ptr = fx.bcx.use_var(fx.exception_slot);
580573
fx.lib_call(
581574
"_Unwind_Resume",
582575
vec![AbiParam::new(fx.pointer_type)],
@@ -1204,7 +1197,7 @@ fn codegen_panic_inner<'tcx>(
12041197
fx.bcx.switch_to_block(cleanup_block);
12051198
fx.bcx.set_cold_block(cleanup_block);
12061199
let exception_ptr = fx.bcx.append_block_param(cleanup_block, fx.pointer_type);
1207-
fx.exception_slot.store(fx, exception_ptr, MemFlags::trusted());
1200+
fx.bcx.def_var(fx.exception_slot, exception_ptr);
12081201
let cleanup_block = fx.get_block(cleanup);
12091202
fx.bcx.ins().jump(cleanup_block, &[]);
12101203

Diff for: src/common.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cranelift_codegen::isa::TargetFrontendConfig;
2-
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
2+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
33
use rustc_abi::{Float, Integer, Primitive};
44
use rustc_index::IndexVec;
55
use rustc_middle::ty::TypeFoldable;
@@ -288,10 +288,8 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
288288
/// When `#[track_caller]` is used, the implicit caller location is stored in this variable.
289289
pub(crate) caller_location: Option<CValue<'tcx>>,
290290

291-
/// During cleanup the exception pointer will be stored at the location pointed to by this
292-
/// pointer.
293-
// FIXME use SSA variable
294-
pub(crate) exception_slot: Pointer,
291+
/// During cleanup the exception pointer will be stored in this variable.
292+
pub(crate) exception_slot: Variable,
295293

296294
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
297295

0 commit comments

Comments
 (0)