Remove sink arguments from compile_and_emit

The data can be accessed after the fact using context.mach_compile_result
This commit is contained in:
bjorn3
2022-01-11 18:17:29 +01:00
parent 55d722db05
commit b803514d55
13 changed files with 155 additions and 138 deletions

View File

@@ -9,7 +9,7 @@
//! contexts concurrently. Typically, you would have one context per compilation thread and only a
//! single ISA instance.
use crate::binemit::{CodeInfo, RelocSink, StackMapSink, TrapSink};
use crate::binemit::CodeInfo;
use crate::dce::do_dce;
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
@@ -18,7 +18,7 @@ use crate::isa::TargetIsa;
use crate::legalizer::simple_legalize;
use crate::licm::do_licm;
use crate::loop_analysis::LoopAnalysis;
use crate::machinst::{MachCompileResult, MachReloc, MachStackMap, MachTrap};
use crate::machinst::MachCompileResult;
use crate::nan_canonicalization::do_nan_canonicalization;
use crate::remove_constant_phis::do_remove_constant_phis;
use crate::result::CodegenResult;
@@ -111,16 +111,11 @@ impl Context {
&mut self,
isa: &dyn TargetIsa,
mem: &mut Vec<u8>,
relocs: &mut dyn RelocSink,
traps: &mut dyn TrapSink,
stack_maps: &mut dyn StackMapSink,
) -> CodegenResult<()> {
let info = self.compile(isa)?;
let old_len = mem.len();
mem.resize(old_len + info.total_size as usize, 0);
let new_info = unsafe {
self.emit_to_memory(mem.as_mut_ptr().add(old_len), relocs, traps, stack_maps)
};
let new_info = unsafe { self.emit_to_memory(mem.as_mut_ptr().add(old_len)) };
debug_assert!(new_info == info);
Ok(())
}
@@ -187,13 +182,7 @@ impl Context {
///
/// Returns information about the emitted code and data.
#[deny(unsafe_op_in_unsafe_fn)]
pub unsafe fn emit_to_memory(
&self,
mem: *mut u8,
relocs: &mut dyn RelocSink,
traps: &mut dyn TrapSink,
stack_maps: &mut dyn StackMapSink,
) -> CodeInfo {
pub unsafe fn emit_to_memory(&self, mem: *mut u8) -> CodeInfo {
let _tt = timing::binemit();
let result = self
.mach_compile_result
@@ -204,32 +193,6 @@ impl Context {
let mem = unsafe { std::slice::from_raw_parts_mut(mem, info.total_size as usize) };
mem.copy_from_slice(result.buffer.data());
for &MachReloc {
offset,
srcloc,
kind,
ref name,
addend,
} in result.buffer.relocs()
{
relocs.reloc_external(offset, srcloc, kind, name, addend);
}
for &MachTrap {
offset,
srcloc,
code,
} in result.buffer.traps()
{
traps.trap(offset, srcloc, code);
}
for &MachStackMap {
offset_end,
ref stack_map,
..
} in result.buffer.stack_maps()
{
stack_maps.add_stack_map(offset_end, stack_map.clone());
}
info
}

View File

@@ -88,7 +88,7 @@ pub mod verifier;
pub mod write;
pub use crate::entity::packed_option;
pub use crate::machinst::buffer::{MachCallSite, MachReloc, MachSrcLoc, MachTrap};
pub use crate::machinst::buffer::{MachCallSite, MachReloc, MachSrcLoc, MachStackMap, MachTrap};
pub use crate::machinst::TextSectionBuilder;
mod bitset;

View File

@@ -1,6 +1,5 @@
//! Provides functionality for compiling and running CLIF IR for `run` tests.
use core::mem;
use cranelift_codegen::binemit::{NullRelocSink, NullStackMapSink, NullTrapSink};
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::{condcodes::IntCC, Function, InstBuilder, Signature};
use cranelift_codegen::isa::TargetIsa;
@@ -241,14 +240,11 @@ fn compile(function: Function, isa: &dyn TargetIsa) -> Result<Mmap, CompilationE
context.func = function;
// Compile and encode the result to machine code.
let relocs = &mut NullRelocSink {};
let traps = &mut NullTrapSink {};
let stack_maps = &mut NullStackMapSink {};
let code_info = context.compile(isa)?;
let mut code_page = MmapMut::map_anon(code_info.total_size as usize)?;
unsafe {
context.emit_to_memory(code_page.as_mut_ptr(), relocs, traps, stack_maps);
context.emit_to_memory(code_page.as_mut_ptr());
};
let code_page = code_page.make_exec()?;

View File

@@ -1,5 +1,4 @@
use cranelift::prelude::*;
use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink};
use cranelift_codegen::settings::{self, Configurable};
use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{default_libcall_names, Linkage, Module};
@@ -48,11 +47,7 @@ fn main() {
bcx.seal_all_blocks();
bcx.finalize();
}
let mut trap_sink = NullTrapSink {};
let mut stack_map_sink = NullStackMapSink {};
module
.define_function(func_a, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_a, &mut ctx).unwrap();
module.clear_context(&mut ctx);
ctx.func.signature = sig_b;
@@ -74,9 +69,7 @@ fn main() {
bcx.seal_all_blocks();
bcx.finalize();
}
module
.define_function(func_b, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_b, &mut ctx).unwrap();
module.clear_context(&mut ctx);
// Perform linking.

View File

@@ -3,9 +3,9 @@
use crate::{compiled_blob::CompiledBlob, memory::Memory};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::Configurable;
use cranelift_codegen::{self, ir, settings};
use cranelift_codegen::{self, ir, settings, MachReloc};
use cranelift_codegen::{
binemit::{Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMapSink, TrapSink},
binemit::{Addend, CodeInfo, CodeOffset, Reloc, RelocSink},
CodegenError,
};
use cranelift_entity::SecondaryMap;
@@ -648,8 +648,6 @@ impl Module for JITModule {
&mut self,
id: FuncId,
ctx: &mut cranelift_codegen::Context,
trap_sink: &mut dyn TrapSink,
stack_map_sink: &mut dyn StackMapSink,
) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {}: {}", id, ctx.func.display());
let decl = self.declarations.get_function_decl(id);
@@ -674,7 +672,17 @@ impl Module for JITModule {
.expect("TODO: handle OOM etc.");
let mut reloc_sink = JITRelocSink::default();
unsafe { ctx.emit_to_memory(ptr, &mut reloc_sink, trap_sink, stack_map_sink) };
unsafe { ctx.emit_to_memory(ptr) };
for &MachReloc {
offset,
srcloc,
kind,
ref name,
addend,
} in ctx.mach_compile_result.as_ref().unwrap().buffer.relocs()
{
reloc_sink.reloc_external(offset, srcloc, kind, name, addend);
}
self.record_function_for_perf(ptr, size, &decl.name);
self.compiled_functions[id] = Some(CompiledBlob {

View File

@@ -1,4 +1,3 @@
use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink};
use cranelift_codegen::ir::*;
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::settings::{self, Configurable};
@@ -56,11 +55,7 @@ fn define_simple_function(module: &mut JITModule) -> FuncId {
bcx.ins().return_(&[]);
}
let mut trap_sink = NullTrapSink {};
let mut stack_map_sink = NullStackMapSink {};
module
.define_function(func_id, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_id, &mut ctx).unwrap();
func_id
}
@@ -205,11 +200,7 @@ fn libcall_function() {
bcx.ins().return_(&[]);
}
let mut trap_sink = NullTrapSink {};
let mut stack_map_sink = NullStackMapSink {};
module
.define_function(func_id, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_id, &mut ctx).unwrap();
module.finalize_definitions();
}

View File

@@ -554,8 +554,6 @@ pub trait Module {
&mut self,
func: FuncId,
ctx: &mut Context,
trap_sink: &mut dyn binemit::TrapSink,
stack_map_sink: &mut dyn binemit::StackMapSink,
) -> ModuleResult<ModuleCompiledFunction>;
/// Define a function, taking the function body from the given `bytes`.
@@ -656,10 +654,8 @@ impl<M: Module> Module for &mut M {
&mut self,
func: FuncId,
ctx: &mut Context,
trap_sink: &mut dyn binemit::TrapSink,
stack_map_sink: &mut dyn binemit::StackMapSink,
) -> ModuleResult<ModuleCompiledFunction> {
(**self).define_function(func, ctx, trap_sink, stack_map_sink)
(**self).define_function(func, ctx)
}
fn define_function_bytes(

View File

@@ -3,9 +3,9 @@
use anyhow::anyhow;
use cranelift_codegen::entity::SecondaryMap;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, ir};
use cranelift_codegen::{self, ir, MachReloc};
use cranelift_codegen::{
binemit::{Addend, CodeOffset, Reloc, RelocSink, StackMapSink, TrapSink},
binemit::{Addend, CodeOffset, Reloc, RelocSink},
CodegenError,
};
use cranelift_module::{
@@ -307,20 +307,23 @@ impl Module for ObjectModule {
&mut self,
func_id: FuncId,
ctx: &mut cranelift_codegen::Context,
trap_sink: &mut dyn TrapSink,
stack_map_sink: &mut dyn StackMapSink,
) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {}: {}", func_id, ctx.func.display());
let mut code: Vec<u8> = Vec::new();
let mut reloc_sink = ObjectRelocSink::default();
ctx.compile_and_emit(
self.isa(),
&mut code,
&mut reloc_sink,
trap_sink,
stack_map_sink,
)?;
ctx.compile_and_emit(self.isa(), &mut code)?;
for &MachReloc {
offset,
srcloc,
kind,
ref name,
addend,
} in ctx.mach_compile_result.as_ref().unwrap().buffer.relocs()
{
reloc_sink.reloc_external(offset, srcloc, kind, name, addend);
}
self.define_function_bytes(func_id, &code, &reloc_sink.relocs)
}

View File

@@ -1,9 +1,6 @@
use cranelift_codegen::ir::*;
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::{
binemit::{NullStackMapSink, NullTrapSink},
settings,
};
use cranelift_codegen::settings;
use cranelift_codegen::{ir::types::I16, Context};
use cranelift_entity::EntityRef;
use cranelift_frontend::*;
@@ -53,11 +50,7 @@ fn define_simple_function(module: &mut ObjectModule) -> FuncId {
bcx.ins().return_(&[]);
}
let mut trap_sink = NullTrapSink {};
let mut stack_map_sink = NullStackMapSink {};
module
.define_function(func_id, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_id, &mut ctx).unwrap();
func_id
}
@@ -194,11 +187,7 @@ fn libcall_function() {
bcx.ins().return_(&[]);
}
let mut trap_sink = NullTrapSink {};
let mut stack_map_sink = NullStackMapSink {};
module
.define_function(func_id, &mut ctx, &mut trap_sink, &mut stack_map_sink)
.unwrap();
module.define_function(func_id, &mut ctx).unwrap();
module.finish();
}

View File

@@ -1,6 +1,5 @@
//! CLI tool to reduce Cranelift IR files crashing during compilation.
use crate::disasm::{PrintRelocs, PrintStackMaps, PrintTraps};
use crate::utils::{parse_sets_and_triple, read_to_string};
use anyhow::{Context as _, Result};
use cranelift_codegen::cursor::{Cursor, FuncCursor};
@@ -1029,17 +1028,9 @@ impl<'a> CrashCheckContext<'a> {
std::panic::set_hook(Box::new(|_| {})); // silence panics
let res = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut relocs = PrintRelocs::new(false);
let mut traps = PrintTraps::new(false);
let mut stack_maps = PrintStackMaps::new(false);
let _ = self.context.compile_and_emit(
self.isa,
&mut self.code_memory,
&mut relocs,
&mut traps,
&mut stack_maps,
);
let _ = self
.context
.compile_and_emit(self.isa, &mut self.code_memory);
})) {
Ok(()) => CheckResult::Succeed,
Err(err) => CheckResult::Crash(get_panic_string(err)),

View File

@@ -3,10 +3,11 @@
use crate::disasm::{print_all, PrintRelocs, PrintStackMaps, PrintTraps};
use crate::utils::{parse_sets_and_triple, read_to_string};
use anyhow::{Context as _, Result};
use cranelift_codegen::binemit::{RelocSink, StackMapSink, TrapSink};
use cranelift_codegen::print_errors::pretty_error;
use cranelift_codegen::settings::FlagsOrIsa;
use cranelift_codegen::timing;
use cranelift_codegen::Context;
use cranelift_codegen::{timing, MachReloc, MachStackMap, MachTrap};
use cranelift_reader::{parse_test, ParseOptions};
use std::path::Path;
use std::path::PathBuf;
@@ -79,9 +80,36 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
// Compile and encode the result to machine code.
context
.compile_and_emit(isa, &mut mem, &mut relocs, &mut traps, &mut stack_maps)
.compile_and_emit(isa, &mut mem)
.map_err(|err| anyhow::anyhow!("{}", pretty_error(&context.func, err)))?;
let code_info = context.mach_compile_result.as_ref().unwrap().code_info();
let result = context.mach_compile_result.as_ref().unwrap();
let code_info = result.code_info();
for &MachReloc {
offset,
srcloc,
kind,
ref name,
addend,
} in result.buffer.relocs()
{
relocs.reloc_external(offset, srcloc, kind, name, addend);
}
for &MachTrap {
offset,
srcloc,
code,
} in result.buffer.traps()
{
traps.trap(offset, srcloc, code);
}
for &MachStackMap {
offset_end,
ref stack_map,
..
} in result.buffer.stack_maps()
{
stack_maps.add_stack_map(offset_end, stack_map.clone());
}
if options.print {
println!("{}", context.func.display());

View File

@@ -10,11 +10,12 @@
use crate::disasm::{print_all, PrintRelocs, PrintStackMaps, PrintTraps};
use crate::utils::parse_sets_and_triple;
use anyhow::{Context as _, Result};
use cranelift_codegen::binemit::{RelocSink, StackMapSink, TrapSink};
use cranelift_codegen::ir::DisplayFunctionAnnotations;
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
use cranelift_codegen::settings::FlagsOrIsa;
use cranelift_codegen::timing;
use cranelift_codegen::Context;
use cranelift_codegen::{timing, MachReloc, MachStackMap, MachTrap};
use cranelift_entity::EntityRef;
use cranelift_wasm::{translate_module, DummyEnvironment, FuncIndex, ReturnMode};
use std::io::Read;
@@ -267,9 +268,36 @@ fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -
}
} else {
context
.compile_and_emit(isa, &mut mem, &mut relocs, &mut traps, &mut stack_maps)
.compile_and_emit(isa, &mut mem)
.map_err(|err| anyhow::anyhow!("{}", pretty_error(&context.func, err)))?;
let code_info = context.mach_compile_result.as_ref().unwrap().code_info();
let result = context.mach_compile_result.as_ref().unwrap();
let code_info = result.code_info();
for &MachReloc {
offset,
srcloc,
kind,
ref name,
addend,
} in result.buffer.relocs()
{
relocs.reloc_external(offset, srcloc, kind, name, addend);
}
for &MachTrap {
offset,
srcloc,
code,
} in result.buffer.traps()
{
traps.trap(offset, srcloc, code);
}
for &MachStackMap {
offset_end,
ref stack_map,
..
} in result.buffer.stack_maps()
{
stack_maps.add_stack_map(offset_end, stack_map.clone());
}
if options.print_size {
println!(