add a "raw" function definition interface to cranelift-module (#1400)

* move trap site definitions into cranelift-module

`cranelift-faerie` and `cranelift-object` already have identical
definitions of structures to represent trap sites.  We might as well
merge them ahead of work to define functions via a raw slice of bytes
with associated traps, which will need some kind of common structure for
representing traps anyway.

* cranelift-module: add `define_function_bytes` interface

This interface is useful when the client needs to precisely specify the
ordering of bytes in a particular function.

* add comment about saving files for `perf`
This commit is contained in:
Nathan Froyd
2020-02-21 18:14:37 -05:00
committed by GitHub
parent c5d6805284
commit 09c6c5db44
10 changed files with 188 additions and 42 deletions

View File

@@ -10,9 +10,10 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, binemit, ir};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleError,
ModuleNamespace, ModuleResult,
ModuleNamespace, ModuleResult, TrapSite,
};
use faerie;
use std::convert::TryInto;
use std::fs::File;
use target_lexicon::Triple;
@@ -200,6 +201,31 @@ impl Backend for FaerieBackend {
Ok(FaerieCompiledFunction { code_length })
}
fn define_function_bytes(
&mut self,
_id: FuncId,
name: &str,
bytes: &[u8],
_namespace: &ModuleNamespace<Self>,
traps: Vec<TrapSite>,
) -> ModuleResult<FaerieCompiledFunction> {
let code_length: u32 = match bytes.len().try_into() {
Ok(code_length) => code_length,
_ => Err(ModuleError::FunctionTooLarge(name.to_string()))?,
};
if let Some(ref mut trap_manifest) = self.trap_manifest {
let trap_sink = FaerieTrapSink::new_with_sites(name, code_length, traps);
trap_manifest.add_sink(trap_sink);
}
self.artifact
.define(name, bytes.to_vec())
.expect("inconsistent declaration");
Ok(FaerieCompiledFunction { code_length })
}
fn define_data(
&mut self,
_id: DataId,

View File

@@ -2,17 +2,7 @@
//! for every function in the module. This data may be useful at runtime.
use cranelift_codegen::{binemit, ir};
/// Record of the arguments cranelift passes to `TrapSink::trap`
#[derive(Debug)]
pub struct FaerieTrapSite {
/// Offset into function
pub offset: binemit::CodeOffset,
/// Source location given to cranelift
pub srcloc: ir::SourceLoc,
/// Trap code, as determined by cranelift
pub code: ir::TrapCode,
}
use cranelift_module::TrapSite;
/// Record of the trap sites for a given function
#[derive(Debug)]
@@ -22,7 +12,7 @@ pub struct FaerieTrapSink {
/// Total code size of function
pub code_size: u32,
/// All trap sites collected in function
pub sites: Vec<FaerieTrapSite>,
pub sites: Vec<TrapSite>,
}
impl FaerieTrapSink {
@@ -34,11 +24,20 @@ impl FaerieTrapSink {
code_size,
}
}
/// Create a `FaerieTrapSink` pre-populated with `traps`
pub fn new_with_sites(name: &str, code_size: u32, traps: Vec<TrapSite>) -> Self {
Self {
sites: traps,
name: name.to_owned(),
code_size,
}
}
}
impl binemit::TrapSink for FaerieTrapSink {
fn trap(&mut self, offset: binemit::CodeOffset, srcloc: ir::SourceLoc, code: ir::TrapCode) {
self.sites.push(FaerieTrapSite {
self.sites.push(TrapSite {
offset,
srcloc,
code,