metal2vulkan 0.1.0

Translate Metal AIR / LLVM IR to Vulkan SPIR-V with a native Rust emitter
Documentation
//! Shared SPIR-V type/constant interner.
//!
//! Phase 2 of the metal2vulkan refactor unifies the two type-interning layers — the native
//! emitter and the passes-layer `Ctx` — behind one structure. This module holds the emitter's
//! type/constant dedup caches. The builder methods that mint the actual SPIR-V instructions stay
//! on `Emitter` (they push to the module and request capabilities), while the crate-owned module
//! owns the single result-id cursor.

use crate::native::ir::LlType;
use spirv::{StorageClass, Word};
use std::collections::HashMap;

/// The emitter's type + constant interner: dedup caches keyed by structural type/value. The
/// builder methods that emit the corresponding SPIR-V type/constant instructions live on
/// `Emitter`; this struct owns only deduplication state.
pub(crate) struct TypeInterner {
    pub(crate) types: HashMap<LlType, Word>,
    pub(crate) signed_int_types: HashMap<LlType, Word>,
    pub(crate) ptr_types: HashMap<(StorageClass, LlType), Word>,
    pub(crate) function_types: HashMap<Vec<Word>, Word>,
    pub(crate) uint_constants: HashMap<u32, Word>,
    pub(crate) int_constants: HashMap<(u32, u64), Word>,
    pub(crate) bool_constants: HashMap<bool, Word>,
    pub(crate) float32_constants: HashMap<u32, Word>,
    pub(crate) float16_constants: HashMap<u16, Word>,
    pub(crate) null_constants: HashMap<LlType, Word>,
    pub(crate) composite_constants: HashMap<(LlType, Vec<Word>), Word>,
    pub(crate) undefs: HashMap<LlType, Word>,
}

impl TypeInterner {
    /// A fresh interner with empty caches.
    pub(crate) fn new() -> Self {
        Self {
            types: HashMap::new(),
            signed_int_types: HashMap::new(),
            ptr_types: HashMap::new(),
            function_types: HashMap::new(),
            uint_constants: HashMap::new(),
            int_constants: HashMap::new(),
            bool_constants: HashMap::new(),
            float32_constants: HashMap::new(),
            float16_constants: HashMap::new(),
            null_constants: HashMap::new(),
            composite_constants: HashMap::new(),
            undefs: HashMap::new(),
        }
    }
}