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