Skip to content

Commit

Permalink
Also emit vcode when emitting clif ir while using new style backends
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 14, 2020
1 parent cd21269 commit 44b3310
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
10 changes: 10 additions & 0 deletions src/base.rs
Expand Up @@ -118,6 +118,8 @@ pub(crate) fn codegen_fn<'tcx>(
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
context.dce(cx.module.isa()).unwrap();

context.want_disasm = crate::pretty_clif::should_write_ir(tcx);

// Define function
let module = &mut cx.module;
tcx.sess.time("define function", || {
Expand All @@ -140,6 +142,14 @@ pub(crate) fn codegen_fn<'tcx>(
&clif_comments,
);

if let Some(mach_compile_result) = &context.mach_compile_result {
if let Some(disasm) = &mach_compile_result.disasm {
crate::pretty_clif::write_ir_file(tcx, &format!("{}.vcode", tcx.symbol_name(instance).name), |file| {
file.write_all(disasm.as_bytes())
})
}
}

// Define debuginfo for function
let isa = cx.module.isa();
let debug_context = &mut cx.debug_context;
Expand Down
96 changes: 54 additions & 42 deletions src/pretty_clif.rs
Expand Up @@ -53,6 +53,7 @@
//! ```

use std::fmt;
use std::io::Write;

use cranelift_codegen::{
entity::SecondaryMap,
Expand Down Expand Up @@ -200,32 +201,24 @@ impl<M: Module> FunctionCx<'_, '_, M> {
}
}

pub(crate) fn write_clif_file<'tcx>(
tcx: TyCtxt<'tcx>,
postfix: &str,
isa: Option<&dyn cranelift_codegen::isa::TargetIsa>,
instance: Instance<'tcx>,
context: &cranelift_codegen::Context,
mut clif_comments: &CommentWriter,
) {
use std::io::Write;

if !cfg!(debug_assertions)
&& !tcx
pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool {
cfg!(debug_assertions)
|| tcx
.sess
.opts
.output_types
.contains_key(&OutputType::LlvmAssembly)
{
}

pub(crate) fn write_ir_file<'tcx>(
tcx: TyCtxt<'tcx>,
name: &str,
write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>,
) {
if !should_write_ir(tcx) {
return;
}

let value_ranges = isa.map(|isa| {
context
.build_value_labels_ranges(isa)
.expect("value location ranges")
});

let clif_output_dir = tcx.output_filenames(LOCAL_CRATE).with_extension("clif");

match std::fs::create_dir(&clif_output_dir) {
Expand All @@ -234,39 +227,58 @@ pub(crate) fn write_clif_file<'tcx>(
res @ Err(_) => res.unwrap(),
}

let clif_file_name = clif_output_dir.join(format!(
let clif_file_name = clif_output_dir.join(name);

let res: std::io::Result<()> = try {
let mut file = std::fs::File::create(clif_file_name)?;
write(&mut file)?;
};
if let Err(err) = res {
tcx.sess.warn(&format!("error writing ir file: {}", err));
}
}

pub(crate) fn write_clif_file<'tcx>(
tcx: TyCtxt<'tcx>,
postfix: &str,
isa: Option<&dyn cranelift_codegen::isa::TargetIsa>,
instance: Instance<'tcx>,
context: &cranelift_codegen::Context,
mut clif_comments: &CommentWriter,
) {
write_ir_file(tcx, &format!(
"{}.{}.clif",
tcx.symbol_name(instance).name,
postfix
));
), |file| {
let value_ranges = isa.map(|isa| {
context
.build_value_labels_ranges(isa)
.expect("value location ranges")
});

let mut clif = String::new();
cranelift_codegen::write::decorate_function(
&mut clif_comments,
&mut clif,
&context.func,
&DisplayFunctionAnnotations {
isa: Some(&*crate::build_isa(
tcx.sess, true, /* PIC doesn't matter here */
)),
value_ranges: value_ranges.as_ref(),
},
)
.unwrap();
let mut clif = String::new();
cranelift_codegen::write::decorate_function(
&mut clif_comments,
&mut clif,
&context.func,
&DisplayFunctionAnnotations {
isa: Some(&*crate::build_isa(
tcx.sess, true, /* PIC doesn't matter here */
)),
value_ranges: value_ranges.as_ref(),
},
)
.unwrap();

let res: std::io::Result<()> = try {
let mut file = std::fs::File::create(clif_file_name)?;
let target_triple = crate::target_triple(tcx.sess);
writeln!(file, "test compile")?;
writeln!(file, "set is_pic")?;
writeln!(file, "set enable_simd")?;
writeln!(file, "target {} haswell", target_triple)?;
writeln!(file, "target {} haswell", crate::target_triple(tcx.sess))?;
writeln!(file)?;
file.write_all(clif.as_bytes())?;
};
if let Err(err) = res {
tcx.sess.warn(&format!("err writing clif file: {}", err));
}
Ok(())
});
}

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

0 comments on commit 44b3310

Please sign in to comment.