[cranelift] Rejigger the compile API (#4540)

* Move `emit_to_memory` to `MachCompileResult`

This small refactoring makes it clearer to me that emitting to memory
doesn't require anything else from the compilation `Context`. While it's
a trivial change, it's a small public API change that shouldn't cause
too much trouble, and doesn't seem RFC-worthy. Happy to hear different
opinions about this, though!

* hide the MachCompileResult behind a method

* Add a `CompileError` wrapper type that references a `Function`

* Rename MachCompileResult to CompiledCode

* Additionally remove the last unsafe API in cranelift-codegen
This commit is contained in:
Benjamin Bouvier
2022-08-02 21:05:40 +02:00
committed by GitHub
parent 37cd96beff
commit ff37c9d8a4
17 changed files with 156 additions and 198 deletions

View File

@@ -4,10 +4,7 @@ use crate::{compiled_blob::CompiledBlob, memory::Memory};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::Configurable;
use cranelift_codegen::{self, ir, settings, MachReloc};
use cranelift_codegen::{
binemit::{CodeInfo, Reloc},
CodegenError,
};
use cranelift_codegen::{binemit::Reloc, CodegenError};
use cranelift_entity::SecondaryMap;
use cranelift_module::{
DataContext, DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleCompiledFunction,
@@ -684,10 +681,8 @@ impl Module for JITModule {
return Err(ModuleError::DuplicateDefinition(decl.name.to_owned()));
}
let CodeInfo {
total_size: code_size,
..
} = ctx.compile(self.isa())?;
let compiled_code = ctx.compile(self.isa())?;
let code_size = compiled_code.code_info().total_size;
let size = code_size as usize;
let ptr = self
@@ -696,14 +691,12 @@ impl Module for JITModule {
.allocate(size, EXECUTABLE_DATA_ALIGNMENT)
.expect("TODO: handle OOM etc.");
unsafe { ctx.emit_to_memory(ptr) };
let relocs = ctx
.mach_compile_result
.as_ref()
.unwrap()
.buffer
.relocs()
.to_vec();
{
let mem = unsafe { std::slice::from_raw_parts_mut(ptr, size) };
mem.copy_from_slice(compiled_code.code_buffer());
}
let relocs = compiled_code.buffer.relocs().to_vec();
self.record_function_for_perf(ptr, size, &decl.name);
self.compiled_functions[id] = Some(CompiledBlob { ptr, size, relocs });