Path: blob/main/cranelift/codegen/src/isa/mod.rs
1693 views
//! Instruction Set Architectures.1//!2//! The `isa` module provides a `TargetIsa` trait which provides the behavior specialization needed3//! by the ISA-independent code generator. The sub-modules of this module provide definitions for4//! the instruction sets that Cranelift can target. Each sub-module has it's own implementation of5//! `TargetIsa`.6//!7//! # Constructing a `TargetIsa` instance8//!9//! The target ISA is built from the following information:10//!11//! - The name of the target ISA as a string. Cranelift is a cross-compiler, so the ISA to target12//! can be selected dynamically. Individual ISAs can be left out when Cranelift is compiled, so a13//! string is used to identify the proper sub-module.14//! - Values for settings that apply to all ISAs. This is represented by a `settings::Flags`15//! instance.16//! - Values for ISA-specific settings.17//!18//! The `isa::lookup()` function is the main entry point which returns an `isa::Builder`19//! appropriate for the requested ISA:20//!21//! ```22//! # #[macro_use] extern crate target_lexicon;23//! use cranelift_codegen::isa;24//! use cranelift_codegen::settings::{self, Configurable};25//! use std::str::FromStr;26//! use target_lexicon::Triple;27//!28//! let shared_builder = settings::builder();29//! let shared_flags = settings::Flags::new(shared_builder);30//!31//! match isa::lookup(triple!("x86_64")) {32//! Err(_) => {33//! // The x86_64 target ISA is not available.34//! }35//! Ok(mut isa_builder) => {36//! isa_builder.set("use_popcnt", "on");37//! let isa = isa_builder.finish(shared_flags);38//! }39//! }40//! ```41//!42//! The configured target ISA trait object is a `Box<TargetIsa>` which can be used for multiple43//! concurrent function compilations.4445use crate::dominator_tree::DominatorTree;46pub use crate::isa::call_conv::CallConv;4748use crate::CodegenResult;49use crate::ir::{self, Function, Type};50#[cfg(feature = "unwind")]51use crate::isa::unwind::{UnwindInfoKind, systemv::RegisterMappingError};52use crate::machinst::{CompiledCode, CompiledCodeStencil, TextSectionBuilder};53use crate::settings;54use crate::settings::Configurable;55use crate::settings::SetResult;56use crate::{Reg, flowgraph};57use alloc::{boxed::Box, sync::Arc, vec::Vec};58use core::fmt;59use core::fmt::{Debug, Formatter};60use cranelift_control::ControlPlane;61use std::string::String;62use target_lexicon::{Architecture, PointerWidth, Triple, triple};6364// This module is made public here for benchmarking purposes. No guarantees are65// made regarding API stability.66#[cfg(feature = "x86")]67pub mod x64;6869#[cfg(feature = "arm64")]70pub mod aarch64;7172#[cfg(feature = "riscv64")]73pub mod riscv64;7475#[cfg(feature = "s390x")]76mod s390x;7778#[cfg(feature = "pulley")]79mod pulley32;80#[cfg(feature = "pulley")]81mod pulley64;82#[cfg(feature = "pulley")]83mod pulley_shared;8485pub mod unwind;8687mod call_conv;88mod winch;8990/// Returns a builder that can create a corresponding `TargetIsa`91/// or `Err(LookupError::SupportDisabled)` if not enabled.92macro_rules! isa_builder {93($name: ident, $cfg_terms: tt, $triple: ident) => {{94#[cfg $cfg_terms]95{96Ok($name::isa_builder($triple))97}98#[cfg(not $cfg_terms)]99{100Err(LookupError::SupportDisabled)101}102}};103}104105/// Look for an ISA for the given `triple`.106/// Return a builder that can create a corresponding `TargetIsa`.107pub fn lookup(triple: Triple) -> Result<Builder, LookupError> {108match triple.architecture {109Architecture::X86_64 => {110isa_builder!(x64, (feature = "x86"), triple)111}112Architecture::Aarch64 { .. } => isa_builder!(aarch64, (feature = "arm64"), triple),113Architecture::S390x { .. } => isa_builder!(s390x, (feature = "s390x"), triple),114Architecture::Riscv64 { .. } => isa_builder!(riscv64, (feature = "riscv64"), triple),115Architecture::Pulley32 | Architecture::Pulley32be => {116isa_builder!(pulley32, (feature = "pulley"), triple)117}118Architecture::Pulley64 | Architecture::Pulley64be => {119isa_builder!(pulley64, (feature = "pulley"), triple)120}121_ => Err(LookupError::Unsupported),122}123}124125/// The string names of all the supported, but possibly not enabled, architectures. The elements of126/// this slice are suitable to be passed to the [lookup_by_name] function to obtain the default127/// configuration for that architecture.128pub const ALL_ARCHITECTURES: &[&str] = &["x86_64", "aarch64", "s390x", "riscv64"];129130/// Look for a supported ISA with the given `name`.131/// Return a builder that can create a corresponding `TargetIsa`.132pub fn lookup_by_name(name: &str) -> Result<Builder, LookupError> {133lookup(triple!(name))134}135136/// Describes reason for target lookup failure137#[derive(PartialEq, Eq, Copy, Clone, Debug)]138pub enum LookupError {139/// Support for this target was disabled in the current build.140SupportDisabled,141142/// Support for this target has not yet been implemented.143Unsupported,144}145146// This is manually implementing Error and Display instead of using thiserror to reduce the amount147// of dependencies used by Cranelift.148impl std::error::Error for LookupError {}149150impl fmt::Display for LookupError {151fn fmt(&self, f: &mut Formatter) -> fmt::Result {152match self {153LookupError::SupportDisabled => write!(f, "Support for this target is disabled"),154LookupError::Unsupported => {155write!(f, "Support for this target has not been implemented yet")156}157}158}159}160161/// The type of a polymorphic TargetISA object which is 'static.162pub type OwnedTargetIsa = Arc<dyn TargetIsa>;163164/// Type alias of `IsaBuilder` used for building Cranelift's ISAs.165pub type Builder = IsaBuilder<CodegenResult<OwnedTargetIsa>>;166167/// Builder for a `TargetIsa`.168/// Modify the ISA-specific settings before creating the `TargetIsa` trait object with `finish`.169#[derive(Clone)]170pub struct IsaBuilder<T> {171triple: Triple,172setup: settings::Builder,173constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,174}175176impl<T> IsaBuilder<T> {177/// Creates a new ISA-builder from its components, namely the `triple` for178/// the ISA, the ISA-specific settings builder, and a final constructor179/// function to generate the ISA from its components.180pub fn new(181triple: Triple,182setup: settings::Builder,183constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,184) -> Self {185IsaBuilder {186triple,187setup,188constructor,189}190}191192/// Creates a new [Builder] from a [TargetIsa], copying all flags in the193/// process.194pub fn from_target_isa(target_isa: &dyn TargetIsa) -> Builder {195// We should always be able to find the builder for the TargetISA, since presumably we196// also generated the previous TargetISA at some point197let triple = target_isa.triple().clone();198let mut builder = self::lookup(triple).expect("Could not find triple for target ISA");199200// Copy ISA Flags201for flag in target_isa.isa_flags() {202builder.set(&flag.name, &flag.value_string()).unwrap();203}204205builder206}207208/// Gets the triple for the builder.209pub fn triple(&self) -> &Triple {210&self.triple211}212213/// Iterates the available settings in the builder.214pub fn iter(&self) -> impl Iterator<Item = settings::Setting> + use<T> {215self.setup.iter()216}217218/// Combine the ISA-specific settings with the provided219/// ISA-independent settings and allocate a fully configured220/// `TargetIsa` trait object. May return an error if some of the221/// flags are inconsistent or incompatible: for example, some222/// platform-independent features, like general SIMD support, may223/// need certain ISA extensions to be enabled.224pub fn finish(&self, shared_flags: settings::Flags) -> T {225(self.constructor)(self.triple.clone(), shared_flags, &self.setup)226}227}228229impl<T> settings::Configurable for IsaBuilder<T> {230fn set(&mut self, name: &str, value: &str) -> SetResult<()> {231self.setup.set(name, value)232}233234fn enable(&mut self, name: &str) -> SetResult<()> {235self.setup.enable(name)236}237}238239/// After determining that an instruction doesn't have an encoding, how should we proceed to240/// legalize it?241///242/// The `Encodings` iterator returns a legalization function to call.243pub type Legalize =244fn(ir::Inst, &mut ir::Function, &mut flowgraph::ControlFlowGraph, &dyn TargetIsa) -> bool;245246/// This struct provides information that a frontend may need to know about a target to247/// produce Cranelift IR for the target.248#[derive(Clone, Copy, Hash)]249pub struct TargetFrontendConfig {250/// The default calling convention of the target.251pub default_call_conv: CallConv,252253/// The pointer width of the target.254pub pointer_width: PointerWidth,255256/// The log2 of the target's page size and alignment.257///258/// Note that this may be an upper-bound that is larger than necessary for259/// some platforms since it may depend on runtime configuration.260pub page_size_align_log2: u8,261}262263impl TargetFrontendConfig {264/// Get the pointer type of this target.265pub fn pointer_type(self) -> ir::Type {266ir::Type::int(self.pointer_bits() as u16).unwrap()267}268269/// Get the width of pointers on this target, in units of bits.270pub fn pointer_bits(self) -> u8 {271self.pointer_width.bits()272}273274/// Get the width of pointers on this target, in units of bytes.275pub fn pointer_bytes(self) -> u8 {276self.pointer_width.bytes()277}278}279280/// Methods that are specialized to a target ISA.281///282/// Implies a Display trait that shows the shared flags, as well as any ISA-specific flags.283pub trait TargetIsa: fmt::Display + Send + Sync {284/// Get the name of this ISA.285fn name(&self) -> &'static str;286287/// Get the target triple that was used to make this trait object.288fn triple(&self) -> &Triple;289290/// Get the ISA-independent flags that were used to make this trait object.291fn flags(&self) -> &settings::Flags;292293/// Get the ISA-dependent flag values that were used to make this trait object.294fn isa_flags(&self) -> Vec<settings::Value>;295296/// Get the ISA-dependent flag values as raw bytes for hashing.297fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>;298299/// Get a flag indicating whether branch protection is enabled.300fn is_branch_protection_enabled(&self) -> bool {301false302}303304/// Get the ISA-dependent maximum vector register size, in bytes.305fn dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32;306307/// Compile the given function.308fn compile_function(309&self,310func: &Function,311domtree: &DominatorTree,312want_disasm: bool,313ctrl_plane: &mut ControlPlane,314) -> CodegenResult<CompiledCodeStencil>;315316#[cfg(feature = "unwind")]317/// Map a regalloc::Reg to its corresponding DWARF register.318fn map_regalloc_reg_to_dwarf(319&self,320_: crate::machinst::Reg,321) -> Result<u16, RegisterMappingError> {322Err(RegisterMappingError::UnsupportedArchitecture)323}324325/// Creates unwind information for the function.326///327/// Returns `None` if there is no unwind information for the function.328#[cfg(feature = "unwind")]329fn emit_unwind_info(330&self,331result: &CompiledCode,332kind: UnwindInfoKind,333) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>;334335/// Creates a new System V Common Information Entry for the ISA.336///337/// Returns `None` if the ISA does not support System V unwind information.338#[cfg(feature = "unwind")]339fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {340// By default, an ISA cannot create a System V CIE341None342}343344/// Returns an object that can be used to build the text section of an345/// executable.346///347/// This object will internally attempt to handle as many relocations as348/// possible using relative calls/jumps/etc between functions.349///350/// The `num_labeled_funcs` argument here is the number of functions which351/// will be "labeled" or might have calls between them, typically the number352/// of defined functions in the object file.353fn text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>;354355/// Returns the minimum function alignment and the preferred function356/// alignment, for performance, required by this ISA.357fn function_alignment(&self) -> FunctionAlignment;358359/// The log2 of the target's page size and alignment.360///361/// Note that this may be an upper-bound that is larger than necessary for362/// some platforms since it may depend on runtime configuration.363fn page_size_align_log2(&self) -> u8;364365/// Create a polymorphic TargetIsa from this specific implementation.366fn wrapped(self) -> OwnedTargetIsa367where368Self: Sized + 'static,369{370Arc::new(self)371}372373/// Generate a `Capstone` context for disassembling bytecode for this architecture.374#[cfg(feature = "disas")]375fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {376Err(capstone::Error::UnsupportedArch)377}378379/// Return the string representation of "reg" accessed as "size" bytes.380/// The returned string will match the usual disassemly view of "reg".381fn pretty_print_reg(&self, reg: Reg, size: u8) -> String;382383/// Returns whether this ISA has a native fused-multiply-and-add instruction384/// for floats.385///386/// Currently this only returns false on x86 when some native features are387/// not detected.388fn has_native_fma(&self) -> bool;389390/// Returns whether this ISA has instructions for `ceil`, `floor`, etc.391fn has_round(&self) -> bool;392393/// Returns whether the CLIF `x86_blendv` instruction is implemented for394/// this ISA for the specified type.395fn has_x86_blendv_lowering(&self, ty: Type) -> bool;396397/// Returns whether the CLIF `x86_pshufb` instruction is implemented for398/// this ISA.399fn has_x86_pshufb_lowering(&self) -> bool;400401/// Returns whether the CLIF `x86_pmulhrsw` instruction is implemented for402/// this ISA.403fn has_x86_pmulhrsw_lowering(&self) -> bool;404405/// Returns whether the CLIF `x86_pmaddubsw` instruction is implemented for406/// this ISA.407fn has_x86_pmaddubsw_lowering(&self) -> bool;408409/// Returns the mode of extension used for integer arguments smaller than410/// the pointer width in function signatures.411///412/// Some platform ABIs require that smaller-than-pointer-width values are413/// either zero or sign-extended to the full register width. This value is414/// propagated to the `AbiParam` value created for signatures. Note that not415/// all ABIs for all platforms require extension of any form, so this is416/// generally only necessary for the `default_call_conv`.417fn default_argument_extension(&self) -> ir::ArgumentExtension;418}419420/// A wrapper around the ISA-dependent flags types which only implements `Hash`.421#[derive(Hash)]422pub struct IsaFlagsHashKey<'a>(&'a [u8]);423424/// Function alignment specifications as required by an ISA, returned by425/// [`TargetIsa::function_alignment`].426#[derive(Copy, Clone)]427pub struct FunctionAlignment {428/// The minimum alignment required by an ISA, where all functions must be429/// aligned to at least this amount.430pub minimum: u32,431/// A "preferred" alignment which should be used for more432/// performance-sensitive situations. This can involve cache-line-aligning433/// for example to get more of a small function into fewer cache lines.434pub preferred: u32,435}436437/// Methods implemented for free for target ISA!438impl<'a> dyn TargetIsa + 'a {439/// Get the default calling convention of this target.440pub fn default_call_conv(&self) -> CallConv {441CallConv::triple_default(self.triple())442}443444/// Get the endianness of this ISA.445pub fn endianness(&self) -> ir::Endianness {446match self.triple().endianness().unwrap() {447target_lexicon::Endianness::Little => ir::Endianness::Little,448target_lexicon::Endianness::Big => ir::Endianness::Big,449}450}451452/// Returns the minimum symbol alignment for this ISA.453pub fn symbol_alignment(&self) -> u64 {454match self.triple().architecture {455// All symbols need to be aligned to at least 2 on s390x.456Architecture::S390x => 2,457_ => 1,458}459}460461/// Get the pointer type of this ISA.462pub fn pointer_type(&self) -> ir::Type {463ir::Type::int(self.pointer_bits() as u16).unwrap()464}465466/// Get the width of pointers on this ISA.467pub(crate) fn pointer_width(&self) -> PointerWidth {468self.triple().pointer_width().unwrap()469}470471/// Get the width of pointers on this ISA, in units of bits.472pub fn pointer_bits(&self) -> u8 {473self.pointer_width().bits()474}475476/// Get the width of pointers on this ISA, in units of bytes.477pub fn pointer_bytes(&self) -> u8 {478self.pointer_width().bytes()479}480481/// Get the information needed by frontends producing Cranelift IR.482pub fn frontend_config(&self) -> TargetFrontendConfig {483TargetFrontendConfig {484default_call_conv: self.default_call_conv(),485pointer_width: self.pointer_width(),486page_size_align_log2: self.page_size_align_log2(),487}488}489}490491impl Debug for &dyn TargetIsa {492fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {493write!(494f,495"TargetIsa {{ triple: {:?}, pointer_width: {:?}}}",496self.triple(),497self.pointer_width()498)499}500}501502503