Skip to content

Commit 44b3310

Browse files
committed
Also emit vcode when emitting clif ir while using new style backends
1 parent cd21269 commit 44b3310

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

src/base.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub(crate) fn codegen_fn<'tcx>(
118118
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
119119
context.dce(cx.module.isa()).unwrap();
120120

121+
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
122+
121123
// Define function
122124
let module = &mut cx.module;
123125
tcx.sess.time("define function", || {
@@ -140,6 +142,14 @@ pub(crate) fn codegen_fn<'tcx>(
140142
&clif_comments,
141143
);
142144

145+
if let Some(mach_compile_result) = &context.mach_compile_result {
146+
if let Some(disasm) = &mach_compile_result.disasm {
147+
crate::pretty_clif::write_ir_file(tcx, &format!("{}.vcode", tcx.symbol_name(instance).name), |file| {
148+
file.write_all(disasm.as_bytes())
149+
})
150+
}
151+
}
152+
143153
// Define debuginfo for function
144154
let isa = cx.module.isa();
145155
let debug_context = &mut cx.debug_context;

src/pretty_clif.rs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
//! ```
5454
5555
use std::fmt;
56+
use std::io::Write;
5657

5758
use 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

272284
impl<M: Module> fmt::Debug for FunctionCx<'_, '_, M> {

0 commit comments

Comments
 (0)