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

@@ -1,6 +1,6 @@
//! Defines `ObjectBackend`.
use crate::traps::{ObjectTrapSink, ObjectTrapSite};
use crate::traps::ObjectTrapSink;
use cranelift_codegen::binemit::{
Addend, CodeOffset, NullStackmapSink, NullTrapSink, Reloc, RelocSink,
};
@@ -9,7 +9,7 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, binemit, ir};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleNamespace,
ModuleResult,
ModuleResult, TrapSite,
};
use object::write::{
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
@@ -79,7 +79,7 @@ pub struct ObjectBackend {
object: Object,
functions: SecondaryMap<FuncId, Option<SymbolId>>,
data_objects: SecondaryMap<DataId, Option<SymbolId>>,
traps: SecondaryMap<FuncId, Vec<ObjectTrapSite>>,
traps: SecondaryMap<FuncId, Vec<TrapSite>>,
relocs: Vec<SymbolRelocs>,
libcalls: HashMap<ir::LibCall, SymbolId>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
@@ -226,6 +226,23 @@ impl Backend for ObjectBackend {
Ok(ObjectCompiledFunction)
}
fn define_function_bytes(
&mut self,
func_id: FuncId,
_name: &str,
bytes: &[u8],
_namespace: &ModuleNamespace<Self>,
traps: Vec<TrapSite>,
) -> ModuleResult<ObjectCompiledFunction> {
let symbol = self.functions[func_id].unwrap();
let section = self.object.section_id(StandardSection::Text);
let _offset = self
.object
.add_symbol_data(symbol, section, bytes, self.function_alignment);
self.traps[func_id] = traps;
Ok(ObjectCompiledFunction)
}
fn define_data(
&mut self,
data_id: DataId,
@@ -462,7 +479,7 @@ pub struct ObjectProduct {
/// Symbol IDs for data objects (both declared and defined).
pub data_objects: SecondaryMap<DataId, Option<SymbolId>>,
/// Trap sites for defined functions.
pub traps: SecondaryMap<FuncId, Vec<ObjectTrapSite>>,
pub traps: SecondaryMap<FuncId, Vec<TrapSite>>,
}
impl ObjectProduct {

View File

@@ -29,7 +29,7 @@ mod backend;
mod traps;
pub use crate::backend::{ObjectBackend, ObjectBuilder, ObjectProduct, ObjectTrapCollection};
pub use crate::traps::{ObjectTrapSink, ObjectTrapSite};
pub use crate::traps::ObjectTrapSink;
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -2,28 +2,18 @@
//! 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(Clone)]
pub struct ObjectTrapSite {
/// 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(Default, Clone)]
pub struct ObjectTrapSink {
/// All trap sites collected in function
pub sites: Vec<ObjectTrapSite>,
pub sites: Vec<TrapSite>,
}
impl binemit::TrapSink for ObjectTrapSink {
fn trap(&mut self, offset: binemit::CodeOffset, srcloc: ir::SourceLoc, code: ir::TrapCode) {
self.sites.push(ObjectTrapSite {
self.sites.push(TrapSite {
offset,
srcloc,
code,