Skip to content

Refactorings for enabling parallel compilation (part 1) #1264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ configuration options.
## Not yet supported

* Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041))
* On Linux there is support for invoking an external assembler for `global_asm!` and `asm!`.
`llvm_asm!` will remain unimplemented forever. `asm!` doesn't yet support reg classes. You
have to specify specific registers instead.
* On UNIX there is support for invoking an external assembler for `global_asm!` and `asm!`.
* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), some basic things work)

## License
Expand Down
14 changes: 12 additions & 2 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn main() {
#[cfg(not(any(jit, windows)))]
test_tls();

#[cfg(all(not(jit), target_arch = "x86_64", target_os = "linux"))]
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "darwin")))]
unsafe {
global_asm_test();
}
Expand All @@ -343,7 +343,7 @@ fn main() {
}
}

#[cfg(all(not(jit), target_arch = "x86_64", target_os = "linux"))]
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "darwin")))]
extern "C" {
fn global_asm_test();
}
Expand All @@ -358,6 +358,16 @@ global_asm! {
"
}

#[cfg(all(not(jit), target_arch = "x86_64", target_os = "darwin"))]
global_asm! {
"
.global _global_asm_test
_global_asm_test:
// comment that would normally be removed by LLVM
ret
"
}

#[repr(C)]
enum c_void {
_1,
Expand Down
122 changes: 71 additions & 51 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,50 @@ use rustc_index::vec::IndexVec;
use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::layout::FnAbiOf;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::SymbolName;

use indexmap::IndexSet;

use crate::constant::ConstantCx;
use crate::prelude::*;
use crate::pretty_clif::CommentWriter;

pub(crate) fn codegen_fn<'tcx>(
struct CodegenedFunction<'tcx> {
instance: Instance<'tcx>,
symbol_name: SymbolName<'tcx>,
func_id: FuncId,
func: Function,
clif_comments: CommentWriter,
source_info_set: IndexSet<SourceInfo>,
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
}

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

let _inst_guard =
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));

let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
let codegened_func = codegen_fn(cx, cached_func, module, instance);

compile_fn(cx, cached_context, module, codegened_func);
}

fn codegen_fn<'tcx>(
cx: &mut crate::CodegenCx<'tcx>,
cached_func: Function,
module: &mut dyn Module,
instance: Instance<'tcx>,
) -> CodegenedFunction<'tcx> {
debug_assert!(!instance.substs.needs_infer());

let tcx = cx.tcx;

let mir = tcx.instance_mir(instance.def);
let _mir_guard = crate::PrintOnPanic(|| {
let mut buf = Vec::new();
Expand All @@ -38,11 +64,10 @@ pub(crate) fn codegen_fn<'tcx>(
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();

// Make the FunctionBuilder
let mut func_ctx = FunctionBuilderContext::new();
let mut func = std::mem::replace(&mut cx.cached_context.func, Function::new());
let mut func = cached_func;
func.clear();
func.name = ExternalName::user(0, func_id.as_u32());
func.signature = sig;
func.collect_debug_info();
Expand Down Expand Up @@ -82,27 +107,7 @@ pub(crate) fn codegen_fn<'tcx>(
next_ssa_var: 0,
};

let arg_uninhabited = fx
.mir
.args_iter()
.any(|arg| fx.layout_of(fx.monomorphize(fx.mir.local_decls[arg].ty)).abi.is_uninhabited());

if !crate::constant::check_constants(&mut fx) {
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
// compilation should have been aborted
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
} else if arg_uninhabited {
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
} else {
tcx.sess.time("codegen clif ir", || {
tcx.sess
.time("codegen prelude", || crate::abi::codegen_fn_prelude(&mut fx, start_block));
codegen_fn_content(&mut fx);
});
}
tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));

// Recover all necessary data from fx, before accessing func will prevent future access to it.
let instance = fx.instance;
Expand All @@ -124,36 +129,31 @@ pub(crate) fn codegen_fn<'tcx>(
// Verify function
verify_func(tcx, &clif_comments, &func);

compile_fn(
cx,
module,
CodegenedFunction {
instance,
symbol_name.name,
symbol_name,
func_id,
func,
clif_comments,
source_info_set,
local_map,
);
}
}

fn compile_fn<'tcx>(
cx: &mut crate::CodegenCx<'tcx>,
cached_context: &mut Context,
module: &mut dyn Module,
instance: Instance<'tcx>,
symbol_name: &str,
func_id: FuncId,
func: Function,
mut clif_comments: CommentWriter,
source_info_set: IndexSet<SourceInfo>,
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
codegened_func: CodegenedFunction<'tcx>,
) {
let tcx = cx.tcx;

let mut clif_comments = codegened_func.clif_comments;

// Store function in context
let context = &mut cx.cached_context;
let context = cached_context;
context.clear();
context.func = func;
context.func = codegened_func.func;

// If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128`
// instruction, which doesn't have an encoding.
Expand All @@ -170,7 +170,7 @@ fn compile_fn<'tcx>(
crate::optimize::optimize_function(
tcx,
module.isa(),
instance,
codegened_func.instance,
context,
&mut clif_comments,
);
Expand Down Expand Up @@ -206,23 +206,23 @@ fn compile_fn<'tcx>(
// Define function
tcx.sess.time("define function", || {
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
module.define_function(func_id, context).unwrap();
module.define_function(codegened_func.func_id, context).unwrap();
});

// Write optimized function to file for debugging
crate::pretty_clif::write_clif_file(
tcx,
"opt",
module.isa(),
instance,
codegened_func.instance,
&context.func,
&clif_comments,
);

if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
crate::pretty_clif::write_ir_file(
tcx,
|| format!("{}.vcode", tcx.symbol_name(instance).name),
|| format!("{}.vcode", tcx.symbol_name(codegened_func.instance).name),
|file| file.write_all(disasm.as_bytes()),
)
}
Expand All @@ -234,16 +234,16 @@ fn compile_fn<'tcx>(
tcx.sess.time("generate debug info", || {
if let Some(debug_context) = debug_context {
debug_context.define_function(
instance,
func_id,
symbol_name,
codegened_func.instance,
codegened_func.func_id,
codegened_func.symbol_name.name,
isa,
context,
&source_info_set,
local_map,
&codegened_func.source_info_set,
codegened_func.local_map,
);
}
unwind_context.add_function(func_id, &context, isa);
unwind_context.add_function(codegened_func.func_id, &context, isa);
});
}

Expand All @@ -269,7 +269,27 @@ pub(crate) fn verify_func(
});
}

fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
if !crate::constant::check_constants(fx) {
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
// compilation should have been aborted
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
return;
}

let arg_uninhabited = fx
.mir
.args_iter()
.any(|arg| fx.layout_of(fx.monomorphize(fx.mir.local_decls[arg].ty)).abi.is_uninhabited());
if arg_uninhabited {
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
return;
}
fx.tcx.sess.time("codegen prelude", || crate::abi::codegen_fn_prelude(fx, start_block));

for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
let block = fx.get_block(bb);
fx.bcx.switch_to_block(block);
Expand Down
Loading