Skip to content
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// source for rustc_* is not included in the rust-src component; disable the errors about this
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "macro-error"],
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "unresolved-macro-call"],
"rust-analyzer.assist.importMergeBehavior": "last",
"rust-analyzer.cargo.runBuildScripts": true,
"rust-analyzer.linkedProjects": [
Expand Down
8 changes: 4 additions & 4 deletions src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub(crate) fn import_function<'tcx>(
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
/// Instance must be monomorphized
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
let func_id = import_function(self.tcx, self.cx.module, inst);
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
let func_id = import_function(self.tcx, self.module, inst);
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);

if self.clif_comments.enabled() {
self.add_comment(func_ref, format!("{:?}", inst));
Expand All @@ -89,8 +89,8 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
args: &[Value],
) -> &[Value] {
let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) };
let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap();
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
let func_id = self.module.declare_function(&name, Linkage::Import, &sig).unwrap();
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
let call_inst = self.bcx.ins().call(func_ref, args);
if self.clif_comments.enabled() {
self.add_comment(call_inst, format!("easy_call {}", name));
Expand Down
4 changes: 2 additions & 2 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_span::symbol::sym;
pub(crate) fn codegen(
tcx: TyCtxt<'_>,
module: &mut impl Module,
unwind_context: &mut UnwindContext<'_>,
unwind_context: &mut UnwindContext,
) -> bool {
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
use rustc_middle::middle::dependency_format::Linkage;
Expand All @@ -29,7 +29,7 @@ pub(crate) fn codegen(

fn codegen_inner(
module: &mut impl Module,
unwind_context: &mut UnwindContext<'_>,
unwind_context: &mut UnwindContext,
kind: AllocatorKind,
) {
let usize_ty = module.target_config().pointer_type();
Expand Down
28 changes: 16 additions & 12 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use rustc_target::abi::call::FnAbi;
use crate::constant::ConstantCx;
use crate::prelude::*;

pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) {
pub(crate) fn codegen_fn<'tcx>(
cx: &mut crate::CodegenCx<'tcx>,
module: &mut dyn Module,
instance: Instance<'tcx>,
) {
let tcx = cx.tcx;

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

// Declare function
let symbol_name = tcx.symbol_name(instance);
let sig = get_function_sig(tcx, cx.module.isa().triple(), instance);
let func_id = cx.module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
let sig = get_function_sig(tcx, module.isa().triple(), instance);
let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();

cx.cached_context.clear();

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

// Make FunctionCx
let pointer_type = cx.module.target_config().pointer_type();
let pointer_type = module.target_config().pointer_type();
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);

let mut fx = FunctionCx {
cx,
module,
tcx,
pointer_type,
vtables: FxHashMap::default(),
Expand Down Expand Up @@ -94,7 +99,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
let source_info_set = fx.source_info_set;
let local_map = fx.local_map;

fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module);
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);

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

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

// Define function
let module = &mut cx.module;
tcx.sess.time("define function", || {
module
.define_function(func_id, context, &mut NullTrapSink {}, &mut NullStackMapSink {})
Expand All @@ -134,7 +138,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
crate::pretty_clif::write_clif_file(
tcx,
"opt",
Some(cx.module.isa()),
Some(module.isa()),
instance,
&context,
&clif_comments,
Expand All @@ -149,7 +153,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
}

// Define debuginfo for function
let isa = cx.module.isa();
let isa = module.isa();
let debug_context = &mut cx.debug_context;
let unwind_context = &mut cx.unwind_context;
tcx.sess.time("generate debug info", || {
Expand Down Expand Up @@ -654,7 +658,7 @@ fn codegen_stmt<'tcx>(
// FIXME use emit_small_memset where possible
let addr = lval.to_ptr().get_addr(fx);
let val = operand.load_scalar(fx);
fx.bcx.call_memset(fx.cx.module.target_config(), addr, val, times);
fx.bcx.call_memset(fx.module.target_config(), addr, val, times);
} else {
let loop_block = fx.bcx.create_block();
let loop_block2 = fx.bcx.create_block();
Expand Down Expand Up @@ -834,7 +838,7 @@ fn codegen_stmt<'tcx>(
let elem_size: u64 = pointee.size.bytes();
let bytes =
if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, bytes);
fx.bcx.call_memcpy(fx.module.target_config(), dst, src, bytes);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
}
}

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

pub(crate) fn triple(&self) -> &target_lexicon::Triple {
self.cx.module.isa().triple()
self.module.isa().triple()
}

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

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

let local_msg_id = self.cx.module.declare_data_in_func(msg_id, self.bcx.func);
let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func);
if self.clif_comments.enabled() {
self.add_comment(local_msg_id, msg);
}
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ fn bool_env_var(key: &str) -> bool {
env::var(key).as_ref().map(|val| &**val) == Ok("1")
}

/// The mode to use for compilation.
#[derive(Copy, Clone, Debug)]
pub enum CodegenMode {
/// AOT compile the crate. This is the default.
Aot,
/// JIT compile and execute the crate.
Jit,
/// JIT compile and execute the crate, but only compile functions the first time they are used.
JitLazy,
}

Expand All @@ -25,6 +29,7 @@ impl FromStr for CodegenMode {
}
}

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

impl BackendConfig {
/// Parse the configuration passed in using `-Cllvm-args`.
pub fn from_opts(opts: &[String]) -> Result<Self, String> {
fn parse_bool(name: &str, value: &str) -> Result<bool, String> {
value.parse().map_err(|_| format!("failed to parse value `{}` for {}", value, name))
Expand Down
31 changes: 15 additions & 16 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_middle::ty::ConstKind;
use cranelift_codegen::ir::GlobalValueData;
use cranelift_module::*;

use crate::{prelude::*, CodegenCx};
use crate::prelude::*;

pub(crate) struct ConstantCx {
todo: Vec<TodoItem>,
Expand Down Expand Up @@ -78,19 +78,19 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
all_constants_ok
}

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

pub(crate) fn codegen_tls_ref<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
def_id: DefId,
layout: TyAndLayout<'tcx>,
) -> CValue<'tcx> {
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
}
Expand All @@ -103,8 +103,8 @@ fn codegen_static_ref<'tcx>(
def_id: DefId,
layout: TyAndLayout<'tcx>,
) -> CPlace<'tcx> {
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", def_id));
}
Expand Down Expand Up @@ -191,29 +191,28 @@ pub(crate) fn codegen_const_value<'tcx>(
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
let data_id = data_id_for_alloc_id(
&mut fx.constants_cx,
fx.cx.module,
fx.module,
ptr.alloc_id,
alloc.mutability,
);
let local_data_id =
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
}
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
}
Some(GlobalAlloc::Function(instance)) => {
let func_id =
crate::abi::import_function(fx.tcx, fx.cx.module, instance);
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
let local_func_id =
fx.cx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
}
Some(GlobalAlloc::Static(def_id)) => {
assert!(fx.tcx.is_static(def_id));
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false);
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id =
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", def_id));
}
Expand Down Expand Up @@ -255,9 +254,9 @@ fn pointer_for_allocation<'tcx>(
let alloc_id = fx.tcx.create_memory_alloc(alloc);
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
let data_id =
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.cx.module, alloc_id, alloc.mutability);
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);

let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
}
Expand Down
Loading