Merge pull request #1264 from bjorn3/parallel_comp_refactor
Refactorings for enabling parallel compilation (part 1)
This commit is contained in:
commit
523f0db7db
@ -52,9 +52,7 @@ configuration options.
|
|||||||
## Not yet supported
|
## Not yet supported
|
||||||
|
|
||||||
* Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041))
|
* 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!`.
|
* On UNIX 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.
|
|
||||||
* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), some basic things work)
|
* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), some basic things work)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
@ -321,7 +321,7 @@ fn main() {
|
|||||||
#[cfg(not(any(jit, windows)))]
|
#[cfg(not(any(jit, windows)))]
|
||||||
test_tls();
|
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 {
|
unsafe {
|
||||||
global_asm_test();
|
global_asm_test();
|
||||||
}
|
}
|
||||||
@ -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" {
|
extern "C" {
|
||||||
fn global_asm_test();
|
fn global_asm_test();
|
||||||
}
|
}
|
||||||
@ -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)]
|
#[repr(C)]
|
||||||
enum c_void {
|
enum c_void {
|
||||||
_1,
|
_1,
|
||||||
|
122
src/base.rs
122
src/base.rs
@ -5,6 +5,7 @@ use rustc_index::vec::IndexVec;
|
|||||||
use rustc_middle::ty::adjustment::PointerCast;
|
use rustc_middle::ty::adjustment::PointerCast;
|
||||||
use rustc_middle::ty::layout::FnAbiOf;
|
use rustc_middle::ty::layout::FnAbiOf;
|
||||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||||
|
use rustc_middle::ty::SymbolName;
|
||||||
|
|
||||||
use indexmap::IndexSet;
|
use indexmap::IndexSet;
|
||||||
|
|
||||||
@ -12,17 +13,42 @@ use crate::constant::ConstantCx;
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::pretty_clif::CommentWriter;
|
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>,
|
cx: &mut crate::CodegenCx<'tcx>,
|
||||||
|
cached_context: &mut Context,
|
||||||
module: &mut dyn Module,
|
module: &mut dyn Module,
|
||||||
instance: Instance<'tcx>,
|
instance: Instance<'tcx>,
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx;
|
let tcx = cx.tcx;
|
||||||
|
|
||||||
let _inst_guard =
|
let _inst_guard =
|
||||||
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
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());
|
debug_assert!(!instance.substs.needs_infer());
|
||||||
|
|
||||||
|
let tcx = cx.tcx;
|
||||||
|
|
||||||
let mir = tcx.instance_mir(instance.def);
|
let mir = tcx.instance_mir(instance.def);
|
||||||
let _mir_guard = crate::PrintOnPanic(|| {
|
let _mir_guard = crate::PrintOnPanic(|| {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
@ -38,11 +64,10 @@ pub(crate) fn codegen_fn<'tcx>(
|
|||||||
let sig = get_function_sig(tcx, module.isa().triple(), instance);
|
let sig = get_function_sig(tcx, module.isa().triple(), instance);
|
||||||
let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
|
let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
|
||||||
|
|
||||||
cx.cached_context.clear();
|
|
||||||
|
|
||||||
// Make the FunctionBuilder
|
// Make the FunctionBuilder
|
||||||
let mut func_ctx = FunctionBuilderContext::new();
|
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.name = ExternalName::user(0, func_id.as_u32());
|
||||||
func.signature = sig;
|
func.signature = sig;
|
||||||
func.collect_debug_info();
|
func.collect_debug_info();
|
||||||
@ -82,27 +107,7 @@ pub(crate) fn codegen_fn<'tcx>(
|
|||||||
next_ssa_var: 0,
|
next_ssa_var: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
let arg_uninhabited = fx
|
tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));
|
||||||
.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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recover all necessary data from fx, before accessing func will prevent future access to it.
|
// Recover all necessary data from fx, before accessing func will prevent future access to it.
|
||||||
let instance = fx.instance;
|
let instance = fx.instance;
|
||||||
@ -124,36 +129,31 @@ pub(crate) fn codegen_fn<'tcx>(
|
|||||||
// Verify function
|
// Verify function
|
||||||
verify_func(tcx, &clif_comments, &func);
|
verify_func(tcx, &clif_comments, &func);
|
||||||
|
|
||||||
compile_fn(
|
CodegenedFunction {
|
||||||
cx,
|
|
||||||
module,
|
|
||||||
instance,
|
instance,
|
||||||
symbol_name.name,
|
symbol_name,
|
||||||
func_id,
|
func_id,
|
||||||
func,
|
func,
|
||||||
clif_comments,
|
clif_comments,
|
||||||
source_info_set,
|
source_info_set,
|
||||||
local_map,
|
local_map,
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_fn<'tcx>(
|
fn compile_fn<'tcx>(
|
||||||
cx: &mut crate::CodegenCx<'tcx>,
|
cx: &mut crate::CodegenCx<'tcx>,
|
||||||
|
cached_context: &mut Context,
|
||||||
module: &mut dyn Module,
|
module: &mut dyn Module,
|
||||||
instance: Instance<'tcx>,
|
codegened_func: CodegenedFunction<'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>>,
|
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx;
|
let tcx = cx.tcx;
|
||||||
|
|
||||||
|
let mut clif_comments = codegened_func.clif_comments;
|
||||||
|
|
||||||
// Store function in context
|
// Store function in context
|
||||||
let context = &mut cx.cached_context;
|
let context = cached_context;
|
||||||
context.clear();
|
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`
|
// If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128`
|
||||||
// instruction, which doesn't have an encoding.
|
// instruction, which doesn't have an encoding.
|
||||||
@ -170,7 +170,7 @@ fn compile_fn<'tcx>(
|
|||||||
crate::optimize::optimize_function(
|
crate::optimize::optimize_function(
|
||||||
tcx,
|
tcx,
|
||||||
module.isa(),
|
module.isa(),
|
||||||
instance,
|
codegened_func.instance,
|
||||||
context,
|
context,
|
||||||
&mut clif_comments,
|
&mut clif_comments,
|
||||||
);
|
);
|
||||||
@ -206,7 +206,7 @@ fn compile_fn<'tcx>(
|
|||||||
// Define function
|
// Define function
|
||||||
tcx.sess.time("define function", || {
|
tcx.sess.time("define function", || {
|
||||||
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
|
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
|
// Write optimized function to file for debugging
|
||||||
@ -214,7 +214,7 @@ fn compile_fn<'tcx>(
|
|||||||
tcx,
|
tcx,
|
||||||
"opt",
|
"opt",
|
||||||
module.isa(),
|
module.isa(),
|
||||||
instance,
|
codegened_func.instance,
|
||||||
&context.func,
|
&context.func,
|
||||||
&clif_comments,
|
&clif_comments,
|
||||||
);
|
);
|
||||||
@ -222,7 +222,7 @@ fn compile_fn<'tcx>(
|
|||||||
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
||||||
crate::pretty_clif::write_ir_file(
|
crate::pretty_clif::write_ir_file(
|
||||||
tcx,
|
tcx,
|
||||||
|| format!("{}.vcode", tcx.symbol_name(instance).name),
|
|| format!("{}.vcode", tcx.symbol_name(codegened_func.instance).name),
|
||||||
|file| file.write_all(disasm.as_bytes()),
|
|file| file.write_all(disasm.as_bytes()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -234,16 +234,16 @@ fn compile_fn<'tcx>(
|
|||||||
tcx.sess.time("generate debug info", || {
|
tcx.sess.time("generate debug info", || {
|
||||||
if let Some(debug_context) = debug_context {
|
if let Some(debug_context) = debug_context {
|
||||||
debug_context.define_function(
|
debug_context.define_function(
|
||||||
instance,
|
codegened_func.instance,
|
||||||
func_id,
|
codegened_func.func_id,
|
||||||
symbol_name,
|
codegened_func.symbol_name.name,
|
||||||
isa,
|
isa,
|
||||||
context,
|
context,
|
||||||
&source_info_set,
|
&codegened_func.source_info_set,
|
||||||
local_map,
|
codegened_func.local_map,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
unwind_context.add_function(func_id, &context, isa);
|
unwind_context.add_function(codegened_func.func_id, &context, isa);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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() {
|
for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
|
||||||
let block = fx.get_block(bb);
|
let block = fx.get_block(bb);
|
||||||
fx.bcx.switch_to_block(block);
|
fx.bcx.switch_to_block(block);
|
||||||
|
@ -1,25 +1,31 @@
|
|||||||
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
||||||
//! standalone executable.
|
//! standalone executable.
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
|
||||||
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
|
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
|
||||||
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
|
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
|
||||||
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
||||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||||
use rustc_metadata::EncodedMetadata;
|
use rustc_metadata::EncodedMetadata;
|
||||||
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
||||||
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
|
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
|
||||||
use rustc_session::cgu_reuse_tracker::CguReuse;
|
use rustc_session::cgu_reuse_tracker::CguReuse;
|
||||||
use rustc_session::config::{DebugInfo, OutputType};
|
use rustc_session::config::{DebugInfo, OutputFilenames, OutputType};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
|
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
|
||||||
use cranelift_object::{ObjectBuilder, ObjectModule};
|
use cranelift_object::{ObjectBuilder, ObjectModule};
|
||||||
|
|
||||||
|
use crate::global_asm::GlobalAsmConfig;
|
||||||
use crate::{prelude::*, BackendConfig};
|
use crate::{prelude::*, BackendConfig};
|
||||||
|
|
||||||
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
|
struct ModuleCodegenResult {
|
||||||
|
module_regular: CompiledModule,
|
||||||
|
module_global_asm: Option<CompiledModule>,
|
||||||
|
existing_work_product: Option<(WorkProductId, WorkProduct)>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
|
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
|
||||||
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
|
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
|
||||||
@ -27,7 +33,79 @@ impl<HCX> HashStable<HCX> for ModuleCodegenResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_module(sess: &Session, isa: Box<dyn TargetIsa>, name: String) -> ObjectModule {
|
pub(crate) struct OngoingCodegen {
|
||||||
|
modules: Vec<Result<ModuleCodegenResult, String>>,
|
||||||
|
allocator_module: Option<CompiledModule>,
|
||||||
|
metadata_module: Option<CompiledModule>,
|
||||||
|
metadata: EncodedMetadata,
|
||||||
|
crate_info: CrateInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OngoingCodegen {
|
||||||
|
pub(crate) fn join(
|
||||||
|
self,
|
||||||
|
sess: &Session,
|
||||||
|
backend_config: &BackendConfig,
|
||||||
|
) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
|
||||||
|
let mut work_products = FxHashMap::default();
|
||||||
|
let mut modules = vec![];
|
||||||
|
|
||||||
|
for module_codegen_result in self.modules {
|
||||||
|
let module_codegen_result = match module_codegen_result {
|
||||||
|
Ok(module_codegen_result) => module_codegen_result,
|
||||||
|
Err(err) => sess.fatal(&err),
|
||||||
|
};
|
||||||
|
let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
|
||||||
|
module_codegen_result;
|
||||||
|
|
||||||
|
if let Some((work_product_id, work_product)) = existing_work_product {
|
||||||
|
work_products.insert(work_product_id, work_product);
|
||||||
|
} else {
|
||||||
|
let work_product = if backend_config.disable_incr_cache {
|
||||||
|
None
|
||||||
|
} else if let Some(module_global_asm) = &module_global_asm {
|
||||||
|
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
||||||
|
sess,
|
||||||
|
&module_regular.name,
|
||||||
|
&[
|
||||||
|
("o", &module_regular.object.as_ref().unwrap()),
|
||||||
|
("asm.o", &module_global_asm.object.as_ref().unwrap()),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
||||||
|
sess,
|
||||||
|
&module_regular.name,
|
||||||
|
&[("o", &module_regular.object.as_ref().unwrap())],
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if let Some((work_product_id, work_product)) = work_product {
|
||||||
|
work_products.insert(work_product_id, work_product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
modules.push(module_regular);
|
||||||
|
if let Some(module_global_asm) = module_global_asm {
|
||||||
|
modules.push(module_global_asm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
CodegenResults {
|
||||||
|
modules,
|
||||||
|
allocator_module: self.allocator_module,
|
||||||
|
metadata_module: self.metadata_module,
|
||||||
|
metadata: self.metadata,
|
||||||
|
crate_info: self.crate_info,
|
||||||
|
},
|
||||||
|
work_products,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> ObjectModule {
|
||||||
|
let isa = crate::build_isa(sess, backend_config);
|
||||||
|
|
||||||
let mut builder =
|
let mut builder =
|
||||||
ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
|
ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
|
||||||
// Unlike cg_llvm, cg_clif defaults to disabling -Zfunction-sections. For cg_llvm binary size
|
// Unlike cg_llvm, cg_clif defaults to disabling -Zfunction-sections. For cg_llvm binary size
|
||||||
@ -37,15 +115,15 @@ fn make_module(sess: &Session, isa: Box<dyn TargetIsa>, name: String) -> ObjectM
|
|||||||
ObjectModule::new(builder)
|
ObjectModule::new(builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_module(
|
fn emit_cgu(
|
||||||
tcx: TyCtxt<'_>,
|
output_filenames: &OutputFilenames,
|
||||||
backend_config: &BackendConfig,
|
prof: &SelfProfilerRef,
|
||||||
name: String,
|
name: String,
|
||||||
kind: ModuleKind,
|
|
||||||
module: ObjectModule,
|
module: ObjectModule,
|
||||||
debug: Option<DebugContext<'_>>,
|
debug: Option<DebugContext<'_>>,
|
||||||
unwind_context: UnwindContext,
|
unwind_context: UnwindContext,
|
||||||
) -> ModuleCodegenResult {
|
global_asm_object_file: Option<PathBuf>,
|
||||||
|
) -> Result<ModuleCodegenResult, String> {
|
||||||
let mut product = module.finish();
|
let mut product = module.finish();
|
||||||
|
|
||||||
if let Some(mut debug) = debug {
|
if let Some(mut debug) = debug {
|
||||||
@ -54,71 +132,117 @@ fn emit_module(
|
|||||||
|
|
||||||
unwind_context.emit(&mut product);
|
unwind_context.emit(&mut product);
|
||||||
|
|
||||||
let tmp_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(&name));
|
let module_regular =
|
||||||
let obj = product.object.write().unwrap();
|
emit_module(output_filenames, prof, product.object, ModuleKind::Regular, name.clone())?;
|
||||||
|
|
||||||
tcx.sess.prof.artifact_size("object_file", name.clone(), obj.len().try_into().unwrap());
|
Ok(ModuleCodegenResult {
|
||||||
|
module_regular,
|
||||||
|
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
|
||||||
|
name: format!("{name}.asm"),
|
||||||
|
kind: ModuleKind::Regular,
|
||||||
|
object: Some(global_asm_object_file),
|
||||||
|
dwarf_object: None,
|
||||||
|
bytecode: None,
|
||||||
|
}),
|
||||||
|
existing_work_product: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if let Err(err) = std::fs::write(&tmp_file, obj) {
|
fn emit_module(
|
||||||
tcx.sess.fatal(&format!("error writing object file: {}", err));
|
output_filenames: &OutputFilenames,
|
||||||
}
|
prof: &SelfProfilerRef,
|
||||||
|
object: cranelift_object::object::write::Object<'_>,
|
||||||
let work_product = if backend_config.disable_incr_cache {
|
kind: ModuleKind,
|
||||||
None
|
name: String,
|
||||||
} else {
|
) -> Result<CompiledModule, String> {
|
||||||
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
let tmp_file = output_filenames.temp_path(OutputType::Object, Some(&name));
|
||||||
tcx.sess,
|
let mut file = match File::create(&tmp_file) {
|
||||||
&name,
|
Ok(file) => file,
|
||||||
&[("o", &tmp_file)],
|
Err(err) => return Err(format!("error creating object file: {}", err)),
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ModuleCodegenResult(
|
if let Err(err) = object.write_stream(&mut file) {
|
||||||
CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None },
|
return Err(format!("error writing object file: {}", err));
|
||||||
work_product,
|
}
|
||||||
)
|
|
||||||
|
prof.artifact_size("object_file", &*name, file.metadata().unwrap().len());
|
||||||
|
|
||||||
|
Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reuse_workproduct_for_cgu(
|
fn reuse_workproduct_for_cgu(
|
||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
cgu: &CodegenUnit<'_>,
|
cgu: &CodegenUnit<'_>,
|
||||||
work_products: &mut FxHashMap<WorkProductId, WorkProduct>,
|
) -> Result<ModuleCodegenResult, String> {
|
||||||
) -> CompiledModule {
|
|
||||||
let work_product = cgu.previous_work_product(tcx);
|
let work_product = cgu.previous_work_product(tcx);
|
||||||
let obj_out = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
|
let obj_out_regular =
|
||||||
let source_file = rustc_incremental::in_incr_comp_dir_sess(
|
tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
|
||||||
|
let source_file_regular = rustc_incremental::in_incr_comp_dir_sess(
|
||||||
&tcx.sess,
|
&tcx.sess,
|
||||||
&work_product.saved_files.get("o").expect("no saved object file in work product"),
|
&work_product.saved_files.get("o").expect("no saved object file in work product"),
|
||||||
);
|
);
|
||||||
if let Err(err) = rustc_fs_util::link_or_copy(&source_file, &obj_out) {
|
|
||||||
tcx.sess.err(&format!(
|
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_regular, &obj_out_regular) {
|
||||||
|
return Err(format!(
|
||||||
"unable to copy {} to {}: {}",
|
"unable to copy {} to {}: {}",
|
||||||
source_file.display(),
|
source_file_regular.display(),
|
||||||
obj_out.display(),
|
obj_out_regular.display(),
|
||||||
err
|
err
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
let obj_out_global_asm =
|
||||||
|
crate::global_asm::add_file_stem_postfix(obj_out_regular.clone(), ".asm");
|
||||||
|
let has_global_asm = if let Some(asm_o) = work_product.saved_files.get("asm.o") {
|
||||||
|
let source_file_global_asm = rustc_incremental::in_incr_comp_dir_sess(&tcx.sess, asm_o);
|
||||||
|
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_global_asm, &obj_out_global_asm)
|
||||||
|
{
|
||||||
|
return Err(format!(
|
||||||
|
"unable to copy {} to {}: {}",
|
||||||
|
source_file_regular.display(),
|
||||||
|
obj_out_regular.display(),
|
||||||
|
err
|
||||||
|
));
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
work_products.insert(cgu.work_product_id(), work_product);
|
Ok(ModuleCodegenResult {
|
||||||
|
module_regular: CompiledModule {
|
||||||
CompiledModule {
|
name: cgu.name().to_string(),
|
||||||
name: cgu.name().to_string(),
|
kind: ModuleKind::Regular,
|
||||||
kind: ModuleKind::Regular,
|
object: Some(obj_out_regular),
|
||||||
object: Some(obj_out),
|
dwarf_object: None,
|
||||||
dwarf_object: None,
|
bytecode: None,
|
||||||
bytecode: None,
|
},
|
||||||
}
|
module_global_asm: if has_global_asm {
|
||||||
|
Some(CompiledModule {
|
||||||
|
name: cgu.name().to_string(),
|
||||||
|
kind: ModuleKind::Regular,
|
||||||
|
object: Some(obj_out_global_asm),
|
||||||
|
dwarf_object: None,
|
||||||
|
bytecode: None,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
existing_work_product: Some((cgu.work_product_id(), work_product)),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module_codegen(
|
fn module_codegen(
|
||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
(backend_config, cgu_name): (BackendConfig, rustc_span::Symbol),
|
(backend_config, global_asm_config, cgu_name): (
|
||||||
) -> ModuleCodegenResult {
|
BackendConfig,
|
||||||
|
Arc<GlobalAsmConfig>,
|
||||||
|
rustc_span::Symbol,
|
||||||
|
),
|
||||||
|
) -> Result<ModuleCodegenResult, String> {
|
||||||
let cgu = tcx.codegen_unit(cgu_name);
|
let cgu = tcx.codegen_unit(cgu_name);
|
||||||
let mono_items = cgu.items_in_deterministic_order(tcx);
|
let mono_items = cgu.items_in_deterministic_order(tcx);
|
||||||
|
|
||||||
let isa = crate::build_isa(tcx.sess, &backend_config);
|
let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
|
||||||
let mut module = make_module(tcx.sess, isa, cgu_name.as_str().to_string());
|
|
||||||
|
|
||||||
let mut cx = crate::CodegenCx::new(
|
let mut cx = crate::CodegenCx::new(
|
||||||
tcx,
|
tcx,
|
||||||
@ -128,32 +252,22 @@ fn module_codegen(
|
|||||||
cgu_name,
|
cgu_name,
|
||||||
);
|
);
|
||||||
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
||||||
|
let mut cached_context = Context::new();
|
||||||
for (mono_item, _) in mono_items {
|
for (mono_item, _) in mono_items {
|
||||||
match mono_item {
|
match mono_item {
|
||||||
MonoItem::Fn(inst) => {
|
MonoItem::Fn(inst) => {
|
||||||
cx.tcx
|
cx.tcx.sess.time("codegen fn", || {
|
||||||
.sess
|
crate::base::codegen_and_compile_fn(
|
||||||
.time("codegen fn", || crate::base::codegen_fn(&mut cx, &mut module, inst));
|
&mut cx,
|
||||||
|
&mut cached_context,
|
||||||
|
&mut module,
|
||||||
|
inst,
|
||||||
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
|
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
|
||||||
MonoItem::GlobalAsm(item_id) => {
|
MonoItem::GlobalAsm(item_id) => {
|
||||||
let item = cx.tcx.hir().item(item_id);
|
crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
|
||||||
if let rustc_hir::ItemKind::GlobalAsm(asm) = item.kind {
|
|
||||||
if !asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
|
|
||||||
cx.global_asm.push_str("\n.intel_syntax noprefix\n");
|
|
||||||
} else {
|
|
||||||
cx.global_asm.push_str("\n.att_syntax\n");
|
|
||||||
}
|
|
||||||
for piece in asm.template {
|
|
||||||
match *piece {
|
|
||||||
InlineAsmTemplatePiece::String(ref s) => cx.global_asm.push_str(s),
|
|
||||||
InlineAsmTemplatePiece::Placeholder { .. } => todo!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cx.global_asm.push_str("\n.att_syntax\n\n");
|
|
||||||
} else {
|
|
||||||
bug!("Expected GlobalAsm found {:?}", item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,23 +279,28 @@ fn module_codegen(
|
|||||||
cgu.is_primary(),
|
cgu.is_primary(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let global_asm_object_file = match crate::global_asm::compile_global_asm(
|
||||||
|
&global_asm_config,
|
||||||
|
cgu.name().as_str(),
|
||||||
|
&cx.global_asm,
|
||||||
|
) {
|
||||||
|
Ok(global_asm_object_file) => global_asm_object_file,
|
||||||
|
Err(err) => tcx.sess.fatal(&err),
|
||||||
|
};
|
||||||
|
|
||||||
let debug_context = cx.debug_context;
|
let debug_context = cx.debug_context;
|
||||||
let unwind_context = cx.unwind_context;
|
let unwind_context = cx.unwind_context;
|
||||||
let codegen_result = tcx.sess.time("write object file", || {
|
tcx.sess.time("write object file", || {
|
||||||
emit_module(
|
emit_cgu(
|
||||||
tcx,
|
&global_asm_config.output_filenames,
|
||||||
&backend_config,
|
&tcx.sess.prof,
|
||||||
cgu.name().as_str().to_string(),
|
cgu.name().as_str().to_string(),
|
||||||
ModuleKind::Regular,
|
|
||||||
module,
|
module,
|
||||||
debug_context,
|
debug_context,
|
||||||
unwind_context,
|
unwind_context,
|
||||||
|
global_asm_object_file,
|
||||||
)
|
)
|
||||||
});
|
})
|
||||||
|
|
||||||
codegen_global_asm(tcx, cgu.name().as_str(), &cx.global_asm);
|
|
||||||
|
|
||||||
codegen_result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run_aot(
|
pub(crate) fn run_aot(
|
||||||
@ -189,9 +308,7 @@ pub(crate) fn run_aot(
|
|||||||
backend_config: BackendConfig,
|
backend_config: BackendConfig,
|
||||||
metadata: EncodedMetadata,
|
metadata: EncodedMetadata,
|
||||||
need_metadata_module: bool,
|
need_metadata_module: bool,
|
||||||
) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
|
) -> Box<OngoingCodegen> {
|
||||||
let mut work_products = FxHashMap::default();
|
|
||||||
|
|
||||||
let cgus = if tcx.sess.opts.output_types.should_codegen() {
|
let cgus = if tcx.sess.opts.output_types.should_codegen() {
|
||||||
tcx.collect_and_partition_mono_items(()).1
|
tcx.collect_and_partition_mono_items(()).1
|
||||||
} else {
|
} else {
|
||||||
@ -206,62 +323,59 @@ pub(crate) fn run_aot(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
|
||||||
|
|
||||||
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
|
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
|
||||||
cgus.iter()
|
cgus.iter()
|
||||||
.map(|cgu| {
|
.map(|cgu| {
|
||||||
let cgu_reuse = determine_cgu_reuse(tcx, cgu);
|
let cgu_reuse = if backend_config.disable_incr_cache {
|
||||||
|
CguReuse::No
|
||||||
|
} else {
|
||||||
|
determine_cgu_reuse(tcx, cgu)
|
||||||
|
};
|
||||||
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
|
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
|
||||||
|
|
||||||
match cgu_reuse {
|
match cgu_reuse {
|
||||||
_ if backend_config.disable_incr_cache => {}
|
CguReuse::No => {
|
||||||
CguReuse::No => {}
|
let dep_node = cgu.codegen_dep_node(tcx);
|
||||||
CguReuse::PreLto => {
|
tcx.dep_graph
|
||||||
return reuse_workproduct_for_cgu(tcx, &*cgu, &mut work_products);
|
.with_task(
|
||||||
|
dep_node,
|
||||||
|
tcx,
|
||||||
|
(backend_config.clone(), global_asm_config.clone(), cgu.name()),
|
||||||
|
module_codegen,
|
||||||
|
Some(rustc_middle::dep_graph::hash_result),
|
||||||
|
)
|
||||||
|
.0
|
||||||
}
|
}
|
||||||
|
CguReuse::PreLto => reuse_workproduct_for_cgu(tcx, &*cgu),
|
||||||
CguReuse::PostLto => unreachable!(),
|
CguReuse::PostLto => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
||||||
let dep_node = cgu.codegen_dep_node(tcx);
|
|
||||||
let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task(
|
|
||||||
dep_node,
|
|
||||||
tcx,
|
|
||||||
(backend_config.clone(), cgu.name()),
|
|
||||||
module_codegen,
|
|
||||||
Some(rustc_middle::dep_graph::hash_result),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some((id, product)) = work_product {
|
|
||||||
work_products.insert(id, product);
|
|
||||||
}
|
|
||||||
|
|
||||||
module
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
});
|
});
|
||||||
|
|
||||||
tcx.sess.abort_if_errors();
|
tcx.sess.abort_if_errors();
|
||||||
|
|
||||||
let isa = crate::build_isa(tcx.sess, &backend_config);
|
let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
|
||||||
let mut allocator_module = make_module(tcx.sess, isa, "allocator_shim".to_string());
|
|
||||||
assert_eq!(pointer_ty(tcx), allocator_module.target_config().pointer_type());
|
|
||||||
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
|
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
|
||||||
let created_alloc_shim =
|
let created_alloc_shim =
|
||||||
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
|
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
|
||||||
|
|
||||||
let allocator_module = if created_alloc_shim {
|
let allocator_module = if created_alloc_shim {
|
||||||
let ModuleCodegenResult(module, work_product) = emit_module(
|
let mut product = allocator_module.finish();
|
||||||
tcx,
|
allocator_unwind_context.emit(&mut product);
|
||||||
&backend_config,
|
|
||||||
"allocator_shim".to_string(),
|
match emit_module(
|
||||||
|
tcx.output_filenames(()),
|
||||||
|
&tcx.sess.prof,
|
||||||
|
product.object,
|
||||||
ModuleKind::Allocator,
|
ModuleKind::Allocator,
|
||||||
allocator_module,
|
"allocator_shim".to_owned(),
|
||||||
None,
|
) {
|
||||||
allocator_unwind_context,
|
Ok(allocator_module) => Some(allocator_module),
|
||||||
);
|
Err(err) => tcx.sess.fatal(err),
|
||||||
if let Some((id, product)) = work_product {
|
|
||||||
work_products.insert(id, product);
|
|
||||||
}
|
}
|
||||||
Some(module)
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@ -308,102 +422,13 @@ pub(crate) fn run_aot(
|
|||||||
}
|
}
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
Box::new((
|
Box::new(OngoingCodegen {
|
||||||
CodegenResults {
|
modules,
|
||||||
modules,
|
allocator_module,
|
||||||
allocator_module,
|
metadata_module,
|
||||||
metadata_module,
|
metadata,
|
||||||
metadata,
|
crate_info: CrateInfo::new(tcx, target_cpu),
|
||||||
crate_info: CrateInfo::new(tcx, target_cpu),
|
})
|
||||||
},
|
|
||||||
work_products,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
|
|
||||||
use std::io::Write;
|
|
||||||
use std::process::{Command, Stdio};
|
|
||||||
|
|
||||||
if global_asm.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg!(not(feature = "inline_asm"))
|
|
||||||
|| tcx.sess.target.is_like_osx
|
|
||||||
|| tcx.sess.target.is_like_windows
|
|
||||||
{
|
|
||||||
if global_asm.contains("__rust_probestack") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME fix linker error on macOS
|
|
||||||
if cfg!(not(feature = "inline_asm")) {
|
|
||||||
tcx.sess.fatal(
|
|
||||||
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
tcx.sess.fatal("asm! and global_asm! are not yet supported on macOS and Windows");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let assembler = crate::toolchain::get_toolchain_binary(tcx.sess, "as");
|
|
||||||
let linker = crate::toolchain::get_toolchain_binary(tcx.sess, "ld");
|
|
||||||
|
|
||||||
// Remove all LLVM style comments
|
|
||||||
let global_asm = global_asm
|
|
||||||
.lines()
|
|
||||||
.map(|line| if let Some(index) = line.find("//") { &line[0..index] } else { line })
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
let output_object_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu_name));
|
|
||||||
|
|
||||||
// Assemble `global_asm`
|
|
||||||
let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
|
|
||||||
let mut child = Command::new(assembler)
|
|
||||||
.arg("-o")
|
|
||||||
.arg(&global_asm_object_file)
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.expect("Failed to spawn `as`.");
|
|
||||||
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
|
|
||||||
let status = child.wait().expect("Failed to wait for `as`.");
|
|
||||||
if !status.success() {
|
|
||||||
tcx.sess.fatal(&format!("Failed to assemble `{}`", global_asm));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Link the global asm and main object file together
|
|
||||||
let main_object_file = add_file_stem_postfix(output_object_file.clone(), ".main");
|
|
||||||
std::fs::rename(&output_object_file, &main_object_file).unwrap();
|
|
||||||
let status = Command::new(linker)
|
|
||||||
.arg("-r") // Create a new object file
|
|
||||||
.arg("-o")
|
|
||||||
.arg(output_object_file)
|
|
||||||
.arg(&main_object_file)
|
|
||||||
.arg(&global_asm_object_file)
|
|
||||||
.status()
|
|
||||||
.unwrap();
|
|
||||||
if !status.success() {
|
|
||||||
tcx.sess.fatal(&format!(
|
|
||||||
"Failed to link `{}` and `{}` together",
|
|
||||||
main_object_file.display(),
|
|
||||||
global_asm_object_file.display(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::fs::remove_file(global_asm_object_file).unwrap();
|
|
||||||
std::fs::remove_file(main_object_file).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {
|
|
||||||
let mut new_filename = path.file_stem().unwrap().to_owned();
|
|
||||||
new_filename.push(postfix);
|
|
||||||
if let Some(extension) = path.extension() {
|
|
||||||
new_filename.push(".");
|
|
||||||
new_filename.push(extension);
|
|
||||||
}
|
|
||||||
path.set_file_name(new_filename);
|
|
||||||
path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
|
// Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
|
||||||
|
@ -111,6 +111,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
&backend_config,
|
&backend_config,
|
||||||
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
|
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
|
||||||
);
|
);
|
||||||
|
let mut cached_context = Context::new();
|
||||||
|
|
||||||
let (_, cgus) = tcx.collect_and_partition_mono_items(());
|
let (_, cgus) = tcx.collect_and_partition_mono_items(());
|
||||||
let mono_items = cgus
|
let mono_items = cgus
|
||||||
@ -129,10 +130,17 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
CodegenMode::Aot => unreachable!(),
|
CodegenMode::Aot => unreachable!(),
|
||||||
CodegenMode::Jit => {
|
CodegenMode::Jit => {
|
||||||
cx.tcx.sess.time("codegen fn", || {
|
cx.tcx.sess.time("codegen fn", || {
|
||||||
crate::base::codegen_fn(&mut cx, &mut jit_module, inst)
|
crate::base::codegen_and_compile_fn(
|
||||||
|
&mut cx,
|
||||||
|
&mut cached_context,
|
||||||
|
&mut jit_module,
|
||||||
|
inst,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
CodegenMode::JitLazy => codegen_shim(&mut cx, &mut jit_module, inst),
|
CodegenMode::JitLazy => {
|
||||||
|
codegen_shim(&mut cx, &mut cached_context, &mut jit_module, inst)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
MonoItem::Static(def_id) => {
|
MonoItem::Static(def_id) => {
|
||||||
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
|
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
|
||||||
@ -259,7 +267,14 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
|
|||||||
false,
|
false,
|
||||||
Symbol::intern("dummy_cgu_name"),
|
Symbol::intern("dummy_cgu_name"),
|
||||||
);
|
);
|
||||||
tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, jit_module, instance));
|
tcx.sess.time("codegen fn", || {
|
||||||
|
crate::base::codegen_and_compile_fn(
|
||||||
|
&mut cx,
|
||||||
|
&mut Context::new(),
|
||||||
|
jit_module,
|
||||||
|
instance,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
assert!(cx.global_asm.is_empty());
|
assert!(cx.global_asm.is_empty());
|
||||||
jit_module.finalize_definitions();
|
jit_module.finalize_definitions();
|
||||||
@ -334,7 +349,12 @@ fn load_imported_symbols_for_jit(
|
|||||||
imported_symbols
|
imported_symbols
|
||||||
}
|
}
|
||||||
|
|
||||||
fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: Instance<'tcx>) {
|
fn codegen_shim<'tcx>(
|
||||||
|
cx: &mut CodegenCx<'tcx>,
|
||||||
|
cached_context: &mut Context,
|
||||||
|
module: &mut JITModule,
|
||||||
|
inst: Instance<'tcx>,
|
||||||
|
) {
|
||||||
let tcx = cx.tcx;
|
let tcx = cx.tcx;
|
||||||
|
|
||||||
let pointer_type = module.target_config().pointer_type();
|
let pointer_type = module.target_config().pointer_type();
|
||||||
@ -357,8 +377,9 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: In
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
cx.cached_context.clear();
|
let context = cached_context;
|
||||||
let trampoline = &mut cx.cached_context.func;
|
context.clear();
|
||||||
|
let trampoline = &mut context.func;
|
||||||
trampoline.signature = sig.clone();
|
trampoline.signature = sig.clone();
|
||||||
|
|
||||||
let mut builder_ctx = FunctionBuilderContext::new();
|
let mut builder_ctx = FunctionBuilderContext::new();
|
||||||
@ -381,5 +402,5 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: In
|
|||||||
let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec();
|
let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec();
|
||||||
trampoline_builder.ins().return_(&ret_vals);
|
trampoline_builder.ins().return_(&ret_vals);
|
||||||
|
|
||||||
module.define_function(func_id, &mut cx.cached_context).unwrap();
|
module.define_function(func_id, context).unwrap();
|
||||||
}
|
}
|
||||||
|
114
src/global_asm.rs
Normal file
114
src/global_asm.rs
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
||||||
|
//! standalone executable.
|
||||||
|
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
||||||
|
use rustc_hir::ItemId;
|
||||||
|
use rustc_session::config::{OutputFilenames, OutputType};
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String, item_id: ItemId) {
|
||||||
|
let item = tcx.hir().item(item_id);
|
||||||
|
if let rustc_hir::ItemKind::GlobalAsm(asm) = item.kind {
|
||||||
|
if !asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
|
||||||
|
global_asm.push_str("\n.intel_syntax noprefix\n");
|
||||||
|
} else {
|
||||||
|
global_asm.push_str("\n.att_syntax\n");
|
||||||
|
}
|
||||||
|
for piece in asm.template {
|
||||||
|
match *piece {
|
||||||
|
InlineAsmTemplatePiece::String(ref s) => global_asm.push_str(s),
|
||||||
|
InlineAsmTemplatePiece::Placeholder { .. } => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
global_asm.push_str("\n.att_syntax\n\n");
|
||||||
|
} else {
|
||||||
|
bug!("Expected GlobalAsm found {:?}", item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct GlobalAsmConfig {
|
||||||
|
asm_enabled: bool,
|
||||||
|
assembler: PathBuf,
|
||||||
|
pub(crate) output_filenames: Arc<OutputFilenames>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GlobalAsmConfig {
|
||||||
|
pub(crate) fn new(tcx: TyCtxt<'_>) -> Self {
|
||||||
|
let asm_enabled = cfg!(feature = "inline_asm") && !tcx.sess.target.is_like_windows;
|
||||||
|
|
||||||
|
GlobalAsmConfig {
|
||||||
|
asm_enabled,
|
||||||
|
assembler: crate::toolchain::get_toolchain_binary(tcx.sess, "as"),
|
||||||
|
output_filenames: tcx.output_filenames(()).clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn compile_global_asm(
|
||||||
|
config: &GlobalAsmConfig,
|
||||||
|
cgu_name: &str,
|
||||||
|
global_asm: &str,
|
||||||
|
) -> Result<Option<PathBuf>, String> {
|
||||||
|
if global_asm.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !config.asm_enabled {
|
||||||
|
if global_asm.contains("__rust_probestack") {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME fix linker error on macOS
|
||||||
|
if cfg!(not(feature = "inline_asm")) {
|
||||||
|
return Err(
|
||||||
|
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift"
|
||||||
|
.to_owned(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Err("asm! and global_asm! are not yet supported on Windows".to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all LLVM style comments
|
||||||
|
let global_asm = global_asm
|
||||||
|
.lines()
|
||||||
|
.map(|line| if let Some(index) = line.find("//") { &line[0..index] } else { line })
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
let output_object_file = config.output_filenames.temp_path(OutputType::Object, Some(cgu_name));
|
||||||
|
|
||||||
|
// Assemble `global_asm`
|
||||||
|
let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
|
||||||
|
let mut child = Command::new(&config.assembler)
|
||||||
|
.arg("-o")
|
||||||
|
.arg(&global_asm_object_file)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to spawn `as`.");
|
||||||
|
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
|
||||||
|
let status = child.wait().expect("Failed to wait for `as`.");
|
||||||
|
if !status.success() {
|
||||||
|
return Err(format!("Failed to assemble `{}`", global_asm));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(global_asm_object_file))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {
|
||||||
|
let mut new_filename = path.file_stem().unwrap().to_owned();
|
||||||
|
new_filename.push(postfix);
|
||||||
|
if let Some(extension) = path.extension() {
|
||||||
|
new_filename.push(".");
|
||||||
|
new_filename.push(extension);
|
||||||
|
}
|
||||||
|
path.set_file_name(new_filename);
|
||||||
|
path
|
||||||
|
}
|
35
src/lib.rs
35
src/lib.rs
@ -25,7 +25,7 @@ extern crate rustc_target;
|
|||||||
extern crate rustc_driver;
|
extern crate rustc_driver;
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::cell::Cell;
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
||||||
use rustc_codegen_ssa::traits::CodegenBackend;
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
||||||
use rustc_codegen_ssa::CodegenResults;
|
use rustc_codegen_ssa::CodegenResults;
|
||||||
@ -56,6 +56,7 @@ mod constant;
|
|||||||
mod debuginfo;
|
mod debuginfo;
|
||||||
mod discriminant;
|
mod discriminant;
|
||||||
mod driver;
|
mod driver;
|
||||||
|
mod global_asm;
|
||||||
mod inline_asm;
|
mod inline_asm;
|
||||||
mod intrinsics;
|
mod intrinsics;
|
||||||
mod linkage;
|
mod linkage;
|
||||||
@ -123,7 +124,6 @@ struct CodegenCx<'tcx> {
|
|||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
global_asm: String,
|
global_asm: String,
|
||||||
inline_asm_index: Cell<usize>,
|
inline_asm_index: Cell<usize>,
|
||||||
cached_context: Context,
|
|
||||||
debug_context: Option<DebugContext<'tcx>>,
|
debug_context: Option<DebugContext<'tcx>>,
|
||||||
unwind_context: UnwindContext,
|
unwind_context: UnwindContext,
|
||||||
cgu_name: Symbol,
|
cgu_name: Symbol,
|
||||||
@ -150,7 +150,6 @@ impl<'tcx> CodegenCx<'tcx> {
|
|||||||
tcx,
|
tcx,
|
||||||
global_asm: String::new(),
|
global_asm: String::new(),
|
||||||
inline_asm_index: Cell::new(0),
|
inline_asm_index: Cell::new(0),
|
||||||
cached_context: Context::new(),
|
|
||||||
debug_context,
|
debug_context,
|
||||||
unwind_context,
|
unwind_context,
|
||||||
cgu_name,
|
cgu_name,
|
||||||
@ -159,7 +158,7 @@ impl<'tcx> CodegenCx<'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct CraneliftCodegenBackend {
|
pub struct CraneliftCodegenBackend {
|
||||||
pub config: Option<BackendConfig>,
|
pub config: RefCell<Option<BackendConfig>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodegenBackend for CraneliftCodegenBackend {
|
impl CodegenBackend for CraneliftCodegenBackend {
|
||||||
@ -169,6 +168,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||||||
Lto::No | Lto::ThinLocal => {}
|
Lto::No | Lto::ThinLocal => {}
|
||||||
Lto::Thin | Lto::Fat => sess.warn("LTO is not supported. You may get a linker error."),
|
Lto::Thin | Lto::Fat => sess.warn("LTO is not supported. You may get a linker error."),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut config = self.config.borrow_mut();
|
||||||
|
if config.is_none() {
|
||||||
|
let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
|
||||||
|
.unwrap_or_else(|err| sess.fatal(&err));
|
||||||
|
*config = Some(new_config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
|
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
|
||||||
@ -186,15 +192,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||||||
need_metadata_module: bool,
|
need_metadata_module: bool,
|
||||||
) -> Box<dyn Any> {
|
) -> Box<dyn Any> {
|
||||||
tcx.sess.abort_if_errors();
|
tcx.sess.abort_if_errors();
|
||||||
let config = if let Some(config) = self.config.clone() {
|
let config = self.config.borrow().clone().unwrap();
|
||||||
config
|
|
||||||
} else {
|
|
||||||
if !tcx.sess.unstable_options() && !tcx.sess.opts.cg.llvm_args.is_empty() {
|
|
||||||
tcx.sess.fatal("`-Z unstable-options` must be passed to allow configuring cg_clif");
|
|
||||||
}
|
|
||||||
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
|
|
||||||
.unwrap_or_else(|err| tcx.sess.fatal(&err))
|
|
||||||
};
|
|
||||||
match config.codegen_mode {
|
match config.codegen_mode {
|
||||||
CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
|
CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
|
||||||
CodegenMode::Jit | CodegenMode::JitLazy => {
|
CodegenMode::Jit | CodegenMode::JitLazy => {
|
||||||
@ -210,12 +208,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
|||||||
fn join_codegen(
|
fn join_codegen(
|
||||||
&self,
|
&self,
|
||||||
ongoing_codegen: Box<dyn Any>,
|
ongoing_codegen: Box<dyn Any>,
|
||||||
_sess: &Session,
|
sess: &Session,
|
||||||
_outputs: &OutputFilenames,
|
_outputs: &OutputFilenames,
|
||||||
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
|
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
|
||||||
Ok(*ongoing_codegen
|
Ok(ongoing_codegen
|
||||||
.downcast::<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)>()
|
.downcast::<driver::aot::OngoingCodegen>()
|
||||||
.unwrap())
|
.unwrap()
|
||||||
|
.join(sess, self.config.borrow().as_ref().unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn link(
|
fn link(
|
||||||
@ -312,5 +311,5 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Box<dyn isa::Tar
|
|||||||
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
||||||
Box::new(CraneliftCodegenBackend { config: None })
|
Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,8 @@ use rustc_session::Session;
|
|||||||
/// Tries to infer the path of a binary for the target toolchain from the linker name.
|
/// Tries to infer the path of a binary for the target toolchain from the linker name.
|
||||||
pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
|
pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
|
||||||
let (mut linker, _linker_flavor) = linker_and_flavor(sess);
|
let (mut linker, _linker_flavor) = linker_and_flavor(sess);
|
||||||
let linker_file_name = linker
|
let linker_file_name =
|
||||||
.file_name()
|
linker.file_name().unwrap().to_str().expect("linker filename should be valid UTF-8");
|
||||||
.and_then(|name| name.to_str())
|
|
||||||
.unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));
|
|
||||||
|
|
||||||
if linker_file_name == "ld.lld" {
|
if linker_file_name == "ld.lld" {
|
||||||
if tool != "ld" {
|
if tool != "ld" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user