Skip to content

Commit 0319b31

Browse files
authored
Merge pull request #1159 from bjorn3/driver_refactorings
Driver refactorings
2 parents 542e73e + 69102db commit 0319b31

19 files changed

+255
-289
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
// source for rustc_* is not included in the rust-src component; disable the errors about this
3-
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "macro-error"],
3+
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "unresolved-macro-call"],
44
"rust-analyzer.assist.importMergeBehavior": "last",
55
"rust-analyzer.cargo.runBuildScripts": true,
66
"rust-analyzer.linkedProjects": [

src/abi/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ pub(crate) fn import_function<'tcx>(
7171
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
7272
/// Instance must be monomorphized
7373
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
74-
let func_id = import_function(self.tcx, self.cx.module, inst);
75-
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
74+
let func_id = import_function(self.tcx, self.module, inst);
75+
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
7676

7777
if self.clif_comments.enabled() {
7878
self.add_comment(func_ref, format!("{:?}", inst));
@@ -89,8 +89,8 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
8989
args: &[Value],
9090
) -> &[Value] {
9191
let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) };
92-
let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap();
93-
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
92+
let func_id = self.module.declare_function(&name, Linkage::Import, &sig).unwrap();
93+
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
9494
let call_inst = self.bcx.ins().call(func_ref, args);
9595
if self.clif_comments.enabled() {
9696
self.add_comment(call_inst, format!("easy_call {}", name));

src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::symbol::sym;
1111
pub(crate) fn codegen(
1212
tcx: TyCtxt<'_>,
1313
module: &mut impl Module,
14-
unwind_context: &mut UnwindContext<'_>,
14+
unwind_context: &mut UnwindContext,
1515
) -> bool {
1616
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
1717
use rustc_middle::middle::dependency_format::Linkage;
@@ -29,7 +29,7 @@ pub(crate) fn codegen(
2929

3030
fn codegen_inner(
3131
module: &mut impl Module,
32-
unwind_context: &mut UnwindContext<'_>,
32+
unwind_context: &mut UnwindContext,
3333
kind: AllocatorKind,
3434
) {
3535
let usize_ty = module.target_config().pointer_type();

src/base.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use rustc_target::abi::call::FnAbi;
99
use crate::constant::ConstantCx;
1010
use crate::prelude::*;
1111

12-
pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
12+
pub(crate) fn codegen_fn<'tcx>(
13+
cx: &mut crate::CodegenCx<'tcx>,
14+
module: &mut dyn Module,
15+
instance: Instance<'tcx>,
16+
) {
1317
let tcx = cx.tcx;
1418

1519
let _inst_guard =
@@ -20,8 +24,8 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
2024

2125
// Declare function
2226
let symbol_name = tcx.symbol_name(instance);
23-
let sig = get_function_sig(tcx, cx.module.isa().triple(), instance);
24-
let func_id = cx.module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
27+
let sig = get_function_sig(tcx, module.isa().triple(), instance);
28+
let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
2529

2630
cx.cached_context.clear();
2731

@@ -40,11 +44,12 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
4044
(0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect();
4145

4246
// Make FunctionCx
43-
let pointer_type = cx.module.target_config().pointer_type();
47+
let pointer_type = module.target_config().pointer_type();
4448
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
4549

4650
let mut fx = FunctionCx {
4751
cx,
52+
module,
4853
tcx,
4954
pointer_type,
5055
vtables: FxHashMap::default(),
@@ -94,7 +99,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
9499
let source_info_set = fx.source_info_set;
95100
let local_map = fx.local_map;
96101

97-
fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module);
102+
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
98103

99104
// Store function in context
100105
let context = &mut cx.cached_context;
@@ -114,16 +119,15 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
114119
// instruction, which doesn't have an encoding.
115120
context.compute_cfg();
116121
context.compute_domtree();
117-
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
118-
context.dce(cx.module.isa()).unwrap();
122+
context.eliminate_unreachable_code(module.isa()).unwrap();
123+
context.dce(module.isa()).unwrap();
119124
// Some Cranelift optimizations expect the domtree to not yet be computed and as such don't
120125
// invalidate it when it would change.
121126
context.domtree.clear();
122127

123128
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
124129

125130
// Define function
126-
let module = &mut cx.module;
127131
tcx.sess.time("define function", || {
128132
module
129133
.define_function(func_id, context, &mut NullTrapSink {}, &mut NullStackMapSink {})
@@ -134,7 +138,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
134138
crate::pretty_clif::write_clif_file(
135139
tcx,
136140
"opt",
137-
Some(cx.module.isa()),
141+
Some(module.isa()),
138142
instance,
139143
&context,
140144
&clif_comments,
@@ -149,7 +153,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
149153
}
150154

151155
// Define debuginfo for function
152-
let isa = cx.module.isa();
156+
let isa = module.isa();
153157
let debug_context = &mut cx.debug_context;
154158
let unwind_context = &mut cx.unwind_context;
155159
tcx.sess.time("generate debug info", || {
@@ -654,7 +658,7 @@ fn codegen_stmt<'tcx>(
654658
// FIXME use emit_small_memset where possible
655659
let addr = lval.to_ptr().get_addr(fx);
656660
let val = operand.load_scalar(fx);
657-
fx.bcx.call_memset(fx.cx.module.target_config(), addr, val, times);
661+
fx.bcx.call_memset(fx.module.target_config(), addr, val, times);
658662
} else {
659663
let loop_block = fx.bcx.create_block();
660664
let loop_block2 = fx.bcx.create_block();
@@ -834,7 +838,7 @@ fn codegen_stmt<'tcx>(
834838
let elem_size: u64 = pointee.size.bytes();
835839
let bytes =
836840
if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
837-
fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, bytes);
841+
fx.bcx.call_memcpy(fx.module.target_config(), dst, src, bytes);
838842
}
839843
}
840844
}

src/common.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
228228
}
229229
}
230230

231-
pub(crate) struct FunctionCx<'m, 'clif, 'tcx> {
232-
pub(crate) cx: &'clif mut crate::CodegenCx<'m, 'tcx>,
231+
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
232+
pub(crate) cx: &'clif mut crate::CodegenCx<'tcx>,
233+
pub(crate) module: &'m mut dyn Module,
233234
pub(crate) tcx: TyCtxt<'tcx>,
234235
pub(crate) pointer_type: Type, // Cached from module
235236
pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
@@ -341,7 +342,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
341342
}
342343

343344
pub(crate) fn triple(&self) -> &target_lexicon::Triple {
344-
self.cx.module.isa().triple()
345+
self.module.isa().triple()
345346
}
346347

347348
pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
@@ -354,15 +355,14 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
354355
let mut data_ctx = DataContext::new();
355356
data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice());
356357
let msg_id = self
357-
.cx
358358
.module
359359
.declare_data(&format!("__{}_{:08x}", prefix, msg_hash), Linkage::Local, false, false)
360360
.unwrap();
361361

362362
// Ignore DuplicateDefinition error, as the data will be the same
363-
let _ = self.cx.module.define_data(msg_id, &data_ctx);
363+
let _ = self.module.define_data(msg_id, &data_ctx);
364364

365-
let local_msg_id = self.cx.module.declare_data_in_func(msg_id, self.bcx.func);
365+
let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func);
366366
if self.clif_comments.enabled() {
367367
self.add_comment(local_msg_id, msg);
368368
}

src/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ fn bool_env_var(key: &str) -> bool {
55
env::var(key).as_ref().map(|val| &**val) == Ok("1")
66
}
77

8+
/// The mode to use for compilation.
89
#[derive(Copy, Clone, Debug)]
910
pub enum CodegenMode {
11+
/// AOT compile the crate. This is the default.
1012
Aot,
13+
/// JIT compile and execute the crate.
1114
Jit,
15+
/// JIT compile and execute the crate, but only compile functions the first time they are used.
1216
JitLazy,
1317
}
1418

@@ -25,6 +29,7 @@ impl FromStr for CodegenMode {
2529
}
2630
}
2731

32+
/// Configuration of cg_clif as passed in through `-Cllvm-args` and various env vars.
2833
#[derive(Clone, Debug)]
2934
pub struct BackendConfig {
3035
/// Should the crate be AOT compiled or JIT executed.
@@ -76,6 +81,7 @@ impl Default for BackendConfig {
7681
}
7782

7883
impl BackendConfig {
84+
/// Parse the configuration passed in using `-Cllvm-args`.
7985
pub fn from_opts(opts: &[String]) -> Result<Self, String> {
8086
fn parse_bool(name: &str, value: &str) -> Result<bool, String> {
8187
value.parse().map_err(|_| format!("failed to parse value `{}` for {}", value, name))

src/constant.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::ConstKind;
1313
use cranelift_codegen::ir::GlobalValueData;
1414
use cranelift_module::*;
1515

16-
use crate::{prelude::*, CodegenCx};
16+
use crate::prelude::*;
1717

1818
pub(crate) struct ConstantCx {
1919
todo: Vec<TodoItem>,
@@ -78,19 +78,19 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
7878
all_constants_ok
7979
}
8080

81-
pub(crate) fn codegen_static(cx: &mut CodegenCx<'_, '_>, def_id: DefId) {
81+
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
8282
let mut constants_cx = ConstantCx::new();
8383
constants_cx.todo.push(TodoItem::Static(def_id));
84-
constants_cx.finalize(cx.tcx, &mut *cx.module);
84+
constants_cx.finalize(tcx, module);
8585
}
8686

8787
pub(crate) fn codegen_tls_ref<'tcx>(
8888
fx: &mut FunctionCx<'_, '_, 'tcx>,
8989
def_id: DefId,
9090
layout: TyAndLayout<'tcx>,
9191
) -> CValue<'tcx> {
92-
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
93-
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
92+
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
93+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
9494
if fx.clif_comments.enabled() {
9595
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
9696
}
@@ -103,8 +103,8 @@ fn codegen_static_ref<'tcx>(
103103
def_id: DefId,
104104
layout: TyAndLayout<'tcx>,
105105
) -> CPlace<'tcx> {
106-
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
107-
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
106+
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
107+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
108108
if fx.clif_comments.enabled() {
109109
fx.add_comment(local_data_id, format!("{:?}", def_id));
110110
}
@@ -191,29 +191,28 @@ pub(crate) fn codegen_const_value<'tcx>(
191191
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
192192
let data_id = data_id_for_alloc_id(
193193
&mut fx.constants_cx,
194-
fx.cx.module,
194+
fx.module,
195195
ptr.alloc_id,
196196
alloc.mutability,
197197
);
198198
let local_data_id =
199-
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
199+
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
200200
if fx.clif_comments.enabled() {
201201
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
202202
}
203203
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
204204
}
205205
Some(GlobalAlloc::Function(instance)) => {
206-
let func_id =
207-
crate::abi::import_function(fx.tcx, fx.cx.module, instance);
206+
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
208207
let local_func_id =
209-
fx.cx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
208+
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
210209
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
211210
}
212211
Some(GlobalAlloc::Static(def_id)) => {
213212
assert!(fx.tcx.is_static(def_id));
214-
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
213+
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
215214
let local_data_id =
216-
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
215+
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
217216
if fx.clif_comments.enabled() {
218217
fx.add_comment(local_data_id, format!("{:?}", def_id));
219218
}
@@ -255,9 +254,9 @@ fn pointer_for_allocation<'tcx>(
255254
let alloc_id = fx.tcx.create_memory_alloc(alloc);
256255
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
257256
let data_id =
258-
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.cx.module, alloc_id, alloc.mutability);
257+
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);
259258

260-
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
259+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
261260
if fx.clif_comments.enabled() {
262261
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
263262
}

0 commit comments

Comments
 (0)