diff --git a/cranelift/codegen/src/context.rs b/cranelift/codegen/src/context.rs index 2cebe0d3c3..91781d1895 100644 --- a/cranelift/codegen/src/context.rs +++ b/cranelift/codegen/src/context.rs @@ -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, - 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 } diff --git a/cranelift/codegen/src/lib.rs b/cranelift/codegen/src/lib.rs index e860990372..3d5ca20035 100644 --- a/cranelift/codegen/src/lib.rs +++ b/cranelift/codegen/src/lib.rs @@ -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; diff --git a/cranelift/filetests/src/function_runner.rs b/cranelift/filetests/src/function_runner.rs index 4046072a23..116dd73774 100644 --- a/cranelift/filetests/src/function_runner.rs +++ b/cranelift/filetests/src/function_runner.rs @@ -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 ModuleResult { 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 { diff --git a/cranelift/jit/tests/basic.rs b/cranelift/jit/tests/basic.rs index f53480702d..a1a038f6fe 100644 --- a/cranelift/jit/tests/basic.rs +++ b/cranelift/jit/tests/basic.rs @@ -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(); } diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 191d468ed7..3d5816973f 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -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; /// Define a function, taking the function body from the given `bytes`. @@ -656,10 +654,8 @@ impl 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 { - (**self).define_function(func, ctx, trap_sink, stack_map_sink) + (**self).define_function(func, ctx) } fn define_function_bytes( diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index 57c450de49..d3eea47fae 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -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 { info!("defining function {}: {}", func_id, ctx.func.display()); let mut code: Vec = 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) } diff --git a/cranelift/object/tests/basic.rs b/cranelift/object/tests/basic.rs index a5add02295..8a3764f9b8 100644 --- a/cranelift/object/tests/basic.rs +++ b/cranelift/object/tests/basic.rs @@ -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(); } diff --git a/cranelift/src/bugpoint.rs b/cranelift/src/bugpoint.rs index e16e91ae40..995e416764 100644 --- a/cranelift/src/bugpoint.rs +++ b/cranelift/src/bugpoint.rs @@ -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)), diff --git a/cranelift/src/compile.rs b/cranelift/src/compile.rs index c1993e8b65..abada4ea34 100644 --- a/cranelift/src/compile.rs +++ b/cranelift/src/compile.rs @@ -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()); diff --git a/cranelift/src/wasm.rs b/cranelift/src/wasm.rs index bb3b93b098..d377858f5b 100644 --- a/cranelift/src/wasm.rs +++ b/cranelift/src/wasm.rs @@ -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!( diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs index f02fee01d2..278aeabbed 100644 --- a/crates/cranelift/src/compiler.rs +++ b/crates/cranelift/src/compiler.rs @@ -7,12 +7,13 @@ use crate::{ CompiledFunction, FunctionAddressMap, Relocation, RelocationTarget, }; use anyhow::{Context as _, Result}; +use cranelift_codegen::binemit::{RelocSink as _, StackMapSink as _, TrapSink as _}; use cranelift_codegen::ir::{self, ExternalName, InstBuilder, MemFlags}; use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::print_errors::pretty_error; -use cranelift_codegen::settings; -use cranelift_codegen::MachSrcLoc; use cranelift_codegen::{binemit, Context}; +use cranelift_codegen::{settings, MachReloc, MachTrap}; +use cranelift_codegen::{MachSrcLoc, MachStackMap}; use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_frontend::FunctionBuilder; use cranelift_wasm::{ @@ -170,15 +171,37 @@ impl wasmtime_environ::Compiler for Compiler { let mut trap_sink = TrapSink::new(); let mut stack_map_sink = StackMapSink::default(); context - .compile_and_emit( - isa, - &mut code_buf, - &mut reloc_sink, - &mut trap_sink, - &mut stack_map_sink, - ) + .compile_and_emit(isa, &mut code_buf) .map_err(|error| CompileError::Codegen(pretty_error(&context.func, error)))?; + let result = context.mach_compile_result.as_ref().unwrap(); + for &MachReloc { + offset, + srcloc, + kind, + ref name, + addend, + } in result.buffer.relocs() + { + reloc_sink.reloc_external(offset, srcloc, kind, name, addend); + } + for &MachTrap { + offset, + srcloc, + code, + } in result.buffer.traps() + { + trap_sink.trap(offset, srcloc, code); + } + for &MachStackMap { + offset_end, + ref stack_map, + .. + } in result.buffer.stack_maps() + { + stack_map_sink.add_stack_map(offset_end, stack_map.clone()); + } + let unwind_info = context .create_unwind_info(isa) .map_err(|error| CompileError::Codegen(pretty_error(&context.func, error)))?; @@ -535,18 +558,26 @@ impl Compiler { ) -> Result { let mut code_buf = Vec::new(); let mut reloc_sink = TrampolineRelocSink::default(); - let mut trap_sink = binemit::NullTrapSink {}; - let mut stack_map_sink = binemit::NullStackMapSink {}; context - .compile_and_emit( - isa, - &mut code_buf, - &mut reloc_sink, - &mut trap_sink, - &mut stack_map_sink, - ) + .compile_and_emit(isa, &mut code_buf) .map_err(|error| CompileError::Codegen(pretty_error(&context.func, error)))?; + for &MachReloc { + offset, + srcloc, + kind, + ref name, + addend, + } in context + .mach_compile_result + .as_ref() + .unwrap() + .buffer + .relocs() + { + reloc_sink.reloc_external(offset, srcloc, kind, name, addend); + } + let unwind_info = context .create_unwind_info(isa) .map_err(|error| CompileError::Codegen(pretty_error(&context.func, error)))?;