@@ -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 (
@@ -744,31 +747,6 @@ pub(crate) unsafe fn codegen(
744
747
create_msvc_imps ( cgcx, llcx, llmod) ;
745
748
}
746
749
747
- // A codegen-specific pass manager is used to generate object
748
- // files for an LLVM module.
749
- //
750
- // Apparently each of these pass managers is a one-shot kind of
751
- // thing, so we create a new one for each type of output. The
752
- // pass manager passed to the closure should be ensured to not
753
- // escape the closure itself, and the manager should only be
754
- // used once.
755
- unsafe fn with_codegen < ' ll , F , R > (
756
- tm : & ' ll llvm:: TargetMachine ,
757
- llmod : & ' ll llvm:: Module ,
758
- no_builtins : bool ,
759
- f : F ,
760
- ) -> R
761
- where
762
- F : FnOnce ( & ' ll mut PassManager < ' ll > ) -> R ,
763
- {
764
- unsafe {
765
- let cpm = llvm:: LLVMCreatePassManager ( ) ;
766
- llvm:: LLVMAddAnalysisPasses ( tm, cpm) ;
767
- llvm:: LLVMRustAddLibraryInfo ( cpm, llmod, no_builtins) ;
768
- f ( cpm)
769
- }
770
- }
771
-
772
750
// Two things to note:
773
751
// - If object files are just LLVM bitcode we write bitcode, copy it to
774
752
// the .o file, and delete the bitcode if it wasn't otherwise
@@ -890,21 +868,17 @@ pub(crate) unsafe fn codegen(
890
868
} else {
891
869
llmod
892
870
} ;
893
- unsafe {
894
- with_codegen ( tm, llmod, config. no_builtins , |cpm| {
895
- write_output_file (
896
- dcx,
897
- tm,
898
- cpm,
899
- llmod,
900
- & path,
901
- None ,
902
- llvm:: FileType :: AssemblyFile ,
903
- & cgcx. prof ,
904
- config. verify_llvm_ir ,
905
- )
906
- } ) ?;
907
- }
871
+ write_output_file (
872
+ dcx,
873
+ tm,
874
+ config. no_builtins ,
875
+ llmod,
876
+ & path,
877
+ None ,
878
+ llvm:: FileType :: AssemblyFile ,
879
+ & cgcx. prof ,
880
+ config. verify_llvm_ir ,
881
+ ) ?;
908
882
}
909
883
910
884
match config. emit_obj {
@@ -928,21 +902,17 @@ pub(crate) unsafe fn codegen(
928
902
( _, SplitDwarfKind :: Split ) => Some ( dwo_out. as_path ( ) ) ,
929
903
} ;
930
904
931
- unsafe {
932
- with_codegen ( tm, llmod, config. no_builtins , |cpm| {
933
- write_output_file (
934
- dcx,
935
- tm,
936
- cpm,
937
- llmod,
938
- & obj_out,
939
- dwo_out,
940
- llvm:: FileType :: ObjectFile ,
941
- & cgcx. prof ,
942
- config. verify_llvm_ir ,
943
- )
944
- } ) ?;
945
- }
905
+ write_output_file (
906
+ dcx,
907
+ tm,
908
+ config. no_builtins ,
909
+ llmod,
910
+ & obj_out,
911
+ dwo_out,
912
+ llvm:: FileType :: ObjectFile ,
913
+ & cgcx. prof ,
914
+ config. verify_llvm_ir ,
915
+ ) ?;
946
916
}
947
917
948
918
EmitObj :: Bitcode => {
0 commit comments