1 //! Intermediate representation of a function.
2 //!
3 //! The `Function` struct defined in this module owns all of its basic blocks and
4 //! instructions.
5 
6 use crate::entity::{PrimaryMap, SecondaryMap};
7 use crate::ir::{
8     self, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, DynamicStackSlots,
9     DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, InstructionData,
10     JumpTable, JumpTableData, Layout, Opcode, SigRef, Signature, SourceLocs, StackSlot,
11     StackSlotData, StackSlots, Table, TableData, Type,
12 };
13 use crate::isa::CallConv;
14 use crate::value_label::ValueLabelsRanges;
15 use crate::write::write_function;
16 use crate::HashMap;
17 #[cfg(feature = "enable-serde")]
18 use alloc::string::String;
19 use core::fmt;
20 
21 #[cfg(feature = "enable-serde")]
22 use serde::de::{Deserializer, Error};
23 #[cfg(feature = "enable-serde")]
24 use serde::ser::Serializer;
25 #[cfg(feature = "enable-serde")]
26 use serde::{Deserialize, Serialize};
27 
28 use super::entities::UserExternalNameRef;
29 use super::extname::UserFuncName;
30 use super::{RelSourceLoc, SourceLoc, UserExternalName};
31 
32 /// A version marker used to ensure that serialized clif ir is never deserialized with a
33 /// different version of Cranelift.
34 #[derive(Copy, Clone, Debug, PartialEq, Hash)]
35 pub struct VersionMarker;
36 
37 #[cfg(feature = "enable-serde")]
38 impl Serialize for VersionMarker {
39     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40     where
41         S: Serializer,
42     {
43         crate::VERSION.serialize(serializer)
44     }
45 }
46 
47 #[cfg(feature = "enable-serde")]
48 impl<'de> Deserialize<'de> for VersionMarker {
49     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
50     where
51         D: Deserializer<'de>,
52     {
53         let version = String::deserialize(deserializer)?;
54         if version != crate::VERSION {
55             return Err(D::Error::custom(&format!(
56                 "Expected a clif ir function for version {}, found one for version {}",
57                 crate::VERSION,
58                 version,
59             )));
60         }
61         Ok(VersionMarker)
62     }
63 }
64 
65 /// Function parameters used when creating this function, and that will become applied after
66 /// compilation to materialize the final `CompiledCode`.
67 #[derive(Clone)]
68 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
69 pub struct FunctionParameters {
70     /// The first `SourceLoc` appearing in the function, serving as a base for every relative
71     /// source loc in the function.
72     base_srcloc: Option<SourceLoc>,
73 
74     /// External user-defined function references.
75     user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,
76 
77     /// Inverted mapping of `user_named_funcs`, to deduplicate internally.
78     user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,
79 }
80 
81 impl FunctionParameters {
82     /// Creates a new `FunctionParameters` with the given name.
83     pub fn new() -> Self {
84         Self {
85             base_srcloc: None,
86             user_named_funcs: Default::default(),
87             user_ext_name_to_ref: Default::default(),
88         }
89     }
90 
91     /// Returns the base `SourceLoc`.
92     ///
93     /// If it was never explicitly set with `ensure_base_srcloc`, will return an invalid
94     /// `SourceLoc`.
95     pub fn base_srcloc(&self) -> SourceLoc {
96         self.base_srcloc.unwrap_or_default()
97     }
98 
99     /// Sets the base `SourceLoc`, if not set yet, and returns the base value.
100     pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {
101         match self.base_srcloc {
102             Some(val) => val,
103             None => {
104                 self.base_srcloc = Some(srcloc);
105                 srcloc
106             }
107         }
108     }
109 
110     /// Retrieve a `UserExternalNameRef` for the given name, or add a new one.
111     ///
112     /// This method internally deduplicates same `UserExternalName` so they map to the same
113     /// reference.
114     pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {
115         if let Some(reff) = self.user_ext_name_to_ref.get(&name) {
116             *reff
117         } else {
118             let reff = self.user_named_funcs.push(name.clone());
119             self.user_ext_name_to_ref.insert(name, reff);
120             reff
121         }
122     }
123 
124     /// Resets an already existing user function name to a new value.
125     pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {
126         if let Some(prev_name) = self.user_named_funcs.get_mut(index) {
127             self.user_ext_name_to_ref.remove(prev_name);
128             *prev_name = name.clone();
129             self.user_ext_name_to_ref.insert(name, index);
130         }
131     }
132 
133     /// Returns the internal mapping of `UserExternalNameRef` to `UserExternalName`.
134     pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {
135         &self.user_named_funcs
136     }
137 
138     fn clear(&mut self) {
139         self.base_srcloc = None;
140         self.user_named_funcs.clear();
141         self.user_ext_name_to_ref.clear();
142     }
143 }
144 
145 /// Function fields needed when compiling a function.
146 ///
147 /// Additionally, these fields can be the same for two functions that would be compiled the same
148 /// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.
149 #[derive(Clone, PartialEq, Hash)]
150 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
151 pub struct FunctionStencil {
152     /// A version marker used to ensure that serialized clif ir is never deserialized with a
153     /// different version of Cranelift.
154     // Note: This must be the first field to ensure that Serde will deserialize it before
155     // attempting to deserialize other fields that are potentially changed between versions.
156     pub version_marker: VersionMarker,
157 
158     /// Signature of this function.
159     pub signature: Signature,
160 
161     /// Sized stack slots allocated in this function.
162     pub sized_stack_slots: StackSlots,
163 
164     /// Dynamic stack slots allocated in this function.
165     pub dynamic_stack_slots: DynamicStackSlots,
166 
167     /// Global values referenced.
168     pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
169 
170     /// Tables referenced.
171     pub tables: PrimaryMap<ir::Table, ir::TableData>,
172 
173     /// Data flow graph containing the primary definition of all instructions, blocks and values.
174     pub dfg: DataFlowGraph,
175 
176     /// Layout of blocks and instructions in the function body.
177     pub layout: Layout,
178 
179     /// Source locations.
180     ///
181     /// Track the original source location for each instruction. The source locations are not
182     /// interpreted by Cranelift, only preserved.
183     pub srclocs: SourceLocs,
184 
185     /// An optional global value which represents an expression evaluating to
186     /// the stack limit for this function. This `GlobalValue` will be
187     /// interpreted in the prologue, if necessary, to insert a stack check to
188     /// ensure that a trap happens if the stack pointer goes below the
189     /// threshold specified here.
190     pub stack_limit: Option<ir::GlobalValue>,
191 }
192 
193 impl FunctionStencil {
194     fn clear(&mut self) {
195         self.signature.clear(CallConv::Fast);
196         self.sized_stack_slots.clear();
197         self.dynamic_stack_slots.clear();
198         self.global_values.clear();
199         self.tables.clear();
200         self.dfg.clear();
201         self.layout.clear();
202         self.srclocs.clear();
203         self.stack_limit = None;
204     }
205 
206     /// Creates a jump table in the function, to be used by `br_table` instructions.
207     pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
208         self.dfg.jump_tables.push(data)
209     }
210 
211     /// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`
212     /// and `stack_addr` instructions.
213     pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
214         self.sized_stack_slots.push(data)
215     }
216 
217     /// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,
218     /// `dynamic_stack_store` and `dynamic_stack_addr` instructions.
219     pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
220         self.dynamic_stack_slots.push(data)
221     }
222 
223     /// Adds a signature which can later be used to declare an external function import.
224     pub fn import_signature(&mut self, signature: Signature) -> SigRef {
225         self.dfg.signatures.push(signature)
226     }
227 
228     /// Declares a global value accessible to the function.
229     pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
230         self.global_values.push(data)
231     }
232 
233     /// Find the global dyn_scale value associated with given DynamicType
234     pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
235         self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
236     }
237 
238     /// Find the global dyn_scale for the given stack slot.
239     pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
240         let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
241         self.get_dyn_scale(dyn_ty)
242     }
243 
244     /// Get a concrete `Type` from a user defined `DynamicType`.
245     pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
246         self.dfg
247             .dynamic_types
248             .get(ty)
249             .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {}", ty))
250             .concrete()
251     }
252 
253     /// Declares a table accessible to the function.
254     pub fn create_table(&mut self, data: TableData) -> Table {
255         self.tables.push(data)
256     }
257 
258     /// Find a presumed unique special-purpose function parameter value.
259     ///
260     /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
261     pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
262         let entry = self.layout.entry_block().expect("Function is empty");
263         self.signature
264             .special_param_index(purpose)
265             .map(|i| self.dfg.block_params(entry)[i])
266     }
267 
268     /// Starts collection of debug information.
269     pub fn collect_debug_info(&mut self) {
270         self.dfg.collect_debug_info();
271     }
272 
273     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
274     /// Does nothing if called with a non-jump or non-branch instruction.
275     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
276         match &mut self.dfg.insts[inst] {
277             InstructionData::Jump {
278                 destination: dest, ..
279             } => {
280                 if dest.block(&self.dfg.value_lists) == old_dest {
281                     dest.set_block(new_dest, &mut self.dfg.value_lists)
282                 }
283             }
284 
285             InstructionData::Brif {
286                 blocks: [block_then, block_else],
287                 ..
288             } => {
289                 if block_then.block(&self.dfg.value_lists) == old_dest {
290                     block_then.set_block(new_dest, &mut self.dfg.value_lists);
291                 }
292 
293                 if block_else.block(&self.dfg.value_lists) == old_dest {
294                     block_else.set_block(new_dest, &mut self.dfg.value_lists);
295                 }
296             }
297 
298             InstructionData::BranchTable { table, .. } => {
299                 for entry in self.dfg.jump_tables[*table].all_branches_mut() {
300                     if *entry == old_dest {
301                         *entry = new_dest;
302                     }
303                 }
304             }
305 
306             inst => debug_assert!(!inst.opcode().is_branch()),
307         }
308     }
309 
310     /// Checks that the specified block can be encoded as a basic block.
311     ///
312     /// On error, returns the first invalid instruction and an error message.
313     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
314         let dfg = &self.dfg;
315         let inst_iter = self.layout.block_insts(block);
316 
317         // Ignore all instructions prior to the first branch.
318         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
319 
320         // A conditional branch is permitted in a basic block only when followed
321         // by a terminal jump instruction.
322         if let Some(_branch) = inst_iter.next() {
323             if let Some(next) = inst_iter.next() {
324                 match dfg.insts[next].opcode() {
325                     Opcode::Jump => (),
326                     _ => return Err((next, "post-branch instruction not jump")),
327                 }
328             }
329         }
330 
331         Ok(())
332     }
333 
334     /// Returns true if the function is function that doesn't call any other functions. This is not
335     /// to be confused with a "leaf function" in Windows terminology.
336     pub fn is_leaf(&self) -> bool {
337         // Conservative result: if there's at least one function signature referenced in this
338         // function, assume it is not a leaf.
339         self.dfg.signatures.is_empty()
340     }
341 
342     /// Replace the `dst` instruction's data with the `src` instruction's data
343     /// and then remove `src`.
344     ///
345     /// `src` and its result values should not be used at all, as any uses would
346     /// be left dangling after calling this method.
347     ///
348     /// `src` and `dst` must have the same number of resulting values, and
349     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
350     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
351         debug_assert_eq!(
352             self.dfg.inst_results(dst).len(),
353             self.dfg.inst_results(src).len()
354         );
355         debug_assert!(self
356             .dfg
357             .inst_results(dst)
358             .iter()
359             .zip(self.dfg.inst_results(src))
360             .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
361 
362         self.dfg.insts[dst] = self.dfg.insts[src];
363         self.layout.remove_inst(src);
364     }
365 
366     /// Size occupied by all stack slots associated with this function.
367     ///
368     /// Does not include any padding necessary due to offsets
369     pub fn fixed_stack_size(&self) -> u32 {
370         self.sized_stack_slots.values().map(|ss| ss.size).sum()
371     }
372 
373     /// Returns the list of relative source locations for this function.
374     pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
375         &self.srclocs
376     }
377 }
378 
379 /// Functions can be cloned, but it is not a very fast operation.
380 /// The clone will have all the same entity numbers as the original.
381 #[derive(Clone)]
382 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
383 pub struct Function {
384     /// Name of this function.
385     ///
386     /// Mostly used by `.clif` files, only there for debugging / naming purposes.
387     pub name: UserFuncName,
388 
389     /// All the fields required for compiling a function, independently of details irrelevant to
390     /// compilation and that are stored in the `FunctionParameters` `params` field instead.
391     pub stencil: FunctionStencil,
392 
393     /// All the parameters that can be applied onto the function stencil, that is, that don't
394     /// matter when caching compilation artifacts.
395     pub params: FunctionParameters,
396 }
397 
398 impl core::ops::Deref for Function {
399     type Target = FunctionStencil;
400 
401     fn deref(&self) -> &Self::Target {
402         &self.stencil
403     }
404 }
405 
406 impl core::ops::DerefMut for Function {
407     fn deref_mut(&mut self) -> &mut Self::Target {
408         &mut self.stencil
409     }
410 }
411 
412 impl Function {
413     /// Create a function with the given name and signature.
414     pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
415         Self {
416             name,
417             stencil: FunctionStencil {
418                 version_marker: VersionMarker,
419                 signature: sig,
420                 sized_stack_slots: StackSlots::new(),
421                 dynamic_stack_slots: DynamicStackSlots::new(),
422                 global_values: PrimaryMap::new(),
423                 tables: PrimaryMap::new(),
424                 dfg: DataFlowGraph::new(),
425                 layout: Layout::new(),
426                 srclocs: SecondaryMap::new(),
427                 stack_limit: None,
428             },
429             params: FunctionParameters::new(),
430         }
431     }
432 
433     /// Clear all data structures in this function.
434     pub fn clear(&mut self) {
435         self.stencil.clear();
436         self.params.clear();
437         self.name = UserFuncName::default();
438     }
439 
440     /// Create a new empty, anonymous function with a Fast calling convention.
441     pub fn new() -> Self {
442         Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
443     }
444 
445     /// Return an object that can display this function with correct ISA-specific annotations.
446     pub fn display(&self) -> DisplayFunction<'_> {
447         DisplayFunction(self, Default::default())
448     }
449 
450     /// Return an object that can display this function with correct ISA-specific annotations.
451     pub fn display_with<'a>(
452         &'a self,
453         annotations: DisplayFunctionAnnotations<'a>,
454     ) -> DisplayFunction<'a> {
455         DisplayFunction(self, annotations)
456     }
457 
458     /// Sets an absolute source location for the given instruction.
459     ///
460     /// If no base source location has been set yet, records it at the same time.
461     pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
462         let base = self.params.ensure_base_srcloc(srcloc);
463         self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
464     }
465 
466     /// Returns an absolute source location for the given instruction.
467     pub fn srcloc(&self, inst: Inst) -> SourceLoc {
468         let base = self.params.base_srcloc();
469         self.stencil.srclocs[inst].expand(base)
470     }
471 
472     /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.
473     pub fn declare_imported_user_function(
474         &mut self,
475         name: UserExternalName,
476     ) -> UserExternalNameRef {
477         self.params.ensure_user_func_name(name)
478     }
479 
480     /// Declare an external function import.
481     pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
482         self.stencil.dfg.ext_funcs.push(data)
483     }
484 }
485 
486 /// Additional annotations for function display.
487 #[derive(Default)]
488 pub struct DisplayFunctionAnnotations<'a> {
489     /// Enable value labels annotations.
490     pub value_ranges: Option<&'a ValueLabelsRanges>,
491 }
492 
493 /// Wrapper type capable of displaying a `Function` with correct ISA annotations.
494 pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
495 
496 impl<'a> fmt::Display for DisplayFunction<'a> {
497     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
498         write_function(fmt, self.0)
499     }
500 }
501 
502 impl fmt::Display for Function {
503     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
504         write_function(fmt, self)
505     }
506 }
507 
508 impl fmt::Debug for Function {
509     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
510         write_function(fmt, self)
511     }
512 }
513