[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

@@ -2,7 +2,7 @@
use regalloc2::checker::CheckerErrors;
use crate::verifier::VerifierErrors;
use crate::{ir::Function, verifier::VerifierErrors};
use std::string::String;
/// A compilation error.
@@ -81,3 +81,22 @@ impl From<VerifierErrors> for CodegenError {
CodegenError::Verifier { 0: source }
}
}
/// Compilation error, with the accompanying function to help printing it.
pub struct CompileError<'a> {
/// Underlying `CodegenError` that triggered the error.
pub inner: CodegenError,
/// Function we tried to compile, for display purposes.
pub func: &'a Function,
}
// By default, have `CompileError` be displayed as the internal error, and let consumers care if
// they want to use the func field for adding details.
impl<'a> core::fmt::Debug for CompileError<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}
/// A convenient alias for a `Result` that uses `CompileError` as the error type.
pub type CompileResult<'a, T> = Result<T, CompileError<'a>>;