Path: blob/main/cranelift/codegen/src/ir/function.rs
3069 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::DebugTags;8use crate::ir::{9self, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, DynamicStackSlots,10DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, JumpTable,11JumpTableData, Layout, MemoryType, MemoryTypeData, SigRef, Signature, SourceLocs, StackSlot,12StackSlotData, StackSlots, Type, pcc::Fact,13};14use crate::isa::CallConv;15use crate::write::{write_function, write_function_spec};16#[cfg(feature = "enable-serde")]17use alloc::string::String;18use core::fmt;1920#[cfg(feature = "enable-serde")]21use serde::de::{Deserializer, Error};22#[cfg(feature = "enable-serde")]23use serde::ser::Serializer;24#[cfg(feature = "enable-serde")]25use serde::{Deserialize, Serialize};2627use super::entities::UserExternalNameRef;28use super::extname::UserFuncName;29use super::{RelSourceLoc, SourceLoc, UserExternalName};3031/// A version marker used to ensure that serialized clif ir is never deserialized with a32/// different version of Cranelift.33#[derive(Default, Copy, Clone, Debug, PartialEq, Hash)]34pub struct VersionMarker;3536#[cfg(feature = "enable-serde")]37impl Serialize for VersionMarker {38fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>39where40S: Serializer,41{42crate::VERSION.serialize(serializer)43}44}4546#[cfg(feature = "enable-serde")]47impl<'de> Deserialize<'de> for VersionMarker {48fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>49where50D: Deserializer<'de>,51{52let version = String::deserialize(deserializer)?;53if version != crate::VERSION {54return Err(D::Error::custom(&format!(55"Expected a clif ir function for version {}, found one for version {}",56crate::VERSION,57version,58)));59}60Ok(VersionMarker)61}62}6364/// Function parameters used when creating this function, and that will become applied after65/// compilation to materialize the final `CompiledCode`.66#[derive(Clone, PartialEq)]67#[cfg_attr(68feature = "enable-serde",69derive(serde_derive::Serialize, serde_derive::Deserialize)70)]71pub struct FunctionParameters {72/// The first `SourceLoc` appearing in the function, serving as a base for every relative73/// source loc in the function.74base_srcloc: Option<SourceLoc>,7576/// External user-defined function references.77user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,7879/// Inverted mapping of `user_named_funcs`, to deduplicate internally.80user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,81}8283impl FunctionParameters {84/// Creates a new `FunctionParameters` with the given name.85pub fn new() -> Self {86Self {87base_srcloc: None,88user_named_funcs: Default::default(),89user_ext_name_to_ref: Default::default(),90}91}9293/// Returns the base `SourceLoc`.94///95/// If it was never explicitly set with `ensure_base_srcloc`, will return an invalid96/// `SourceLoc`.97pub fn base_srcloc(&self) -> SourceLoc {98self.base_srcloc.unwrap_or_default()99}100101/// Sets the base `SourceLoc`, if not set yet, and returns the base value.102pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {103match self.base_srcloc {104Some(val) => val,105None => {106self.base_srcloc = Some(srcloc);107srcloc108}109}110}111112/// Retrieve a `UserExternalNameRef` for the given name, or add a new one.113///114/// This method internally deduplicates same `UserExternalName` so they map to the same115/// reference.116pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {117if let Some(reff) = self.user_ext_name_to_ref.get(&name) {118*reff119} else {120let reff = self.user_named_funcs.push(name.clone());121self.user_ext_name_to_ref.insert(name, reff);122reff123}124}125126/// Resets an already existing user function name to a new value.127pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {128if let Some(prev_name) = self.user_named_funcs.get_mut(index) {129self.user_ext_name_to_ref.remove(prev_name);130*prev_name = name.clone();131self.user_ext_name_to_ref.insert(name, index);132}133}134135/// Returns the internal mapping of `UserExternalNameRef` to `UserExternalName`.136pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {137&self.user_named_funcs138}139140fn clear(&mut self) {141self.base_srcloc = None;142self.user_named_funcs.clear();143self.user_ext_name_to_ref.clear();144}145}146147/// Function fields needed when compiling a function.148///149/// Additionally, these fields can be the same for two functions that would be compiled the same150/// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.151#[derive(Clone, PartialEq, Hash)]152#[cfg_attr(153feature = "enable-serde",154derive(serde_derive::Serialize, serde_derive::Deserialize)155)]156pub struct FunctionStencil {157/// A version marker used to ensure that serialized clif ir is never deserialized with a158/// different version of Cranelift.159// Note: This must be the first field to ensure that Serde will deserialize it before160// attempting to deserialize other fields that are potentially changed between versions.161pub version_marker: VersionMarker,162163/// Signature of this function.164pub signature: Signature,165166/// Sized stack slots allocated in this function.167pub sized_stack_slots: StackSlots,168169/// Dynamic stack slots allocated in this function.170pub dynamic_stack_slots: DynamicStackSlots,171172/// Global values referenced.173pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,174175/// Global value proof-carrying-code facts.176pub global_value_facts: SecondaryMap<ir::GlobalValue, Option<Fact>>,177178/// Memory types for proof-carrying code.179pub memory_types: PrimaryMap<ir::MemoryType, ir::MemoryTypeData>,180181/// Data flow graph containing the primary definition of all instructions, blocks and values.182pub dfg: DataFlowGraph,183184/// Layout of blocks and instructions in the function body.185pub layout: Layout,186187/// Source locations.188///189/// Track the original source location for each instruction. The source locations are not190/// interpreted by Cranelift, only preserved.191pub srclocs: SourceLocs,192193/// Opaque debug-info tags on sequence-point and call194/// instructions.195///196/// These tags are not interpreted by Cranelift, and are passed197/// through to compilation-result metadata. The only semantic198/// structure that Cranelift imposes is that when inlining, it199/// prepends the callsite call instruction's tags to the tags on200/// inlined instructions.201///202/// In order to ensure clarity around guaranteed compiler203/// behavior, tags are only permitted on instructions whose204/// presence and sequence will remain the same in the compiled205/// output: namely, `sequence_point` instructions and ordinary206/// call instructions.207pub debug_tags: DebugTags,208209/// An optional global value which represents an expression evaluating to210/// the stack limit for this function. This `GlobalValue` will be211/// interpreted in the prologue, if necessary, to insert a stack check to212/// ensure that a trap happens if the stack pointer goes below the213/// threshold specified here.214pub stack_limit: Option<ir::GlobalValue>,215}216217impl FunctionStencil {218fn clear(&mut self) {219self.signature.clear(CallConv::Fast);220self.sized_stack_slots.clear();221self.dynamic_stack_slots.clear();222self.global_values.clear();223self.global_value_facts.clear();224self.memory_types.clear();225self.dfg.clear();226self.layout.clear();227self.srclocs.clear();228self.debug_tags.clear();229self.stack_limit = None;230}231232/// Creates a jump table in the function, to be used by `br_table` instructions.233pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {234self.dfg.jump_tables.push(data)235}236237/// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`238/// and `stack_addr` instructions.239pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {240self.sized_stack_slots.push(data)241}242243/// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,244/// `dynamic_stack_store` and `dynamic_stack_addr` instructions.245pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {246self.dynamic_stack_slots.push(data)247}248249/// Adds a signature which can later be used to declare an external function import.250pub fn import_signature(&mut self, signature: Signature) -> SigRef {251self.dfg.signatures.push(signature)252}253254/// Declares a global value accessible to the function.255pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {256self.global_values.push(data)257}258259/// Declares a memory type for use by the function.260pub fn create_memory_type(&mut self, data: MemoryTypeData) -> MemoryType {261self.memory_types.push(data)262}263264/// Find the global dyn_scale value associated with given DynamicType.265pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {266self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale267}268269/// Find the global dyn_scale for the given stack slot.270pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {271let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;272self.get_dyn_scale(dyn_ty)273}274275/// Get a concrete `Type` from a user defined `DynamicType`.276pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {277self.dfg278.dynamic_types279.get(ty)280.unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))281.concrete()282}283284/// Find a presumed unique special-purpose function parameter value.285///286/// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.287pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {288let entry = self.layout.entry_block().expect("Function is empty");289self.signature290.special_param_index(purpose)291.map(|i| self.dfg.block_params(entry)[i])292}293294/// Starts collection of debug information.295pub fn collect_debug_info(&mut self) {296self.dfg.collect_debug_info();297}298299/// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.300/// Does nothing if called with a non-jump or non-branch instruction.301pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {302for dest in self.dfg.insts[inst]303.branch_destination_mut(&mut self.dfg.jump_tables, &mut self.dfg.exception_tables)304{305if dest.block(&self.dfg.value_lists) == old_dest {306dest.set_block(new_dest, &mut self.dfg.value_lists)307}308}309}310311/// Checks that the specified block can be encoded as a basic block.312///313/// On error, returns the first invalid instruction and an error message.314pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {315let dfg = &self.dfg;316let inst_iter = self.layout.block_insts(block);317318// Ignore all instructions prior to the first branch.319let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());320321if let Some(_branch) = inst_iter.next() {322if let Some(next) = inst_iter.next() {323return Err((next, "post-terminator instruction"));324}325}326327Ok(())328}329330/// Returns an iterator over the blocks succeeding the given block.331pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {332self.layout.last_inst(block).into_iter().flat_map(|inst| {333self.dfg.insts[inst]334.branch_destination(&self.dfg.jump_tables, &self.dfg.exception_tables)335.iter()336.map(|block| block.block(&self.dfg.value_lists))337})338}339340/// Replace the `dst` instruction's data with the `src` instruction's data341/// and then remove `src`.342///343/// `src` and its result values should not be used at all, as any uses would344/// be left dangling after calling this method.345///346/// `src` and `dst` must have the same number of resulting values, and347/// `src`'s i^th value must have the same type as `dst`'s i^th value.348pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {349debug_assert_eq!(350self.dfg.inst_results(dst).len(),351self.dfg.inst_results(src).len()352);353debug_assert!(354self.dfg355.inst_results(dst)356.iter()357.zip(self.dfg.inst_results(src))358.all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))359);360361self.dfg.insts[dst] = self.dfg.insts[src];362self.layout.remove_inst(src);363}364365/// Size occupied by all stack slots associated with this function.366///367/// Does not include any padding necessary due to offsets368pub fn fixed_stack_size(&self) -> u32 {369self.sized_stack_slots.values().map(|ss| ss.size).sum()370}371372/// Returns the list of relative source locations for this function.373pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {374&self.srclocs375}376}377378/// Functions can be cloned, but it is not a very fast operation.379/// The clone will have all the same entity numbers as the original.380#[derive(Clone, PartialEq)]381#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]382pub struct Function {383/// Name of this function.384///385/// Mostly used by `.clif` files, only there for debugging / naming purposes.386pub name: UserFuncName,387388/// All the fields required for compiling a function, independently of details irrelevant to389/// compilation and that are stored in the `FunctionParameters` `params` field instead.390pub stencil: FunctionStencil,391392/// All the parameters that can be applied onto the function stencil, that is, that don't393/// matter when caching compilation artifacts.394pub params: FunctionParameters,395}396397impl core::ops::Deref for Function {398type Target = FunctionStencil;399400fn deref(&self) -> &Self::Target {401&self.stencil402}403}404405impl core::ops::DerefMut for Function {406fn deref_mut(&mut self) -> &mut Self::Target {407&mut self.stencil408}409}410411impl Function {412/// Create a function with the given name and signature.413pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {414Self {415name,416stencil: FunctionStencil {417version_marker: VersionMarker,418signature: sig,419sized_stack_slots: StackSlots::new(),420dynamic_stack_slots: DynamicStackSlots::new(),421global_values: PrimaryMap::new(),422global_value_facts: SecondaryMap::new(),423memory_types: PrimaryMap::new(),424dfg: DataFlowGraph::new(),425layout: Layout::new(),426srclocs: SecondaryMap::new(),427stack_limit: None,428debug_tags: DebugTags::default(),429},430params: FunctionParameters::new(),431}432}433434/// Clear all data structures in this function.435pub fn clear(&mut self) {436self.stencil.clear();437self.params.clear();438self.name = UserFuncName::default();439}440441/// Create a new empty, anonymous function with a Fast calling convention.442pub fn new() -> Self {443Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))444}445446/// Return an object that can display this function with correct ISA-specific annotations.447pub fn display(&self) -> DisplayFunction<'_> {448DisplayFunction(self)449}450451/// Return an object that can display this function's name and signature.452pub fn display_spec(&self) -> DisplayFunctionSpec<'_> {453DisplayFunctionSpec(self)454}455456/// Sets an absolute source location for the given instruction.457///458/// If no base source location has been set yet, records it at the same time.459pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {460let base = self.params.ensure_base_srcloc(srcloc);461self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);462}463464/// Returns an absolute source location for the given instruction.465pub fn srcloc(&self, inst: Inst) -> SourceLoc {466let base = self.params.base_srcloc();467self.stencil.srclocs[inst].expand(base)468}469470/// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.471pub fn declare_imported_user_function(472&mut self,473name: UserExternalName,474) -> UserExternalNameRef {475self.params.ensure_user_func_name(name)476}477478/// Declare an external function import.479pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {480self.stencil.dfg.ext_funcs.push(data)481}482}483484/// Wrapper type capable of displaying a `Function`.485pub struct DisplayFunction<'a>(&'a Function);486487impl<'a> fmt::Display for DisplayFunction<'a> {488fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {489write_function(fmt, self.0)490}491}492493impl fmt::Display for Function {494fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {495write_function(fmt, self)496}497}498499impl fmt::Debug for Function {500fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {501write_function(fmt, self)502}503}504505/// Wrapper type capable of displaying a 'Function's name and signature.506pub struct DisplayFunctionSpec<'a>(&'a Function);507508impl<'a> fmt::Display for DisplayFunctionSpec<'a> {509fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {510write_function_spec(fmt, self.0)511}512}513514impl<'a> fmt::Debug for DisplayFunctionSpec<'a> {515fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {516write_function_spec(fmt, self.0)517}518}519520521