cranelift_codegen/
context.rs

1//! Cranelift compilation context and main entry point.
2//!
3//! When compiling many small functions, it is important to avoid repeatedly allocating and
4//! deallocating the data structures needed for compilation. The `Context` struct is used to hold
5//! on to memory allocations between function compilations.
6//!
7//! The context does not hold a `TargetIsa` instance which has to be provided as an argument
8//! instead. This is because an ISA instance is immutable and can be used by multiple compilation
9//! contexts concurrently. Typically, you would have one context per compilation thread and only a
10//! single ISA instance.
11
12use crate::alias_analysis::AliasAnalysis;
13use crate::dominator_tree::DominatorTree;
14use crate::egraph::EgraphPass;
15use crate::flowgraph::ControlFlowGraph;
16use crate::ir::Function;
17use crate::isa::TargetIsa;
18use crate::legalizer::simple_legalize;
19use crate::loop_analysis::LoopAnalysis;
20use crate::machinst::{CompiledCode, CompiledCodeStencil};
21use crate::nan_canonicalization::do_nan_canonicalization;
22use crate::remove_constant_phis::do_remove_constant_phis;
23use crate::result::{CodegenResult, CompileResult};
24use crate::settings::{FlagsOrIsa, OptLevel};
25use crate::trace;
26use crate::unreachable_code::eliminate_unreachable_code;
27use crate::verifier::{verify_context, VerifierErrors, VerifierResult};
28use crate::{timing, CompileError};
29#[cfg(feature = "souper-harvest")]
30use alloc::string::String;
31use alloc::vec::Vec;
32use cranelift_control::ControlPlane;
33use target_lexicon::Architecture;
34
35#[cfg(feature = "souper-harvest")]
36use crate::souper_harvest::do_souper_harvest;
37
38/// Persistent data structures and compilation pipeline.
39pub struct Context {
40    /// The function we're compiling.
41    pub func: Function,
42
43    /// The control flow graph of `func`.
44    pub cfg: ControlFlowGraph,
45
46    /// Dominator tree for `func`.
47    pub domtree: DominatorTree,
48
49    /// Loop analysis of `func`.
50    pub loop_analysis: LoopAnalysis,
51
52    /// Result of MachBackend compilation, if computed.
53    pub(crate) compiled_code: Option<CompiledCode>,
54
55    /// Flag: do we want a disassembly with the CompiledCode?
56    pub want_disasm: bool,
57}
58
59impl Context {
60    /// Allocate a new compilation context.
61    ///
62    /// The returned instance should be reused for compiling multiple functions in order to avoid
63    /// needless allocator thrashing.
64    pub fn new() -> Self {
65        Self::for_function(Function::new())
66    }
67
68    /// Allocate a new compilation context with an existing Function.
69    ///
70    /// The returned instance should be reused for compiling multiple functions in order to avoid
71    /// needless allocator thrashing.
72    pub fn for_function(func: Function) -> Self {
73        Self {
74            func,
75            cfg: ControlFlowGraph::new(),
76            domtree: DominatorTree::new(),
77            loop_analysis: LoopAnalysis::new(),
78            compiled_code: None,
79            want_disasm: false,
80        }
81    }
82
83    /// Clear all data structures in this context.
84    pub fn clear(&mut self) {
85        self.func.clear();
86        self.cfg.clear();
87        self.domtree.clear();
88        self.loop_analysis.clear();
89        self.compiled_code = None;
90        self.want_disasm = false;
91    }
92
93    /// Returns the compilation result for this function, available after any `compile` function
94    /// has been called.
95    pub fn compiled_code(&self) -> Option<&CompiledCode> {
96        self.compiled_code.as_ref()
97    }
98
99    /// Returns the compilation result for this function, available after any `compile` function
100    /// has been called.
101    pub fn take_compiled_code(&mut self) -> Option<CompiledCode> {
102        self.compiled_code.take()
103    }
104
105    /// Set the flag to request a disassembly when compiling with a
106    /// `MachBackend` backend.
107    pub fn set_disasm(&mut self, val: bool) {
108        self.want_disasm = val;
109    }
110
111    /// Compile the function, and emit machine code into a `Vec<u8>`.
112    #[deprecated = "use Context::compile"]
113    pub fn compile_and_emit(
114        &mut self,
115        isa: &dyn TargetIsa,
116        mem: &mut Vec<u8>,
117        ctrl_plane: &mut ControlPlane,
118    ) -> CompileResult<&CompiledCode> {
119        let compiled_code = self.compile(isa, ctrl_plane)?;
120        mem.extend_from_slice(compiled_code.code_buffer());
121        Ok(compiled_code)
122    }
123
124    /// Internally compiles the function into a stencil.
125    ///
126    /// Public only for testing and fuzzing purposes.
127    pub fn compile_stencil(
128        &mut self,
129        isa: &dyn TargetIsa,
130        ctrl_plane: &mut ControlPlane,
131    ) -> CodegenResult<CompiledCodeStencil> {
132        let _tt = timing::compile();
133
134        self.verify_if(isa)?;
135
136        self.optimize(isa, ctrl_plane)?;
137
138        isa.compile_function(&self.func, &self.domtree, self.want_disasm, ctrl_plane)
139    }
140
141    /// Optimize the function, performing all compilation steps up to
142    /// but not including machine-code lowering and register
143    /// allocation.
144    ///
145    /// Public only for testing purposes.
146    pub fn optimize(
147        &mut self,
148        isa: &dyn TargetIsa,
149        ctrl_plane: &mut ControlPlane,
150    ) -> CodegenResult<()> {
151        log::debug!(
152            "Number of CLIF instructions to optimize: {}",
153            self.func.dfg.num_insts()
154        );
155        log::debug!(
156            "Number of CLIF blocks to optimize: {}",
157            self.func.dfg.num_blocks()
158        );
159
160        let opt_level = isa.flags().opt_level();
161        crate::trace!(
162            "Optimizing (opt level {:?}):\n{}",
163            opt_level,
164            self.func.display()
165        );
166
167        self.compute_cfg();
168        if isa.flags().enable_nan_canonicalization() {
169            self.canonicalize_nans(isa)?;
170        }
171
172        self.legalize(isa)?;
173
174        self.compute_domtree();
175        self.eliminate_unreachable_code(isa)?;
176        self.remove_constant_phis(isa)?;
177
178        self.func.dfg.resolve_all_aliases();
179
180        if opt_level != OptLevel::None {
181            self.egraph_pass(isa, ctrl_plane)?;
182        }
183
184        Ok(())
185    }
186
187    /// Compile the function,
188    ///
189    /// Run the function through all the passes necessary to generate
190    /// code for the target ISA represented by `isa`. The generated
191    /// machine code is not relocated. Instead, any relocations can be
192    /// obtained from `compiled_code.buffer.relocs()`.
193    ///
194    /// Performs any optimizations that are enabled, unless
195    /// `optimize()` was already invoked.
196    ///
197    /// Returns the generated machine code as well as information about
198    /// the function's code and read-only data.
199    pub fn compile(
200        &mut self,
201        isa: &dyn TargetIsa,
202        ctrl_plane: &mut ControlPlane,
203    ) -> CompileResult<&CompiledCode> {
204        let stencil = self
205            .compile_stencil(isa, ctrl_plane)
206            .map_err(|error| CompileError {
207                inner: error,
208                func: &self.func,
209            })?;
210        Ok(self
211            .compiled_code
212            .insert(stencil.apply_params(&self.func.params)))
213    }
214
215    /// If available, return information about the code layout in the
216    /// final machine code: the offsets (in bytes) of each basic-block
217    /// start, and all basic-block edges.
218    #[deprecated = "use CompiledCode::get_code_bb_layout"]
219    pub fn get_code_bb_layout(&self) -> Option<(Vec<usize>, Vec<(usize, usize)>)> {
220        self.compiled_code().map(CompiledCode::get_code_bb_layout)
221    }
222
223    /// Creates unwind information for the function.
224    ///
225    /// Returns `None` if the function has no unwind information.
226    #[cfg(feature = "unwind")]
227    #[deprecated = "use CompiledCode::create_unwind_info"]
228    pub fn create_unwind_info(
229        &self,
230        isa: &dyn TargetIsa,
231    ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
232        self.compiled_code().unwrap().create_unwind_info(isa)
233    }
234
235    /// Run the verifier on the function.
236    ///
237    /// Also check that the dominator tree and control flow graph are consistent with the function.
238    ///
239    /// TODO: rename to "CLIF validate" or similar.
240    pub fn verify<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> VerifierResult<()> {
241        let mut errors = VerifierErrors::default();
242        let _ = verify_context(&self.func, &self.cfg, &self.domtree, fisa, &mut errors);
243
244        if errors.is_empty() {
245            Ok(())
246        } else {
247            Err(errors)
248        }
249    }
250
251    /// Run the verifier only if the `enable_verifier` setting is true.
252    pub fn verify_if<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> CodegenResult<()> {
253        let fisa = fisa.into();
254        if fisa.flags.enable_verifier() {
255            self.verify(fisa)?;
256        }
257        Ok(())
258    }
259
260    /// Perform constant-phi removal on the function.
261    pub fn remove_constant_phis<'a, FOI: Into<FlagsOrIsa<'a>>>(
262        &mut self,
263        fisa: FOI,
264    ) -> CodegenResult<()> {
265        do_remove_constant_phis(&mut self.func, &mut self.domtree);
266        self.verify_if(fisa)?;
267        Ok(())
268    }
269
270    /// Perform NaN canonicalizing rewrites on the function.
271    pub fn canonicalize_nans(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> {
272        // Currently only RiscV64 is the only arch that may not have vector support.
273        let has_vector_support = match isa.triple().architecture {
274            Architecture::Riscv64(_) => match isa.isa_flags().iter().find(|f| f.name == "has_v") {
275                Some(value) => value.as_bool().unwrap_or(false),
276                None => false,
277            },
278            _ => true,
279        };
280        do_nan_canonicalization(&mut self.func, has_vector_support);
281        self.verify_if(isa)
282    }
283
284    /// Run the legalizer for `isa` on the function.
285    pub fn legalize(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> {
286        // Legalization invalidates the domtree and loop_analysis by mutating the CFG.
287        // TODO: Avoid doing this when legalization doesn't actually mutate the CFG.
288        self.domtree.clear();
289        self.loop_analysis.clear();
290
291        // Run some specific legalizations only.
292        simple_legalize(&mut self.func, isa);
293        self.verify_if(isa)
294    }
295
296    /// Compute the control flow graph.
297    pub fn compute_cfg(&mut self) {
298        self.cfg.compute(&self.func)
299    }
300
301    /// Compute dominator tree.
302    pub fn compute_domtree(&mut self) {
303        self.domtree.compute(&self.func, &self.cfg)
304    }
305
306    /// Compute the loop analysis.
307    pub fn compute_loop_analysis(&mut self) {
308        self.loop_analysis
309            .compute(&self.func, &self.cfg, &self.domtree)
310    }
311
312    /// Compute the control flow graph and dominator tree.
313    pub fn flowgraph(&mut self) {
314        self.compute_cfg();
315        self.compute_domtree()
316    }
317
318    /// Perform unreachable code elimination.
319    pub fn eliminate_unreachable_code<'a, FOI>(&mut self, fisa: FOI) -> CodegenResult<()>
320    where
321        FOI: Into<FlagsOrIsa<'a>>,
322    {
323        eliminate_unreachable_code(&mut self.func, &mut self.cfg, &self.domtree);
324        self.verify_if(fisa)
325    }
326
327    /// Replace all redundant loads with the known values in
328    /// memory. These are loads whose values were already loaded by
329    /// other loads earlier, as well as loads whose values were stored
330    /// by a store instruction to the same instruction (so-called
331    /// "store-to-load forwarding").
332    pub fn replace_redundant_loads(&mut self) -> CodegenResult<()> {
333        let mut analysis = AliasAnalysis::new(&self.func, &self.domtree);
334        analysis.compute_and_update_aliases(&mut self.func);
335        Ok(())
336    }
337
338    /// Harvest candidate left-hand sides for superoptimization with Souper.
339    #[cfg(feature = "souper-harvest")]
340    pub fn souper_harvest(
341        &mut self,
342        out: &mut std::sync::mpsc::Sender<String>,
343    ) -> CodegenResult<()> {
344        do_souper_harvest(&self.func, out);
345        Ok(())
346    }
347
348    /// Run optimizations via the egraph infrastructure.
349    pub fn egraph_pass<'a, FOI>(
350        &mut self,
351        fisa: FOI,
352        ctrl_plane: &mut ControlPlane,
353    ) -> CodegenResult<()>
354    where
355        FOI: Into<FlagsOrIsa<'a>>,
356    {
357        let _tt = timing::egraph();
358
359        trace!(
360            "About to optimize with egraph phase:\n{}",
361            self.func.display()
362        );
363        let fisa = fisa.into();
364        self.compute_loop_analysis();
365        let mut alias_analysis = AliasAnalysis::new(&self.func, &self.domtree);
366        let mut pass = EgraphPass::new(
367            &mut self.func,
368            &self.domtree,
369            &self.loop_analysis,
370            &mut alias_analysis,
371            &fisa.flags,
372            ctrl_plane,
373        );
374        pass.run();
375        log::debug!("egraph stats: {:?}", pass.stats);
376        trace!("pinned_union_count: {}", pass.eclasses.pinned_union_count);
377        trace!("After egraph optimization:\n{}", self.func.display());
378
379        self.verify_if(fisa)
380    }
381}