Skip to main content

cranelift_frontend/
frontend.rs

1//! A frontend for building Cranelift IR from other languages.
2use crate::ssa::{SSABuilder, SideEffects};
3use crate::variable::Variable;
4use alloc::{vec, vec::Vec};
5use core::fmt::{self, Debug};
6use cranelift_codegen::cursor::{Cursor, CursorPosition, FuncCursor};
7use cranelift_codegen::entity::{EntityRef, EntitySet, PrimaryMap, SecondaryMap};
8use cranelift_codegen::ir;
9use cranelift_codegen::ir::condcodes::IntCC;
10use cranelift_codegen::ir::{
11    AbiParam, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, ExtFuncData,
12    ExternalName, FuncRef, Function, GlobalValue, GlobalValueData, Inst, InstBuilder,
13    InstBuilderBase, InstructionData, JumpTable, JumpTableData, LibCall, MemFlagsData,
14    RelSourceLoc, SigRef, Signature, StackSlot, StackSlotData, Type, Value, ValueLabel,
15    ValueLabelAssignments, ValueLabelStart, types,
16};
17use cranelift_codegen::isa::TargetFrontendConfig;
18use cranelift_codegen::packed_option::PackedOption;
19use cranelift_codegen::traversals::Dfs;
20use smallvec::SmallVec;
21
22mod safepoints;
23
24/// Structure used for translating a series of functions into Cranelift IR.
25///
26/// In order to reduce memory reallocations when compiling multiple functions,
27/// [`FunctionBuilderContext`] holds various data structures which are cleared between
28/// functions, rather than dropped, preserving the underlying allocations.
29#[derive(Default)]
30pub struct FunctionBuilderContext {
31    ssa: SSABuilder,
32    status: SecondaryMap<Block, BlockStatus>,
33    variables: PrimaryMap<Variable, Type>,
34    safepoints: safepoints::SafepointSpiller,
35}
36
37/// Temporary object used to build a single Cranelift IR [`Function`].
38pub struct FunctionBuilder<'a> {
39    /// The function currently being built.
40    /// This field is public so the function can be re-borrowed.
41    pub func: &'a mut Function,
42
43    /// Source location to assign to all new instructions.
44    srcloc: ir::SourceLoc,
45
46    func_ctx: &'a mut FunctionBuilderContext,
47    position: PackedOption<Block>,
48}
49
50#[derive(Clone, Default, Eq, PartialEq)]
51enum BlockStatus {
52    /// No instructions have been added.
53    #[default]
54    Empty,
55    /// Some instructions have been added, but no terminator.
56    Partial,
57    /// A terminator has been added; no further instructions may be added.
58    Filled,
59}
60
61impl FunctionBuilderContext {
62    /// Creates a [`FunctionBuilderContext`] structure. The structure is automatically cleared after
63    /// each [`FunctionBuilder`] completes translating a function.
64    pub fn new() -> Self {
65        Self::default()
66    }
67
68    fn clear(&mut self) {
69        let FunctionBuilderContext {
70            ssa,
71            status,
72            variables,
73            safepoints,
74        } = self;
75        ssa.clear();
76        status.clear();
77        variables.clear();
78        safepoints.clear();
79    }
80
81    fn is_empty(&self) -> bool {
82        self.ssa.is_empty() && self.status.is_empty() && self.variables.is_empty()
83    }
84}
85
86/// Implementation of the [`InstBuilder`] that has
87/// one convenience method per Cranelift IR instruction.
88pub struct FuncInstBuilder<'short, 'long: 'short> {
89    builder: &'short mut FunctionBuilder<'long>,
90    block: Block,
91}
92
93impl<'short, 'long> FuncInstBuilder<'short, 'long> {
94    fn new(builder: &'short mut FunctionBuilder<'long>, block: Block) -> Self {
95        Self { builder, block }
96    }
97}
98
99impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {
100    fn data_flow_graph(&self) -> &DataFlowGraph {
101        &self.builder.func.dfg
102    }
103
104    fn data_flow_graph_mut(&mut self) -> &mut DataFlowGraph {
105        &mut self.builder.func.dfg
106    }
107
108    // This implementation is richer than `InsertBuilder` because we use the data of the
109    // instruction being inserted to add related info to the DFG and the SSA building system,
110    // and perform debug sanity checks.
111    fn build(self, data: InstructionData, ctrl_typevar: Type) -> (Inst, &'short mut DataFlowGraph) {
112        // We only insert the Block in the layout when an instruction is added to it
113        self.builder.ensure_inserted_block();
114
115        let inst = self.builder.func.dfg.make_inst(data);
116        self.builder.func.dfg.make_inst_results(inst, ctrl_typevar);
117        self.builder.func.layout.append_inst(inst, self.block);
118        if !self.builder.srcloc.is_default() {
119            self.builder.func.set_srcloc(inst, self.builder.srcloc);
120        }
121
122        match &self.builder.func.dfg.insts[inst] {
123            ir::InstructionData::Jump {
124                destination: dest, ..
125            } => {
126                // If the user has supplied jump arguments we must adapt the arguments of
127                // the destination block
128                let block = dest.block(&self.builder.func.dfg.value_lists);
129                self.builder.declare_successor(block, inst);
130            }
131
132            ir::InstructionData::Brif {
133                blocks: [branch_then, branch_else],
134                ..
135            } => {
136                let block_then = branch_then.block(&self.builder.func.dfg.value_lists);
137                let block_else = branch_else.block(&self.builder.func.dfg.value_lists);
138
139                self.builder.declare_successor(block_then, inst);
140                if block_then != block_else {
141                    self.builder.declare_successor(block_else, inst);
142                }
143            }
144
145            ir::InstructionData::BranchTable { table, .. } => {
146                let pool = &self.builder.func.dfg.value_lists;
147
148                // Unlike most other jumps/branches and like try_call,
149                // jump tables are capable of having the same successor appear
150                // multiple times, so we must deduplicate.
151                let mut unique = EntitySet::<Block>::new();
152                for dest_block in self
153                    .builder
154                    .func
155                    .stencil
156                    .dfg
157                    .jump_tables
158                    .get(*table)
159                    .expect("you are referencing an undeclared jump table")
160                    .all_branches()
161                {
162                    let block = dest_block.block(pool);
163                    if !unique.insert(block) {
164                        continue;
165                    }
166
167                    // Call `declare_block_predecessor` instead of `declare_successor` for
168                    // avoiding the borrow checker.
169                    self.builder
170                        .func_ctx
171                        .ssa
172                        .declare_block_predecessor(block, inst);
173                }
174            }
175
176            ir::InstructionData::TryCall { exception, .. }
177            | ir::InstructionData::TryCallIndirect { exception, .. } => {
178                let pool = &self.builder.func.dfg.value_lists;
179
180                // Unlike most other jumps/branches and like br_table,
181                // exception tables are capable of having the same successor
182                // appear multiple times, so we must deduplicate.
183                let mut unique = EntitySet::<Block>::new();
184                for dest_block in self
185                    .builder
186                    .func
187                    .stencil
188                    .dfg
189                    .exception_tables
190                    .get(*exception)
191                    .expect("you are referencing an undeclared exception table")
192                    .all_branches()
193                {
194                    let block = dest_block.block(pool);
195                    if !unique.insert(block) {
196                        continue;
197                    }
198
199                    // Call `declare_block_predecessor` instead of `declare_successor` for
200                    // avoiding the borrow checker.
201                    self.builder
202                        .func_ctx
203                        .ssa
204                        .declare_block_predecessor(block, inst);
205                }
206            }
207
208            inst => assert!(!inst.opcode().is_branch()),
209        }
210
211        if data.opcode().is_terminator() {
212            self.builder.fill_current_block()
213        }
214        (inst, &mut self.builder.func.dfg)
215    }
216
217    fn build_aux_inst(&mut self, data: InstructionData, ctrl_typevar: Type) -> Inst {
218        // Reborrow the underlying `FunctionBuilder` to append the auxiliary
219        // instruction to the current block, leaving `self` intact so the
220        // caller can still build its final instruction.
221        self.builder.ins().build(data, ctrl_typevar).0
222    }
223}
224
225#[derive(Debug, Copy, Clone, PartialEq, Eq)]
226/// An error encountered when calling [`FunctionBuilder::try_use_var`].
227pub enum UseVariableError {
228    UsedBeforeDeclared(Variable),
229}
230
231impl fmt::Display for UseVariableError {
232    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233        match self {
234            UseVariableError::UsedBeforeDeclared(variable) => {
235                write!(
236                    f,
237                    "variable {} was used before it was defined",
238                    variable.index()
239                )?;
240            }
241        }
242        Ok(())
243    }
244}
245
246impl core::error::Error for UseVariableError {}
247
248#[derive(Debug, Copy, Clone, Eq, PartialEq)]
249/// An error encountered when defining the initial value of a variable.
250pub enum DefVariableError {
251    /// The variable was instantiated with a value of the wrong type.
252    ///
253    /// note: to obtain the type of the value, you can call
254    /// [`cranelift_codegen::ir::dfg::DataFlowGraph::value_type`] (using the
255    /// `FunctionBuilder.func.dfg` field)
256    TypeMismatch(Variable, Value),
257    /// The value was defined (in a call to [`FunctionBuilder::def_var`]) before
258    /// it was declared (in a call to [`FunctionBuilder::declare_var`]).
259    DefinedBeforeDeclared(Variable),
260}
261
262impl fmt::Display for DefVariableError {
263    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264        match self {
265            DefVariableError::TypeMismatch(variable, value) => {
266                write!(
267                    f,
268                    "the types of variable {} and value {} are not the same.
269                    The `Value` supplied to `def_var` must be of the same type as
270                    the variable was declared to be of in `declare_var`.",
271                    variable.index(),
272                    value.as_u32()
273                )?;
274            }
275            DefVariableError::DefinedBeforeDeclared(variable) => {
276                write!(
277                    f,
278                    "the value of variable {} was declared before it was defined",
279                    variable.index()
280                )?;
281            }
282        }
283        Ok(())
284    }
285}
286
287/// This module allows you to create a function in Cranelift IR in a straightforward way, hiding
288/// all the complexity of its internal representation.
289///
290/// The module is parametrized by one type which is the representation of variables in your
291/// origin language. It offers a way to conveniently append instruction to your program flow.
292/// You are responsible to split your instruction flow into extended blocks (declared with
293/// [`create_block`](Self::create_block)) whose properties are:
294///
295/// - branch and jump instructions can only point at the top of extended blocks;
296/// - the last instruction of each block is a terminator instruction which has no natural successor,
297///   and those instructions can only appear at the end of extended blocks.
298///
299/// The parameters of Cranelift IR instructions are Cranelift IR values, which can only be created
300/// as results of other Cranelift IR instructions. To be able to create variables redefined multiple
301/// times in your program, use the [`def_var`](Self::def_var) and [`use_var`](Self::use_var) command,
302/// that will maintain the correspondence between your variables and Cranelift IR SSA values.
303///
304/// The first block for which you call [`switch_to_block`](Self::switch_to_block) will be assumed to
305/// be the beginning of the function.
306///
307/// At creation, a [`FunctionBuilder`] instance borrows an already allocated `Function` which it
308/// modifies with the information stored in the mutable borrowed
309/// [`FunctionBuilderContext`]. The function passed in argument should be newly created with
310/// [`Function::with_name_signature()`], whereas the [`FunctionBuilderContext`] can be kept as is
311/// between two function translations.
312///
313/// # Errors
314///
315/// The functions below will panic in debug mode whenever you try to modify the Cranelift IR
316/// function in a way that violate the coherence of the code. For instance: switching to a new
317/// [`Block`] when you haven't filled the current one with a terminator instruction, inserting a
318/// return instruction with arguments that don't match the function's signature.
319impl<'a> FunctionBuilder<'a> {
320    /// Creates a new [`FunctionBuilder`] structure that will operate on a [`Function`] using a
321    /// [`FunctionBuilderContext`].
322    pub fn new(func: &'a mut Function, func_ctx: &'a mut FunctionBuilderContext) -> Self {
323        debug_assert!(func_ctx.is_empty());
324        Self {
325            func,
326            srcloc: Default::default(),
327            func_ctx,
328            position: Default::default(),
329        }
330    }
331
332    /// Get the block that this builder is currently at.
333    pub fn current_block(&self) -> Option<Block> {
334        self.position.expand()
335    }
336
337    /// Set the source location that should be assigned to all new instructions.
338    pub fn set_srcloc(&mut self, srcloc: ir::SourceLoc) {
339        self.srcloc = srcloc;
340    }
341
342    /// Get the current source location that this builder is using.
343    pub fn srcloc(&self) -> ir::SourceLoc {
344        self.srcloc
345    }
346
347    /// Creates a new [`Block`] and returns its reference.
348    pub fn create_block(&mut self) -> Block {
349        let block = self.func.dfg.make_block();
350        self.func_ctx.ssa.declare_block(block);
351        block
352    }
353
354    /// Mark a block as "cold".
355    ///
356    /// This will try to move it out of the ordinary path of execution
357    /// when lowered to machine code.
358    pub fn set_cold_block(&mut self, block: Block) {
359        self.func.layout.set_cold(block);
360    }
361
362    /// Insert `block` in the layout *after* the existing block `after`.
363    pub fn insert_block_after(&mut self, block: Block, after: Block) {
364        self.func.layout.insert_block_after(block, after);
365    }
366
367    /// After the call to this function, new instructions will be inserted into the designated
368    /// block, in the order they are declared. You must declare the types of the [`Block`] arguments
369    /// you will use here.
370    ///
371    /// When inserting the terminator instruction (which doesn't have a fallthrough to its immediate
372    /// successor), the block will be declared filled and it will not be possible to append
373    /// instructions to it.
374    pub fn switch_to_block(&mut self, block: Block) {
375        log::trace!("switch to {block:?}");
376
377        // First we check that the previous block has been filled.
378        debug_assert!(
379            self.position.is_none()
380                || self.is_unreachable()
381                || self.is_pristine(self.position.unwrap())
382                || self.is_filled(self.position.unwrap()),
383            "you have to fill your block before switching"
384        );
385        // We cannot switch to a filled block
386        debug_assert!(
387            !self.is_filled(block),
388            "you cannot switch to a block which is already filled"
389        );
390
391        // Then we change the cursor position.
392        self.position = PackedOption::from(block);
393    }
394
395    /// Declares that all the predecessors of this block are known.
396    ///
397    /// Function to call with `block` as soon as the last branch instruction to `block` has been
398    /// created. Forgetting to call this method on every block will cause inconsistencies in the
399    /// produced functions.
400    pub fn seal_block(&mut self, block: Block) {
401        let side_effects = self.func_ctx.ssa.seal_block(block, self.func);
402        self.handle_ssa_side_effects(side_effects);
403    }
404
405    /// Effectively calls [seal_block](Self::seal_block) on all unsealed blocks in the function.
406    ///
407    /// It's more efficient to seal [`Block`]s as soon as possible, during
408    /// translation, but for frontends where this is impractical to do, this
409    /// function can be used at the end of translating all blocks to ensure
410    /// that everything is sealed.
411    pub fn seal_all_blocks(&mut self) {
412        let side_effects = self.func_ctx.ssa.seal_all_blocks(self.func);
413        self.handle_ssa_side_effects(side_effects);
414    }
415
416    /// Declares the type of a variable.
417    ///
418    /// This allows the variable to be defined and used later (by calling
419    /// [`FunctionBuilder::def_var`] and [`FunctionBuilder::use_var`]
420    /// respectively).
421    pub fn declare_var(&mut self, ty: Type) -> Variable {
422        self.func_ctx.variables.push(ty)
423    }
424
425    /// Declare that all uses of the given variable must be included in stack
426    /// map metadata.
427    ///
428    /// All values that are uses of this variable will be spilled to the stack
429    /// before each safepoint and reloaded afterwards. Stack maps allow the
430    /// garbage collector to identify the on-stack GC roots. Between spilling
431    /// the stack and it being reloading again, the stack can be updated to
432    /// facilitate moving GCs.
433    ///
434    /// This must be called before any definition of the variable.
435    ///
436    /// # Panics
437    ///
438    /// Panics if the variable's type is larger than 16 bytes or if this
439    /// variable has not been declared yet. In debug builds, also panics if
440    /// the variable has already been defined.
441    pub fn declare_var_needs_stack_map(&mut self, var: Variable) {
442        log::trace!("declare_var_needs_stack_map({var:?})");
443        let ty = self.func_ctx.variables[var];
444        assert!(ty != types::INVALID);
445        assert!(ty.bytes() <= 16);
446        self.func_ctx.ssa.mark_var_needs_stack_map(var);
447    }
448
449    /// Returns the Cranelift IR necessary to use a previously defined user
450    /// variable, returning an error if this is not possible.
451    pub fn try_use_var(&mut self, var: Variable) -> Result<Value, UseVariableError> {
452        // Assert that we're about to add instructions to this block using the definition of the
453        // given variable. ssa.use_var is the only part of this crate which can add block parameters
454        // behind the caller's back. If we disallow calling append_block_param as soon as use_var is
455        // called, then we enforce a strict separation between user parameters and SSA parameters.
456        self.ensure_inserted_block();
457
458        let (val, side_effects) = {
459            let ty = *self
460                .func_ctx
461                .variables
462                .get(var)
463                .ok_or(UseVariableError::UsedBeforeDeclared(var))?;
464            debug_assert_ne!(
465                ty,
466                types::INVALID,
467                "variable {var:?} is used but its type has not been declared"
468            );
469            self.func_ctx
470                .ssa
471                .use_var(self.func, var, ty, self.position.unwrap())
472        };
473        self.handle_ssa_side_effects(side_effects);
474
475        Ok(val)
476    }
477
478    /// Returns the Cranelift IR value corresponding to the utilization at the current program
479    /// position of a previously defined user variable.
480    pub fn use_var(&mut self, var: Variable) -> Value {
481        self.try_use_var(var).unwrap_or_else(|_| {
482            panic!("variable {var:?} is used but its type has not been declared")
483        })
484    }
485
486    /// Registers a new definition of a user variable. This function will return
487    /// an error if the value supplied does not match the type the variable was
488    /// declared to have.
489    pub fn try_def_var(&mut self, var: Variable, val: Value) -> Result<(), DefVariableError> {
490        log::trace!("try_def_var: {var:?} = {val:?}");
491
492        let var_ty = *self
493            .func_ctx
494            .variables
495            .get(var)
496            .ok_or(DefVariableError::DefinedBeforeDeclared(var))?;
497        if var_ty != self.func.dfg.value_type(val) {
498            return Err(DefVariableError::TypeMismatch(var, val));
499        }
500
501        self.func_ctx.ssa.def_var(var, val, self.position.unwrap());
502        Ok(())
503    }
504
505    /// Register a new definition of a user variable. The type of the value must be
506    /// the same as the type registered for the variable.
507    pub fn def_var(&mut self, var: Variable, val: Value) {
508        self.try_def_var(var, val)
509            .unwrap_or_else(|error| match error {
510                DefVariableError::TypeMismatch(var, val) => {
511                    panic!("declared type of variable {var:?} doesn't match type of value {val}");
512                }
513                DefVariableError::DefinedBeforeDeclared(var) => {
514                    panic!("variable {var:?} is used but its type has not been declared");
515                }
516            })
517    }
518
519    /// Set label for [`Value`]
520    ///
521    /// This will not do anything unless
522    /// [`func.dfg.collect_debug_info`](DataFlowGraph::collect_debug_info) is called first.
523    pub fn set_val_label(&mut self, val: Value, label: ValueLabel) {
524        if let Some(values_labels) = self.func.stencil.dfg.values_labels.as_mut() {
525            use alloc::collections::btree_map::Entry;
526
527            let start = ValueLabelStart {
528                from: RelSourceLoc::from_base_offset(self.func.params.base_srcloc(), self.srcloc),
529                label,
530            };
531
532            match values_labels.entry(val) {
533                Entry::Occupied(mut e) => match e.get_mut() {
534                    ValueLabelAssignments::Starts(starts) => starts.push(start),
535                    _ => panic!("Unexpected ValueLabelAssignments at this stage"),
536                },
537                Entry::Vacant(e) => {
538                    e.insert(ValueLabelAssignments::Starts(vec![start]));
539                }
540            }
541        }
542    }
543
544    /// Declare that the given value is a GC reference that requires inclusion
545    /// in a stack map when it is live across GC safepoints.
546    ///
547    /// All values that are uses of this variable will be spilled to the stack
548    /// before each safepoint and reloaded afterwards. Stack maps allow the
549    /// garbage collector to identify the on-stack GC roots. Between spilling
550    /// the stack and it being reloading again, the stack can be updated to
551    /// facilitate moving GCs.
552    ///
553    /// # Panics
554    ///
555    /// Panics if `val` is larger than 16 bytes.
556    pub fn declare_value_needs_stack_map(&mut self, val: Value) {
557        log::trace!("declare_value_needs_stack_map({val:?})");
558
559        // We rely on these properties in `insert_safepoint_spills`.
560        let size = self.func.dfg.value_type(val).bytes();
561        assert!(size <= 16);
562        assert!(size.is_power_of_two());
563
564        self.func_ctx.ssa.stack_map_values_mut().insert(val);
565    }
566
567    /// Creates a jump table in the function, to be used by [`br_table`](InstBuilder::br_table) instructions.
568    pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
569        self.func.create_jump_table(data)
570    }
571
572    /// Creates a sized stack slot in the function, to be used by [`stack_load`](InstBuilder::stack_load),
573    /// [`stack_store`](InstBuilder::stack_store) and [`stack_addr`](InstBuilder::stack_addr) instructions.
574    pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
575        self.func.create_sized_stack_slot(data)
576    }
577
578    /// Creates a dynamic stack slot in the function, to be used by
579    /// [`dynamic_stack_load`](InstBuilder::dynamic_stack_load),
580    /// [`dynamic_stack_store`](InstBuilder::dynamic_stack_store) and
581    /// [`dynamic_stack_addr`](InstBuilder::dynamic_stack_addr) instructions.
582    pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
583        self.func.create_dynamic_stack_slot(data)
584    }
585
586    /// Adds a signature which can later be used to declare an external function import.
587    pub fn import_signature(&mut self, signature: Signature) -> SigRef {
588        self.func.import_signature(signature)
589    }
590
591    /// Declare an external function import.
592    pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
593        self.func.import_function(data)
594    }
595
596    /// Declares a global value accessible to the function.
597    pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
598        self.func.create_global_value(data)
599    }
600
601    /// Returns an object with the [`InstBuilder`]
602    /// trait that allows to conveniently append an instruction to the current [`Block`] being built.
603    pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short, 'a> {
604        let block = self
605            .position
606            .expect("Please call switch_to_block before inserting instructions");
607        FuncInstBuilder::new(self, block)
608    }
609
610    /// Make sure that the current block is inserted in the layout.
611    pub fn ensure_inserted_block(&mut self) {
612        let block = self.position.unwrap();
613        if self.is_pristine(block) {
614            if !self.func.layout.is_block_inserted(block) {
615                self.func.layout.append_block(block);
616            }
617            self.func_ctx.status[block] = BlockStatus::Partial;
618        } else {
619            debug_assert!(
620                !self.is_filled(block),
621                "you cannot add an instruction to a block already filled"
622            );
623        }
624    }
625
626    /// Returns a [`FuncCursor`] pointed at the current position ready for inserting instructions.
627    ///
628    /// This can be used to insert SSA code that doesn't need to access locals and that doesn't
629    /// need to know about [`FunctionBuilder`] at all.
630    pub fn cursor(&mut self) -> FuncCursor<'_> {
631        self.ensure_inserted_block();
632        FuncCursor::new(self.func)
633            .with_srcloc(self.srcloc)
634            .at_bottom(self.position.unwrap())
635    }
636
637    /// Append parameters to the given [`Block`] corresponding to the function
638    /// parameters. This can be used to set up the block parameters for the
639    /// entry block.
640    pub fn append_block_params_for_function_params(&mut self, block: Block) {
641        debug_assert!(
642            !self.func_ctx.ssa.has_any_predecessors(block),
643            "block parameters for function parameters should only be added to the entry block"
644        );
645
646        // These parameters count as "user" parameters here because they aren't
647        // inserted by the SSABuilder.
648        debug_assert!(
649            self.is_pristine(block),
650            "You can't add block parameters after adding any instruction"
651        );
652
653        for argtyp in &self.func.stencil.signature.params {
654            self.func
655                .stencil
656                .dfg
657                .append_block_param(block, argtyp.value_type);
658        }
659    }
660
661    /// Append parameters to the given [`Block`] corresponding to the function
662    /// return values. This can be used to set up the block parameters for a
663    /// function exit block.
664    pub fn append_block_params_for_function_returns(&mut self, block: Block) {
665        // These parameters count as "user" parameters here because they aren't
666        // inserted by the SSABuilder.
667        debug_assert!(
668            self.is_pristine(block),
669            "You can't add block parameters after adding any instruction"
670        );
671
672        for argtyp in &self.func.stencil.signature.returns {
673            self.func
674                .stencil
675                .dfg
676                .append_block_param(block, argtyp.value_type);
677        }
678    }
679
680    /// Declare that translation of the current function is complete.
681    ///
682    /// This resets the state of the [`FunctionBuilderContext`] in preparation to
683    /// be used for another function.
684    pub fn finalize(mut self, frontend_config: TargetFrontendConfig) {
685        // Check that all the `Block`s are filled and sealed.
686        #[cfg(debug_assertions)]
687        {
688            for block in self.func_ctx.status.keys() {
689                if !self.is_pristine(block) {
690                    assert!(
691                        self.func_ctx.ssa.is_sealed(block),
692                        "FunctionBuilder finalized, but block {block} is not sealed",
693                    );
694                    assert!(
695                        self.is_filled(block),
696                        "FunctionBuilder finalized, but block {block} is not filled",
697                    );
698                }
699            }
700        }
701
702        // In debug mode, check that all blocks are valid basic blocks.
703        #[cfg(debug_assertions)]
704        {
705            // Iterate manually to provide more helpful error messages.
706            for block in self.func_ctx.status.keys() {
707                if let Err((inst, msg)) = self.func.is_block_basic(block) {
708                    let inst_str = self.func.dfg.display_inst(inst);
709                    panic!("{block} failed basic block invariants on {inst_str}: {msg}");
710                }
711            }
712        }
713
714        // If we have any values that need inclusion in stack maps, then we need
715        // to run our pass to spill those values to the stack at safepoints and
716        // generate stack maps.
717        if !self.func_ctx.ssa.stack_map_values().is_empty() {
718            self.func_ctx.safepoints.run(
719                &mut self.func,
720                self.func_ctx.ssa.stack_map_values(),
721                frontend_config.pointer_type(),
722            );
723        }
724
725        // Clear the state (but preserve the allocated buffers) in preparation
726        // for translation another function.
727        self.func_ctx.clear();
728    }
729}
730
731/// All the functions documented in the previous block are write-only and help you build a valid
732/// Cranelift IR functions via multiple debug asserts. However, you might need to improve the
733/// performance of your translation perform more complex transformations to your Cranelift IR
734/// function. The functions below help you inspect the function you're creating and modify it
735/// in ways that can be unsafe if used incorrectly.
736impl<'a> FunctionBuilder<'a> {
737    /// Retrieves all the parameters for a [`Block`] currently inferred from the jump instructions
738    /// inserted that target it and the SSA construction.
739    pub fn block_params(&self, block: Block) -> &[Value] {
740        self.func.dfg.block_params(block)
741    }
742
743    /// Retrieves the signature with reference `sigref` previously added with
744    /// [`import_signature`](Self::import_signature).
745    pub fn signature(&self, sigref: SigRef) -> Option<&Signature> {
746        self.func.dfg.signatures.get(sigref)
747    }
748
749    /// Creates a parameter for a specific [`Block`] by appending it to the list of already existing
750    /// parameters.
751    ///
752    /// **Note:** this function has to be called at the creation of the `Block` before adding
753    /// instructions to it, otherwise this could interfere with SSA construction.
754    pub fn append_block_param(&mut self, block: Block, ty: Type) -> Value {
755        debug_assert!(
756            self.is_pristine(block),
757            "You can't add block parameters after adding any instruction"
758        );
759        self.func.dfg.append_block_param(block, ty)
760    }
761
762    /// Returns the result values of an instruction.
763    pub fn inst_results(&self, inst: Inst) -> &[Value] {
764        self.func.dfg.inst_results(inst)
765    }
766
767    /// Changes the destination of a jump instruction after creation.
768    ///
769    /// **Note:** You are responsible for maintaining the coherence with the arguments of
770    /// other jump instructions.
771    pub fn change_jump_destination(&mut self, inst: Inst, old_block: Block, new_block: Block) {
772        let dfg = &mut self.func.dfg;
773        for block in
774            dfg.insts[inst].branch_destination_mut(&mut dfg.jump_tables, &mut dfg.exception_tables)
775        {
776            if block.block(&dfg.value_lists) == old_block {
777                self.func_ctx.ssa.remove_block_predecessor(old_block, inst);
778                block.set_block(new_block, &mut dfg.value_lists);
779                self.func_ctx.ssa.declare_block_predecessor(new_block, inst);
780            }
781        }
782    }
783
784    /// Returns `true` if and only if the current [`Block`] is sealed and has no predecessors declared.
785    ///
786    /// The entry block of a function is never unreachable.
787    pub fn is_unreachable(&self) -> bool {
788        let is_entry = match self.func.layout.entry_block() {
789            None => false,
790            Some(entry) => self.position.unwrap() == entry,
791        };
792        !is_entry
793            && self.func_ctx.ssa.is_sealed(self.position.unwrap())
794            && !self
795                .func_ctx
796                .ssa
797                .has_any_predecessors(self.position.unwrap())
798    }
799
800    /// Returns `true` if and only if no instructions have been added since the last call to
801    /// [`switch_to_block`](Self::switch_to_block).
802    fn is_pristine(&self, block: Block) -> bool {
803        self.func_ctx.status[block] == BlockStatus::Empty
804    }
805
806    /// Returns `true` if and only if a terminator instruction has been inserted since the
807    /// last call to [`switch_to_block`](Self::switch_to_block).
808    fn is_filled(&self, block: Block) -> bool {
809        self.func_ctx.status[block] == BlockStatus::Filled
810    }
811}
812
813/// Helper functions
814impl<'a> FunctionBuilder<'a> {
815    /// Calls libc.memcpy
816    ///
817    /// Copies the `size` bytes from `src` to `dest`, assumes that `src + size`
818    /// won't overlap onto `dest`. If `dest` and `src` overlap, the behavior is
819    /// undefined. Applications in which `dest` and `src` might overlap should
820    /// use `call_memmove` instead.
821    pub fn call_memcpy(
822        &mut self,
823        config: TargetFrontendConfig,
824        dest: Value,
825        src: Value,
826        size: Value,
827    ) {
828        let pointer_type = config.pointer_type();
829        let signature = {
830            let mut s = Signature::new(config.default_call_conv);
831            s.params.push(AbiParam::new(pointer_type));
832            s.params.push(AbiParam::new(pointer_type));
833            s.params.push(AbiParam::new(pointer_type));
834            s.returns.push(AbiParam::new(pointer_type));
835            self.import_signature(s)
836        };
837
838        let libc_memcpy = self.import_function(ExtFuncData {
839            name: ExternalName::LibCall(LibCall::Memcpy),
840            signature,
841            colocated: false,
842            patchable: false,
843        });
844
845        self.ins().call(libc_memcpy, &[dest, src, size]);
846    }
847
848    /// Optimised memcpy or memmove for small copies.
849    ///
850    /// # Codegen safety
851    ///
852    /// The following properties must hold to prevent UB:
853    ///
854    /// * `src_align` and `dest_align` are an upper-bound on the alignment of `src` respectively `dest`.
855    /// * If `non_overlapping` is true, then this must be correct.
856    pub fn emit_small_memory_copy(
857        &mut self,
858        config: TargetFrontendConfig,
859        dest: Value,
860        src: Value,
861        size: u64,
862        dest_align: u8,
863        src_align: u8,
864        non_overlapping: bool,
865        mut flags: MemFlagsData,
866    ) {
867        // Currently the result of guess work, not actual profiling.
868        const THRESHOLD: u64 = 4;
869
870        if size == 0 {
871            return;
872        }
873
874        let access_size = greatest_divisible_power_of_two(size);
875        assert!(
876            access_size.is_power_of_two(),
877            "`size` is not a power of two"
878        );
879        assert!(
880            access_size >= u64::from(::core::cmp::min(src_align, dest_align)),
881            "`size` is smaller than `dest` and `src`'s alignment value."
882        );
883
884        let (access_size, int_type) = if access_size <= 8 {
885            (access_size, Type::int((access_size * 8) as u16).unwrap())
886        } else {
887            (8, types::I64)
888        };
889
890        let load_and_store_amount = size / access_size;
891
892        if load_and_store_amount > THRESHOLD {
893            let size_value = self.ins().iconst(config.pointer_type(), size as i64);
894            if non_overlapping {
895                self.call_memcpy(config, dest, src, size_value);
896            } else {
897                self.call_memmove(config, dest, src, size_value);
898            }
899            return;
900        }
901
902        if u64::from(src_align) >= access_size && u64::from(dest_align) >= access_size {
903            flags.set_aligned();
904        }
905
906        // Load all of the memory first. This is necessary in case `dest` overlaps.
907        // It can also improve performance a bit.
908        let registers: smallvec::SmallVec<[_; THRESHOLD as usize]> = (0..load_and_store_amount)
909            .map(|i| {
910                let offset = (access_size * i) as i32;
911                (self.ins().load(int_type, flags, src, offset), offset)
912            })
913            .collect();
914
915        for (value, offset) in registers {
916            self.ins().store(flags, value, dest, offset);
917        }
918    }
919
920    /// Calls libc.memset
921    ///
922    /// Writes `size` bytes of i8 value `ch` to memory starting at `buffer`.
923    pub fn call_memset(
924        &mut self,
925        config: TargetFrontendConfig,
926        buffer: Value,
927        ch: Value,
928        size: Value,
929    ) {
930        let pointer_type = config.pointer_type();
931        let signature = {
932            let mut s = Signature::new(config.default_call_conv);
933            s.params.push(AbiParam::new(pointer_type));
934            s.params.push(AbiParam::new(types::I32));
935            s.params.push(AbiParam::new(pointer_type));
936            s.returns.push(AbiParam::new(pointer_type));
937            self.import_signature(s)
938        };
939
940        let libc_memset = self.import_function(ExtFuncData {
941            name: ExternalName::LibCall(LibCall::Memset),
942            signature,
943            colocated: false,
944            patchable: false,
945        });
946
947        let ch = self.ins().uextend(types::I32, ch);
948        self.ins().call(libc_memset, &[buffer, ch, size]);
949    }
950
951    /// Calls libc.memset
952    ///
953    /// Writes `size` bytes of value `ch` to memory starting at `buffer`.
954    pub fn emit_small_memset(
955        &mut self,
956        config: TargetFrontendConfig,
957        buffer: Value,
958        ch: u8,
959        size: u64,
960        buffer_align: u8,
961        mut flags: MemFlagsData,
962    ) {
963        // Currently the result of guess work, not actual profiling.
964        const THRESHOLD: u64 = 4;
965
966        if size == 0 {
967            return;
968        }
969
970        let access_size = greatest_divisible_power_of_two(size);
971        assert!(
972            access_size.is_power_of_two(),
973            "`size` is not a power of two"
974        );
975        assert!(
976            access_size >= u64::from(buffer_align),
977            "`size` is smaller than `dest` and `src`'s alignment value."
978        );
979
980        let (access_size, int_type) = if access_size <= 8 {
981            (access_size, Type::int((access_size * 8) as u16).unwrap())
982        } else {
983            (8, types::I64)
984        };
985
986        let load_and_store_amount = size / access_size;
987
988        if load_and_store_amount > THRESHOLD {
989            let ch = self.ins().iconst(types::I8, i64::from(ch));
990            let size = self.ins().iconst(config.pointer_type(), size as i64);
991            self.call_memset(config, buffer, ch, size);
992        } else {
993            if u64::from(buffer_align) >= access_size {
994                flags.set_aligned();
995            }
996
997            let ch = u64::from(ch);
998            let raw_value = if int_type == types::I64 {
999                ch * 0x0101010101010101_u64
1000            } else if int_type == types::I32 {
1001                ch * 0x01010101_u64
1002            } else if int_type == types::I16 {
1003                (ch << 8) | ch
1004            } else {
1005                assert_eq!(int_type, types::I8);
1006                ch
1007            };
1008
1009            let value = self.ins().iconst(int_type, raw_value as i64);
1010            for i in 0..load_and_store_amount {
1011                let offset = (access_size * i) as i32;
1012                self.ins().store(flags, value, buffer, offset);
1013            }
1014        }
1015    }
1016
1017    /// Calls libc.memmove
1018    ///
1019    /// Copies `size` bytes from memory starting at `source` to memory starting
1020    /// at `dest`. `source` is always read before writing to `dest`.
1021    pub fn call_memmove(
1022        &mut self,
1023        config: TargetFrontendConfig,
1024        dest: Value,
1025        source: Value,
1026        size: Value,
1027    ) {
1028        let pointer_type = config.pointer_type();
1029        let signature = {
1030            let mut s = Signature::new(config.default_call_conv);
1031            s.params.push(AbiParam::new(pointer_type));
1032            s.params.push(AbiParam::new(pointer_type));
1033            s.params.push(AbiParam::new(pointer_type));
1034            s.returns.push(AbiParam::new(pointer_type));
1035            self.import_signature(s)
1036        };
1037
1038        let libc_memmove = self.import_function(ExtFuncData {
1039            name: ExternalName::LibCall(LibCall::Memmove),
1040            signature,
1041            colocated: false,
1042            patchable: false,
1043        });
1044
1045        self.ins().call(libc_memmove, &[dest, source, size]);
1046    }
1047
1048    /// Calls libc.memcmp
1049    ///
1050    /// Compares `size` bytes from memory starting at `left` to memory starting
1051    /// at `right`. Returns `0` if all `n` bytes are equal.  If the first difference
1052    /// is at offset `i`, returns a positive integer if `ugt(left[i], right[i])`
1053    /// and a negative integer if `ult(left[i], right[i])`.
1054    ///
1055    /// Returns a C `int`, which is currently always [`types::I32`].
1056    pub fn call_memcmp(
1057        &mut self,
1058        config: TargetFrontendConfig,
1059        left: Value,
1060        right: Value,
1061        size: Value,
1062    ) -> Value {
1063        let pointer_type = config.pointer_type();
1064        let signature = {
1065            let mut s = Signature::new(config.default_call_conv);
1066            s.params.reserve(3);
1067            s.params.push(AbiParam::new(pointer_type));
1068            s.params.push(AbiParam::new(pointer_type));
1069            s.params.push(AbiParam::new(pointer_type));
1070            s.returns.push(AbiParam::new(types::I32));
1071            self.import_signature(s)
1072        };
1073
1074        let libc_memcmp = self.import_function(ExtFuncData {
1075            name: ExternalName::LibCall(LibCall::Memcmp),
1076            signature,
1077            colocated: false,
1078            patchable: false,
1079        });
1080
1081        let call = self.ins().call(libc_memcmp, &[left, right, size]);
1082        self.func.dfg.first_result(call)
1083    }
1084
1085    /// Optimised [`Self::call_memcmp`] for small copies.
1086    ///
1087    /// This implements the byte slice comparison `int_cc(left[..size], right[..size])`.
1088    ///
1089    /// `left_align` and `right_align` are the statically-known alignments of the
1090    /// `left` and `right` pointers respectively.  These are used to know whether
1091    /// to mark `load`s as aligned.  It's always fine to pass `1` for these, but
1092    /// passing something higher than the true alignment may trap or otherwise
1093    /// misbehave as described in [`MemFlagsData::aligned`].
1094    ///
1095    /// Note that `memcmp` is a *big-endian* and *unsigned* comparison.
1096    /// As such, this panics when called with `IntCC::Signed*`.
1097    pub fn emit_small_memory_compare(
1098        &mut self,
1099        config: TargetFrontendConfig,
1100        int_cc: IntCC,
1101        left: Value,
1102        right: Value,
1103        size: u64,
1104        left_align: core::num::NonZeroU8,
1105        right_align: core::num::NonZeroU8,
1106        flags: MemFlagsData,
1107    ) -> Value {
1108        use IntCC::*;
1109        let (zero_cc, empty_imm) = match int_cc {
1110            //
1111            Equal => (Equal, 1),
1112            NotEqual => (NotEqual, 0),
1113
1114            UnsignedLessThan => (SignedLessThan, 0),
1115            UnsignedGreaterThanOrEqual => (SignedGreaterThanOrEqual, 1),
1116            UnsignedGreaterThan => (SignedGreaterThan, 0),
1117            UnsignedLessThanOrEqual => (SignedLessThanOrEqual, 1),
1118
1119            SignedLessThan
1120            | SignedGreaterThanOrEqual
1121            | SignedGreaterThan
1122            | SignedLessThanOrEqual => {
1123                panic!("Signed comparison {int_cc} not supported by memcmp")
1124            }
1125        };
1126
1127        if size == 0 {
1128            return self.ins().iconst(types::I8, empty_imm);
1129        }
1130
1131        // Future work could consider expanding this to handle more-complex scenarios.
1132        if let Some(small_type) = size.try_into().ok().and_then(Type::int_with_byte_size) {
1133            if let Equal | NotEqual = zero_cc {
1134                let mut left_flags = flags;
1135                if size == left_align.get() as u64 {
1136                    left_flags.set_aligned();
1137                }
1138                let mut right_flags = flags;
1139                if size == right_align.get() as u64 {
1140                    right_flags.set_aligned();
1141                }
1142                let left_val = self.ins().load(small_type, left_flags, left, 0);
1143                let right_val = self.ins().load(small_type, right_flags, right, 0);
1144                return self.ins().icmp(int_cc, left_val, right_val);
1145            } else if small_type == types::I8 {
1146                // Once the big-endian loads from wasmtime#2492 are implemented in
1147                // the backends, we could easily handle comparisons for more sizes here.
1148                // But for now, just handle single bytes where we don't need to worry.
1149
1150                let mut aligned_flags = flags;
1151                aligned_flags.set_aligned();
1152                let left_val = self.ins().load(small_type, aligned_flags, left, 0);
1153                let right_val = self.ins().load(small_type, aligned_flags, right, 0);
1154                return self.ins().icmp(int_cc, left_val, right_val);
1155            }
1156        }
1157
1158        let pointer_type = config.pointer_type();
1159        let size = self.ins().iconst(pointer_type, size as i64);
1160        let cmp = self.call_memcmp(config, left, right, size);
1161        self.ins().icmp_imm_s(zero_cc, cmp, 0)
1162    }
1163}
1164
1165fn greatest_divisible_power_of_two(size: u64) -> u64 {
1166    (size as i64 & -(size as i64)) as u64
1167}
1168
1169// Helper functions
1170impl<'a> FunctionBuilder<'a> {
1171    /// A Block is 'filled' when a terminator instruction is present.
1172    fn fill_current_block(&mut self) {
1173        self.func_ctx.status[self.position.unwrap()] = BlockStatus::Filled;
1174    }
1175
1176    fn declare_successor(&mut self, dest_block: Block, jump_inst: Inst) {
1177        self.func_ctx
1178            .ssa
1179            .declare_block_predecessor(dest_block, jump_inst);
1180    }
1181
1182    fn handle_ssa_side_effects(&mut self, side_effects: SideEffects) {
1183        let SideEffects {
1184            instructions_added_to_blocks,
1185        } = side_effects;
1186
1187        for modified_block in instructions_added_to_blocks {
1188            if self.is_pristine(modified_block) {
1189                self.func_ctx.status[modified_block] = BlockStatus::Partial;
1190            }
1191        }
1192    }
1193}
1194
1195#[cfg(test)]
1196mod tests {
1197    use super::greatest_divisible_power_of_two;
1198    use crate::Variable;
1199    use crate::frontend::{
1200        DefVariableError, FunctionBuilder, FunctionBuilderContext, UseVariableError,
1201    };
1202    use alloc::string::ToString;
1203    use cranelift_codegen::ir::condcodes::IntCC;
1204    use cranelift_codegen::ir::{
1205        AbiParam, BlockCall, ExceptionTableData, ExtFuncData, ExternalName, Function, InstBuilder,
1206        MemFlagsData, Signature, UserExternalName, UserFuncName, Value, types::*,
1207    };
1208    use cranelift_codegen::isa::{CallConv, TargetFrontendConfig, TargetIsa};
1209    use cranelift_codegen::settings;
1210    use cranelift_codegen::verifier::verify_function;
1211    use target_lexicon::PointerWidth;
1212
1213    fn sample_function(lazy_seal: bool) {
1214        let mut sig = Signature::new(CallConv::SystemV);
1215        sig.returns.push(AbiParam::new(I32));
1216        sig.params.push(AbiParam::new(I32));
1217
1218        let mut fn_ctx = FunctionBuilderContext::new();
1219        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1220        {
1221            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1222
1223            let block0 = builder.create_block();
1224            let block1 = builder.create_block();
1225            let block2 = builder.create_block();
1226            let block3 = builder.create_block();
1227            let x = builder.declare_var(I32);
1228            let y = builder.declare_var(I32);
1229            let z = builder.declare_var(I32);
1230
1231            builder.append_block_params_for_function_params(block0);
1232
1233            builder.switch_to_block(block0);
1234            if !lazy_seal {
1235                builder.seal_block(block0);
1236            }
1237            {
1238                let tmp = builder.block_params(block0)[0]; // the first function parameter
1239                builder.def_var(x, tmp);
1240            }
1241            {
1242                let tmp = builder.ins().iconst(I32, 2);
1243                builder.def_var(y, tmp);
1244            }
1245            {
1246                let arg1 = builder.use_var(x);
1247                let arg2 = builder.use_var(y);
1248                let tmp = builder.ins().iadd(arg1, arg2);
1249                builder.def_var(z, tmp);
1250            }
1251            builder.ins().jump(block1, &[]);
1252
1253            builder.switch_to_block(block1);
1254            {
1255                let arg1 = builder.use_var(y);
1256                let arg2 = builder.use_var(z);
1257                let tmp = builder.ins().iadd(arg1, arg2);
1258                builder.def_var(z, tmp);
1259            }
1260            {
1261                let arg = builder.use_var(y);
1262                builder.ins().brif(arg, block3, &[], block2, &[]);
1263            }
1264
1265            builder.switch_to_block(block2);
1266            if !lazy_seal {
1267                builder.seal_block(block2);
1268            }
1269            {
1270                let arg1 = builder.use_var(z);
1271                let arg2 = builder.use_var(x);
1272                let tmp = builder.ins().isub(arg1, arg2);
1273                builder.def_var(z, tmp);
1274            }
1275            {
1276                let arg = builder.use_var(y);
1277                builder.ins().return_(&[arg]);
1278            }
1279
1280            builder.switch_to_block(block3);
1281            if !lazy_seal {
1282                builder.seal_block(block3);
1283            }
1284
1285            {
1286                let arg1 = builder.use_var(y);
1287                let arg2 = builder.use_var(x);
1288                let tmp = builder.ins().isub(arg1, arg2);
1289                builder.def_var(y, tmp);
1290            }
1291            builder.ins().jump(block1, &[]);
1292            if !lazy_seal {
1293                builder.seal_block(block1);
1294            }
1295
1296            if lazy_seal {
1297                builder.seal_all_blocks();
1298            }
1299
1300            builder.finalize(systemv_frontend_config());
1301        }
1302
1303        let flags = settings::Flags::new(settings::builder());
1304        // println!("{}", func.display(None));
1305        if let Err(errors) = verify_function(&func, &flags) {
1306            panic!("{}\n{}", func.display(), errors)
1307        }
1308    }
1309
1310    #[test]
1311    fn sample() {
1312        sample_function(false)
1313    }
1314
1315    #[test]
1316    fn sample_with_lazy_seal() {
1317        sample_function(true)
1318    }
1319
1320    #[track_caller]
1321    fn check(func: &Function, expected_ir: &str) {
1322        let expected_ir = expected_ir.trim();
1323        let actual_ir = func.display().to_string();
1324        let actual_ir = actual_ir.trim();
1325        assert!(
1326            expected_ir == actual_ir,
1327            "Expected:\n{expected_ir}\nGot:\n{actual_ir}"
1328        );
1329    }
1330
1331    /// Helper function to construct a fixed frontend configuration.
1332    fn systemv_frontend_config() -> TargetFrontendConfig {
1333        TargetFrontendConfig {
1334            default_call_conv: CallConv::SystemV,
1335            pointer_width: PointerWidth::U64,
1336            page_size_align_log2: 12,
1337        }
1338    }
1339
1340    #[test]
1341    fn memcpy() {
1342        let frontend_config = systemv_frontend_config();
1343        let mut sig = Signature::new(frontend_config.default_call_conv);
1344        sig.returns.push(AbiParam::new(I32));
1345
1346        let mut fn_ctx = FunctionBuilderContext::new();
1347        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1348        {
1349            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1350
1351            let block0 = builder.create_block();
1352            let x = builder.declare_var(frontend_config.pointer_type());
1353            let y = builder.declare_var(frontend_config.pointer_type());
1354            let _z = builder.declare_var(I32);
1355
1356            builder.append_block_params_for_function_params(block0);
1357            builder.switch_to_block(block0);
1358
1359            let src = builder.use_var(x);
1360            let dest = builder.use_var(y);
1361            let size = builder.use_var(y);
1362            builder.call_memcpy(frontend_config, dest, src, size);
1363            builder.ins().return_(&[size]);
1364
1365            builder.seal_all_blocks();
1366            builder.finalize(systemv_frontend_config());
1367        }
1368
1369        check(
1370            &func,
1371            "function %sample() -> i32 system_v {
1372    sig0 = (i64, i64, i64) -> i64 system_v
1373    fn0 = %Memcpy sig0
1374
1375block0:
1376    v4 = iconst.i64 0
1377    v1 -> v4
1378    v3 = iconst.i64 0
1379    v0 -> v3
1380    v2 = call fn0(v1, v0, v1)  ; v1 = 0, v0 = 0, v1 = 0
1381    return v1  ; v1 = 0
1382}
1383",
1384        );
1385    }
1386
1387    #[test]
1388    fn small_memcpy() {
1389        let frontend_config = systemv_frontend_config();
1390        let mut sig = Signature::new(frontend_config.default_call_conv);
1391        sig.returns.push(AbiParam::new(I32));
1392
1393        let mut fn_ctx = FunctionBuilderContext::new();
1394        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1395        {
1396            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1397
1398            let block0 = builder.create_block();
1399            let x = builder.declare_var(frontend_config.pointer_type());
1400            let y = builder.declare_var(frontend_config.pointer_type());
1401
1402            builder.append_block_params_for_function_params(block0);
1403            builder.switch_to_block(block0);
1404
1405            let src = builder.use_var(x);
1406            let dest = builder.use_var(y);
1407            let size = 8;
1408            builder.emit_small_memory_copy(
1409                frontend_config,
1410                dest,
1411                src,
1412                size,
1413                8,
1414                8,
1415                true,
1416                MemFlagsData::new(),
1417            );
1418            builder.ins().return_(&[dest]);
1419
1420            builder.seal_all_blocks();
1421            builder.finalize(systemv_frontend_config());
1422        }
1423
1424        check(
1425            &func,
1426            "function %sample() -> i32 system_v {
1427block0:
1428    v4 = iconst.i64 0
1429    v1 -> v4
1430    v3 = iconst.i64 0
1431    v0 -> v3
1432    v2 = load.i64 aligned v0  ; v0 = 0
1433    store aligned v2, v1  ; v1 = 0
1434    return v1  ; v1 = 0
1435}
1436",
1437        );
1438    }
1439
1440    #[test]
1441    fn not_so_small_memcpy() {
1442        let frontend_config = systemv_frontend_config();
1443        let mut sig = Signature::new(frontend_config.default_call_conv);
1444        sig.returns.push(AbiParam::new(I32));
1445
1446        let mut fn_ctx = FunctionBuilderContext::new();
1447        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1448        {
1449            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1450
1451            let block0 = builder.create_block();
1452            let x = builder.declare_var(frontend_config.pointer_type());
1453            let y = builder.declare_var(frontend_config.pointer_type());
1454            builder.append_block_params_for_function_params(block0);
1455            builder.switch_to_block(block0);
1456
1457            let src = builder.use_var(x);
1458            let dest = builder.use_var(y);
1459            let size = 8192;
1460            builder.emit_small_memory_copy(
1461                frontend_config,
1462                dest,
1463                src,
1464                size,
1465                8,
1466                8,
1467                true,
1468                MemFlagsData::new(),
1469            );
1470            builder.ins().return_(&[dest]);
1471
1472            builder.seal_all_blocks();
1473            builder.finalize(systemv_frontend_config());
1474        }
1475
1476        check(
1477            &func,
1478            "function %sample() -> i32 system_v {
1479    sig0 = (i64, i64, i64) -> i64 system_v
1480    fn0 = %Memcpy sig0
1481
1482block0:
1483    v5 = iconst.i64 0
1484    v1 -> v5
1485    v4 = iconst.i64 0
1486    v0 -> v4
1487    v2 = iconst.i64 8192
1488    v3 = call fn0(v1, v0, v2)  ; v1 = 0, v0 = 0, v2 = 8192
1489    return v1  ; v1 = 0
1490}
1491",
1492        );
1493    }
1494
1495    #[test]
1496    fn small_memset() {
1497        let frontend_config = systemv_frontend_config();
1498        let mut sig = Signature::new(frontend_config.default_call_conv);
1499        sig.returns.push(AbiParam::new(I32));
1500
1501        let mut fn_ctx = FunctionBuilderContext::new();
1502        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1503        {
1504            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1505
1506            let block0 = builder.create_block();
1507            let y = builder.declare_var(frontend_config.pointer_type());
1508            builder.append_block_params_for_function_params(block0);
1509            builder.switch_to_block(block0);
1510
1511            let dest = builder.use_var(y);
1512            let size = 8;
1513            builder.emit_small_memset(frontend_config, dest, 1, size, 8, MemFlagsData::new());
1514            builder.ins().return_(&[dest]);
1515
1516            builder.seal_all_blocks();
1517            builder.finalize(systemv_frontend_config());
1518        }
1519
1520        check(
1521            &func,
1522            "function %sample() -> i32 system_v {
1523block0:
1524    v2 = iconst.i64 0
1525    v0 -> v2
1526    v1 = iconst.i64 0x0101_0101_0101_0101
1527    store aligned v1, v0  ; v1 = 0x0101_0101_0101_0101, v0 = 0
1528    return v0  ; v0 = 0
1529}
1530",
1531        );
1532    }
1533
1534    #[test]
1535    fn not_so_small_memset() {
1536        let frontend_config = systemv_frontend_config();
1537        let mut sig = Signature::new(frontend_config.default_call_conv);
1538        sig.returns.push(AbiParam::new(I32));
1539
1540        let mut fn_ctx = FunctionBuilderContext::new();
1541        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1542        {
1543            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1544
1545            let block0 = builder.create_block();
1546            let y = builder.declare_var(frontend_config.pointer_type());
1547            builder.append_block_params_for_function_params(block0);
1548            builder.switch_to_block(block0);
1549
1550            let dest = builder.use_var(y);
1551            let size = 8192;
1552            builder.emit_small_memset(frontend_config, dest, 1, size, 8, MemFlagsData::new());
1553            builder.ins().return_(&[dest]);
1554
1555            builder.seal_all_blocks();
1556            builder.finalize(systemv_frontend_config());
1557        }
1558
1559        check(
1560            &func,
1561            "function %sample() -> i32 system_v {
1562    sig0 = (i64, i32, i64) -> i64 system_v
1563    fn0 = %Memset sig0
1564
1565block0:
1566    v5 = iconst.i64 0
1567    v0 -> v5
1568    v1 = iconst.i8 1
1569    v2 = iconst.i64 8192
1570    v3 = uextend.i32 v1  ; v1 = 1
1571    v4 = call fn0(v0, v3, v2)  ; v0 = 0, v2 = 8192
1572    return v0  ; v0 = 0
1573}
1574",
1575        );
1576    }
1577
1578    #[test]
1579    fn memcmp() {
1580        use core::str::FromStr;
1581        use cranelift_codegen::isa;
1582
1583        let shared_builder = settings::builder();
1584        let shared_flags = settings::Flags::new(shared_builder);
1585
1586        let triple =
1587            ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple");
1588
1589        let target = isa::lookup(triple)
1590            .ok()
1591            .map(|b| b.finish(shared_flags))
1592            .expect("This test requires x86_64 support.")
1593            .expect("Should be able to create backend with default flags");
1594
1595        let mut sig = Signature::new(target.default_call_conv());
1596        sig.returns.push(AbiParam::new(I32));
1597
1598        let mut fn_ctx = FunctionBuilderContext::new();
1599        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1600        {
1601            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1602
1603            let block0 = builder.create_block();
1604            let x = builder.declare_var(target.pointer_type());
1605            let y = builder.declare_var(target.pointer_type());
1606            let z = builder.declare_var(target.pointer_type());
1607            builder.append_block_params_for_function_params(block0);
1608            builder.switch_to_block(block0);
1609
1610            let left = builder.use_var(x);
1611            let right = builder.use_var(y);
1612            let size = builder.use_var(z);
1613            let cmp = builder.call_memcmp(target.frontend_config(), left, right, size);
1614            builder.ins().return_(&[cmp]);
1615
1616            builder.seal_all_blocks();
1617            builder.finalize(systemv_frontend_config());
1618        }
1619
1620        check(
1621            &func,
1622            "function %sample() -> i32 system_v {
1623    sig0 = (i64, i64, i64) -> i32 system_v
1624    fn0 = %Memcmp sig0
1625
1626block0:
1627    v6 = iconst.i64 0
1628    v2 -> v6
1629    v5 = iconst.i64 0
1630    v1 -> v5
1631    v4 = iconst.i64 0
1632    v0 -> v4
1633    v3 = call fn0(v0, v1, v2)  ; v0 = 0, v1 = 0, v2 = 0
1634    return v3
1635}
1636",
1637        );
1638    }
1639
1640    #[test]
1641    fn small_memcmp_zero_size() {
1642        let align_eight = std::num::NonZeroU8::new(8).unwrap();
1643        small_memcmp_helper(
1644            "
1645block0:
1646    v4 = iconst.i64 0
1647    v1 -> v4
1648    v3 = iconst.i64 0
1649    v0 -> v3
1650    v2 = iconst.i8 1
1651    return v2  ; v2 = 1",
1652            |builder, target, x, y| {
1653                builder.emit_small_memory_compare(
1654                    target.frontend_config(),
1655                    IntCC::UnsignedGreaterThanOrEqual,
1656                    x,
1657                    y,
1658                    0,
1659                    align_eight,
1660                    align_eight,
1661                    MemFlagsData::new(),
1662                )
1663            },
1664        );
1665    }
1666
1667    #[test]
1668    fn small_memcmp_byte_ugt() {
1669        let align_one = std::num::NonZeroU8::new(1).unwrap();
1670        small_memcmp_helper(
1671            "
1672block0:
1673    v6 = iconst.i64 0
1674    v1 -> v6
1675    v5 = iconst.i64 0
1676    v0 -> v5
1677    v2 = load.i8 aligned v0  ; v0 = 0
1678    v3 = load.i8 aligned v1  ; v1 = 0
1679    v4 = icmp ugt v2, v3
1680    return v4",
1681            |builder, target, x, y| {
1682                builder.emit_small_memory_compare(
1683                    target.frontend_config(),
1684                    IntCC::UnsignedGreaterThan,
1685                    x,
1686                    y,
1687                    1,
1688                    align_one,
1689                    align_one,
1690                    MemFlagsData::new(),
1691                )
1692            },
1693        );
1694    }
1695
1696    #[test]
1697    fn small_memcmp_aligned_eq() {
1698        let align_four = std::num::NonZeroU8::new(4).unwrap();
1699        small_memcmp_helper(
1700            "
1701block0:
1702    v6 = iconst.i64 0
1703    v1 -> v6
1704    v5 = iconst.i64 0
1705    v0 -> v5
1706    v2 = load.i32 aligned v0  ; v0 = 0
1707    v3 = load.i32 aligned v1  ; v1 = 0
1708    v4 = icmp eq v2, v3
1709    return v4",
1710            |builder, target, x, y| {
1711                builder.emit_small_memory_compare(
1712                    target.frontend_config(),
1713                    IntCC::Equal,
1714                    x,
1715                    y,
1716                    4,
1717                    align_four,
1718                    align_four,
1719                    MemFlagsData::new(),
1720                )
1721            },
1722        );
1723    }
1724
1725    #[test]
1726    fn small_memcmp_ipv6_ne() {
1727        let align_two = std::num::NonZeroU8::new(2).unwrap();
1728        small_memcmp_helper(
1729            "
1730block0:
1731    v6 = iconst.i64 0
1732    v1 -> v6
1733    v5 = iconst.i64 0
1734    v0 -> v5
1735    v2 = load.i128 v0  ; v0 = 0
1736    v3 = load.i128 v1  ; v1 = 0
1737    v4 = icmp ne v2, v3
1738    return v4",
1739            |builder, target, x, y| {
1740                builder.emit_small_memory_compare(
1741                    target.frontend_config(),
1742                    IntCC::NotEqual,
1743                    x,
1744                    y,
1745                    16,
1746                    align_two,
1747                    align_two,
1748                    MemFlagsData::new(),
1749                )
1750            },
1751        );
1752    }
1753
1754    #[test]
1755    fn small_memcmp_odd_size_uge() {
1756        let one = std::num::NonZeroU8::new(1).unwrap();
1757        small_memcmp_helper(
1758            "
1759    sig0 = (i64, i64, i64) -> i32 system_v
1760    fn0 = %Memcmp sig0
1761
1762block0:
1763    v7 = iconst.i64 0
1764    v1 -> v7
1765    v6 = iconst.i64 0
1766    v0 -> v6
1767    v2 = iconst.i64 3
1768    v3 = call fn0(v0, v1, v2)  ; v0 = 0, v1 = 0, v2 = 3
1769    v4 = iconst.i32 0
1770    v5 = icmp sge v3, v4  ; v4 = 0
1771    return v5",
1772            |builder, target, x, y| {
1773                builder.emit_small_memory_compare(
1774                    target.frontend_config(),
1775                    IntCC::UnsignedGreaterThanOrEqual,
1776                    x,
1777                    y,
1778                    3,
1779                    one,
1780                    one,
1781                    MemFlagsData::new(),
1782                )
1783            },
1784        );
1785    }
1786
1787    fn small_memcmp_helper(
1788        expected: &str,
1789        f: impl FnOnce(&mut FunctionBuilder, &dyn TargetIsa, Value, Value) -> Value,
1790    ) {
1791        use core::str::FromStr;
1792        use cranelift_codegen::isa;
1793
1794        let shared_builder = settings::builder();
1795        let shared_flags = settings::Flags::new(shared_builder);
1796
1797        let triple =
1798            ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple");
1799
1800        let target = isa::lookup(triple)
1801            .ok()
1802            .map(|b| b.finish(shared_flags))
1803            .expect("This test requires x86_64 support.")
1804            .expect("Should be able to create backend with default flags");
1805
1806        let mut sig = Signature::new(target.default_call_conv());
1807        sig.returns.push(AbiParam::new(I8));
1808
1809        let mut fn_ctx = FunctionBuilderContext::new();
1810        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1811        {
1812            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1813
1814            let block0 = builder.create_block();
1815            let x = builder.declare_var(target.pointer_type());
1816            let y = builder.declare_var(target.pointer_type());
1817            builder.append_block_params_for_function_params(block0);
1818            builder.switch_to_block(block0);
1819
1820            let left = builder.use_var(x);
1821            let right = builder.use_var(y);
1822            let ret = f(&mut builder, &*target, left, right);
1823            builder.ins().return_(&[ret]);
1824
1825            builder.seal_all_blocks();
1826            builder.finalize(systemv_frontend_config());
1827        }
1828
1829        check(
1830            &func,
1831            &format!("function %sample() -> i8 system_v {{{expected}\n}}\n"),
1832        );
1833    }
1834
1835    #[test]
1836    fn undef_vector_vars() {
1837        let mut sig = Signature::new(CallConv::SystemV);
1838        sig.returns.push(AbiParam::new(I8X16));
1839        sig.returns.push(AbiParam::new(I8X16));
1840        sig.returns.push(AbiParam::new(F32X4));
1841
1842        let mut fn_ctx = FunctionBuilderContext::new();
1843        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1844        {
1845            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1846
1847            let block0 = builder.create_block();
1848            let a = builder.declare_var(I8X16);
1849            let b = builder.declare_var(I8X16);
1850            let c = builder.declare_var(F32X4);
1851            builder.switch_to_block(block0);
1852
1853            let a = builder.use_var(a);
1854            let b = builder.use_var(b);
1855            let c = builder.use_var(c);
1856            builder.ins().return_(&[a, b, c]);
1857
1858            builder.seal_all_blocks();
1859            builder.finalize(systemv_frontend_config());
1860        }
1861
1862        check(
1863            &func,
1864            "function %sample() -> i8x16, i8x16, f32x4 system_v {
1865    const0 = 0x00000000000000000000000000000000
1866
1867block0:
1868    v5 = f32const 0.0
1869    v6 = splat.f32x4 v5  ; v5 = 0.0
1870    v2 -> v6
1871    v4 = vconst.i8x16 const0
1872    v1 -> v4
1873    v3 = vconst.i8x16 const0
1874    v0 -> v3
1875    return v0, v1, v2  ; v0 = const0, v1 = const0
1876}
1877",
1878        );
1879    }
1880
1881    #[test]
1882    fn test_greatest_divisible_power_of_two() {
1883        assert_eq!(64, greatest_divisible_power_of_two(64));
1884        assert_eq!(16, greatest_divisible_power_of_two(48));
1885        assert_eq!(8, greatest_divisible_power_of_two(24));
1886        assert_eq!(1, greatest_divisible_power_of_two(25));
1887    }
1888
1889    #[test]
1890    fn try_use_var() {
1891        let sig = Signature::new(CallConv::SystemV);
1892
1893        let mut fn_ctx = FunctionBuilderContext::new();
1894        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1895        {
1896            let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1897
1898            let block0 = builder.create_block();
1899            builder.append_block_params_for_function_params(block0);
1900            builder.switch_to_block(block0);
1901
1902            assert_eq!(
1903                builder.try_use_var(Variable::from_u32(0)),
1904                Err(UseVariableError::UsedBeforeDeclared(Variable::from_u32(0)))
1905            );
1906
1907            let value = builder.ins().iconst(cranelift_codegen::ir::types::I32, 0);
1908
1909            assert_eq!(
1910                builder.try_def_var(Variable::from_u32(0), value),
1911                Err(DefVariableError::DefinedBeforeDeclared(Variable::from_u32(
1912                    0
1913                )))
1914            );
1915        }
1916    }
1917
1918    #[test]
1919    fn test_builder_with_iconst_and_negative_constant() {
1920        let sig = Signature::new(CallConv::SystemV);
1921        let mut fn_ctx = FunctionBuilderContext::new();
1922        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1923
1924        let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1925
1926        let block0 = builder.create_block();
1927        builder.switch_to_block(block0);
1928        builder.ins().iconst(I32, -1);
1929        builder.ins().return_(&[]);
1930
1931        builder.seal_all_blocks();
1932        builder.finalize(systemv_frontend_config());
1933
1934        let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());
1935        let ctx = cranelift_codegen::Context::for_function(func);
1936        ctx.verify(&flags).expect("should be valid");
1937
1938        check(
1939            &ctx.func,
1940            "function %sample() system_v {
1941block0:
1942    v0 = iconst.i32 -1
1943    return
1944}",
1945        );
1946    }
1947
1948    #[test]
1949    fn try_call() {
1950        let mut sig = Signature::new(CallConv::SystemV);
1951        sig.params.push(AbiParam::new(I8));
1952        sig.returns.push(AbiParam::new(I32));
1953        let mut fn_ctx = FunctionBuilderContext::new();
1954        let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);
1955
1956        let sig0 = func.import_signature(Signature::new(CallConv::SystemV));
1957        let name = func.declare_imported_user_function(UserExternalName::new(0, 0));
1958        let fn0 = func.import_function(ExtFuncData {
1959            name: ExternalName::User(name),
1960            signature: sig0,
1961            colocated: false,
1962            patchable: false,
1963        });
1964
1965        let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);
1966
1967        let block0 = builder.create_block();
1968        let block1 = builder.create_block();
1969        let block2 = builder.create_block();
1970        let block3 = builder.create_block();
1971
1972        let my_var = builder.declare_var(I32);
1973
1974        builder.switch_to_block(block0);
1975        let branch_val = builder.append_block_param(block0, I8);
1976        builder.ins().brif(branch_val, block1, &[], block2, &[]);
1977
1978        builder.switch_to_block(block1);
1979        let one = builder.ins().iconst(I32, 1);
1980        builder.def_var(my_var, one);
1981
1982        let normal_return = BlockCall::new(block3, [], &mut builder.func.dfg.value_lists);
1983        let exception_table = builder
1984            .func
1985            .dfg
1986            .exception_tables
1987            .push(ExceptionTableData::new(sig0, normal_return, []));
1988        builder.ins().try_call(fn0, &[], exception_table);
1989
1990        builder.switch_to_block(block2);
1991        let two = builder.ins().iconst(I32, 2);
1992        builder.def_var(my_var, two);
1993
1994        let normal_return = BlockCall::new(block3, [], &mut builder.func.dfg.value_lists);
1995        let exception_table = builder
1996            .func
1997            .dfg
1998            .exception_tables
1999            .push(ExceptionTableData::new(sig0, normal_return, []));
2000        builder.ins().try_call(fn0, &[], exception_table);
2001
2002        builder.switch_to_block(block3);
2003        let ret_val = builder.use_var(my_var);
2004        builder.ins().return_(&[ret_val]);
2005
2006        builder.seal_all_blocks();
2007        builder.finalize(systemv_frontend_config());
2008
2009        let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());
2010        let ctx = cranelift_codegen::Context::for_function(func);
2011        ctx.verify(&flags).expect("should be valid");
2012
2013        check(
2014            &ctx.func,
2015            "function %sample(i8) -> i32 system_v {
2016    sig0 = () system_v
2017    fn0 = u0:0 sig0
2018
2019block0(v0: i8):
2020    brif v0, block1, block2
2021
2022block1:
2023    v1 = iconst.i32 1
2024    try_call fn0(), sig0, block3(v1), []  ; v1 = 1
2025
2026block2:
2027    v2 = iconst.i32 2
2028    try_call fn0(), sig0, block3(v2), []  ; v2 = 2
2029
2030block3(v3: i32):
2031    return v3
2032}",
2033        );
2034    }
2035}