cranelift_codegen/result.rs
1//! Result and error types representing the outcome of compiling a function.
2
3use regalloc2::checker::CheckerErrors;
4
5use crate::{ir::Function, verifier::VerifierErrors};
6use std::string::String;
7
8/// 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 bug
16 /// in Cranelift itself.
17 Verifier(VerifierErrors),
18
19 /// An implementation limit was exceeded.
20 ///
21 /// Cranelift can compile very large and complicated functions, but the [implementation has
22 /// 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-limits
25 ImplLimitExceeded,
26
27 /// 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 limit
30 /// is exceeded, compilation fails.
31 CodeTooLarge,
32
33 /// Something is not supported by the code generator. This might be an indication that a
34 /// feature is used without explicitly enabling it, or that something is temporarily
35 /// unsupported by a given target backend.
36 Unsupported(String),
37
38 /// A failure to map Cranelift register representation to a DWARF register representation.
39 #[cfg(feature = "unwind")]
40 RegisterMappingError(crate::isa::unwind::systemv::RegisterMappingError),
41
42 /// Register allocator internal error discovered by the symbolic checker.
43 Regalloc(CheckerErrors),
44}
45
46/// A convenient alias for a `Result` that uses `CodegenError` as the error type.
47pub type CodegenResult<T> = Result<T, CodegenError>;
48
49// This is manually implementing Error and Display instead of using thiserror to reduce the amount
50// of dependencies used by Cranelift.
51impl std::error::Error for CodegenError {
52 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53 match self {
54 CodegenError::Verifier(source) => Some(source),
55 CodegenError::ImplLimitExceeded { .. }
56 | CodegenError::CodeTooLarge { .. }
57 | CodegenError::Unsupported { .. } => None,
58 #[cfg(feature = "unwind")]
59 CodegenError::RegisterMappingError { .. } => None,
60 CodegenError::Regalloc(..) => None,
61 }
62 }
63}
64
65impl std::fmt::Display for CodegenError {
66 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
67 match self {
68 CodegenError::Verifier(_) => write!(f, "Verifier errors"),
69 CodegenError::ImplLimitExceeded => write!(f, "Implementation limit exceeded"),
70 CodegenError::CodeTooLarge => write!(f, "Code for function is too large"),
71 CodegenError::Unsupported(feature) => write!(f, "Unsupported feature: {}", feature),
72 #[cfg(feature = "unwind")]
73 CodegenError::RegisterMappingError(_0) => write!(f, "Register mapping error"),
74 CodegenError::Regalloc(errors) => write!(f, "Regalloc validation errors: {:?}", errors),
75 }
76 }
77}
78
79impl From<VerifierErrors> for CodegenError {
80 fn from(source: VerifierErrors) -> Self {
81 CodegenError::Verifier { 0: source }
82 }
83}
84
85/// Compilation error, with the accompanying function to help printing it.
86pub struct CompileError<'a> {
87 /// Underlying `CodegenError` that triggered the error.
88 pub inner: CodegenError,
89 /// Function we tried to compile, for display purposes.
90 pub func: &'a Function,
91}
92
93// By default, have `CompileError` be displayed as the internal error, and let consumers care if
94// they want to use the func field for adding details.
95impl<'a> core::fmt::Debug for CompileError<'a> {
96 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97 self.inner.fmt(f)
98 }
99}
100
101/// A convenient alias for a `Result` that uses `CompileError` as the error type.
102pub type CompileResult<'a, T> = Result<T, CompileError<'a>>;