3 use gccjit::OutputKind;
4 use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
5 use rustc_codegen_ssa::back::write::{CodegenContext, EmitObj, ModuleConfig};
6 use rustc_errors::Handler;
7 use rustc_session::config::OutputType;
8 use rustc_span::fatal_error::FatalError;
9 use rustc_target::spec::SplitDebuginfo;
11 use crate::{GccCodegenBackend, GccContext};
13 pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, module: ModuleCodegen<GccContext>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
14 let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
16 let context = &module.module_llvm.context;
18 let module_name = module.name.clone();
19 let module_name = Some(&module_name[..]);
21 let _bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
22 let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
24 if config.bitcode_needed() {
35 .generic_activity_with_arg("LLVM_module_codegen_emit_asm", &*module.name);
36 let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
37 context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
40 match config.emit_obj {
41 EmitObj::ObjectCode(_) => {
44 .generic_activity_with_arg("LLVM_module_codegen_emit_obj", &*module.name);
45 if env::var("CG_GCCJIT_DUMP_MODULE_NAMES").as_deref() == Ok("1") {
46 println!("Module {}", module.name);
48 if env::var("CG_GCCJIT_DUMP_ALL_MODULES").as_deref() == Ok("1") || env::var("CG_GCCJIT_DUMP_MODULE").as_deref() == Ok(&module.name) {
49 println!("Dumping reproducer {}", module.name);
50 let _ = fs::create_dir("/tmp/reproducers");
51 // FIXME(antoyo): segfault in dump_reproducer_to_file() might be caused by
52 // transmuting an rvalue to an lvalue.
53 // Segfault is actually in gcc::jit::reproducer::get_identifier_as_lvalue
54 context.dump_reproducer_to_file(&format!("/tmp/reproducers/{}.c", module.name));
55 println!("Dumped reproducer {}", module.name);
57 if env::var("CG_GCCJIT_DUMP_TO_FILE").as_deref() == Ok("1") {
58 let _ = fs::create_dir("/tmp/gccjit_dumps");
59 let path = &format!("/tmp/gccjit_dumps/{}.c", module.name);
60 context.dump_to_file(path, true);
62 context.compile_to_file(OutputKind::ObjectFile, obj_out.to_str().expect("path to str"));
73 Ok(module.into_compiled_module(
74 config.emit_obj != EmitObj::None,
75 cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
77 &cgcx.output_filenames,
81 pub(crate) fn link(_cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, mut _modules: Vec<ModuleCodegen<GccContext>>) -> Result<ModuleCodegen<GccContext>, FatalError> {