@@ -17,6 +17,7 @@ use rustc_codegen_ssa::back::write::{
1717} ;
1818use rustc_codegen_ssa:: traits:: * ;
1919use rustc_codegen_ssa:: { CompiledModule , ModuleCodegen } ;
20+ use rustc_data_structures:: profiling:: SelfProfilerRef ;
2021use rustc_data_structures:: small_c_str:: SmallCStr ;
2122use rustc_errors:: { FatalError , Handler , Level } ;
2223use rustc_fs_util:: { link_or_copy, path_to_c_string} ;
@@ -53,6 +54,7 @@ pub fn write_output_file(
5354 output : & Path ,
5455 dwo_output : Option < & Path > ,
5556 file_type : llvm:: FileType ,
57+ self_profiler_ref : & SelfProfilerRef ,
5658) -> Result < ( ) , FatalError > {
5759 unsafe {
5860 let output_c = path_to_c_string ( output) ;
@@ -76,6 +78,19 @@ pub fn write_output_file(
7678 file_type,
7779 )
7880 } ;
81+
82+ // Record artifact sizes for self-profiling
83+ if result == llvm:: LLVMRustResult :: Success {
84+ let artifact_kind = match file_type {
85+ llvm:: FileType :: ObjectFile => "object_file" ,
86+ llvm:: FileType :: AssemblyFile => "assembly_file" ,
87+ } ;
88+ record_artifact_size ( self_profiler_ref, artifact_kind, output) ;
89+ if let Some ( dwo_file) = dwo_output {
90+ record_artifact_size ( self_profiler_ref, "dwo_file" , dwo_file) ;
91+ }
92+ }
93+
7994 result. into_result ( ) . map_err ( |( ) | {
8095 let msg = format ! ( "could not write output to {}" , output. display( ) ) ;
8196 llvm_err ( handler, & msg)
@@ -752,6 +767,14 @@ pub(crate) unsafe fn codegen(
752767 let thin = ThinBuffer :: new ( llmod) ;
753768 let data = thin. data ( ) ;
754769
770+ if let Some ( bitcode_filename) = bc_out. file_name ( ) {
771+ cgcx. prof . artifact_size (
772+ "llvm_bitcode" ,
773+ bitcode_filename. to_string_lossy ( ) ,
774+ data. len ( ) as u64 ,
775+ ) ;
776+ }
777+
755778 if config. emit_bc || config. emit_obj == EmitObj :: Bitcode {
756779 let _timer = cgcx. prof . generic_activity_with_arg (
757780 "LLVM_module_codegen_emit_bitcode" ,
@@ -812,6 +835,11 @@ pub(crate) unsafe fn codegen(
812835 }
813836
814837 let result = llvm:: LLVMRustPrintModule ( llmod, out_c. as_ptr ( ) , demangle_callback) ;
838+
839+ if result == llvm:: LLVMRustResult :: Success {
840+ record_artifact_size ( & cgcx. prof , "llvm_ir" , & out) ;
841+ }
842+
815843 result. into_result ( ) . map_err ( |( ) | {
816844 let msg = format ! ( "failed to write LLVM IR to {}" , out. display( ) ) ;
817845 llvm_err ( diag_handler, & msg)
@@ -842,6 +870,7 @@ pub(crate) unsafe fn codegen(
842870 & path,
843871 None ,
844872 llvm:: FileType :: AssemblyFile ,
873+ & cgcx. prof ,
845874 )
846875 } ) ?;
847876 }
@@ -875,6 +904,7 @@ pub(crate) unsafe fn codegen(
875904 & obj_out,
876905 dwo_out,
877906 llvm:: FileType :: ObjectFile ,
907+ & cgcx. prof ,
878908 )
879909 } ) ?;
880910 }
@@ -1131,3 +1161,19 @@ fn create_msvc_imps(
11311161 symbol_name. starts_with ( b"__llvm_profile_" )
11321162 }
11331163}
1164+
1165+ fn record_artifact_size (
1166+ self_profiler_ref : & SelfProfilerRef ,
1167+ artifact_kind : & ' static str ,
1168+ path : & Path ,
1169+ ) {
1170+ // Don't stat the file if we are not going to record its size.
1171+ if !self_profiler_ref. enabled ( ) {
1172+ return ;
1173+ }
1174+
1175+ if let Some ( artifact_name) = path. file_name ( ) {
1176+ let file_size = std:: fs:: metadata ( path) . map ( |m| m. len ( ) ) . unwrap_or ( 0 ) ;
1177+ self_profiler_ref. artifact_size ( artifact_kind, artifact_name. to_string_lossy ( ) , file_size) ;
1178+ }
1179+ }
0 commit comments