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, JumpTables, 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     /// Jump tables used in this function.
174     pub jump_tables: JumpTables,
175 
176     /// Data flow graph containing the primary definition of all instructions, blocks and values.
177     pub dfg: DataFlowGraph,
178 
179     /// Layout of blocks and instructions in the function body.
180     pub layout: Layout,
181 
182     /// Source locations.
183     ///
184     /// Track the original source location for each instruction. The source locations are not
185     /// interpreted by Cranelift, only preserved.
186     pub srclocs: SourceLocs,
187 
188     /// An optional global value which represents an expression evaluating to
189     /// the stack limit for this function. This `GlobalValue` will be
190     /// interpreted in the prologue, if necessary, to insert a stack check to
191     /// ensure that a trap happens if the stack pointer goes below the
192     /// threshold specified here.
193     pub stack_limit: Option<ir::GlobalValue>,
194 }
195 
196 impl FunctionStencil {
197     fn clear(&mut self) {
198         self.signature.clear(CallConv::Fast);
199         self.sized_stack_slots.clear();
200         self.dynamic_stack_slots.clear();
201         self.global_values.clear();
202         self.tables.clear();
203         self.jump_tables.clear();
204         self.dfg.clear();
205         self.layout.clear();
206         self.srclocs.clear();
207         self.stack_limit = None;
208     }
209 
210     /// Creates a jump table in the function, to be used by `br_table` instructions.
211     pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
212         self.jump_tables.push(data)
213     }
214 
215     /// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`
216     /// and `stack_addr` instructions.
217     pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
218         self.sized_stack_slots.push(data)
219     }
220 
221     /// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,
222     /// `dynamic_stack_store` and `dynamic_stack_addr` instructions.
223     pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
224         self.dynamic_stack_slots.push(data)
225     }
226 
227     /// Adds a signature which can later be used to declare an external function import.
228     pub fn import_signature(&mut self, signature: Signature) -> SigRef {
229         self.dfg.signatures.push(signature)
230     }
231 
232     /// Declares a global value accessible to the function.
233     pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
234         self.global_values.push(data)
235     }
236 
237     /// Find the global dyn_scale value associated with given DynamicType
238     pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
239         self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
240     }
241 
242     /// Find the global dyn_scale for the given stack slot.
243     pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
244         let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
245         self.get_dyn_scale(dyn_ty)
246     }
247 
248     /// Get a concrete `Type` from a user defined `DynamicType`.
249     pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
250         self.dfg
251             .dynamic_types
252             .get(ty)
253             .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {}", ty))
254             .concrete()
255     }
256 
257     /// Declares a table accessible to the function.
258     pub fn create_table(&mut self, data: TableData) -> Table {
259         self.tables.push(data)
260     }
261 
262     /// Find a presumed unique special-purpose function parameter value.
263     ///
264     /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
265     pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
266         let entry = self.layout.entry_block().expect("Function is empty");
267         self.signature
268             .special_param_index(purpose)
269             .map(|i| self.dfg.block_params(entry)[i])
270     }
271 
272     /// Starts collection of debug information.
273     pub fn collect_debug_info(&mut self) {
274         self.dfg.collect_debug_info();
275     }
276 
277     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
278     /// Does nothing if called with a non-jump or non-branch instruction.
279     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
280         match &mut self.dfg.insts[inst] {
281             InstructionData::Jump {
282                 destination: dest, ..
283             } => {
284                 if dest.block(&self.dfg.value_lists) == old_dest {
285                     dest.set_block(new_dest, &mut self.dfg.value_lists)
286                 }
287             }
288 
289             InstructionData::Brif {
290                 blocks: [block_then, block_else],
291                 ..
292             } => {
293                 if block_then.block(&self.dfg.value_lists) == old_dest {
294                     block_then.set_block(new_dest, &mut self.dfg.value_lists);
295                 }
296 
297                 if block_else.block(&self.dfg.value_lists) == old_dest {
298                     block_else.set_block(new_dest, &mut self.dfg.value_lists);
299                 }
300             }
301 
302             InstructionData::BranchTable {
303                 table,
304                 destination: default_dest,
305                 ..
306             } => {
307                 self.jump_tables[*table].iter_mut().for_each(|entry| {
308                     if *entry == old_dest {
309                         *entry = new_dest;
310                     }
311                 });
312 
313                 if *default_dest == old_dest {
314                     *default_dest = new_dest;
315                 }
316             }
317 
318             inst => debug_assert!(!inst.opcode().is_branch()),
319         }
320     }
321 
322     /// Checks that the specified block can be encoded as a basic block.
323     ///
324     /// On error, returns the first invalid instruction and an error message.
325     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
326         let dfg = &self.dfg;
327         let inst_iter = self.layout.block_insts(block);
328 
329         // Ignore all instructions prior to the first branch.
330         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
331 
332         // A conditional branch is permitted in a basic block only when followed
333         // by a terminal jump instruction.
334         if let Some(_branch) = inst_iter.next() {
335             if let Some(next) = inst_iter.next() {
336                 match dfg.insts[next].opcode() {
337                     Opcode::Jump => (),
338                     _ => return Err((next, "post-branch instruction not jump")),
339                 }
340             }
341         }
342 
343         Ok(())
344     }
345 
346     /// Returns true if the function is function that doesn't call any other functions. This is not
347     /// to be confused with a "leaf function" in Windows terminology.
348     pub fn is_leaf(&self) -> bool {
349         // Conservative result: if there's at least one function signature referenced in this
350         // function, assume it is not a leaf.
351         self.dfg.signatures.is_empty()
352     }
353 
354     /// Replace the `dst` instruction's data with the `src` instruction's data
355     /// and then remove `src`.
356     ///
357     /// `src` and its result values should not be used at all, as any uses would
358     /// be left dangling after calling this method.
359     ///
360     /// `src` and `dst` must have the same number of resulting values, and
361     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
362     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
363         debug_assert_eq!(
364             self.dfg.inst_results(dst).len(),
365             self.dfg.inst_results(src).len()
366         );
367         debug_assert!(self
368             .dfg
369             .inst_results(dst)
370             .iter()
371             .zip(self.dfg.inst_results(src))
372             .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
373 
374         self.dfg.insts[dst] = self.dfg.insts[src];
375         self.layout.remove_inst(src);
376     }
377 
378     /// Size occupied by all stack slots associated with this function.
379     ///
380     /// Does not include any padding necessary due to offsets
381     pub fn fixed_stack_size(&self) -> u32 {
382         self.sized_stack_slots.values().map(|ss| ss.size).sum()
383     }
384 
385     /// Returns the list of relative source locations for this function.
386     pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
387         &self.srclocs
388     }
389 }
390 
391 /// Functions can be cloned, but it is not a very fast operation.
392 /// The clone will have all the same entity numbers as the original.
393 #[derive(Clone)]
394 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
395 pub struct Function {
396     /// Name of this function.
397     ///
398     /// Mostly used by `.clif` files, only there for debugging / naming purposes.
399     pub name: UserFuncName,
400 
401     /// All the fields required for compiling a function, independently of details irrelevant to
402     /// compilation and that are stored in the `FunctionParameters` `params` field instead.
403     pub stencil: FunctionStencil,
404 
405     /// All the parameters that can be applied onto the function stencil, that is, that don't
406     /// matter when caching compilation artifacts.
407     pub params: FunctionParameters,
408 }
409 
410 impl core::ops::Deref for Function {
411     type Target = FunctionStencil;
412 
413     fn deref(&self) -> &Self::Target {
414         &self.stencil
415     }
416 }
417 
418 impl core::ops::DerefMut for Function {
419     fn deref_mut(&mut self) -> &mut Self::Target {
420         &mut self.stencil
421     }
422 }
423 
424 impl Function {
425     /// Create a function with the given name and signature.
426     pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
427         Self {
428             name,
429             stencil: FunctionStencil {
430                 version_marker: VersionMarker,
431                 signature: sig,
432                 sized_stack_slots: StackSlots::new(),
433                 dynamic_stack_slots: DynamicStackSlots::new(),
434                 global_values: PrimaryMap::new(),
435                 tables: PrimaryMap::new(),
436                 jump_tables: PrimaryMap::new(),
437                 dfg: DataFlowGraph::new(),
438                 layout: Layout::new(),
439                 srclocs: SecondaryMap::new(),
440                 stack_limit: None,
441             },
442             params: FunctionParameters::new(),
443         }
444     }
445 
446     /// Clear all data structures in this function.
447     pub fn clear(&mut self) {
448         self.stencil.clear();
449         self.params.clear();
450         self.name = UserFuncName::default();
451     }
452 
453     /// Create a new empty, anonymous function with a Fast calling convention.
454     pub fn new() -> Self {
455         Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
456     }
457 
458     /// Return an object that can display this function with correct ISA-specific annotations.
459     pub fn display(&self) -> DisplayFunction<'_> {
460         DisplayFunction(self, Default::default())
461     }
462 
463     /// Return an object that can display this function with correct ISA-specific annotations.
464     pub fn display_with<'a>(
465         &'a self,
466         annotations: DisplayFunctionAnnotations<'a>,
467     ) -> DisplayFunction<'a> {
468         DisplayFunction(self, annotations)
469     }
470 
471     /// Sets an absolute source location for the given instruction.
472     ///
473     /// If no base source location has been set yet, records it at the same time.
474     pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
475         let base = self.params.ensure_base_srcloc(srcloc);
476         self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
477     }
478 
479     /// Returns an absolute source location for the given instruction.
480     pub fn srcloc(&self, inst: Inst) -> SourceLoc {
481         let base = self.params.base_srcloc();
482         self.stencil.srclocs[inst].expand(base)
483     }
484 
485     /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.
486     pub fn declare_imported_user_function(
487         &mut self,
488         name: UserExternalName,
489     ) -> UserExternalNameRef {
490         self.params.ensure_user_func_name(name)
491     }
492 
493     /// Declare an external function import.
494     pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
495         self.stencil.dfg.ext_funcs.push(data)
496     }
497 }
498 
499 /// Additional annotations for function display.
500 #[derive(Default)]
501 pub struct DisplayFunctionAnnotations<'a> {
502     /// Enable value labels annotations.
503     pub value_ranges: Option<&'a ValueLabelsRanges>,
504 }
505 
506 /// Wrapper type capable of displaying a `Function` with correct ISA annotations.
507 pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
508 
509 impl<'a> fmt::Display for DisplayFunction<'a> {
510     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
511         write_function(fmt, self.0)
512     }
513 }
514 
515 impl fmt::Display for Function {
516     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
517         write_function(fmt, self)
518     }
519 }
520 
521 impl fmt::Debug for Function {
522     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
523         write_function(fmt, self)
524     }
525 }
526