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