@@ -12,7 +12,8 @@ use crate::type_::Type;
1212use crate :: LlvmCodegenBackend ;
1313use crate :: ModuleLlvm ;
1414use rustc_codegen_ssa:: back:: write:: {
15- BitcodeSection , CodegenContext , EmitObj , ModuleConfig , TargetMachineFactoryFn ,
15+ BitcodeSection , CodegenContext , EmitObj , ModuleConfig , TargetMachineFactoryConfig ,
16+ TargetMachineFactoryFn ,
1617} ;
1718use rustc_codegen_ssa:: traits:: * ;
1819use rustc_codegen_ssa:: { CompiledModule , ModuleCodegen } ;
@@ -22,7 +23,9 @@ use rustc_fs_util::{link_or_copy, path_to_c_string};
2223use rustc_hir:: def_id:: LOCAL_CRATE ;
2324use rustc_middle:: bug;
2425use rustc_middle:: ty:: TyCtxt ;
25- use rustc_session:: config:: { self , Lto , OutputType , Passes , SanitizerSet , SwitchWithOptPath } ;
26+ use rustc_session:: config:: {
27+ self , Lto , OutputType , Passes , SanitizerSet , SplitDwarfKind , SwitchWithOptPath ,
28+ } ;
2629use rustc_session:: Session ;
2730use rustc_span:: symbol:: sym;
2831use rustc_span:: InnerSpan ;
@@ -51,18 +54,31 @@ pub fn write_output_file(
5154 pm : & llvm:: PassManager < ' ll > ,
5255 m : & ' ll llvm:: Module ,
5356 output : & Path ,
57+ dwo_output : Option < & Path > ,
5458 file_type : llvm:: FileType ,
5559) -> Result < ( ) , FatalError > {
5660 unsafe {
5761 let output_c = path_to_c_string ( output) ;
58- let result = llvm:: LLVMRustWriteOutputFile (
59- target,
60- pm,
61- m,
62- output_c. as_ptr ( ) ,
63- std:: ptr:: null ( ) ,
64- file_type,
65- ) ;
62+ let result = if let Some ( dwo_output) = dwo_output {
63+ let dwo_output_c = path_to_c_string ( dwo_output) ;
64+ llvm:: LLVMRustWriteOutputFile (
65+ target,
66+ pm,
67+ m,
68+ output_c. as_ptr ( ) ,
69+ dwo_output_c. as_ptr ( ) ,
70+ file_type,
71+ )
72+ } else {
73+ llvm:: LLVMRustWriteOutputFile (
74+ target,
75+ pm,
76+ m,
77+ output_c. as_ptr ( ) ,
78+ std:: ptr:: null ( ) ,
79+ file_type,
80+ )
81+ } ;
6682 result. into_result ( ) . map_err ( |( ) | {
6783 let msg = format ! ( "could not write output to {}" , output. display( ) ) ;
6884 llvm_err ( handler, & msg)
@@ -71,12 +87,17 @@ pub fn write_output_file(
7187}
7288
7389pub fn create_informational_target_machine ( sess : & Session ) -> & ' static mut llvm:: TargetMachine {
74- target_machine_factory ( sess, config:: OptLevel :: No ) ( )
90+ let config = TargetMachineFactoryConfig { split_dwarf_file : None } ;
91+ target_machine_factory ( sess, config:: OptLevel :: No ) ( config)
7592 . unwrap_or_else ( |err| llvm_err ( sess. diagnostic ( ) , & err) . raise ( ) )
7693}
7794
78- pub fn create_target_machine ( tcx : TyCtxt < ' _ > ) -> & ' static mut llvm:: TargetMachine {
79- target_machine_factory ( & tcx. sess , tcx. backend_optimization_level ( LOCAL_CRATE ) ) ( )
95+ pub fn create_target_machine ( tcx : TyCtxt < ' _ > , mod_name : & str ) -> & ' static mut llvm:: TargetMachine {
96+ let split_dwarf_file = tcx
97+ . output_filenames ( LOCAL_CRATE )
98+ . split_dwarf_file ( tcx. sess . opts . debugging_opts . split_dwarf , Some ( mod_name) ) ;
99+ let config = TargetMachineFactoryConfig { split_dwarf_file } ;
100+ target_machine_factory ( & tcx. sess , tcx. backend_optimization_level ( LOCAL_CRATE ) ) ( config)
80101 . unwrap_or_else ( |err| llvm_err ( tcx. sess . diagnostic ( ) , & err) . raise ( ) )
81102}
82103
@@ -172,8 +193,10 @@ pub fn target_machine_factory(
172193 let use_init_array =
173194 !sess. opts . debugging_opts . use_ctors_section . unwrap_or ( sess. target . use_ctors_section ) ;
174195
175- Arc :: new ( move || {
176- let split_dwarf_file = std:: ptr:: null ( ) ;
196+ Arc :: new ( move |config : TargetMachineFactoryConfig | {
197+ let split_dwarf_file = config. split_dwarf_file . unwrap_or_default ( ) ;
198+ let split_dwarf_file = CString :: new ( split_dwarf_file. to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
199+
177200 let tm = unsafe {
178201 llvm:: LLVMRustCreateTargetMachine (
179202 triple. as_ptr ( ) ,
@@ -192,7 +215,7 @@ pub fn target_machine_factory(
192215 emit_stack_size_section,
193216 relax_elf_relocations,
194217 use_init_array,
195- split_dwarf_file,
218+ split_dwarf_file. as_ptr ( ) ,
196219 )
197220 } ;
198221
@@ -796,7 +819,15 @@ pub(crate) unsafe fn codegen(
796819 llmod
797820 } ;
798821 with_codegen ( tm, llmod, config. no_builtins , |cpm| {
799- write_output_file ( diag_handler, tm, cpm, llmod, & path, llvm:: FileType :: AssemblyFile )
822+ write_output_file (
823+ diag_handler,
824+ tm,
825+ cpm,
826+ llmod,
827+ & path,
828+ None ,
829+ llvm:: FileType :: AssemblyFile ,
830+ )
800831 } ) ?;
801832 }
802833
@@ -805,13 +836,23 @@ pub(crate) unsafe fn codegen(
805836 let _timer = cgcx
806837 . prof
807838 . generic_activity_with_arg ( "LLVM_module_codegen_emit_obj" , & module. name [ ..] ) ;
839+
840+ let dwo_out = cgcx. output_filenames . temp_path_dwo ( module_name) ;
841+ let dwo_out = match cgcx. split_dwarf_kind {
842+ // Don't change how DWARF is emitted in single mode (or when disabled).
843+ SplitDwarfKind :: None | SplitDwarfKind :: Single => None ,
844+ // Emit (a subset of the) DWARF into a separate file in split mode.
845+ SplitDwarfKind :: Split => Some ( dwo_out. as_path ( ) ) ,
846+ } ;
847+
808848 with_codegen ( tm, llmod, config. no_builtins , |cpm| {
809849 write_output_file (
810850 diag_handler,
811851 tm,
812852 cpm,
813853 llmod,
814854 & obj_out,
855+ dwo_out,
815856 llvm:: FileType :: ObjectFile ,
816857 )
817858 } ) ?;
@@ -839,6 +880,7 @@ pub(crate) unsafe fn codegen(
839880
840881 Ok ( module. into_compiled_module (
841882 config. emit_obj != EmitObj :: None ,
883+ cgcx. split_dwarf_kind == SplitDwarfKind :: Split ,
842884 config. emit_bc ,
843885 & cgcx. output_filenames ,
844886 ) )
0 commit comments