5353//! ```
5454
5555use std:: fmt;
56+ use std:: io:: Write ;
5657
5758use cranelift_codegen:: {
5859 entity:: SecondaryMap ,
@@ -200,32 +201,24 @@ impl<M: Module> FunctionCx<'_, '_, M> {
200201 }
201202}
202203
203- pub ( crate ) fn write_clif_file < ' tcx > (
204- tcx : TyCtxt < ' tcx > ,
205- postfix : & str ,
206- isa : Option < & dyn cranelift_codegen:: isa:: TargetIsa > ,
207- instance : Instance < ' tcx > ,
208- context : & cranelift_codegen:: Context ,
209- mut clif_comments : & CommentWriter ,
210- ) {
211- use std:: io:: Write ;
212-
213- if !cfg ! ( debug_assertions)
214- && !tcx
204+ pub ( crate ) fn should_write_ir ( tcx : TyCtxt < ' _ > ) -> bool {
205+ cfg ! ( debug_assertions)
206+ || tcx
215207 . sess
216208 . opts
217209 . output_types
218210 . contains_key ( & OutputType :: LlvmAssembly )
219- {
211+ }
212+
213+ pub ( crate ) fn write_ir_file < ' tcx > (
214+ tcx : TyCtxt < ' tcx > ,
215+ name : & str ,
216+ write : impl FnOnce ( & mut dyn Write ) -> std:: io:: Result < ( ) > ,
217+ ) {
218+ if !should_write_ir ( tcx) {
220219 return ;
221220 }
222221
223- let value_ranges = isa. map ( |isa| {
224- context
225- . build_value_labels_ranges ( isa)
226- . expect ( "value location ranges" )
227- } ) ;
228-
229222 let clif_output_dir = tcx. output_filenames ( LOCAL_CRATE ) . with_extension ( "clif" ) ;
230223
231224 match std:: fs:: create_dir ( & clif_output_dir) {
@@ -234,39 +227,58 @@ pub(crate) fn write_clif_file<'tcx>(
234227 res @ Err ( _) => res. unwrap ( ) ,
235228 }
236229
237- let clif_file_name = clif_output_dir. join ( format ! (
230+ let clif_file_name = clif_output_dir. join ( name) ;
231+
232+ let res: std:: io:: Result < ( ) > = try {
233+ let mut file = std:: fs:: File :: create ( clif_file_name) ?;
234+ write ( & mut file) ?;
235+ } ;
236+ if let Err ( err) = res {
237+ tcx. sess . warn ( & format ! ( "error writing ir file: {}" , err) ) ;
238+ }
239+ }
240+
241+ pub ( crate ) fn write_clif_file < ' tcx > (
242+ tcx : TyCtxt < ' tcx > ,
243+ postfix : & str ,
244+ isa : Option < & dyn cranelift_codegen:: isa:: TargetIsa > ,
245+ instance : Instance < ' tcx > ,
246+ context : & cranelift_codegen:: Context ,
247+ mut clif_comments : & CommentWriter ,
248+ ) {
249+ write_ir_file ( tcx, & format ! (
238250 "{}.{}.clif" ,
239251 tcx. symbol_name( instance) . name,
240252 postfix
241- ) ) ;
253+ ) , |file| {
254+ let value_ranges = isa. map ( |isa| {
255+ context
256+ . build_value_labels_ranges ( isa)
257+ . expect ( "value location ranges" )
258+ } ) ;
242259
243- let mut clif = String :: new ( ) ;
244- cranelift_codegen:: write:: decorate_function (
245- & mut clif_comments,
246- & mut clif,
247- & context. func ,
248- & DisplayFunctionAnnotations {
249- isa : Some ( & * crate :: build_isa (
250- tcx. sess , true , /* PIC doesn't matter here */
251- ) ) ,
252- value_ranges : value_ranges. as_ref ( ) ,
253- } ,
254- )
255- . unwrap ( ) ;
260+ let mut clif = String :: new ( ) ;
261+ cranelift_codegen:: write:: decorate_function (
262+ & mut clif_comments,
263+ & mut clif,
264+ & context. func ,
265+ & DisplayFunctionAnnotations {
266+ isa : Some ( & * crate :: build_isa (
267+ tcx. sess , true , /* PIC doesn't matter here */
268+ ) ) ,
269+ value_ranges : value_ranges. as_ref ( ) ,
270+ } ,
271+ )
272+ . unwrap ( ) ;
256273
257- let res: std:: io:: Result < ( ) > = try {
258- let mut file = std:: fs:: File :: create ( clif_file_name) ?;
259- let target_triple = crate :: target_triple ( tcx. sess ) ;
260274 writeln ! ( file, "test compile" ) ?;
261275 writeln ! ( file, "set is_pic" ) ?;
262276 writeln ! ( file, "set enable_simd" ) ?;
263- writeln ! ( file, "target {} haswell" , target_triple) ?;
277+ writeln ! ( file, "target {} haswell" , crate :: target_triple( tcx . sess ) ) ?;
264278 writeln ! ( file) ?;
265279 file. write_all ( clif. as_bytes ( ) ) ?;
266- } ;
267- if let Err ( err) = res {
268- tcx. sess . warn ( & format ! ( "err writing clif file: {}" , err) ) ;
269- }
280+ Ok ( ( ) )
281+ } ) ;
270282}
271283
272284impl < M : Module > fmt:: Debug for FunctionCx < ' _ , ' _ , M > {
0 commit comments