Path: blob/main/cranelift/frontend/src/frontend.rs
1692 views
//! A frontend for building Cranelift IR from other languages.1use crate::ssa::{SSABuilder, SideEffects};2use crate::variable::Variable;3use alloc::vec::Vec;4use core::fmt::{self, Debug};5use cranelift_codegen::cursor::{Cursor, CursorPosition, FuncCursor};6use cranelift_codegen::entity::{EntityRef, EntitySet, PrimaryMap, SecondaryMap};7use cranelift_codegen::ir;8use cranelift_codegen::ir::condcodes::IntCC;9use cranelift_codegen::ir::{10AbiParam, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, ExtFuncData,11ExternalName, FuncRef, Function, GlobalValue, GlobalValueData, Inst, InstBuilder,12InstBuilderBase, InstructionData, JumpTable, JumpTableData, LibCall, MemFlags, RelSourceLoc,13SigRef, Signature, StackSlot, StackSlotData, Type, Value, ValueLabel, ValueLabelAssignments,14ValueLabelStart, types,15};16use cranelift_codegen::isa::TargetFrontendConfig;17use cranelift_codegen::packed_option::PackedOption;18use cranelift_codegen::traversals::Dfs;19use smallvec::SmallVec;2021mod safepoints;2223/// Structure used for translating a series of functions into Cranelift IR.24///25/// In order to reduce memory reallocations when compiling multiple functions,26/// [`FunctionBuilderContext`] holds various data structures which are cleared between27/// functions, rather than dropped, preserving the underlying allocations.28#[derive(Default)]29pub struct FunctionBuilderContext {30ssa: SSABuilder,31status: SecondaryMap<Block, BlockStatus>,32variables: PrimaryMap<Variable, Type>,33stack_map_vars: EntitySet<Variable>,34stack_map_values: EntitySet<Value>,35safepoints: safepoints::SafepointSpiller,36}3738/// Temporary object used to build a single Cranelift IR [`Function`].39pub struct FunctionBuilder<'a> {40/// The function currently being built.41/// This field is public so the function can be re-borrowed.42pub func: &'a mut Function,4344/// Source location to assign to all new instructions.45srcloc: ir::SourceLoc,4647func_ctx: &'a mut FunctionBuilderContext,48position: PackedOption<Block>,49}5051#[derive(Clone, Default, Eq, PartialEq)]52enum BlockStatus {53/// No instructions have been added.54#[default]55Empty,56/// Some instructions have been added, but no terminator.57Partial,58/// A terminator has been added; no further instructions may be added.59Filled,60}6162impl FunctionBuilderContext {63/// Creates a [`FunctionBuilderContext`] structure. The structure is automatically cleared after64/// each [`FunctionBuilder`] completes translating a function.65pub fn new() -> Self {66Self::default()67}6869fn clear(&mut self) {70let FunctionBuilderContext {71ssa,72status,73variables,74stack_map_vars,75stack_map_values,76safepoints,77} = self;78ssa.clear();79status.clear();80variables.clear();81stack_map_values.clear();82stack_map_vars.clear();83safepoints.clear();84}8586fn is_empty(&self) -> bool {87self.ssa.is_empty() && self.status.is_empty() && self.variables.is_empty()88}89}9091/// Implementation of the [`InstBuilder`] that has92/// one convenience method per Cranelift IR instruction.93pub struct FuncInstBuilder<'short, 'long: 'short> {94builder: &'short mut FunctionBuilder<'long>,95block: Block,96}9798impl<'short, 'long> FuncInstBuilder<'short, 'long> {99fn new(builder: &'short mut FunctionBuilder<'long>, block: Block) -> Self {100Self { builder, block }101}102}103104impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {105fn data_flow_graph(&self) -> &DataFlowGraph {106&self.builder.func.dfg107}108109fn data_flow_graph_mut(&mut self) -> &mut DataFlowGraph {110&mut self.builder.func.dfg111}112113// This implementation is richer than `InsertBuilder` because we use the data of the114// instruction being inserted to add related info to the DFG and the SSA building system,115// and perform debug sanity checks.116fn build(self, data: InstructionData, ctrl_typevar: Type) -> (Inst, &'short mut DataFlowGraph) {117// We only insert the Block in the layout when an instruction is added to it118self.builder.ensure_inserted_block();119120let inst = self.builder.func.dfg.make_inst(data);121self.builder.func.dfg.make_inst_results(inst, ctrl_typevar);122self.builder.func.layout.append_inst(inst, self.block);123if !self.builder.srcloc.is_default() {124self.builder.func.set_srcloc(inst, self.builder.srcloc);125}126127match &self.builder.func.dfg.insts[inst] {128ir::InstructionData::Jump {129destination: dest, ..130} => {131// If the user has supplied jump arguments we must adapt the arguments of132// the destination block133let block = dest.block(&self.builder.func.dfg.value_lists);134self.builder.declare_successor(block, inst);135}136137ir::InstructionData::Brif {138blocks: [branch_then, branch_else],139..140} => {141let block_then = branch_then.block(&self.builder.func.dfg.value_lists);142let block_else = branch_else.block(&self.builder.func.dfg.value_lists);143144self.builder.declare_successor(block_then, inst);145if block_then != block_else {146self.builder.declare_successor(block_else, inst);147}148}149150ir::InstructionData::BranchTable { table, .. } => {151let pool = &self.builder.func.dfg.value_lists;152153// Unlike most other jumps/branches and like try_call,154// jump tables are capable of having the same successor appear155// multiple times, so we must deduplicate.156let mut unique = EntitySet::<Block>::new();157for dest_block in self158.builder159.func160.stencil161.dfg162.jump_tables163.get(*table)164.expect("you are referencing an undeclared jump table")165.all_branches()166{167let block = dest_block.block(pool);168if !unique.insert(block) {169continue;170}171172// Call `declare_block_predecessor` instead of `declare_successor` for173// avoiding the borrow checker.174self.builder175.func_ctx176.ssa177.declare_block_predecessor(block, inst);178}179}180181ir::InstructionData::TryCall { exception, .. }182| ir::InstructionData::TryCallIndirect { exception, .. } => {183let pool = &self.builder.func.dfg.value_lists;184185// Unlike most other jumps/branches and like br_table,186// exception tables are capable of having the same successor187// appear multiple times, so we must deduplicate.188let mut unique = EntitySet::<Block>::new();189for dest_block in self190.builder191.func192.stencil193.dfg194.exception_tables195.get(*exception)196.expect("you are referencing an undeclared exception table")197.all_branches()198{199let block = dest_block.block(pool);200if !unique.insert(block) {201continue;202}203204// Call `declare_block_predecessor` instead of `declare_successor` for205// avoiding the borrow checker.206self.builder207.func_ctx208.ssa209.declare_block_predecessor(block, inst);210}211}212213inst => assert!(!inst.opcode().is_branch()),214}215216if data.opcode().is_terminator() {217self.builder.fill_current_block()218}219(inst, &mut self.builder.func.dfg)220}221}222223#[derive(Debug, Copy, Clone, PartialEq, Eq)]224/// An error encountered when calling [`FunctionBuilder::try_use_var`].225pub enum UseVariableError {226UsedBeforeDeclared(Variable),227}228229impl fmt::Display for UseVariableError {230fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {231match self {232UseVariableError::UsedBeforeDeclared(variable) => {233write!(234f,235"variable {} was used before it was defined",236variable.index()237)?;238}239}240Ok(())241}242}243244impl std::error::Error for UseVariableError {}245246#[derive(Debug, Copy, Clone, Eq, PartialEq)]247/// An error encountered when defining the initial value of a variable.248pub enum DefVariableError {249/// The variable was instantiated with a value of the wrong type.250///251/// note: to obtain the type of the value, you can call252/// [`cranelift_codegen::ir::dfg::DataFlowGraph::value_type`] (using the253/// `FunctionBuilder.func.dfg` field)254TypeMismatch(Variable, Value),255/// The value was defined (in a call to [`FunctionBuilder::def_var`]) before256/// it was declared (in a call to [`FunctionBuilder::declare_var`]).257DefinedBeforeDeclared(Variable),258}259260impl fmt::Display for DefVariableError {261fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {262match self {263DefVariableError::TypeMismatch(variable, value) => {264write!(265f,266"the types of variable {} and value {} are not the same.267The `Value` supplied to `def_var` must be of the same type as268the variable was declared to be of in `declare_var`.",269variable.index(),270value.as_u32()271)?;272}273DefVariableError::DefinedBeforeDeclared(variable) => {274write!(275f,276"the value of variable {} was declared before it was defined",277variable.index()278)?;279}280}281Ok(())282}283}284285/// This module allows you to create a function in Cranelift IR in a straightforward way, hiding286/// all the complexity of its internal representation.287///288/// The module is parametrized by one type which is the representation of variables in your289/// origin language. It offers a way to conveniently append instruction to your program flow.290/// You are responsible to split your instruction flow into extended blocks (declared with291/// [`create_block`](Self::create_block)) whose properties are:292///293/// - branch and jump instructions can only point at the top of extended blocks;294/// - the last instruction of each block is a terminator instruction which has no natural successor,295/// and those instructions can only appear at the end of extended blocks.296///297/// The parameters of Cranelift IR instructions are Cranelift IR values, which can only be created298/// as results of other Cranelift IR instructions. To be able to create variables redefined multiple299/// times in your program, use the [`def_var`](Self::def_var) and [`use_var`](Self::use_var) command,300/// that will maintain the correspondence between your variables and Cranelift IR SSA values.301///302/// The first block for which you call [`switch_to_block`](Self::switch_to_block) will be assumed to303/// be the beginning of the function.304///305/// At creation, a [`FunctionBuilder`] instance borrows an already allocated `Function` which it306/// modifies with the information stored in the mutable borrowed307/// [`FunctionBuilderContext`]. The function passed in argument should be newly created with308/// [`Function::with_name_signature()`], whereas the [`FunctionBuilderContext`] can be kept as is309/// between two function translations.310///311/// # Errors312///313/// The functions below will panic in debug mode whenever you try to modify the Cranelift IR314/// function in a way that violate the coherence of the code. For instance: switching to a new315/// [`Block`] when you haven't filled the current one with a terminator instruction, inserting a316/// return instruction with arguments that don't match the function's signature.317impl<'a> FunctionBuilder<'a> {318/// Creates a new [`FunctionBuilder`] structure that will operate on a [`Function`] using a319/// [`FunctionBuilderContext`].320pub fn new(func: &'a mut Function, func_ctx: &'a mut FunctionBuilderContext) -> Self {321debug_assert!(func_ctx.is_empty());322Self {323func,324srcloc: Default::default(),325func_ctx,326position: Default::default(),327}328}329330/// Get the block that this builder is currently at.331pub fn current_block(&self) -> Option<Block> {332self.position.expand()333}334335/// Set the source location that should be assigned to all new instructions.336pub fn set_srcloc(&mut self, srcloc: ir::SourceLoc) {337self.srcloc = srcloc;338}339340/// Creates a new [`Block`] and returns its reference.341pub fn create_block(&mut self) -> Block {342let block = self.func.dfg.make_block();343self.func_ctx.ssa.declare_block(block);344block345}346347/// Mark a block as "cold".348///349/// This will try to move it out of the ordinary path of execution350/// when lowered to machine code.351pub fn set_cold_block(&mut self, block: Block) {352self.func.layout.set_cold(block);353}354355/// Insert `block` in the layout *after* the existing block `after`.356pub fn insert_block_after(&mut self, block: Block, after: Block) {357self.func.layout.insert_block_after(block, after);358}359360/// After the call to this function, new instructions will be inserted into the designated361/// block, in the order they are declared. You must declare the types of the [`Block`] arguments362/// you will use here.363///364/// When inserting the terminator instruction (which doesn't have a fallthrough to its immediate365/// successor), the block will be declared filled and it will not be possible to append366/// instructions to it.367pub fn switch_to_block(&mut self, block: Block) {368log::trace!("switch to {block:?}");369370// First we check that the previous block has been filled.371debug_assert!(372self.position.is_none()373|| self.is_unreachable()374|| self.is_pristine(self.position.unwrap())375|| self.is_filled(self.position.unwrap()),376"you have to fill your block before switching"377);378// We cannot switch to a filled block379debug_assert!(380!self.is_filled(block),381"you cannot switch to a block which is already filled"382);383384// Then we change the cursor position.385self.position = PackedOption::from(block);386}387388/// Declares that all the predecessors of this block are known.389///390/// Function to call with `block` as soon as the last branch instruction to `block` has been391/// created. Forgetting to call this method on every block will cause inconsistencies in the392/// produced functions.393pub fn seal_block(&mut self, block: Block) {394let side_effects = self.func_ctx.ssa.seal_block(block, self.func);395self.handle_ssa_side_effects(side_effects);396}397398/// Effectively calls [seal_block](Self::seal_block) on all unsealed blocks in the function.399///400/// It's more efficient to seal [`Block`]s as soon as possible, during401/// translation, but for frontends where this is impractical to do, this402/// function can be used at the end of translating all blocks to ensure403/// that everything is sealed.404pub fn seal_all_blocks(&mut self) {405let side_effects = self.func_ctx.ssa.seal_all_blocks(self.func);406self.handle_ssa_side_effects(side_effects);407}408409/// Declares the type of a variable.410///411/// This allows the variable to be defined and used later (by calling412/// [`FunctionBuilder::def_var`] and [`FunctionBuilder::use_var`]413/// respectively).414pub fn declare_var(&mut self, ty: Type) -> Variable {415self.func_ctx.variables.push(ty)416}417418/// Declare that all uses of the given variable must be included in stack419/// map metadata.420///421/// All values that are uses of this variable will be spilled to the stack422/// before each safepoint and their location on the stack included in stack423/// maps. Stack maps allow the garbage collector to identify the on-stack GC424/// roots.425///426/// This does not affect any pre-existing uses of the variable.427///428/// # Panics429///430/// Panics if the variable's type is larger than 16 bytes or if this431/// variable has not been declared yet.432pub fn declare_var_needs_stack_map(&mut self, var: Variable) {433log::trace!("declare_var_needs_stack_map({var:?})");434let ty = self.func_ctx.variables[var];435assert!(ty != types::INVALID);436assert!(ty.bytes() <= 16);437self.func_ctx.stack_map_vars.insert(var);438}439440/// Returns the Cranelift IR necessary to use a previously defined user441/// variable, returning an error if this is not possible.442pub fn try_use_var(&mut self, var: Variable) -> Result<Value, UseVariableError> {443// Assert that we're about to add instructions to this block using the definition of the444// given variable. ssa.use_var is the only part of this crate which can add block parameters445// behind the caller's back. If we disallow calling append_block_param as soon as use_var is446// called, then we enforce a strict separation between user parameters and SSA parameters.447self.ensure_inserted_block();448449let (val, side_effects) = {450let ty = *self451.func_ctx452.variables453.get(var)454.ok_or(UseVariableError::UsedBeforeDeclared(var))?;455debug_assert_ne!(456ty,457types::INVALID,458"variable {var:?} is used but its type has not been declared"459);460self.func_ctx461.ssa462.use_var(self.func, var, ty, self.position.unwrap())463};464self.handle_ssa_side_effects(side_effects);465466Ok(val)467}468469/// Returns the Cranelift IR value corresponding to the utilization at the current program470/// position of a previously defined user variable.471pub fn use_var(&mut self, var: Variable) -> Value {472self.try_use_var(var).unwrap_or_else(|_| {473panic!("variable {var:?} is used but its type has not been declared")474})475}476477/// Registers a new definition of a user variable. This function will return478/// an error if the value supplied does not match the type the variable was479/// declared to have.480pub fn try_def_var(&mut self, var: Variable, val: Value) -> Result<(), DefVariableError> {481log::trace!("try_def_var: {var:?} = {val:?}");482483let var_ty = *self484.func_ctx485.variables486.get(var)487.ok_or(DefVariableError::DefinedBeforeDeclared(var))?;488if var_ty != self.func.dfg.value_type(val) {489return Err(DefVariableError::TypeMismatch(var, val));490}491492self.func_ctx.ssa.def_var(var, val, self.position.unwrap());493Ok(())494}495496/// Register a new definition of a user variable. The type of the value must be497/// the same as the type registered for the variable.498pub fn def_var(&mut self, var: Variable, val: Value) {499self.try_def_var(var, val)500.unwrap_or_else(|error| match error {501DefVariableError::TypeMismatch(var, val) => {502panic!("declared type of variable {var:?} doesn't match type of value {val}");503}504DefVariableError::DefinedBeforeDeclared(var) => {505panic!("variable {var:?} is used but its type has not been declared");506}507})508}509510/// Set label for [`Value`]511///512/// This will not do anything unless513/// [`func.dfg.collect_debug_info`](DataFlowGraph::collect_debug_info) is called first.514pub fn set_val_label(&mut self, val: Value, label: ValueLabel) {515if let Some(values_labels) = self.func.stencil.dfg.values_labels.as_mut() {516use alloc::collections::btree_map::Entry;517518let start = ValueLabelStart {519from: RelSourceLoc::from_base_offset(self.func.params.base_srcloc(), self.srcloc),520label,521};522523match values_labels.entry(val) {524Entry::Occupied(mut e) => match e.get_mut() {525ValueLabelAssignments::Starts(starts) => starts.push(start),526_ => panic!("Unexpected ValueLabelAssignments at this stage"),527},528Entry::Vacant(e) => {529e.insert(ValueLabelAssignments::Starts(vec![start]));530}531}532}533}534535/// Declare that the given value is a GC reference that requires inclusion536/// in a stack map when it is live across GC safepoints.537///538/// At the current moment, values that need inclusion in stack maps are539/// spilled before safepoints, but they are not reloaded afterwards. This540/// means that moving GCs are not yet supported, however the intention is to541/// add this support in the near future.542///543/// # Panics544///545/// Panics if `val` is larger than 16 bytes.546pub fn declare_value_needs_stack_map(&mut self, val: Value) {547log::trace!("declare_value_needs_stack_map({val:?})");548549// We rely on these properties in `insert_safepoint_spills`.550let size = self.func.dfg.value_type(val).bytes();551assert!(size <= 16);552assert!(size.is_power_of_two());553554self.func_ctx.stack_map_values.insert(val);555}556557/// Creates a jump table in the function, to be used by [`br_table`](InstBuilder::br_table) instructions.558pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {559self.func.create_jump_table(data)560}561562/// Creates a sized stack slot in the function, to be used by [`stack_load`](InstBuilder::stack_load),563/// [`stack_store`](InstBuilder::stack_store) and [`stack_addr`](InstBuilder::stack_addr) instructions.564pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {565self.func.create_sized_stack_slot(data)566}567568/// Creates a dynamic stack slot in the function, to be used by569/// [`dynamic_stack_load`](InstBuilder::dynamic_stack_load),570/// [`dynamic_stack_store`](InstBuilder::dynamic_stack_store) and571/// [`dynamic_stack_addr`](InstBuilder::dynamic_stack_addr) instructions.572pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {573self.func.create_dynamic_stack_slot(data)574}575576/// Adds a signature which can later be used to declare an external function import.577pub fn import_signature(&mut self, signature: Signature) -> SigRef {578self.func.import_signature(signature)579}580581/// Declare an external function import.582pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {583self.func.import_function(data)584}585586/// Declares a global value accessible to the function.587pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {588self.func.create_global_value(data)589}590591/// Returns an object with the [`InstBuilder`]592/// trait that allows to conveniently append an instruction to the current [`Block`] being built.593pub fn ins<'short>(&'short mut self) -> FuncInstBuilder<'short, 'a> {594let block = self595.position596.expect("Please call switch_to_block before inserting instructions");597FuncInstBuilder::new(self, block)598}599600/// Make sure that the current block is inserted in the layout.601pub fn ensure_inserted_block(&mut self) {602let block = self.position.unwrap();603if self.is_pristine(block) {604if !self.func.layout.is_block_inserted(block) {605self.func.layout.append_block(block);606}607self.func_ctx.status[block] = BlockStatus::Partial;608} else {609debug_assert!(610!self.is_filled(block),611"you cannot add an instruction to a block already filled"612);613}614}615616/// Returns a [`FuncCursor`] pointed at the current position ready for inserting instructions.617///618/// This can be used to insert SSA code that doesn't need to access locals and that doesn't619/// need to know about [`FunctionBuilder`] at all.620pub fn cursor(&mut self) -> FuncCursor<'_> {621self.ensure_inserted_block();622FuncCursor::new(self.func)623.with_srcloc(self.srcloc)624.at_bottom(self.position.unwrap())625}626627/// Append parameters to the given [`Block`] corresponding to the function628/// parameters. This can be used to set up the block parameters for the629/// entry block.630pub fn append_block_params_for_function_params(&mut self, block: Block) {631debug_assert!(632!self.func_ctx.ssa.has_any_predecessors(block),633"block parameters for function parameters should only be added to the entry block"634);635636// These parameters count as "user" parameters here because they aren't637// inserted by the SSABuilder.638debug_assert!(639self.is_pristine(block),640"You can't add block parameters after adding any instruction"641);642643for argtyp in &self.func.stencil.signature.params {644self.func645.stencil646.dfg647.append_block_param(block, argtyp.value_type);648}649}650651/// Append parameters to the given [`Block`] corresponding to the function652/// return values. This can be used to set up the block parameters for a653/// function exit block.654pub fn append_block_params_for_function_returns(&mut self, block: Block) {655// These parameters count as "user" parameters here because they aren't656// inserted by the SSABuilder.657debug_assert!(658self.is_pristine(block),659"You can't add block parameters after adding any instruction"660);661662for argtyp in &self.func.stencil.signature.returns {663self.func664.stencil665.dfg666.append_block_param(block, argtyp.value_type);667}668}669670/// Declare that translation of the current function is complete.671///672/// This resets the state of the [`FunctionBuilderContext`] in preparation to673/// be used for another function.674pub fn finalize(mut self) {675// Check that all the `Block`s are filled and sealed.676#[cfg(debug_assertions)]677{678for block in self.func_ctx.status.keys() {679if !self.is_pristine(block) {680assert!(681self.func_ctx.ssa.is_sealed(block),682"FunctionBuilder finalized, but block {block} is not sealed",683);684assert!(685self.is_filled(block),686"FunctionBuilder finalized, but block {block} is not filled",687);688}689}690}691692// In debug mode, check that all blocks are valid basic blocks.693#[cfg(debug_assertions)]694{695// Iterate manually to provide more helpful error messages.696for block in self.func_ctx.status.keys() {697if let Err((inst, msg)) = self.func.is_block_basic(block) {698let inst_str = self.func.dfg.display_inst(inst);699panic!("{block} failed basic block invariants on {inst_str}: {msg}");700}701}702}703704// Propagate the needs-stack-map bit from variables to each of their705// associated values.706for var in self.func_ctx.stack_map_vars.iter() {707for val in self.func_ctx.ssa.values_for_var(var) {708log::trace!("propagating needs-stack-map from {var:?} to {val:?}");709debug_assert_eq!(self.func.dfg.value_type(val), self.func_ctx.variables[var]);710self.func_ctx.stack_map_values.insert(val);711}712}713714// If we have any values that need inclusion in stack maps, then we need715// to run our pass to spill those values to the stack at safepoints and716// generate stack maps.717if !self.func_ctx.stack_map_values.is_empty() {718self.func_ctx719.safepoints720.run(&mut self.func, &self.func_ctx.stack_map_values);721}722723// Clear the state (but preserve the allocated buffers) in preparation724// for translation another function.725self.func_ctx.clear();726}727}728729/// All the functions documented in the previous block are write-only and help you build a valid730/// Cranelift IR functions via multiple debug asserts. However, you might need to improve the731/// performance of your translation perform more complex transformations to your Cranelift IR732/// function. The functions below help you inspect the function you're creating and modify it733/// in ways that can be unsafe if used incorrectly.734impl<'a> FunctionBuilder<'a> {735/// Retrieves all the parameters for a [`Block`] currently inferred from the jump instructions736/// inserted that target it and the SSA construction.737pub fn block_params(&self, block: Block) -> &[Value] {738self.func.dfg.block_params(block)739}740741/// Retrieves the signature with reference `sigref` previously added with742/// [`import_signature`](Self::import_signature).743pub fn signature(&self, sigref: SigRef) -> Option<&Signature> {744self.func.dfg.signatures.get(sigref)745}746747/// Creates a parameter for a specific [`Block`] by appending it to the list of already existing748/// parameters.749///750/// **Note:** this function has to be called at the creation of the `Block` before adding751/// instructions to it, otherwise this could interfere with SSA construction.752pub fn append_block_param(&mut self, block: Block, ty: Type) -> Value {753debug_assert!(754self.is_pristine(block),755"You can't add block parameters after adding any instruction"756);757self.func.dfg.append_block_param(block, ty)758}759760/// Returns the result values of an instruction.761pub fn inst_results(&self, inst: Inst) -> &[Value] {762self.func.dfg.inst_results(inst)763}764765/// Changes the destination of a jump instruction after creation.766///767/// **Note:** You are responsible for maintaining the coherence with the arguments of768/// other jump instructions.769pub fn change_jump_destination(&mut self, inst: Inst, old_block: Block, new_block: Block) {770let dfg = &mut self.func.dfg;771for block in772dfg.insts[inst].branch_destination_mut(&mut dfg.jump_tables, &mut dfg.exception_tables)773{774if block.block(&dfg.value_lists) == old_block {775self.func_ctx.ssa.remove_block_predecessor(old_block, inst);776block.set_block(new_block, &mut dfg.value_lists);777self.func_ctx.ssa.declare_block_predecessor(new_block, inst);778}779}780}781782/// Returns `true` if and only if the current [`Block`] is sealed and has no predecessors declared.783///784/// The entry block of a function is never unreachable.785pub fn is_unreachable(&self) -> bool {786let is_entry = match self.func.layout.entry_block() {787None => false,788Some(entry) => self.position.unwrap() == entry,789};790!is_entry791&& self.func_ctx.ssa.is_sealed(self.position.unwrap())792&& !self793.func_ctx794.ssa795.has_any_predecessors(self.position.unwrap())796}797798/// Returns `true` if and only if no instructions have been added since the last call to799/// [`switch_to_block`](Self::switch_to_block).800fn is_pristine(&self, block: Block) -> bool {801self.func_ctx.status[block] == BlockStatus::Empty802}803804/// Returns `true` if and only if a terminator instruction has been inserted since the805/// last call to [`switch_to_block`](Self::switch_to_block).806fn is_filled(&self, block: Block) -> bool {807self.func_ctx.status[block] == BlockStatus::Filled808}809}810811/// Helper functions812impl<'a> FunctionBuilder<'a> {813/// Calls libc.memcpy814///815/// Copies the `size` bytes from `src` to `dest`, assumes that `src + size`816/// won't overlap onto `dest`. If `dest` and `src` overlap, the behavior is817/// undefined. Applications in which `dest` and `src` might overlap should818/// use `call_memmove` instead.819pub fn call_memcpy(820&mut self,821config: TargetFrontendConfig,822dest: Value,823src: Value,824size: Value,825) {826let pointer_type = config.pointer_type();827let signature = {828let mut s = Signature::new(config.default_call_conv);829s.params.push(AbiParam::new(pointer_type));830s.params.push(AbiParam::new(pointer_type));831s.params.push(AbiParam::new(pointer_type));832s.returns.push(AbiParam::new(pointer_type));833self.import_signature(s)834};835836let libc_memcpy = self.import_function(ExtFuncData {837name: ExternalName::LibCall(LibCall::Memcpy),838signature,839colocated: false,840});841842self.ins().call(libc_memcpy, &[dest, src, size]);843}844845/// Optimised memcpy or memmove for small copies.846///847/// # Codegen safety848///849/// The following properties must hold to prevent UB:850///851/// * `src_align` and `dest_align` are an upper-bound on the alignment of `src` respectively `dest`.852/// * If `non_overlapping` is true, then this must be correct.853pub fn emit_small_memory_copy(854&mut self,855config: TargetFrontendConfig,856dest: Value,857src: Value,858size: u64,859dest_align: u8,860src_align: u8,861non_overlapping: bool,862mut flags: MemFlags,863) {864// Currently the result of guess work, not actual profiling.865const THRESHOLD: u64 = 4;866867if size == 0 {868return;869}870871let access_size = greatest_divisible_power_of_two(size);872assert!(873access_size.is_power_of_two(),874"`size` is not a power of two"875);876assert!(877access_size >= u64::from(::core::cmp::min(src_align, dest_align)),878"`size` is smaller than `dest` and `src`'s alignment value."879);880881let (access_size, int_type) = if access_size <= 8 {882(access_size, Type::int((access_size * 8) as u16).unwrap())883} else {884(8, types::I64)885};886887let load_and_store_amount = size / access_size;888889if load_and_store_amount > THRESHOLD {890let size_value = self.ins().iconst(config.pointer_type(), size as i64);891if non_overlapping {892self.call_memcpy(config, dest, src, size_value);893} else {894self.call_memmove(config, dest, src, size_value);895}896return;897}898899if u64::from(src_align) >= access_size && u64::from(dest_align) >= access_size {900flags.set_aligned();901}902903// Load all of the memory first. This is necessary in case `dest` overlaps.904// It can also improve performance a bit.905let registers: smallvec::SmallVec<[_; THRESHOLD as usize]> = (0..load_and_store_amount)906.map(|i| {907let offset = (access_size * i) as i32;908(self.ins().load(int_type, flags, src, offset), offset)909})910.collect();911912for (value, offset) in registers {913self.ins().store(flags, value, dest, offset);914}915}916917/// Calls libc.memset918///919/// Writes `size` bytes of i8 value `ch` to memory starting at `buffer`.920pub fn call_memset(921&mut self,922config: TargetFrontendConfig,923buffer: Value,924ch: Value,925size: Value,926) {927let pointer_type = config.pointer_type();928let signature = {929let mut s = Signature::new(config.default_call_conv);930s.params.push(AbiParam::new(pointer_type));931s.params.push(AbiParam::new(types::I32));932s.params.push(AbiParam::new(pointer_type));933s.returns.push(AbiParam::new(pointer_type));934self.import_signature(s)935};936937let libc_memset = self.import_function(ExtFuncData {938name: ExternalName::LibCall(LibCall::Memset),939signature,940colocated: false,941});942943let ch = self.ins().uextend(types::I32, ch);944self.ins().call(libc_memset, &[buffer, ch, size]);945}946947/// Calls libc.memset948///949/// Writes `size` bytes of value `ch` to memory starting at `buffer`.950pub fn emit_small_memset(951&mut self,952config: TargetFrontendConfig,953buffer: Value,954ch: u8,955size: u64,956buffer_align: u8,957mut flags: MemFlags,958) {959// Currently the result of guess work, not actual profiling.960const THRESHOLD: u64 = 4;961962if size == 0 {963return;964}965966let access_size = greatest_divisible_power_of_two(size);967assert!(968access_size.is_power_of_two(),969"`size` is not a power of two"970);971assert!(972access_size >= u64::from(buffer_align),973"`size` is smaller than `dest` and `src`'s alignment value."974);975976let (access_size, int_type) = if access_size <= 8 {977(access_size, Type::int((access_size * 8) as u16).unwrap())978} else {979(8, types::I64)980};981982let load_and_store_amount = size / access_size;983984if load_and_store_amount > THRESHOLD {985let ch = self.ins().iconst(types::I8, i64::from(ch));986let size = self.ins().iconst(config.pointer_type(), size as i64);987self.call_memset(config, buffer, ch, size);988} else {989if u64::from(buffer_align) >= access_size {990flags.set_aligned();991}992993let ch = u64::from(ch);994let raw_value = if int_type == types::I64 {995ch * 0x0101010101010101_u64996} else if int_type == types::I32 {997ch * 0x01010101_u64998} else if int_type == types::I16 {999(ch << 8) | ch1000} else {1001assert_eq!(int_type, types::I8);1002ch1003};10041005let value = self.ins().iconst(int_type, raw_value as i64);1006for i in 0..load_and_store_amount {1007let offset = (access_size * i) as i32;1008self.ins().store(flags, value, buffer, offset);1009}1010}1011}10121013/// Calls libc.memmove1014///1015/// Copies `size` bytes from memory starting at `source` to memory starting1016/// at `dest`. `source` is always read before writing to `dest`.1017pub fn call_memmove(1018&mut self,1019config: TargetFrontendConfig,1020dest: Value,1021source: Value,1022size: Value,1023) {1024let pointer_type = config.pointer_type();1025let signature = {1026let mut s = Signature::new(config.default_call_conv);1027s.params.push(AbiParam::new(pointer_type));1028s.params.push(AbiParam::new(pointer_type));1029s.params.push(AbiParam::new(pointer_type));1030s.returns.push(AbiParam::new(pointer_type));1031self.import_signature(s)1032};10331034let libc_memmove = self.import_function(ExtFuncData {1035name: ExternalName::LibCall(LibCall::Memmove),1036signature,1037colocated: false,1038});10391040self.ins().call(libc_memmove, &[dest, source, size]);1041}10421043/// Calls libc.memcmp1044///1045/// Compares `size` bytes from memory starting at `left` to memory starting1046/// at `right`. Returns `0` if all `n` bytes are equal. If the first difference1047/// is at offset `i`, returns a positive integer if `ugt(left[i], right[i])`1048/// and a negative integer if `ult(left[i], right[i])`.1049///1050/// Returns a C `int`, which is currently always [`types::I32`].1051pub fn call_memcmp(1052&mut self,1053config: TargetFrontendConfig,1054left: Value,1055right: Value,1056size: Value,1057) -> Value {1058let pointer_type = config.pointer_type();1059let signature = {1060let mut s = Signature::new(config.default_call_conv);1061s.params.reserve(3);1062s.params.push(AbiParam::new(pointer_type));1063s.params.push(AbiParam::new(pointer_type));1064s.params.push(AbiParam::new(pointer_type));1065s.returns.push(AbiParam::new(types::I32));1066self.import_signature(s)1067};10681069let libc_memcmp = self.import_function(ExtFuncData {1070name: ExternalName::LibCall(LibCall::Memcmp),1071signature,1072colocated: false,1073});10741075let call = self.ins().call(libc_memcmp, &[left, right, size]);1076self.func.dfg.first_result(call)1077}10781079/// Optimised [`Self::call_memcmp`] for small copies.1080///1081/// This implements the byte slice comparison `int_cc(left[..size], right[..size])`.1082///1083/// `left_align` and `right_align` are the statically-known alignments of the1084/// `left` and `right` pointers respectively. These are used to know whether1085/// to mark `load`s as aligned. It's always fine to pass `1` for these, but1086/// passing something higher than the true alignment may trap or otherwise1087/// misbehave as described in [`MemFlags::aligned`].1088///1089/// Note that `memcmp` is a *big-endian* and *unsigned* comparison.1090/// As such, this panics when called with `IntCC::Signed*`.1091pub fn emit_small_memory_compare(1092&mut self,1093config: TargetFrontendConfig,1094int_cc: IntCC,1095left: Value,1096right: Value,1097size: u64,1098left_align: std::num::NonZeroU8,1099right_align: std::num::NonZeroU8,1100flags: MemFlags,1101) -> Value {1102use IntCC::*;1103let (zero_cc, empty_imm) = match int_cc {1104//1105Equal => (Equal, 1),1106NotEqual => (NotEqual, 0),11071108UnsignedLessThan => (SignedLessThan, 0),1109UnsignedGreaterThanOrEqual => (SignedGreaterThanOrEqual, 1),1110UnsignedGreaterThan => (SignedGreaterThan, 0),1111UnsignedLessThanOrEqual => (SignedLessThanOrEqual, 1),11121113SignedLessThan1114| SignedGreaterThanOrEqual1115| SignedGreaterThan1116| SignedLessThanOrEqual => {1117panic!("Signed comparison {int_cc} not supported by memcmp")1118}1119};11201121if size == 0 {1122return self.ins().iconst(types::I8, empty_imm);1123}11241125// Future work could consider expanding this to handle more-complex scenarios.1126if let Some(small_type) = size.try_into().ok().and_then(Type::int_with_byte_size) {1127if let Equal | NotEqual = zero_cc {1128let mut left_flags = flags;1129if size == left_align.get() as u64 {1130left_flags.set_aligned();1131}1132let mut right_flags = flags;1133if size == right_align.get() as u64 {1134right_flags.set_aligned();1135}1136let left_val = self.ins().load(small_type, left_flags, left, 0);1137let right_val = self.ins().load(small_type, right_flags, right, 0);1138return self.ins().icmp(int_cc, left_val, right_val);1139} else if small_type == types::I8 {1140// Once the big-endian loads from wasmtime#2492 are implemented in1141// the backends, we could easily handle comparisons for more sizes here.1142// But for now, just handle single bytes where we don't need to worry.11431144let mut aligned_flags = flags;1145aligned_flags.set_aligned();1146let left_val = self.ins().load(small_type, aligned_flags, left, 0);1147let right_val = self.ins().load(small_type, aligned_flags, right, 0);1148return self.ins().icmp(int_cc, left_val, right_val);1149}1150}11511152let pointer_type = config.pointer_type();1153let size = self.ins().iconst(pointer_type, size as i64);1154let cmp = self.call_memcmp(config, left, right, size);1155self.ins().icmp_imm(zero_cc, cmp, 0)1156}1157}11581159fn greatest_divisible_power_of_two(size: u64) -> u64 {1160(size as i64 & -(size as i64)) as u641161}11621163// Helper functions1164impl<'a> FunctionBuilder<'a> {1165/// A Block is 'filled' when a terminator instruction is present.1166fn fill_current_block(&mut self) {1167self.func_ctx.status[self.position.unwrap()] = BlockStatus::Filled;1168}11691170fn declare_successor(&mut self, dest_block: Block, jump_inst: Inst) {1171self.func_ctx1172.ssa1173.declare_block_predecessor(dest_block, jump_inst);1174}11751176fn handle_ssa_side_effects(&mut self, side_effects: SideEffects) {1177let SideEffects {1178instructions_added_to_blocks,1179} = side_effects;11801181for modified_block in instructions_added_to_blocks {1182if self.is_pristine(modified_block) {1183self.func_ctx.status[modified_block] = BlockStatus::Partial;1184}1185}1186}1187}11881189#[cfg(test)]1190mod tests {1191use super::greatest_divisible_power_of_two;1192use crate::Variable;1193use crate::frontend::{1194DefVariableError, FunctionBuilder, FunctionBuilderContext, UseVariableError,1195};1196use alloc::string::ToString;1197use cranelift_codegen::ir::condcodes::IntCC;1198use cranelift_codegen::ir::{1199AbiParam, BlockCall, ExceptionTableData, ExtFuncData, ExternalName, Function, InstBuilder,1200MemFlags, Signature, UserExternalName, UserFuncName, Value, types::*,1201};1202use cranelift_codegen::isa::{CallConv, TargetFrontendConfig, TargetIsa};1203use cranelift_codegen::settings;1204use cranelift_codegen::verifier::verify_function;1205use target_lexicon::PointerWidth;12061207fn sample_function(lazy_seal: bool) {1208let mut sig = Signature::new(CallConv::SystemV);1209sig.returns.push(AbiParam::new(I32));1210sig.params.push(AbiParam::new(I32));12111212let mut fn_ctx = FunctionBuilderContext::new();1213let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1214{1215let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);12161217let block0 = builder.create_block();1218let block1 = builder.create_block();1219let block2 = builder.create_block();1220let block3 = builder.create_block();1221let x = builder.declare_var(I32);1222let y = builder.declare_var(I32);1223let z = builder.declare_var(I32);12241225builder.append_block_params_for_function_params(block0);12261227builder.switch_to_block(block0);1228if !lazy_seal {1229builder.seal_block(block0);1230}1231{1232let tmp = builder.block_params(block0)[0]; // the first function parameter1233builder.def_var(x, tmp);1234}1235{1236let tmp = builder.ins().iconst(I32, 2);1237builder.def_var(y, tmp);1238}1239{1240let arg1 = builder.use_var(x);1241let arg2 = builder.use_var(y);1242let tmp = builder.ins().iadd(arg1, arg2);1243builder.def_var(z, tmp);1244}1245builder.ins().jump(block1, &[]);12461247builder.switch_to_block(block1);1248{1249let arg1 = builder.use_var(y);1250let arg2 = builder.use_var(z);1251let tmp = builder.ins().iadd(arg1, arg2);1252builder.def_var(z, tmp);1253}1254{1255let arg = builder.use_var(y);1256builder.ins().brif(arg, block3, &[], block2, &[]);1257}12581259builder.switch_to_block(block2);1260if !lazy_seal {1261builder.seal_block(block2);1262}1263{1264let arg1 = builder.use_var(z);1265let arg2 = builder.use_var(x);1266let tmp = builder.ins().isub(arg1, arg2);1267builder.def_var(z, tmp);1268}1269{1270let arg = builder.use_var(y);1271builder.ins().return_(&[arg]);1272}12731274builder.switch_to_block(block3);1275if !lazy_seal {1276builder.seal_block(block3);1277}12781279{1280let arg1 = builder.use_var(y);1281let arg2 = builder.use_var(x);1282let tmp = builder.ins().isub(arg1, arg2);1283builder.def_var(y, tmp);1284}1285builder.ins().jump(block1, &[]);1286if !lazy_seal {1287builder.seal_block(block1);1288}12891290if lazy_seal {1291builder.seal_all_blocks();1292}12931294builder.finalize();1295}12961297let flags = settings::Flags::new(settings::builder());1298// println!("{}", func.display(None));1299if let Err(errors) = verify_function(&func, &flags) {1300panic!("{}\n{}", func.display(), errors)1301}1302}13031304#[test]1305fn sample() {1306sample_function(false)1307}13081309#[test]1310fn sample_with_lazy_seal() {1311sample_function(true)1312}13131314#[track_caller]1315fn check(func: &Function, expected_ir: &str) {1316let expected_ir = expected_ir.trim();1317let actual_ir = func.display().to_string();1318let actual_ir = actual_ir.trim();1319assert!(1320expected_ir == actual_ir,1321"Expected:\n{expected_ir}\nGot:\n{actual_ir}"1322);1323}13241325/// Helper function to construct a fixed frontend configuration.1326fn systemv_frontend_config() -> TargetFrontendConfig {1327TargetFrontendConfig {1328default_call_conv: CallConv::SystemV,1329pointer_width: PointerWidth::U64,1330page_size_align_log2: 12,1331}1332}13331334#[test]1335fn memcpy() {1336let frontend_config = systemv_frontend_config();1337let mut sig = Signature::new(frontend_config.default_call_conv);1338sig.returns.push(AbiParam::new(I32));13391340let mut fn_ctx = FunctionBuilderContext::new();1341let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1342{1343let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);13441345let block0 = builder.create_block();1346let x = builder.declare_var(frontend_config.pointer_type());1347let y = builder.declare_var(frontend_config.pointer_type());1348let _z = builder.declare_var(I32);13491350builder.append_block_params_for_function_params(block0);1351builder.switch_to_block(block0);13521353let src = builder.use_var(x);1354let dest = builder.use_var(y);1355let size = builder.use_var(y);1356builder.call_memcpy(frontend_config, dest, src, size);1357builder.ins().return_(&[size]);13581359builder.seal_all_blocks();1360builder.finalize();1361}13621363check(1364&func,1365"function %sample() -> i32 system_v {1366sig0 = (i64, i64, i64) -> i64 system_v1367fn0 = %Memcpy sig013681369block0:1370v4 = iconst.i64 01371v1 -> v41372v3 = iconst.i64 01373v0 -> v31374v2 = call fn0(v1, v0, v1) ; v1 = 0, v0 = 0, v1 = 01375return v1 ; v1 = 01376}1377",1378);1379}13801381#[test]1382fn small_memcpy() {1383let frontend_config = systemv_frontend_config();1384let mut sig = Signature::new(frontend_config.default_call_conv);1385sig.returns.push(AbiParam::new(I32));13861387let mut fn_ctx = FunctionBuilderContext::new();1388let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1389{1390let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);13911392let block0 = builder.create_block();1393let x = builder.declare_var(frontend_config.pointer_type());1394let y = builder.declare_var(frontend_config.pointer_type());13951396builder.append_block_params_for_function_params(block0);1397builder.switch_to_block(block0);13981399let src = builder.use_var(x);1400let dest = builder.use_var(y);1401let size = 8;1402builder.emit_small_memory_copy(1403frontend_config,1404dest,1405src,1406size,14078,14088,1409true,1410MemFlags::new(),1411);1412builder.ins().return_(&[dest]);14131414builder.seal_all_blocks();1415builder.finalize();1416}14171418check(1419&func,1420"function %sample() -> i32 system_v {1421block0:1422v4 = iconst.i64 01423v1 -> v41424v3 = iconst.i64 01425v0 -> v31426v2 = load.i64 aligned v0 ; v0 = 01427store aligned v2, v1 ; v1 = 01428return v1 ; v1 = 01429}1430",1431);1432}14331434#[test]1435fn not_so_small_memcpy() {1436let frontend_config = systemv_frontend_config();1437let mut sig = Signature::new(frontend_config.default_call_conv);1438sig.returns.push(AbiParam::new(I32));14391440let mut fn_ctx = FunctionBuilderContext::new();1441let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1442{1443let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);14441445let block0 = builder.create_block();1446let x = builder.declare_var(frontend_config.pointer_type());1447let y = builder.declare_var(frontend_config.pointer_type());1448builder.append_block_params_for_function_params(block0);1449builder.switch_to_block(block0);14501451let src = builder.use_var(x);1452let dest = builder.use_var(y);1453let size = 8192;1454builder.emit_small_memory_copy(1455frontend_config,1456dest,1457src,1458size,14598,14608,1461true,1462MemFlags::new(),1463);1464builder.ins().return_(&[dest]);14651466builder.seal_all_blocks();1467builder.finalize();1468}14691470check(1471&func,1472"function %sample() -> i32 system_v {1473sig0 = (i64, i64, i64) -> i64 system_v1474fn0 = %Memcpy sig014751476block0:1477v5 = iconst.i64 01478v1 -> v51479v4 = iconst.i64 01480v0 -> v41481v2 = iconst.i64 81921482v3 = call fn0(v1, v0, v2) ; v1 = 0, v0 = 0, v2 = 81921483return v1 ; v1 = 01484}1485",1486);1487}14881489#[test]1490fn small_memset() {1491let frontend_config = systemv_frontend_config();1492let mut sig = Signature::new(frontend_config.default_call_conv);1493sig.returns.push(AbiParam::new(I32));14941495let mut fn_ctx = FunctionBuilderContext::new();1496let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1497{1498let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);14991500let block0 = builder.create_block();1501let y = builder.declare_var(frontend_config.pointer_type());1502builder.append_block_params_for_function_params(block0);1503builder.switch_to_block(block0);15041505let dest = builder.use_var(y);1506let size = 8;1507builder.emit_small_memset(frontend_config, dest, 1, size, 8, MemFlags::new());1508builder.ins().return_(&[dest]);15091510builder.seal_all_blocks();1511builder.finalize();1512}15131514check(1515&func,1516"function %sample() -> i32 system_v {1517block0:1518v2 = iconst.i64 01519v0 -> v21520v1 = iconst.i64 0x0101_0101_0101_01011521store aligned v1, v0 ; v1 = 0x0101_0101_0101_0101, v0 = 01522return v0 ; v0 = 01523}1524",1525);1526}15271528#[test]1529fn not_so_small_memset() {1530let frontend_config = systemv_frontend_config();1531let mut sig = Signature::new(frontend_config.default_call_conv);1532sig.returns.push(AbiParam::new(I32));15331534let mut fn_ctx = FunctionBuilderContext::new();1535let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1536{1537let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);15381539let block0 = builder.create_block();1540let y = builder.declare_var(frontend_config.pointer_type());1541builder.append_block_params_for_function_params(block0);1542builder.switch_to_block(block0);15431544let dest = builder.use_var(y);1545let size = 8192;1546builder.emit_small_memset(frontend_config, dest, 1, size, 8, MemFlags::new());1547builder.ins().return_(&[dest]);15481549builder.seal_all_blocks();1550builder.finalize();1551}15521553check(1554&func,1555"function %sample() -> i32 system_v {1556sig0 = (i64, i32, i64) -> i64 system_v1557fn0 = %Memset sig015581559block0:1560v5 = iconst.i64 01561v0 -> v51562v1 = iconst.i8 11563v2 = iconst.i64 81921564v3 = uextend.i32 v1 ; v1 = 11565v4 = call fn0(v0, v3, v2) ; v0 = 0, v2 = 81921566return v0 ; v0 = 01567}1568",1569);1570}15711572#[test]1573fn memcmp() {1574use core::str::FromStr;1575use cranelift_codegen::isa;15761577let shared_builder = settings::builder();1578let shared_flags = settings::Flags::new(shared_builder);15791580let triple =1581::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple");15821583let target = isa::lookup(triple)1584.ok()1585.map(|b| b.finish(shared_flags))1586.expect("This test requires x86_64 support.")1587.expect("Should be able to create backend with default flags");15881589let mut sig = Signature::new(target.default_call_conv());1590sig.returns.push(AbiParam::new(I32));15911592let mut fn_ctx = FunctionBuilderContext::new();1593let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1594{1595let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);15961597let block0 = builder.create_block();1598let x = builder.declare_var(target.pointer_type());1599let y = builder.declare_var(target.pointer_type());1600let z = builder.declare_var(target.pointer_type());1601builder.append_block_params_for_function_params(block0);1602builder.switch_to_block(block0);16031604let left = builder.use_var(x);1605let right = builder.use_var(y);1606let size = builder.use_var(z);1607let cmp = builder.call_memcmp(target.frontend_config(), left, right, size);1608builder.ins().return_(&[cmp]);16091610builder.seal_all_blocks();1611builder.finalize();1612}16131614check(1615&func,1616"function %sample() -> i32 system_v {1617sig0 = (i64, i64, i64) -> i32 system_v1618fn0 = %Memcmp sig016191620block0:1621v6 = iconst.i64 01622v2 -> v61623v5 = iconst.i64 01624v1 -> v51625v4 = iconst.i64 01626v0 -> v41627v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 01628return v31629}1630",1631);1632}16331634#[test]1635fn small_memcmp_zero_size() {1636let align_eight = std::num::NonZeroU8::new(8).unwrap();1637small_memcmp_helper(1638"1639block0:1640v4 = iconst.i64 01641v1 -> v41642v3 = iconst.i64 01643v0 -> v31644v2 = iconst.i8 11645return v2 ; v2 = 1",1646|builder, target, x, y| {1647builder.emit_small_memory_compare(1648target.frontend_config(),1649IntCC::UnsignedGreaterThanOrEqual,1650x,1651y,16520,1653align_eight,1654align_eight,1655MemFlags::new(),1656)1657},1658);1659}16601661#[test]1662fn small_memcmp_byte_ugt() {1663let align_one = std::num::NonZeroU8::new(1).unwrap();1664small_memcmp_helper(1665"1666block0:1667v6 = iconst.i64 01668v1 -> v61669v5 = iconst.i64 01670v0 -> v51671v2 = load.i8 aligned v0 ; v0 = 01672v3 = load.i8 aligned v1 ; v1 = 01673v4 = icmp ugt v2, v31674return v4",1675|builder, target, x, y| {1676builder.emit_small_memory_compare(1677target.frontend_config(),1678IntCC::UnsignedGreaterThan,1679x,1680y,16811,1682align_one,1683align_one,1684MemFlags::new(),1685)1686},1687);1688}16891690#[test]1691fn small_memcmp_aligned_eq() {1692let align_four = std::num::NonZeroU8::new(4).unwrap();1693small_memcmp_helper(1694"1695block0:1696v6 = iconst.i64 01697v1 -> v61698v5 = iconst.i64 01699v0 -> v51700v2 = load.i32 aligned v0 ; v0 = 01701v3 = load.i32 aligned v1 ; v1 = 01702v4 = icmp eq v2, v31703return v4",1704|builder, target, x, y| {1705builder.emit_small_memory_compare(1706target.frontend_config(),1707IntCC::Equal,1708x,1709y,17104,1711align_four,1712align_four,1713MemFlags::new(),1714)1715},1716);1717}17181719#[test]1720fn small_memcmp_ipv6_ne() {1721let align_two = std::num::NonZeroU8::new(2).unwrap();1722small_memcmp_helper(1723"1724block0:1725v6 = iconst.i64 01726v1 -> v61727v5 = iconst.i64 01728v0 -> v51729v2 = load.i128 v0 ; v0 = 01730v3 = load.i128 v1 ; v1 = 01731v4 = icmp ne v2, v31732return v4",1733|builder, target, x, y| {1734builder.emit_small_memory_compare(1735target.frontend_config(),1736IntCC::NotEqual,1737x,1738y,173916,1740align_two,1741align_two,1742MemFlags::new(),1743)1744},1745);1746}17471748#[test]1749fn small_memcmp_odd_size_uge() {1750let one = std::num::NonZeroU8::new(1).unwrap();1751small_memcmp_helper(1752"1753sig0 = (i64, i64, i64) -> i32 system_v1754fn0 = %Memcmp sig017551756block0:1757v6 = iconst.i64 01758v1 -> v61759v5 = iconst.i64 01760v0 -> v51761v2 = iconst.i64 31762v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 31763v4 = icmp_imm sge v3, 01764return v4",1765|builder, target, x, y| {1766builder.emit_small_memory_compare(1767target.frontend_config(),1768IntCC::UnsignedGreaterThanOrEqual,1769x,1770y,17713,1772one,1773one,1774MemFlags::new(),1775)1776},1777);1778}17791780fn small_memcmp_helper(1781expected: &str,1782f: impl FnOnce(&mut FunctionBuilder, &dyn TargetIsa, Value, Value) -> Value,1783) {1784use core::str::FromStr;1785use cranelift_codegen::isa;17861787let shared_builder = settings::builder();1788let shared_flags = settings::Flags::new(shared_builder);17891790let triple =1791::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple");17921793let target = isa::lookup(triple)1794.ok()1795.map(|b| b.finish(shared_flags))1796.expect("This test requires x86_64 support.")1797.expect("Should be able to create backend with default flags");17981799let mut sig = Signature::new(target.default_call_conv());1800sig.returns.push(AbiParam::new(I8));18011802let mut fn_ctx = FunctionBuilderContext::new();1803let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1804{1805let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);18061807let block0 = builder.create_block();1808let x = builder.declare_var(target.pointer_type());1809let y = builder.declare_var(target.pointer_type());1810builder.append_block_params_for_function_params(block0);1811builder.switch_to_block(block0);18121813let left = builder.use_var(x);1814let right = builder.use_var(y);1815let ret = f(&mut builder, &*target, left, right);1816builder.ins().return_(&[ret]);18171818builder.seal_all_blocks();1819builder.finalize();1820}18211822check(1823&func,1824&format!("function %sample() -> i8 system_v {{{expected}\n}}\n"),1825);1826}18271828#[test]1829fn undef_vector_vars() {1830let mut sig = Signature::new(CallConv::SystemV);1831sig.returns.push(AbiParam::new(I8X16));1832sig.returns.push(AbiParam::new(I8X16));1833sig.returns.push(AbiParam::new(F32X4));18341835let mut fn_ctx = FunctionBuilderContext::new();1836let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1837{1838let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);18391840let block0 = builder.create_block();1841let a = builder.declare_var(I8X16);1842let b = builder.declare_var(I8X16);1843let c = builder.declare_var(F32X4);1844builder.switch_to_block(block0);18451846let a = builder.use_var(a);1847let b = builder.use_var(b);1848let c = builder.use_var(c);1849builder.ins().return_(&[a, b, c]);18501851builder.seal_all_blocks();1852builder.finalize();1853}18541855check(1856&func,1857"function %sample() -> i8x16, i8x16, f32x4 system_v {1858const0 = 0x0000000000000000000000000000000018591860block0:1861v5 = f32const 0.01862v6 = splat.f32x4 v5 ; v5 = 0.01863v2 -> v61864v4 = vconst.i8x16 const01865v1 -> v41866v3 = vconst.i8x16 const01867v0 -> v31868return v0, v1, v2 ; v0 = const0, v1 = const01869}1870",1871);1872}18731874#[test]1875fn test_greatest_divisible_power_of_two() {1876assert_eq!(64, greatest_divisible_power_of_two(64));1877assert_eq!(16, greatest_divisible_power_of_two(48));1878assert_eq!(8, greatest_divisible_power_of_two(24));1879assert_eq!(1, greatest_divisible_power_of_two(25));1880}18811882#[test]1883fn try_use_var() {1884let sig = Signature::new(CallConv::SystemV);18851886let mut fn_ctx = FunctionBuilderContext::new();1887let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);1888{1889let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);18901891let block0 = builder.create_block();1892builder.append_block_params_for_function_params(block0);1893builder.switch_to_block(block0);18941895assert_eq!(1896builder.try_use_var(Variable::from_u32(0)),1897Err(UseVariableError::UsedBeforeDeclared(Variable::from_u32(0)))1898);18991900let value = builder.ins().iconst(cranelift_codegen::ir::types::I32, 0);19011902assert_eq!(1903builder.try_def_var(Variable::from_u32(0), value),1904Err(DefVariableError::DefinedBeforeDeclared(Variable::from_u32(190501906)))1907);1908}1909}19101911#[test]1912fn test_builder_with_iconst_and_negative_constant() {1913let sig = Signature::new(CallConv::SystemV);1914let mut fn_ctx = FunctionBuilderContext::new();1915let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);19161917let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);19181919let block0 = builder.create_block();1920builder.switch_to_block(block0);1921builder.ins().iconst(I32, -1);1922builder.ins().return_(&[]);19231924builder.seal_all_blocks();1925builder.finalize();19261927let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());1928let ctx = cranelift_codegen::Context::for_function(func);1929ctx.verify(&flags).expect("should be valid");19301931check(1932&ctx.func,1933"function %sample() system_v {1934block0:1935v0 = iconst.i32 -11936return1937}",1938);1939}19401941#[test]1942fn try_call() {1943let mut sig = Signature::new(CallConv::SystemV);1944sig.params.push(AbiParam::new(I8));1945sig.returns.push(AbiParam::new(I32));1946let mut fn_ctx = FunctionBuilderContext::new();1947let mut func = Function::with_name_signature(UserFuncName::testcase("sample"), sig);19481949let sig0 = func.import_signature(Signature::new(CallConv::SystemV));1950let name = func.declare_imported_user_function(UserExternalName::new(0, 0));1951let fn0 = func.import_function(ExtFuncData {1952name: ExternalName::User(name),1953signature: sig0,1954colocated: false,1955});19561957let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);19581959let block0 = builder.create_block();1960let block1 = builder.create_block();1961let block2 = builder.create_block();1962let block3 = builder.create_block();19631964let my_var = builder.declare_var(I32);19651966builder.switch_to_block(block0);1967let branch_val = builder.append_block_param(block0, I8);1968builder.ins().brif(branch_val, block1, &[], block2, &[]);19691970builder.switch_to_block(block1);1971let one = builder.ins().iconst(I32, 1);1972builder.def_var(my_var, one);19731974let normal_return = BlockCall::new(block3, [], &mut builder.func.dfg.value_lists);1975let exception_table = builder1976.func1977.dfg1978.exception_tables1979.push(ExceptionTableData::new(sig0, normal_return, []));1980builder.ins().try_call(fn0, &[], exception_table);19811982builder.switch_to_block(block2);1983let two = builder.ins().iconst(I32, 2);1984builder.def_var(my_var, two);19851986let normal_return = BlockCall::new(block3, [], &mut builder.func.dfg.value_lists);1987let exception_table = builder1988.func1989.dfg1990.exception_tables1991.push(ExceptionTableData::new(sig0, normal_return, []));1992builder.ins().try_call(fn0, &[], exception_table);19931994builder.switch_to_block(block3);1995let ret_val = builder.use_var(my_var);1996builder.ins().return_(&[ret_val]);19971998builder.seal_all_blocks();1999builder.finalize();20002001let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());2002let ctx = cranelift_codegen::Context::for_function(func);2003ctx.verify(&flags).expect("should be valid");20042005check(2006&ctx.func,2007"function %sample(i8) -> i32 system_v {2008sig0 = () system_v2009fn0 = u0:0 sig020102011block0(v0: i8):2012brif v0, block1, block220132014block1:2015v1 = iconst.i32 12016try_call fn0(), sig0, block3(v1), [] ; v1 = 120172018block2:2019v2 = iconst.i32 22020try_call fn0(), sig0, block3(v2), [] ; v2 = 220212022block3(v3: i32):2023return v32024}",2025);2026}2027}202820292030