Path: blob/main/cranelift/codegen/src/result.rs
1693 views
//! Result and error types representing the outcome of compiling a function.12use regalloc2::checker::CheckerErrors;34use crate::ir::pcc::PccError;5use crate::{ir::Function, verifier::VerifierErrors};6use std::string::String;78/// A compilation error.9///10/// When Cranelift fails to compile a function, it will return one of these error codes.11#[derive(Debug)]12pub enum CodegenError {13/// A list of IR verifier errors.14///15/// This always represents a bug, either in the code that generated IR for Cranelift, or a bug16/// in Cranelift itself.17Verifier(VerifierErrors),1819/// An implementation limit was exceeded.20///21/// Cranelift can compile very large and complicated functions, but the [implementation has22/// limits][limits] that cause compilation to fail when they are exceeded.23///24/// [limits]: https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/docs/ir.md#implementation-limits25ImplLimitExceeded,2627/// The code size for the function is too large.28///29/// Different target ISAs may impose a limit on the size of a compiled function. If that limit30/// is exceeded, compilation fails.31CodeTooLarge,3233/// Something is not supported by the code generator. This might be an indication that a34/// feature is used without explicitly enabling it, or that something is temporarily35/// unsupported by a given target backend.36Unsupported(String),3738/// A failure to map Cranelift register representation to a DWARF register representation.39#[cfg(feature = "unwind")]40RegisterMappingError(crate::isa::unwind::systemv::RegisterMappingError),4142/// Register allocator internal error discovered by the symbolic checker.43Regalloc(CheckerErrors),4445/// Proof-carrying-code validation error.46Pcc(PccError),47}4849/// A convenient alias for a `Result` that uses `CodegenError` as the error type.50pub type CodegenResult<T> = Result<T, CodegenError>;5152// This is manually implementing Error and Display instead of using thiserror to reduce the amount53// of dependencies used by Cranelift.54impl std::error::Error for CodegenError {55fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {56match self {57CodegenError::Verifier(source) => Some(source),58CodegenError::ImplLimitExceeded { .. }59| CodegenError::CodeTooLarge { .. }60| CodegenError::Unsupported { .. } => None,61#[cfg(feature = "unwind")]62CodegenError::RegisterMappingError { .. } => None,63CodegenError::Regalloc(..) => None,64CodegenError::Pcc(..) => None,65}66}67}6869impl std::fmt::Display for CodegenError {70fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {71match self {72CodegenError::Verifier(_) => write!(f, "Verifier errors"),73CodegenError::ImplLimitExceeded => write!(f, "Implementation limit exceeded"),74CodegenError::CodeTooLarge => write!(f, "Code for function is too large"),75CodegenError::Unsupported(feature) => write!(f, "Unsupported feature: {feature}"),76#[cfg(feature = "unwind")]77CodegenError::RegisterMappingError(_0) => write!(f, "Register mapping error"),78CodegenError::Regalloc(errors) => write!(f, "Regalloc validation errors: {errors:?}"),7980// NOTE: if this is changed, please update the `is_pcc_error` function defined in81// `wasmtime/crates/fuzzing/src/oracles.rs`82CodegenError::Pcc(e) => write!(f, "Proof-carrying-code validation error: {e:?}"),83}84}85}8687impl From<VerifierErrors> for CodegenError {88fn from(source: VerifierErrors) -> Self {89CodegenError::Verifier { 0: source }90}91}9293/// Compilation error, with the accompanying function to help printing it.94pub struct CompileError<'a> {95/// Underlying `CodegenError` that triggered the error.96pub inner: CodegenError,97/// Function we tried to compile, for display purposes.98pub func: &'a Function,99}100101// By default, have `CompileError` be displayed as the internal error, and let consumers care if102// they want to use the func field for adding details.103impl<'a> core::fmt::Debug for CompileError<'a> {104fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {105self.inner.fmt(f)106}107}108109/// A convenient alias for a `Result` that uses `CompileError` as the error type.110pub type CompileResult<'a, T> = Result<T, CompileError<'a>>;111112113