Files
wasmtime/cranelift/codegen/src/result.rs
Chris Fallin a0318f36f0 Switch Cranelift over to regalloc2. (#3989)
This PR switches Cranelift over to the new register allocator, regalloc2.

See [this document](https://gist.github.com/cfallin/08553421a91f150254fe878f67301801)
for a summary of the design changes. This switchover has implications for
core VCode/MachInst types and the lowering pass.

Overall, this change brings improvements to both compile time and speed of
generated code (runtime), as reported in #3942:

```
Benchmark       Compilation (wallclock)     Execution (wallclock)
blake3-scalar   25% faster                  28% faster
blake3-simd     no diff                     no diff
meshoptimizer   19% faster                  17% faster
pulldown-cmark  17% faster                  no diff
bz2             15% faster                  no diff
SpiderMonkey,   21% faster                  2% faster
  fib(30)
clang.wasm      42% faster                  N/A
```
2022-04-14 10:28:21 -07:00

84 lines
3.3 KiB
Rust

//! Result and error types representing the outcome of compiling a function.
use regalloc2::checker::CheckerErrors;
use crate::verifier::VerifierErrors;
use std::string::String;
/// A compilation error.
///
/// When Cranelift fails to compile a function, it will return one of these error codes.
#[derive(Debug)]
pub enum CodegenError {
/// A list of IR verifier errors.
///
/// This always represents a bug, either in the code that generated IR for Cranelift, or a bug
/// in Cranelift itself.
Verifier(VerifierErrors),
/// An implementation limit was exceeded.
///
/// Cranelift can compile very large and complicated functions, but the [implementation has
/// limits][limits] that cause compilation to fail when they are exceeded.
///
/// [limits]: https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/docs/ir.md#implementation-limits
ImplLimitExceeded,
/// The code size for the function is too large.
///
/// Different target ISAs may impose a limit on the size of a compiled function. If that limit
/// is exceeded, compilation fails.
CodeTooLarge,
/// Something is not supported by the code generator. This might be an indication that a
/// feature is used without explicitly enabling it, or that something is temporarily
/// unsupported by a given target backend.
Unsupported(String),
/// A failure to map Cranelift register representation to a DWARF register representation.
#[cfg(feature = "unwind")]
RegisterMappingError(crate::isa::unwind::systemv::RegisterMappingError),
/// Register allocator internal error discovered by the symbolic checker.
Regalloc(CheckerErrors),
}
/// A convenient alias for a `Result` that uses `CodegenError` as the error type.
pub type CodegenResult<T> = Result<T, CodegenError>;
// This is manually implementing Error and Display instead of using thiserror to reduce the amount
// of dependencies used by Cranelift.
impl std::error::Error for CodegenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CodegenError::Verifier(source) => Some(source),
CodegenError::ImplLimitExceeded { .. }
| CodegenError::CodeTooLarge { .. }
| CodegenError::Unsupported { .. } => None,
#[cfg(feature = "unwind")]
CodegenError::RegisterMappingError { .. } => None,
CodegenError::Regalloc(..) => None,
}
}
}
impl std::fmt::Display for CodegenError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
CodegenError::Verifier(_) => write!(f, "Verifier errors"),
CodegenError::ImplLimitExceeded => write!(f, "Implementation limit exceeded"),
CodegenError::CodeTooLarge => write!(f, "Code for function is too large"),
CodegenError::Unsupported(feature) => write!(f, "Unsupported feature: {}", feature),
#[cfg(feature = "unwind")]
CodegenError::RegisterMappingError(_0) => write!(f, "Register mapping error"),
CodegenError::Regalloc(errors) => write!(f, "Regalloc validation errors: {:?}", errors),
}
}
}
impl From<VerifierErrors> for CodegenError {
fn from(source: VerifierErrors) -> Self {
CodegenError::Verifier { 0: source }
}
}