Path: blob/main/cranelift/codegen/src/ir/function.rs
1693 views
//! Intermediate representation of a function.1//!2//! The `Function` struct defined in this module owns all of its basic blocks and3//! instructions.45use crate::HashMap;6use crate::entity::{PrimaryMap, SecondaryMap};7use crate::ir::{8self, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, DynamicStackSlots,9DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, JumpTable,10JumpTableData, Layout, MemoryType, MemoryTypeData, SigRef, Signature, SourceLocs, StackSlot,11StackSlotData, StackSlots, Type, pcc::Fact,12};13use crate::isa::CallConv;14use crate::write::{write_function, write_function_spec};15#[cfg(feature = "enable-serde")]16use alloc::string::String;17use core::fmt;1819#[cfg(feature = "enable-serde")]20use serde::de::{Deserializer, Error};21#[cfg(feature = "enable-serde")]22use serde::ser::Serializer;23#[cfg(feature = "enable-serde")]24use serde::{Deserialize, Serialize};2526use super::entities::UserExternalNameRef;27use super::extname::UserFuncName;28use super::{RelSourceLoc, SourceLoc, UserExternalName};2930/// A version marker used to ensure that serialized clif ir is never deserialized with a31/// different version of Cranelift.32#[derive(Default, Copy, Clone, Debug, PartialEq, Hash)]33pub struct VersionMarker;3435#[cfg(feature = "enable-serde")]36impl Serialize for VersionMarker {37fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>38where39S: Serializer,40{41crate::VERSION.serialize(serializer)42}43}4445#[cfg(feature = "enable-serde")]46impl<'de> Deserialize<'de> for VersionMarker {47fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>48where49D: Deserializer<'de>,50{51let version = String::deserialize(deserializer)?;52if version != crate::VERSION {53return Err(D::Error::custom(&format!(54"Expected a clif ir function for version {}, found one for version {}",55crate::VERSION,56version,57)));58}59Ok(VersionMarker)60}61}6263/// Function parameters used when creating this function, and that will become applied after64/// compilation to materialize the final `CompiledCode`.65#[derive(Clone, PartialEq)]66#[cfg_attr(67feature = "enable-serde",68derive(serde_derive::Serialize, serde_derive::Deserialize)69)]70pub struct FunctionParameters {71/// The first `SourceLoc` appearing in the function, serving as a base for every relative72/// source loc in the function.73base_srcloc: Option<SourceLoc>,7475/// External user-defined function references.76user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,7778/// Inverted mapping of `user_named_funcs`, to deduplicate internally.79user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,80}8182impl FunctionParameters {83/// Creates a new `FunctionParameters` with the given name.84pub fn new() -> Self {85Self {86base_srcloc: None,87user_named_funcs: Default::default(),88user_ext_name_to_ref: Default::default(),89}90}9192/// Returns the base `SourceLoc`.93///94/// If it was never explicitly set with `ensure_base_srcloc`, will return an invalid95/// `SourceLoc`.96pub fn base_srcloc(&self) -> SourceLoc {97self.base_srcloc.unwrap_or_default()98}99100/// Sets the base `SourceLoc`, if not set yet, and returns the base value.101pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {102match self.base_srcloc {103Some(val) => val,104None => {105self.base_srcloc = Some(srcloc);106srcloc107}108}109}110111/// Retrieve a `UserExternalNameRef` for the given name, or add a new one.112///113/// This method internally deduplicates same `UserExternalName` so they map to the same114/// reference.115pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {116if let Some(reff) = self.user_ext_name_to_ref.get(&name) {117*reff118} else {119let reff = self.user_named_funcs.push(name.clone());120self.user_ext_name_to_ref.insert(name, reff);121reff122}123}124125/// Resets an already existing user function name to a new value.126pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {127if let Some(prev_name) = self.user_named_funcs.get_mut(index) {128self.user_ext_name_to_ref.remove(prev_name);129*prev_name = name.clone();130self.user_ext_name_to_ref.insert(name, index);131}132}133134/// Returns the internal mapping of `UserExternalNameRef` to `UserExternalName`.135pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {136&self.user_named_funcs137}138139fn clear(&mut self) {140self.base_srcloc = None;141self.user_named_funcs.clear();142self.user_ext_name_to_ref.clear();143}144}145146/// Function fields needed when compiling a function.147///148/// Additionally, these fields can be the same for two functions that would be compiled the same149/// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.150#[derive(Clone, PartialEq, Hash)]151#[cfg_attr(152feature = "enable-serde",153derive(serde_derive::Serialize, serde_derive::Deserialize)154)]155pub struct FunctionStencil {156/// A version marker used to ensure that serialized clif ir is never deserialized with a157/// different version of Cranelift.158// Note: This must be the first field to ensure that Serde will deserialize it before159// attempting to deserialize other fields that are potentially changed between versions.160pub version_marker: VersionMarker,161162/// Signature of this function.163pub signature: Signature,164165/// Sized stack slots allocated in this function.166pub sized_stack_slots: StackSlots,167168/// Dynamic stack slots allocated in this function.169pub dynamic_stack_slots: DynamicStackSlots,170171/// Global values referenced.172pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,173174/// Global value proof-carrying-code facts.175pub global_value_facts: SecondaryMap<ir::GlobalValue, Option<Fact>>,176177/// Memory types for proof-carrying code.178pub memory_types: PrimaryMap<ir::MemoryType, ir::MemoryTypeData>,179180/// Data flow graph containing the primary definition of all instructions, blocks and values.181pub dfg: DataFlowGraph,182183/// Layout of blocks and instructions in the function body.184pub layout: Layout,185186/// Source locations.187///188/// Track the original source location for each instruction. The source locations are not189/// interpreted by Cranelift, only preserved.190pub srclocs: SourceLocs,191192/// An optional global value which represents an expression evaluating to193/// the stack limit for this function. This `GlobalValue` will be194/// interpreted in the prologue, if necessary, to insert a stack check to195/// ensure that a trap happens if the stack pointer goes below the196/// threshold specified here.197pub stack_limit: Option<ir::GlobalValue>,198}199200impl FunctionStencil {201fn clear(&mut self) {202self.signature.clear(CallConv::Fast);203self.sized_stack_slots.clear();204self.dynamic_stack_slots.clear();205self.global_values.clear();206self.global_value_facts.clear();207self.memory_types.clear();208self.dfg.clear();209self.layout.clear();210self.srclocs.clear();211self.stack_limit = None;212}213214/// Creates a jump table in the function, to be used by `br_table` instructions.215pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {216self.dfg.jump_tables.push(data)217}218219/// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`220/// and `stack_addr` instructions.221pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {222self.sized_stack_slots.push(data)223}224225/// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,226/// `dynamic_stack_store` and `dynamic_stack_addr` instructions.227pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {228self.dynamic_stack_slots.push(data)229}230231/// Adds a signature which can later be used to declare an external function import.232pub fn import_signature(&mut self, signature: Signature) -> SigRef {233self.dfg.signatures.push(signature)234}235236/// Declares a global value accessible to the function.237pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {238self.global_values.push(data)239}240241/// Declares a memory type for use by the function.242pub fn create_memory_type(&mut self, data: MemoryTypeData) -> MemoryType {243self.memory_types.push(data)244}245246/// Find the global dyn_scale value associated with given DynamicType.247pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {248self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale249}250251/// Find the global dyn_scale for the given stack slot.252pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {253let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;254self.get_dyn_scale(dyn_ty)255}256257/// Get a concrete `Type` from a user defined `DynamicType`.258pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {259self.dfg260.dynamic_types261.get(ty)262.unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))263.concrete()264}265266/// Find a presumed unique special-purpose function parameter value.267///268/// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.269pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {270let entry = self.layout.entry_block().expect("Function is empty");271self.signature272.special_param_index(purpose)273.map(|i| self.dfg.block_params(entry)[i])274}275276/// Starts collection of debug information.277pub fn collect_debug_info(&mut self) {278self.dfg.collect_debug_info();279}280281/// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.282/// Does nothing if called with a non-jump or non-branch instruction.283pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {284for dest in self.dfg.insts[inst]285.branch_destination_mut(&mut self.dfg.jump_tables, &mut self.dfg.exception_tables)286{287if dest.block(&self.dfg.value_lists) == old_dest {288dest.set_block(new_dest, &mut self.dfg.value_lists)289}290}291}292293/// Checks that the specified block can be encoded as a basic block.294///295/// On error, returns the first invalid instruction and an error message.296pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {297let dfg = &self.dfg;298let inst_iter = self.layout.block_insts(block);299300// Ignore all instructions prior to the first branch.301let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());302303if let Some(_branch) = inst_iter.next() {304if let Some(next) = inst_iter.next() {305return Err((next, "post-terminator instruction"));306}307}308309Ok(())310}311312/// Returns an iterator over the blocks succeeding the given block.313pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {314self.layout.last_inst(block).into_iter().flat_map(|inst| {315self.dfg.insts[inst]316.branch_destination(&self.dfg.jump_tables, &self.dfg.exception_tables)317.iter()318.map(|block| block.block(&self.dfg.value_lists))319})320}321322/// Replace the `dst` instruction's data with the `src` instruction's data323/// and then remove `src`.324///325/// `src` and its result values should not be used at all, as any uses would326/// be left dangling after calling this method.327///328/// `src` and `dst` must have the same number of resulting values, and329/// `src`'s i^th value must have the same type as `dst`'s i^th value.330pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {331debug_assert_eq!(332self.dfg.inst_results(dst).len(),333self.dfg.inst_results(src).len()334);335debug_assert!(336self.dfg337.inst_results(dst)338.iter()339.zip(self.dfg.inst_results(src))340.all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))341);342343self.dfg.insts[dst] = self.dfg.insts[src];344self.layout.remove_inst(src);345}346347/// Size occupied by all stack slots associated with this function.348///349/// Does not include any padding necessary due to offsets350pub fn fixed_stack_size(&self) -> u32 {351self.sized_stack_slots.values().map(|ss| ss.size).sum()352}353354/// Returns the list of relative source locations for this function.355pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {356&self.srclocs357}358}359360/// Functions can be cloned, but it is not a very fast operation.361/// The clone will have all the same entity numbers as the original.362#[derive(Clone, PartialEq)]363#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]364pub struct Function {365/// Name of this function.366///367/// Mostly used by `.clif` files, only there for debugging / naming purposes.368pub name: UserFuncName,369370/// All the fields required for compiling a function, independently of details irrelevant to371/// compilation and that are stored in the `FunctionParameters` `params` field instead.372pub stencil: FunctionStencil,373374/// All the parameters that can be applied onto the function stencil, that is, that don't375/// matter when caching compilation artifacts.376pub params: FunctionParameters,377}378379impl core::ops::Deref for Function {380type Target = FunctionStencil;381382fn deref(&self) -> &Self::Target {383&self.stencil384}385}386387impl core::ops::DerefMut for Function {388fn deref_mut(&mut self) -> &mut Self::Target {389&mut self.stencil390}391}392393impl Function {394/// Create a function with the given name and signature.395pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {396Self {397name,398stencil: FunctionStencil {399version_marker: VersionMarker,400signature: sig,401sized_stack_slots: StackSlots::new(),402dynamic_stack_slots: DynamicStackSlots::new(),403global_values: PrimaryMap::new(),404global_value_facts: SecondaryMap::new(),405memory_types: PrimaryMap::new(),406dfg: DataFlowGraph::new(),407layout: Layout::new(),408srclocs: SecondaryMap::new(),409stack_limit: None,410},411params: FunctionParameters::new(),412}413}414415/// Clear all data structures in this function.416pub fn clear(&mut self) {417self.stencil.clear();418self.params.clear();419self.name = UserFuncName::default();420}421422/// Create a new empty, anonymous function with a Fast calling convention.423pub fn new() -> Self {424Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))425}426427/// Return an object that can display this function with correct ISA-specific annotations.428pub fn display(&self) -> DisplayFunction<'_> {429DisplayFunction(self)430}431432/// Return an object that can display this function's name and signature.433pub fn display_spec(&self) -> DisplayFunctionSpec<'_> {434DisplayFunctionSpec(self)435}436437/// Sets an absolute source location for the given instruction.438///439/// If no base source location has been set yet, records it at the same time.440pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {441let base = self.params.ensure_base_srcloc(srcloc);442self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);443}444445/// Returns an absolute source location for the given instruction.446pub fn srcloc(&self, inst: Inst) -> SourceLoc {447let base = self.params.base_srcloc();448self.stencil.srclocs[inst].expand(base)449}450451/// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.452pub fn declare_imported_user_function(453&mut self,454name: UserExternalName,455) -> UserExternalNameRef {456self.params.ensure_user_func_name(name)457}458459/// Declare an external function import.460pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {461self.stencil.dfg.ext_funcs.push(data)462}463}464465/// Wrapper type capable of displaying a `Function`.466pub struct DisplayFunction<'a>(&'a Function);467468impl<'a> fmt::Display for DisplayFunction<'a> {469fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {470write_function(fmt, self.0)471}472}473474impl fmt::Display for Function {475fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {476write_function(fmt, self)477}478}479480impl fmt::Debug for Function {481fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {482write_function(fmt, self)483}484}485486/// Wrapper type capable of displaying a 'Function's name and signature.487pub struct DisplayFunctionSpec<'a>(&'a Function);488489impl<'a> fmt::Display for DisplayFunctionSpec<'a> {490fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {491write_function_spec(fmt, self.0)492}493}494495impl<'a> fmt::Debug for DisplayFunctionSpec<'a> {496fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {497write_function_spec(fmt, self.0)498}499}500501502