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