Skip to main content

cranelift_codegen/isa/
mod.rs

1//! Instruction Set Architectures.
2//!
3//! The `isa` module provides a `TargetIsa` trait which provides the behavior specialization needed
4//! by the ISA-independent code generator. The sub-modules of this module provide definitions for
5//! the instruction sets that Cranelift can target. Each sub-module has it's own implementation of
6//! `TargetIsa`.
7//!
8//! # Constructing a `TargetIsa` instance
9//!
10//! The target ISA is built from the following information:
11//!
12//! - The name of the target ISA as a string. Cranelift is a cross-compiler, so the ISA to target
13//!   can be selected dynamically. Individual ISAs can be left out when Cranelift is compiled, so a
14//!   string is used to identify the proper sub-module.
15//! - Values for settings that apply to all ISAs. This is represented by a `settings::Flags`
16//!   instance.
17//! - Values for ISA-specific settings.
18//!
19//! The `isa::lookup()` function is the main entry point which returns an `isa::Builder`
20//! appropriate for the requested ISA:
21//!
22//! ```
23//! # #[macro_use] extern crate target_lexicon;
24//! use cranelift_codegen::isa;
25//! use cranelift_codegen::settings::{self, Configurable};
26//! use core::str::FromStr;
27//! use target_lexicon::Triple;
28//!
29//! let shared_builder = settings::builder();
30//! let shared_flags = settings::Flags::new(shared_builder);
31//!
32//! match isa::lookup(triple!("x86_64")) {
33//!     Err(_) => {
34//!         // The x86_64 target ISA is not available.
35//!     }
36//!     Ok(mut isa_builder) => {
37//!         isa_builder.set("use_popcnt", "on");
38//!         let isa = isa_builder.finish(shared_flags);
39//!     }
40//! }
41//! ```
42//!
43//! The configured target ISA trait object is a `Box<TargetIsa>` which can be used for multiple
44//! concurrent function compilations.
45
46use crate::dominator_tree::DominatorTree;
47pub use crate::isa::call_conv::CallConv;
48
49use crate::CodegenResult;
50use crate::ir::{self, Function, Type};
51#[cfg(feature = "unwind")]
52use crate::isa::unwind::{UnwindInfoKind, systemv::RegisterMappingError};
53use crate::machinst::{CompiledCodeStencil, TextSectionBuilder};
54use crate::settings;
55use crate::settings::Configurable;
56use crate::settings::SetResult;
57use crate::{Reg, flowgraph};
58use alloc::string::String;
59use alloc::{boxed::Box, sync::Arc, vec::Vec};
60use core::fmt;
61use core::fmt::{Debug, Formatter};
62use cranelift_control::ControlPlane;
63use target_lexicon::{Architecture, PointerWidth, Triple, triple};
64
65// This module is made public here for benchmarking purposes. No guarantees are
66// made regarding API stability.
67#[cfg(feature = "x86")]
68pub mod x64;
69
70#[cfg(feature = "arm64")]
71pub mod aarch64;
72
73#[cfg(feature = "riscv64")]
74pub mod riscv64;
75
76#[cfg(feature = "s390x")]
77mod s390x;
78
79#[cfg(feature = "pulley")]
80mod pulley32;
81#[cfg(feature = "pulley")]
82mod pulley64;
83#[cfg(feature = "pulley")]
84mod pulley_shared;
85
86pub mod unwind;
87
88mod call_conv;
89mod winch;
90
91/// Returns a builder that can create a corresponding `TargetIsa`
92/// or `Err(LookupError::SupportDisabled)` if not enabled.
93macro_rules! isa_builder {
94    ($name: ident, $cfg_terms: tt, $triple: ident) => {{
95        #[cfg $cfg_terms]
96        {
97            Ok($name::isa_builder($triple))
98        }
99        #[cfg(not $cfg_terms)]
100        {
101            Err(LookupError::SupportDisabled)
102        }
103    }};
104}
105
106/// Look for an ISA for the given `triple`.
107/// Return a builder that can create a corresponding `TargetIsa`.
108pub fn lookup(triple: Triple) -> Result<Builder, LookupError> {
109    match triple.architecture {
110        Architecture::X86_64 => {
111            isa_builder!(x64, (feature = "x86"), triple)
112        }
113        Architecture::Aarch64 { .. } => isa_builder!(aarch64, (feature = "arm64"), triple),
114        Architecture::S390x { .. } => isa_builder!(s390x, (feature = "s390x"), triple),
115        Architecture::Riscv64 { .. } => isa_builder!(riscv64, (feature = "riscv64"), triple),
116        Architecture::Pulley32 | Architecture::Pulley32be => {
117            isa_builder!(pulley32, (feature = "pulley"), triple)
118        }
119        Architecture::Pulley64 | Architecture::Pulley64be => {
120            isa_builder!(pulley64, (feature = "pulley"), triple)
121        }
122        _ => Err(LookupError::Unsupported),
123    }
124}
125
126/// The string names of all the supported, but possibly not enabled, architectures. The elements of
127/// this slice are suitable to be passed to the [lookup_by_name] function to obtain the default
128/// configuration for that architecture.
129pub const ALL_ARCHITECTURES: &[&str] = &["x86_64", "aarch64", "s390x", "riscv64"];
130
131/// Look for a supported ISA with the given `name`.
132/// Return a builder that can create a corresponding `TargetIsa`.
133pub fn lookup_by_name(name: &str) -> Result<Builder, LookupError> {
134    lookup(triple!(name))
135}
136
137/// Describes reason for target lookup failure
138#[derive(PartialEq, Eq, Copy, Clone, Debug)]
139pub enum LookupError {
140    /// Support for this target was disabled in the current build.
141    SupportDisabled,
142
143    /// Support for this target has not yet been implemented.
144    Unsupported,
145}
146
147// This is manually implementing Error and Display instead of using thiserror to reduce the amount
148// of dependencies used by Cranelift.
149impl core::error::Error for LookupError {}
150
151impl fmt::Display for LookupError {
152    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
153        match self {
154            LookupError::SupportDisabled => write!(f, "Support for this target is disabled"),
155            LookupError::Unsupported => {
156                write!(f, "Support for this target has not been implemented yet")
157            }
158        }
159    }
160}
161
162/// The type of a polymorphic TargetISA object which is 'static.
163pub type OwnedTargetIsa = Arc<dyn TargetIsa>;
164
165/// Type alias of `IsaBuilder` used for building Cranelift's ISAs.
166pub type Builder = IsaBuilder<CodegenResult<OwnedTargetIsa>>;
167
168/// Builder for a `TargetIsa`.
169/// Modify the ISA-specific settings before creating the `TargetIsa` trait object with `finish`.
170#[derive(Clone)]
171pub struct IsaBuilder<T> {
172    triple: Triple,
173    setup: settings::Builder,
174    constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,
175}
176
177impl<T> IsaBuilder<T> {
178    /// Creates a new ISA-builder from its components, namely the `triple` for
179    /// the ISA, the ISA-specific settings builder, and a final constructor
180    /// function to generate the ISA from its components.
181    pub fn new(
182        triple: Triple,
183        setup: settings::Builder,
184        constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,
185    ) -> Self {
186        IsaBuilder {
187            triple,
188            setup,
189            constructor,
190        }
191    }
192
193    /// Creates a new [Builder] from a [TargetIsa], copying all flags in the
194    /// process.
195    pub fn from_target_isa(target_isa: &dyn TargetIsa) -> Builder {
196        // We should always be able to find the builder for the TargetISA, since presumably we
197        // also generated the previous TargetISA at some point
198        let triple = target_isa.triple().clone();
199        let mut builder = self::lookup(triple).expect("Could not find triple for target ISA");
200
201        // Copy ISA Flags
202        for flag in target_isa.isa_flags() {
203            builder.set(&flag.name, &flag.value_string()).unwrap();
204        }
205
206        builder
207    }
208
209    /// Gets the triple for the builder.
210    pub fn triple(&self) -> &Triple {
211        &self.triple
212    }
213
214    /// Iterates the available settings in the builder.
215    pub fn iter(&self) -> impl Iterator<Item = settings::Setting> + use<T> {
216        self.setup.iter()
217    }
218
219    /// Combine the ISA-specific settings with the provided
220    /// ISA-independent settings and allocate a fully configured
221    /// `TargetIsa` trait object. May return an error if some of the
222    /// flags are inconsistent or incompatible: for example, some
223    /// platform-independent features, like general SIMD support, may
224    /// need certain ISA extensions to be enabled.
225    pub fn finish(&self, shared_flags: settings::Flags) -> T {
226        (self.constructor)(self.triple.clone(), shared_flags, &self.setup)
227    }
228}
229
230impl<T> settings::Configurable for IsaBuilder<T> {
231    fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
232        self.setup.set(name, value)
233    }
234
235    fn enable(&mut self, name: &str) -> SetResult<()> {
236        self.setup.enable(name)
237    }
238}
239
240/// After determining that an instruction doesn't have an encoding, how should we proceed to
241/// legalize it?
242///
243/// The `Encodings` iterator returns a legalization function to call.
244pub type Legalize =
245    fn(ir::Inst, &mut ir::Function, &mut flowgraph::ControlFlowGraph, &dyn TargetIsa) -> bool;
246
247/// This struct provides information that a frontend may need to know about a target to
248/// produce Cranelift IR for the target.
249#[derive(Clone, Copy, Hash)]
250pub struct TargetFrontendConfig {
251    /// The default calling convention of the target.
252    pub default_call_conv: CallConv,
253
254    /// The pointer width of the target.
255    pub pointer_width: PointerWidth,
256
257    /// The log2 of the target's page size and alignment.
258    ///
259    /// Note that this may be an upper-bound that is larger than necessary for
260    /// some platforms since it may depend on runtime configuration.
261    pub page_size_align_log2: u8,
262}
263
264impl TargetFrontendConfig {
265    /// Get the pointer type of this target.
266    pub fn pointer_type(self) -> ir::Type {
267        ir::Type::int(self.pointer_bits() as u16).unwrap()
268    }
269
270    /// Get the width of pointers on this target, in units of bits.
271    pub fn pointer_bits(self) -> u8 {
272        self.pointer_width.bits()
273    }
274
275    /// Get the width of pointers on this target, in units of bytes.
276    pub fn pointer_bytes(self) -> u8 {
277        self.pointer_width.bytes()
278    }
279}
280
281/// Methods that are specialized to a target ISA.
282///
283/// Implies a Display trait that shows the shared flags, as well as any ISA-specific flags.
284pub trait TargetIsa: fmt::Display + Send + Sync {
285    /// Get the name of this ISA.
286    fn name(&self) -> &'static str;
287
288    /// Get the target triple that was used to make this trait object.
289    fn triple(&self) -> &Triple;
290
291    /// Get the ISA-independent flags that were used to make this trait object.
292    fn flags(&self) -> &settings::Flags;
293
294    /// Get the ISA-dependent flag values that were used to make this trait object.
295    fn isa_flags(&self) -> Vec<settings::Value>;
296
297    /// Get the ISA-dependent flag values as raw bytes for hashing.
298    fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>;
299
300    /// Get a flag indicating whether branch protection is enabled.
301    fn is_branch_protection_enabled(&self) -> bool {
302        false
303    }
304
305    /// Get the ISA-dependent maximum vector register size, in bytes.
306    fn dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32;
307
308    /// Compile the given function.
309    fn compile_function(
310        &self,
311        func: &Function,
312        domtree: &DominatorTree,
313        regalloc_ctx: &mut regalloc2::Ctx,
314        want_disasm: bool,
315        ctrl_plane: &mut ControlPlane,
316    ) -> CodegenResult<CompiledCodeStencil>;
317
318    #[cfg(feature = "unwind")]
319    /// Map a regalloc::Reg to its corresponding DWARF register.
320    fn map_regalloc_reg_to_dwarf(
321        &self,
322        _: crate::machinst::Reg,
323    ) -> Result<u16, RegisterMappingError> {
324        Err(RegisterMappingError::UnsupportedArchitecture)
325    }
326
327    /// Creates unwind information for the function.
328    ///
329    /// Returns `None` if there is no unwind information for the function.
330    #[cfg(feature = "unwind")]
331    fn emit_unwind_info(
332        &self,
333        result: &crate::machinst::CompiledCode,
334        kind: UnwindInfoKind,
335    ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>>;
336
337    /// Creates a new System V Common Information Entry for the ISA.
338    ///
339    /// Returns `None` if the ISA does not support System V unwind information.
340    #[cfg(feature = "unwind")]
341    fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
342        // By default, an ISA cannot create a System V CIE
343        None
344    }
345
346    /// Returns an object that can be used to build the text section of an
347    /// executable.
348    ///
349    /// This object will internally attempt to handle as many relocations as
350    /// possible using relative calls/jumps/etc between functions.
351    ///
352    /// The `num_labeled_funcs` argument here is the number of functions which
353    /// will be "labeled" or might have calls between them, typically the number
354    /// of defined functions in the object file.
355    fn text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>;
356
357    /// Returns the minimum function alignment and the preferred function
358    /// alignment, for performance, required by this ISA.
359    fn function_alignment(&self) -> FunctionAlignment;
360
361    /// The log2 of the target's page size and alignment.
362    ///
363    /// Note that this may be an upper-bound that is larger than necessary for
364    /// some platforms since it may depend on runtime configuration.
365    fn page_size_align_log2(&self) -> u8;
366
367    /// Create a polymorphic TargetIsa from this specific implementation.
368    fn wrapped(self) -> OwnedTargetIsa
369    where
370        Self: Sized + 'static,
371    {
372        Arc::new(self)
373    }
374
375    /// Generate a `Capstone` context for disassembling bytecode for this architecture.
376    #[cfg(feature = "disas")]
377    fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
378        Err(capstone::Error::UnsupportedArch)
379    }
380
381    /// Return the string representation of "reg" accessed as "size" bytes.
382    /// The returned string will match the usual disassemly view of "reg".
383    fn pretty_print_reg(&self, reg: Reg, size: u8) -> String;
384
385    /// Returns whether this ISA has a native fused-multiply-and-add instruction
386    /// for floats.
387    ///
388    /// Currently this only returns false on x86 when some native features are
389    /// not detected.
390    fn has_native_fma(&self) -> bool;
391
392    /// Returns whether this ISA has instructions for `ceil`, `floor`, etc.
393    fn has_round(&self) -> bool;
394
395    /// Returns whether the CLIF `blendv` instruction is implemented for
396    /// this ISA for the specified type.
397    fn has_blendv_lowering(&self, ty: Type) -> bool;
398
399    /// Returns whether the CLIF `x86_pshufb` instruction is implemented for
400    /// this ISA.
401    fn has_x86_pshufb_lowering(&self) -> bool;
402
403    /// Returns whether the CLIF `x86_pmulhrsw` instruction is implemented for
404    /// this ISA.
405    fn has_x86_pmulhrsw_lowering(&self) -> bool;
406
407    /// Returns whether the CLIF `x86_pmaddubsw` instruction is implemented for
408    /// this ISA.
409    fn has_x86_pmaddubsw_lowering(&self) -> bool;
410
411    /// Returns the mode of extension used for integer arguments smaller than
412    /// the pointer width in function signatures.
413    ///
414    /// Some platform ABIs require that smaller-than-pointer-width values are
415    /// either zero or sign-extended to the full register width. This value is
416    /// propagated to the `AbiParam` value created for signatures. Note that not
417    /// all ABIs for all platforms require extension of any form, so this is
418    /// generally only necessary for the `default_call_conv`.
419    fn default_argument_extension(&self) -> ir::ArgumentExtension;
420}
421
422/// A wrapper around the ISA-dependent flags types which only implements `Hash`.
423#[derive(Hash)]
424pub struct IsaFlagsHashKey<'a>(&'a [u8]);
425
426/// Function alignment specifications as required by an ISA, returned by
427/// [`TargetIsa::function_alignment`].
428#[derive(Copy, Clone)]
429pub struct FunctionAlignment {
430    /// The minimum alignment required by an ISA, where all functions must be
431    /// aligned to at least this amount.
432    pub minimum: u32,
433    /// A "preferred" alignment which should be used for more
434    /// performance-sensitive situations. This can involve cache-line-aligning
435    /// for example to get more of a small function into fewer cache lines.
436    pub preferred: u32,
437}
438
439/// Methods implemented for free for target ISA!
440impl<'a> dyn TargetIsa + 'a {
441    /// Get the default calling convention of this target.
442    pub fn default_call_conv(&self) -> CallConv {
443        CallConv::triple_default(self.triple())
444    }
445
446    /// Get the endianness of this ISA.
447    pub fn endianness(&self) -> ir::Endianness {
448        match self.triple().endianness().unwrap() {
449            target_lexicon::Endianness::Little => ir::Endianness::Little,
450            target_lexicon::Endianness::Big => ir::Endianness::Big,
451        }
452    }
453
454    /// Returns the minimum symbol alignment for this ISA.
455    pub fn symbol_alignment(&self) -> u64 {
456        match self.triple().architecture {
457            // All symbols need to be aligned to at least 2 on s390x.
458            Architecture::S390x => 2,
459            _ => 1,
460        }
461    }
462
463    /// Get the pointer type of this ISA.
464    pub fn pointer_type(&self) -> ir::Type {
465        ir::Type::int(self.pointer_bits() as u16).unwrap()
466    }
467
468    /// Get the width of pointers on this ISA.
469    pub(crate) fn pointer_width(&self) -> PointerWidth {
470        self.triple().pointer_width().unwrap()
471    }
472
473    /// Get the width of pointers on this ISA, in units of bits.
474    pub fn pointer_bits(&self) -> u8 {
475        self.pointer_width().bits()
476    }
477
478    /// Get the width of pointers on this ISA, in units of bytes.
479    pub fn pointer_bytes(&self) -> u8 {
480        self.pointer_width().bytes()
481    }
482
483    /// Get the information needed by frontends producing Cranelift IR.
484    pub fn frontend_config(&self) -> TargetFrontendConfig {
485        TargetFrontendConfig {
486            default_call_conv: self.default_call_conv(),
487            pointer_width: self.pointer_width(),
488            page_size_align_log2: self.page_size_align_log2(),
489        }
490    }
491}
492
493impl Debug for &dyn TargetIsa {
494    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
495        write!(
496            f,
497            "TargetIsa {{ triple: {:?}, pointer_width: {:?}}}",
498            self.triple(),
499            self.pointer_width()
500        )
501    }
502}