53
53
//! ```
54
54
55
55
use std:: fmt;
56
+ use std:: io:: Write ;
56
57
57
58
use cranelift_codegen:: {
58
59
entity:: SecondaryMap ,
@@ -200,32 +201,24 @@ impl<M: Module> FunctionCx<'_, '_, M> {
200
201
}
201
202
}
202
203
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
215
207
. sess
216
208
. opts
217
209
. output_types
218
210
. 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) {
220
219
return ;
221
220
}
222
221
223
- let value_ranges = isa. map ( |isa| {
224
- context
225
- . build_value_labels_ranges ( isa)
226
- . expect ( "value location ranges" )
227
- } ) ;
228
-
229
222
let clif_output_dir = tcx. output_filenames ( LOCAL_CRATE ) . with_extension ( "clif" ) ;
230
223
231
224
match std:: fs:: create_dir ( & clif_output_dir) {
@@ -234,39 +227,58 @@ pub(crate) fn write_clif_file<'tcx>(
234
227
res @ Err ( _) => res. unwrap ( ) ,
235
228
}
236
229
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 ! (
238
250
"{}.{}.clif" ,
239
251
tcx. symbol_name( instance) . name,
240
252
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
+ } ) ;
242
259
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 ( ) ;
256
273
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 ) ;
260
274
writeln ! ( file, "test compile" ) ?;
261
275
writeln ! ( file, "set is_pic" ) ?;
262
276
writeln ! ( file, "set enable_simd" ) ?;
263
- writeln ! ( file, "target {} haswell" , target_triple) ?;
277
+ writeln ! ( file, "target {} haswell" , crate :: target_triple( tcx . sess ) ) ?;
264
278
writeln ! ( file) ?;
265
279
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
+ } ) ;
270
282
}
271
283
272
284
impl < M : Module > fmt:: Debug for FunctionCx < ' _ , ' _ , M > {
0 commit comments