Skip to main content

celox_backend_cranelift/
cranelift_options.rs

1//! Cranelift-owned compilation policy.
2
3/// Cranelift backend optimization level.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub enum CraneliftOptLevel {
6    /// No Cranelift-level optimizations.
7    None,
8    /// Optimize for execution speed (default).
9    #[default]
10    Speed,
11    /// Optimize for both speed and code size.
12    SpeedAndSize,
13}
14
15impl CraneliftOptLevel {
16    /// Returns the Cranelift settings string for this level.
17    pub fn as_cranelift_str(self) -> &'static str {
18        match self {
19            CraneliftOptLevel::None => "none",
20            CraneliftOptLevel::Speed => "speed",
21            CraneliftOptLevel::SpeedAndSize => "speed_and_size",
22        }
23    }
24}
25
26/// Register allocator algorithm for the Cranelift backend.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28pub enum RegallocAlgorithm {
29    /// Backtracking allocator with range splitting.
30    /// Slower compilation but generates better code with fewer spills.
31    #[default]
32    Backtracking,
33    /// Single-pass allocator.
34    /// Much faster compilation but generates code with more register spills and moves.
35    SinglePass,
36}
37
38impl RegallocAlgorithm {
39    /// Returns the Cranelift settings string for this algorithm.
40    pub fn as_cranelift_str(self) -> &'static str {
41        match self {
42            RegallocAlgorithm::Backtracking => "backtracking",
43            RegallocAlgorithm::SinglePass => "single_pass",
44        }
45    }
46}
47
48/// Fine-grained Cranelift backend options beyond the optimization level.
49#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
50pub struct CraneliftDiagnostics {
51    pub pass_timing: bool,
52}
53
54/// Fine-grained Cranelift backend options beyond the optimization level.
55#[derive(Debug, Clone, Copy)]
56pub struct CraneliftOptions {
57    /// Optimization level (default: Speed).
58    pub opt_level: CraneliftOptLevel,
59    /// Register allocator algorithm (default: Backtracking).
60    pub regalloc_algorithm: RegallocAlgorithm,
61    /// Enable alias analysis during egraph optimization (default: true).
62    /// Only effective when `opt_level` is not `None`.
63    pub enable_alias_analysis: bool,
64    /// Enable the Cranelift IR verifier (default: true).
65    /// Disabling saves compile time at the cost of less validation.
66    pub enable_verifier: bool,
67    /// Enable backend-owned splitting for oversized functions.
68    pub tail_call_split: bool,
69    pub diagnostics: CraneliftDiagnostics,
70}
71
72impl Default for CraneliftOptions {
73    fn default() -> Self {
74        Self {
75            opt_level: CraneliftOptLevel::default(),
76            regalloc_algorithm: RegallocAlgorithm::default(),
77            enable_alias_analysis: true,
78            enable_verifier: true,
79            tail_call_split: true,
80            diagnostics: CraneliftDiagnostics::default(),
81        }
82    }
83}
84
85impl CraneliftOptions {
86    /// Fast compilation preset: no optimizations, single-pass regalloc, no verifier.
87    pub fn fast_compile() -> Self {
88        Self {
89            opt_level: CraneliftOptLevel::None,
90            regalloc_algorithm: RegallocAlgorithm::SinglePass,
91            enable_alias_analysis: false,
92            enable_verifier: false,
93            tail_call_split: true,
94            diagnostics: CraneliftDiagnostics::default(),
95        }
96    }
97
98    /// Select backend defaults without importing the facade's optimization policy.
99    pub fn for_speed_optimization(enabled: bool) -> Self {
100        if enabled {
101            Self::default()
102        } else {
103            Self::fast_compile()
104        }
105    }
106}