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