use derive_new::new;
use pliron::{
attribute::AttrObj,
basic_block::BasicBlock,
opts::mem2reg::AllocInfo,
region::Region,
utils::table::{HMap, SmallMap, SmallSet},
};
use crate::prelude::*;
pub type LogicalResult = core::result::Result<(), ()>;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum DeletionKind {
Keep,
Delete,
}
#[op_interface]
pub trait PromotableRegionOpInterface {
verify_op_succ!();
fn is_region_promotable(
&self,
ctx: &Context,
alloc: &AllocInfo,
region: Ptr<Region>,
has_value_stores: bool,
) -> bool;
fn setup_promotion(
&self,
ctx: &mut Context,
alloc: &AllocInfo,
reaching_def: Value,
has_value_stores: bool,
regions_to_process: &mut SmallMap<Ptr<Region>, Value, 2>,
);
fn finalize_promotion(
&self,
ctx: &mut Context,
alloc: &AllocInfo,
entry_reaching_def: Value,
has_value_stores: bool,
reaching_at_block_end: &HMap<Ptr<BasicBlock>, Value>,
) -> Value;
}
#[type_interface]
pub trait DestructurableTypeInterface {
verify_ty_succ!();
fn subelement_index_map(&self, ctx: &Context) -> Option<HMap<AttrObj, TypeHandle>>;
fn type_at_index(&self, ctx: &Context, index: &AttrObj) -> TypeHandle;
}
#[op_interface]
pub trait DestructurableConstructorOpInterface {
verify_op_succ!();
fn destructurable_values(&self, ctx: &Context) -> Vec<DestructurableValueSlot>;
fn destructure(
&self,
ctx: &mut Context,
value: &DestructurableValueSlot,
used_indices: &SmallSet<AttrObj, 8>,
rewriter: &mut PassRewriter,
new_constructors: &mut Vec<TraitOp<dyn DestructurableConstructorOpInterface>>,
) -> HMap<AttrObj, ValueSlot>;
fn handle_destructuring_complete(
&self,
ctx: &mut Context,
value: &DestructurableValueSlot,
rewriter: &mut PassRewriter,
) -> Option<TraitOp<dyn DestructurableConstructorOpInterface>>;
}
#[op_interface]
pub trait DestructurableAccessorOpInterface {
verify_op_succ!();
fn can_rewire(
&self,
ctx: &Context,
value: &DestructurableValueSlot,
used_indices: &mut SmallSet<AttrObj, 8>,
must_be_safely_used: &mut Vec<ValueSlot>,
) -> bool;
fn rewire(
&self,
ctx: &mut Context,
value: &DestructurableValueSlot,
subvalues: &HMap<AttrObj, ValueSlot>,
rewriter: &mut PassRewriter,
) -> DeletionKind;
}
#[op_interface]
pub trait SafeMemorySlotAccessOpInterface {
verify_op_succ!();
#[allow(clippy::result_unit_err)]
fn ensure_only_safe_accesses(
&self,
ctx: &Context,
value: &ValueSlot,
must_be_safely_used: &mut Vec<ValueSlot>,
) -> LogicalResult;
}
#[derive(new, Debug)]
pub struct ValueSlot {
pub value: Value,
pub elem_ty: TypeHandle,
}
#[derive(Debug)]
pub struct DestructurableValueSlot {
pub slot: ValueSlot,
pub subelement_types: HMap<AttrObj, TypeHandle>,
}