#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragIntAddFace {
pub k: i64,
pub box_idx: u32,
pub add_idx: u32,
}
pub fn expr_fragment_int_add_face(plan: &ExprFragmentPlan) -> Option<FragIntAddFace> {
if plan.params.as_slice() != [FragTy::IntCarrier] || plan.result != FragTy::IntCarrier {
return None;
}
let [n0, n1, n2, n3] = plan.body.nodes.as_slice() else {
return None;
};
if plan.body.result != FragValueId(3) {
return None;
}
let (FragNodeKind::Local { index: 0 }, FragNodeKind::ConstI64(k)) = (&n0.kind, &n1.kind) else {
return None;
};
let FragNodeKind::HostCall {
role: FragHostRole::Box,
func_idx: box_idx,
args: box_args,
} = &n2.kind
else {
return None;
};
let FragNodeKind::HostCall {
role: FragHostRole::Add,
func_idx: add_idx,
args: add_args,
} = &n3.kind
else {
return None;
};
if box_args.as_slice() != [FragValueId(1)]
|| add_args.as_slice() != [FragValueId(0), FragValueId(2)]
{
return None;
}
if n0.ty != FragTy::IntCarrier
|| n1.ty != FragTy::I64
|| n2.ty != FragTy::IntCarrier
|| n3.ty != FragTy::IntCarrier
{
return None;
}
Some(FragIntAddFace {
k: *k,
box_idx: *box_idx,
add_idx: *add_idx,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragProjectFace {
pub struct_idx: u32,
pub field_idx: u32,
}
pub fn expr_fragment_project_face(plan: &ExprFragmentPlan) -> Option<FragProjectFace> {
if plan.params.as_slice() != [FragTy::AdtRef] || plan.result != FragTy::AdtRef {
return None;
}
let [n0, n1] = plan.body.nodes.as_slice() else {
return None;
};
if plan.body.result != FragValueId(1) {
return None;
}
let FragNodeKind::Local { index: 0 } = n0.kind else {
return None;
};
let FragNodeKind::StructGetUser {
ty_idx,
field,
value,
} = n1.kind
else {
return None;
};
if value != FragValueId(0) || field > 1 {
return None;
}
if n0.ty != FragTy::AdtRef || n1.ty != FragTy::AdtRef {
return None;
}
Some(FragProjectFace {
struct_idx: ty_idx,
field_idx: field,
})
}
pub fn frag_ty_is_record_scalar(ty: FragTy) -> bool {
matches!(ty, FragTy::BoolI32 | FragTy::IntCarrier | FragTy::F64)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragRecordProjFace {
pub struct_idx: u32,
pub field_idx: u32,
}
pub fn expr_fragment_record_proj_face(plan: &ExprFragmentPlan) -> Option<FragRecordProjFace> {
if plan.params.as_slice() != [FragTy::AdtRef]
|| !frag_ty_is_record_scalar(plan.result)
|| plan.body.result != FragValueId(1)
{
return None;
}
let [n0, n1] = plan.body.nodes.as_slice() else {
return None;
};
if n0.id != FragValueId(0) || n1.id != FragValueId(1) {
return None;
}
let FragNodeKind::Local { index: 0 } = n0.kind else {
return None;
};
let FragNodeKind::StructGetUser {
ty_idx,
field,
value,
} = n1.kind
else {
return None;
};
if value != FragValueId(0) || n0.ty != FragTy::AdtRef || n1.ty != plan.result {
return None;
}
Some(FragRecordProjFace {
struct_idx: ty_idx,
field_idx: field,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragTagDispatchFace {
pub opt_idx: u32,
pub box_idx: u32,
pub tag: i64,
pub then_c: i64,
pub else_c: i64,
}
fn expr_fragment_tag_dispatch_arm(block: &FragBlock) -> Option<(u32, i64)> {
let [n0, n1] = block.nodes.as_slice() else {
return None;
};
if block.result != FragValueId(1)
|| n0.id != FragValueId(0)
|| n1.id != FragValueId(1)
|| n0.ty != FragTy::I64
|| n1.ty != FragTy::IntCarrier
{
return None;
}
let FragNodeKind::ConstI64(constant) = n0.kind else {
return None;
};
let FragNodeKind::HostCall {
role: FragHostRole::Box,
func_idx,
ref args,
} = n1.kind
else {
return None;
};
(args.as_slice() == [FragValueId(0)]).then_some((func_idx, constant))
}
pub fn expr_fragment_tag_dispatch_face(plan: &ExprFragmentPlan) -> Option<FragTagDispatchFace> {
if plan.params.as_slice() != [FragTy::AdtRef]
|| plan.result != FragTy::IntCarrier
|| plan.body.result != FragValueId(4)
{
return None;
}
let [n0, n1, n2, n3, n4] = plan.body.nodes.as_slice() else {
return None;
};
if [n0.id.0, n1.id.0, n2.id.0, n3.id.0, n4.id.0] != [0, 1, 2, 3, 4]
|| n0.ty != FragTy::AdtRef
|| n1.ty != FragTy::RawI32
|| n2.ty != FragTy::RawI32
|| n3.ty != FragTy::BoolI32
|| n4.ty != FragTy::IntCarrier
{
return None;
}
let FragNodeKind::Local { index: 0 } = n0.kind else {
return None;
};
let FragNodeKind::StructGetUser {
ty_idx: opt_idx,
field: 0,
value: FragValueId(0),
} = n1.kind
else {
return None;
};
let FragNodeKind::ConstI32(tag) = n2.kind else {
return None;
};
let FragNodeKind::Prim {
op: FragPrim::I32Eq,
ref args,
} = n3.kind
else {
return None;
};
let FragNodeKind::If {
cond: FragValueId(3),
ref then_block,
ref else_block,
} = n4.kind
else {
return None;
};
if args.as_slice() != [FragValueId(1), FragValueId(2)] {
return None;
}
let (box_idx, then_c) = expr_fragment_tag_dispatch_arm(then_block)?;
let (else_box_idx, else_c) = expr_fragment_tag_dispatch_arm(else_block)?;
(box_idx == else_box_idx).then_some(FragTagDispatchFace {
opt_idx,
box_idx,
tag: i64::from(tag),
then_c,
else_c,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragVectorGetOrDefaultFace {
pub arr_ty: u32,
pub to_index_idx: u32,
pub box_idx: u32,
pub default: i64,
}
pub fn expr_fragment_vector_get_face(
plan: &ExprFragmentPlan,
) -> Option<FragVectorGetOrDefaultFace> {
if plan.params.as_slice() != [FragTy::AdtRef, FragTy::IntCarrier]
|| plan.result != FragTy::IntCarrier
|| plan.body.result != FragValueId(0)
{
return None;
}
let [n0] = plan.body.nodes.as_slice() else {
return None;
};
if n0.id != FragValueId(0) || n0.ty != FragTy::IntCarrier {
return None;
}
let FragNodeKind::VectorGetOrDefault {
arr_ty,
to_index_idx,
box_idx,
default,
} = n0.kind
else {
return None;
};
(to_index_idx != box_idx).then_some(FragVectorGetOrDefaultFace {
arr_ty,
to_index_idx,
box_idx,
default,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FragIntCmpOp {
Lt,
Gt,
Ge,
Eq,
}
impl FragIntCmpOp {
pub fn lean_ctor(self) -> &'static str {
match self {
FragIntCmpOp::Lt => ".lt",
FragIntCmpOp::Gt => ".gt",
FragIntCmpOp::Ge => ".ge",
FragIntCmpOp::Eq => ".eq",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FragIntCmpFace {
pub op: FragIntCmpOp,
pub helper_idx: u32,
}
fn frag_int_cmp_of_prim(op: FragPrim) -> Option<FragIntCmpOp> {
match op {
FragPrim::I32LtS => Some(FragIntCmpOp::Lt),
FragPrim::I32GtS => Some(FragIntCmpOp::Gt),
FragPrim::I32GeS => Some(FragIntCmpOp::Ge),
_ => None,
}
}
fn frag_int_cmp_prefix(nodes: &[FragNode]) -> Option<(FragIntCmpFace, usize)> {
let [n0, n1, rest @ ..] = nodes else {
return None;
};
if n0.id != FragValueId(0)
|| n1.id != FragValueId(1)
|| n0.ty != FragTy::IntCarrier
|| n1.ty != FragTy::IntCarrier
{
return None;
}
let (FragNodeKind::Local { index: 0 }, FragNodeKind::Local { index: 1 }) = (&n0.kind, &n1.kind)
else {
return None;
};
let [n2, tail @ ..] = rest else {
return None;
};
if n2.id != FragValueId(2) {
return None;
}
match &n2.kind {
FragNodeKind::HostCall {
role: FragHostRole::Eq,
func_idx,
args,
} if n2.ty == FragTy::BoolI32
&& args.as_slice() == [FragValueId(0), FragValueId(1)] =>
{
Some((
FragIntCmpFace {
op: FragIntCmpOp::Eq,
helper_idx: *func_idx,
},
3,
))
}
FragNodeKind::HostCall {
role: FragHostRole::Cmp,
func_idx,
args,
} if n2.ty == FragTy::RawI32
&& args.as_slice() == [FragValueId(0), FragValueId(1)] =>
{
let [n3, n4, ..] = tail else {
return None;
};
if n3.id != FragValueId(3)
|| n4.id != FragValueId(4)
|| n3.ty != FragTy::RawI32
|| n4.ty != FragTy::BoolI32
{
return None;
}
let FragNodeKind::ConstI32(0) = n3.kind else {
return None;
};
let FragNodeKind::Prim { op, args } = &n4.kind else {
return None;
};
if args.as_slice() != [FragValueId(2), FragValueId(3)] {
return None;
}
Some((
FragIntCmpFace {
op: frag_int_cmp_of_prim(*op)?,
helper_idx: *func_idx,
},
5,
))
}
_ => None,
}
}
pub fn expr_fragment_int_cmp_bool_face(plan: &ExprFragmentPlan) -> Option<FragIntCmpFace> {
if plan.params.as_slice() != [FragTy::IntCarrier, FragTy::IntCarrier]
|| plan.result != FragTy::BoolI32
{
return None;
}
let (face, len) = frag_int_cmp_prefix(&plan.body.nodes)?;
(plan.body.nodes.len() == len && plan.body.result == FragValueId(len - 1)).then_some(face)
}
fn frag_int_select_arm(block: &FragBlock, local: u32) -> bool {
let [node] = block.nodes.as_slice() else {
return false;
};
block.result == FragValueId(0)
&& node.id == FragValueId(0)
&& node.ty == FragTy::IntCarrier
&& node.kind == FragNodeKind::Local { index: local }
}
pub fn expr_fragment_int_select_face(plan: &ExprFragmentPlan) -> Option<FragIntCmpFace> {
if plan.params.as_slice() != [FragTy::IntCarrier, FragTy::IntCarrier]
|| plan.result != FragTy::IntCarrier
{
return None;
}
let (face, len) = frag_int_cmp_prefix(&plan.body.nodes)?;
if plan.body.nodes.len() != len + 1 || plan.body.result != FragValueId(len) {
return None;
}
let node = &plan.body.nodes[len];
if node.id != FragValueId(len) || node.ty != FragTy::IntCarrier {
return None;
}
let FragNodeKind::If {
cond,
then_block,
else_block,
} = &node.kind
else {
return None;
};
(*cond == FragValueId(len - 1)
&& frag_int_select_arm(then_block, 0)
&& frag_int_select_arm(else_block, 1))
.then_some(face)
}
fn frag_block_has_user_struct_get(block: &FragBlock) -> bool {
block.nodes.iter().any(|node| match &node.kind {
FragNodeKind::StructGetUser { .. } => true,
FragNodeKind::If {
then_block,
else_block,
..
} => {
frag_block_has_user_struct_get(then_block)
|| frag_block_has_user_struct_get(else_block)
}
_ => false,
})
}
pub fn expr_fragment_is_tag_dispatch(plan: &ExprFragmentPlan) -> bool {
plan.params.as_slice() == [FragTy::AdtRef]
&& plan.result == FragTy::IntCarrier
&& frag_block_has_user_struct_get(&plan.body)
}
fn frag_block_touches_adt_ref(block: &FragBlock) -> bool {
block.nodes.iter().any(|node| {
node.ty == FragTy::AdtRef
|| match &node.kind {
FragNodeKind::StructGetUser { .. } => true,
FragNodeKind::If {
then_block,
else_block,
..
} => {
frag_block_touches_adt_ref(then_block) || frag_block_touches_adt_ref(else_block)
}
_ => false,
}
})
}
pub fn expr_fragment_plan_touches_adt_ref(plan: &ExprFragmentPlan) -> bool {
plan.params.contains(&FragTy::AdtRef)
|| plan.result == FragTy::AdtRef
|| frag_block_touches_adt_ref(&plan.body)
}
fn frag_block_has_host_call_where(block: &FragBlock, want: &dyn Fn(FragHostRole) -> bool) -> bool {
block.nodes.iter().any(|node| match &node.kind {
FragNodeKind::HostCall { role, .. } => want(*role),
FragNodeKind::If {
then_block,
else_block,
..
} => {
frag_block_has_host_call_where(then_block, want)
|| frag_block_has_host_call_where(else_block, want)
}
_ => false,
})
}
pub fn expr_fragment_plan_has_host_calls(plan: &ExprFragmentPlan) -> bool {
frag_block_has_host_call_where(&plan.body, &|_| true)
}
pub fn expr_fragment_plan_calls_host_role(plan: &ExprFragmentPlan, role: FragHostRole) -> bool {
frag_block_has_host_call_where(&plan.body, &|candidate| candidate == role)
}
#[cfg(all(test, feature = "engine"))]
mod record_proj_face_tests {
use super::*;
fn record_proj_plan(struct_idx: u32, field: u32, result: FragTy) -> ExprFragmentPlan {
ExprFragmentPlan {
params: vec![FragTy::AdtRef],
result,
body: FragBlock {
nodes: vec![
FragNode {
id: FragValueId(0),
ty: FragTy::AdtRef,
kind: FragNodeKind::Local { index: 0 },
},
FragNode {
id: FragValueId(1),
ty: result,
kind: FragNodeKind::StructGetUser {
ty_idx: struct_idx,
field,
value: FragValueId(0),
},
},
],
result: FragValueId(1),
},
}
}
#[test]
fn frag_ty_is_record_scalar_is_exactly_the_three_leaves() {
assert!(frag_ty_is_record_scalar(FragTy::BoolI32));
assert!(frag_ty_is_record_scalar(FragTy::IntCarrier));
assert!(frag_ty_is_record_scalar(FragTy::F64));
for ty in [FragTy::I64, FragTy::RawI32, FragTy::Ref, FragTy::AdtRef] {
assert!(!frag_ty_is_record_scalar(ty), "{ty:?} must not be a record leaf");
}
}
#[test]
fn recognizes_person_field_reads() {
assert_eq!(
expr_fragment_record_proj_face(&record_proj_plan(0, 1, FragTy::BoolI32)),
Some(FragRecordProjFace { struct_idx: 0, field_idx: 1 })
);
assert_eq!(
expr_fragment_record_proj_face(&record_proj_plan(0, 0, FragTy::IntCarrier)),
Some(FragRecordProjFace { struct_idx: 0, field_idx: 0 })
);
assert_eq!(
expr_fragment_record_proj_face(&record_proj_plan(7, 5, FragTy::F64)),
Some(FragRecordProjFace { struct_idx: 7, field_idx: 5 })
);
}
#[test]
fn declines_whole_reference_projection() {
assert_eq!(
expr_fragment_record_proj_face(&record_proj_plan(0, 0, FragTy::AdtRef)),
None
);
}
#[test]
fn declines_non_scalar_leaf_result() {
for ty in [FragTy::I64, FragTy::RawI32, FragTy::Ref] {
assert_eq!(expr_fragment_record_proj_face(&record_proj_plan(0, 0, ty)), None);
}
}
#[test]
fn declines_wrong_parameter_shape() {
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.params = vec![FragTy::IntCarrier];
assert_eq!(expr_fragment_record_proj_face(&plan), None);
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.params = vec![FragTy::AdtRef, FragTy::AdtRef];
assert_eq!(expr_fragment_record_proj_face(&plan), None);
}
#[test]
fn declines_node_type_mismatch_and_wrong_body_shape() {
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.body.nodes[1].ty = FragTy::IntCarrier;
assert_eq!(expr_fragment_record_proj_face(&plan), None);
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.body.nodes[1].kind = FragNodeKind::StructGetUser {
ty_idx: 0,
field: 1,
value: FragValueId(1),
};
assert_eq!(expr_fragment_record_proj_face(&plan), None);
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.body.nodes.push(FragNode {
id: FragValueId(2),
ty: FragTy::BoolI32,
kind: FragNodeKind::ConstBool(true),
});
assert_eq!(expr_fragment_record_proj_face(&plan), None);
let mut plan = record_proj_plan(0, 1, FragTy::BoolI32);
plan.body.nodes[0].kind = FragNodeKind::Local { index: 1 };
assert_eq!(expr_fragment_record_proj_face(&plan), None);
}
}