@@ -40,7 +40,7 @@ use crate::errors::{
40
40
WithLlvmError , WriteBytecode ,
41
41
} ;
42
42
use crate :: llvm:: diagnostic:: OptimizationDiagnosticKind :: * ;
43
- use crate :: llvm:: { self , DiagnosticInfo , PassManager } ;
43
+ use crate :: llvm:: { self , DiagnosticInfo } ;
44
44
use crate :: type_:: Type ;
45
45
use crate :: { LlvmCodegenBackend , ModuleLlvm , base, common, llvm_util} ;
46
46
@@ -54,7 +54,7 @@ pub(crate) fn llvm_err<'a>(dcx: DiagCtxtHandle<'_>, err: LlvmError<'a>) -> Fatal
54
54
fn write_output_file < ' ll > (
55
55
dcx : DiagCtxtHandle < ' _ > ,
56
56
target : & ' ll llvm:: TargetMachine ,
57
- pm : & llvm :: PassManager < ' ll > ,
57
+ no_builtins : bool ,
58
58
m : & ' ll llvm:: Module ,
59
59
output : & Path ,
60
60
dwo_output : Option < & Path > ,
@@ -63,39 +63,42 @@ fn write_output_file<'ll>(
63
63
verify_llvm_ir : bool ,
64
64
) -> Result < ( ) , FatalError > {
65
65
debug ! ( "write_output_file output={:?} dwo_output={:?}" , output, dwo_output) ;
66
- unsafe {
67
- let output_c = path_to_c_string ( output) ;
68
- let dwo_output_c;
69
- let dwo_output_ptr = if let Some ( dwo_output) = dwo_output {
70
- dwo_output_c = path_to_c_string ( dwo_output) ;
71
- dwo_output_c. as_ptr ( )
72
- } else {
73
- std:: ptr:: null ( )
74
- } ;
75
- let result = llvm:: LLVMRustWriteOutputFile (
66
+ let output_c = path_to_c_string ( output) ;
67
+ let dwo_output_c;
68
+ let dwo_output_ptr = if let Some ( dwo_output) = dwo_output {
69
+ dwo_output_c = path_to_c_string ( dwo_output) ;
70
+ dwo_output_c. as_ptr ( )
71
+ } else {
72
+ std:: ptr:: null ( )
73
+ } ;
74
+ let result = unsafe {
75
+ let pm = llvm:: LLVMCreatePassManager ( ) ;
76
+ llvm:: LLVMAddAnalysisPasses ( target, pm) ;
77
+ llvm:: LLVMRustAddLibraryInfo ( pm, m, no_builtins) ;
78
+ llvm:: LLVMRustWriteOutputFile (
76
79
target,
77
80
pm,
78
81
m,
79
82
output_c. as_ptr ( ) ,
80
83
dwo_output_ptr,
81
84
file_type,
82
85
verify_llvm_ir,
83
- ) ;
86
+ )
87
+ } ;
84
88
85
- // Record artifact sizes for self-profiling
86
- if result == llvm:: LLVMRustResult :: Success {
87
- let artifact_kind = match file_type {
88
- llvm:: FileType :: ObjectFile => "object_file" ,
89
- llvm:: FileType :: AssemblyFile => "assembly_file" ,
90
- } ;
91
- record_artifact_size ( self_profiler_ref, artifact_kind, output) ;
92
- if let Some ( dwo_file) = dwo_output {
93
- record_artifact_size ( self_profiler_ref, "dwo_file" , dwo_file) ;
94
- }
89
+ // Record artifact sizes for self-profiling
90
+ if result == llvm:: LLVMRustResult :: Success {
91
+ let artifact_kind = match file_type {
92
+ llvm:: FileType :: ObjectFile => "object_file" ,
93
+ llvm:: FileType :: AssemblyFile => "assembly_file" ,
94
+ } ;
95
+ record_artifact_size ( self_profiler_ref, artifact_kind, output) ;
96
+ if let Some ( dwo_file) = dwo_output {
97
+ record_artifact_size ( self_profiler_ref, "dwo_file" , dwo_file) ;
95
98
}
96
-
97
- result. into_result ( ) . map_err ( |( ) | llvm_err ( dcx, LlvmError :: WriteOutput { path : output } ) )
98
99
}
100
+
101
+ result. into_result ( ) . map_err ( |( ) | llvm_err ( dcx, LlvmError :: WriteOutput { path : output } ) )
99
102
}
100
103
101
104
pub ( crate ) fn create_informational_target_machine (
@@ -755,31 +758,6 @@ pub(crate) unsafe fn codegen(
755
758
create_msvc_imps ( cgcx, llcx, llmod) ;
756
759
}
757
760
758
- // A codegen-specific pass manager is used to generate object
759
- // files for an LLVM module.
760
- //
761
- // Apparently each of these pass managers is a one-shot kind of
762
- // thing, so we create a new one for each type of output. The
763
- // pass manager passed to the closure should be ensured to not
764
- // escape the closure itself, and the manager should only be
765
- // used once.
766
- unsafe fn with_codegen < ' ll , F , R > (
767
- tm : & ' ll llvm:: TargetMachine ,
768
- llmod : & ' ll llvm:: Module ,
769
- no_builtins : bool ,
770
- f : F ,
771
- ) -> R
772
- where
773
- F : FnOnce ( & ' ll mut PassManager < ' ll > ) -> R ,
774
- {
775
- unsafe {
776
- let cpm = llvm:: LLVMCreatePassManager ( ) ;
777
- llvm:: LLVMAddAnalysisPasses ( tm, cpm) ;
778
- llvm:: LLVMRustAddLibraryInfo ( cpm, llmod, no_builtins) ;
779
- f ( cpm)
780
- }
781
- }
782
-
783
761
// Note that if object files are just LLVM bitcode we write bitcode,
784
762
// copy it to the .o file, and delete the bitcode if it wasn't
785
763
// otherwise requested.
@@ -898,21 +876,17 @@ pub(crate) unsafe fn codegen(
898
876
} else {
899
877
llmod
900
878
} ;
901
- unsafe {
902
- with_codegen ( tm, llmod, config. no_builtins , |cpm| {
903
- write_output_file (
904
- dcx,
905
- tm,
906
- cpm,
907
- llmod,
908
- & path,
909
- None ,
910
- llvm:: FileType :: AssemblyFile ,
911
- & cgcx. prof ,
912
- config. verify_llvm_ir ,
913
- )
914
- } ) ?;
915
- }
879
+ write_output_file (
880
+ dcx,
881
+ tm,
882
+ config. no_builtins ,
883
+ llmod,
884
+ & path,
885
+ None ,
886
+ llvm:: FileType :: AssemblyFile ,
887
+ & cgcx. prof ,
888
+ config. verify_llvm_ir ,
889
+ ) ?;
916
890
}
917
891
918
892
match config. emit_obj {
@@ -936,21 +910,17 @@ pub(crate) unsafe fn codegen(
936
910
( _, SplitDwarfKind :: Split ) => Some ( dwo_out. as_path ( ) ) ,
937
911
} ;
938
912
939
- unsafe {
940
- with_codegen ( tm, llmod, config. no_builtins , |cpm| {
941
- write_output_file (
942
- dcx,
943
- tm,
944
- cpm,
945
- llmod,
946
- & obj_out,
947
- dwo_out,
948
- llvm:: FileType :: ObjectFile ,
949
- & cgcx. prof ,
950
- config. verify_llvm_ir ,
951
- )
952
- } ) ?;
953
- }
913
+ write_output_file (
914
+ dcx,
915
+ tm,
916
+ config. no_builtins ,
917
+ llmod,
918
+ & obj_out,
919
+ dwo_out,
920
+ llvm:: FileType :: ObjectFile ,
921
+ & cgcx. prof ,
922
+ config. verify_llvm_ir ,
923
+ ) ?;
954
924
}
955
925
956
926
EmitObj :: Bitcode => {
0 commit comments