cranelift_codegen/ir/
dynamic_type.rs

1//! Dynamic IR types
2
3use crate::ir::entities::DynamicType;
4use crate::ir::types::*;
5use crate::ir::GlobalValue;
6use crate::ir::PrimaryMap;
7
8#[cfg(feature = "enable-serde")]
9use serde_derive::{Deserialize, Serialize};
10
11/// A dynamic type object which has a base vector type and a scaling factor.
12#[derive(Clone, PartialEq, Hash)]
13#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
14pub struct DynamicTypeData {
15    /// Base vector type, this is the minimum size of the type.
16    pub base_vector_ty: Type,
17    /// The dynamic scaling factor of the base vector type.
18    pub dynamic_scale: GlobalValue,
19}
20
21impl DynamicTypeData {
22    /// Create a new dynamic type.
23    pub fn new(base_vector_ty: Type, dynamic_scale: GlobalValue) -> Self {
24        assert!(base_vector_ty.is_vector());
25        Self {
26            base_vector_ty,
27            dynamic_scale,
28        }
29    }
30
31    /// Convert 'base_vector_ty' into a concrete dynamic vector type.
32    pub fn concrete(&self) -> Option<Type> {
33        self.base_vector_ty.vector_to_dynamic()
34    }
35}
36
37/// All allocated dynamic types.
38pub type DynamicTypes = PrimaryMap<DynamicType, DynamicTypeData>;
39
40/// Convert a dynamic-vector type to a fixed-vector type.
41pub fn dynamic_to_fixed(ty: Type) -> Type {
42    match ty {
43        I8X8XN => I8X8,
44        I8X16XN => I8X16,
45        I16X4XN => I16X4,
46        I16X8XN => I16X8,
47        I32X2XN => I32X2,
48        I32X4XN => I32X4,
49        I64X2XN => I64X2,
50        F32X4XN => F32X4,
51        F64X2XN => F64X2,
52        _ => unreachable!("unhandled type: {}", ty),
53    }
54}