hugr_llvm/extension/
logic.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use hugr::{
    extension::simple_op::MakeExtensionOp,
    ops::{ExtensionOp, NamedOp, Value},
    std_extensions::logic::{self, LogicOp},
    types::SumType,
    HugrView,
};
use inkwell::IntPredicate;

use crate::{
    custom::CodegenExtsBuilder,
    emit::{emit_value, func::EmitFuncContext, EmitOpArgs},
    sum::LLVMSumValue,
};

use anyhow::{anyhow, Result};

fn emit_logic_op<'c, H: HugrView>(
    context: &mut EmitFuncContext<'c, '_, H>,
    args: EmitOpArgs<'c, '_, ExtensionOp, H>,
) -> Result<()> {
    let lot = LogicOp::from_optype(&args.node().generalise()).ok_or(anyhow!(
        "LogicOpEmitter: from_optype_failed: {:?}",
        args.node().as_ref()
    ))?;
    let builder = context.builder();
    // Turn bool sum inputs into i1's
    let mut inputs = vec![];
    for inp in args.inputs {
        let bool_ty = context.llvm_sum_type(SumType::new_unary(2))?;
        let bool_val = LLVMSumValue::try_new(inp, bool_ty)?;
        inputs.push(bool_val.build_get_tag(builder)?);
    }
    let res = match lot {
        LogicOp::And => {
            let mut acc = inputs[0];
            for inp in inputs.into_iter().skip(1) {
                acc = builder.build_and(acc, inp, "")?;
            }
            acc
        }
        LogicOp::Or => {
            let mut acc = inputs[0];
            for inp in inputs.into_iter().skip(1) {
                acc = builder.build_or(acc, inp, "")?;
            }
            acc
        }
        LogicOp::Eq => {
            let x = inputs.pop().unwrap();
            let y = inputs.pop().unwrap();
            let mut acc = builder.build_int_compare(IntPredicate::EQ, x, y, "")?;
            for inp in inputs {
                let eq = builder.build_int_compare(IntPredicate::EQ, inp, x, "")?;
                acc = builder.build_and(acc, eq, "")?;
            }
            acc
        }
        op => {
            return Err(anyhow!("LogicOpEmitter: Unknown op: {op:?}"));
        }
    };
    // Turn result back into sum
    let res = builder.build_int_cast(res, context.iw_context().bool_type(), "")?;
    let true_val = emit_value(context, &Value::true_val())?;
    let false_val = emit_value(context, &Value::false_val())?;
    let res = context
        .builder()
        .build_select(res, true_val, false_val, "")?;
    args.outputs.finish(context.builder(), vec![res])
}

/// Populates a [CodegenExtsBuilder] with all extensions needed to lower logic
/// ops.
pub fn add_logic_extensions<'a, H: HugrView + 'a>(
    cem: CodegenExtsBuilder<'a, H>,
) -> CodegenExtsBuilder<'a, H> {
    cem.extension_op(logic::EXTENSION_ID, LogicOp::Eq.name(), emit_logic_op)
        .extension_op(logic::EXTENSION_ID, LogicOp::And.name(), emit_logic_op)
        .extension_op(logic::EXTENSION_ID, LogicOp::Or.name(), emit_logic_op)
        .extension_op(logic::EXTENSION_ID, LogicOp::Not.name(), emit_logic_op)
}

impl<'a, H: HugrView + 'a> CodegenExtsBuilder<'a, H> {
    /// Populates a [CodegenExtsBuilder] with all extensions needed to lower
    /// logic ops.
    pub fn add_logic_extensions(self) -> Self {
        add_logic_extensions(self)
    }
}

#[cfg(test)]
mod test {
    use hugr::{
        builder::{Dataflow, DataflowSubContainer},
        extension::{prelude::BOOL_T, ExtensionRegistry},
        std_extensions::logic::{self, LogicOp},
        Hugr,
    };
    use rstest::rstest;

    use crate::{
        check_emission,
        emit::test::SimpleHugrConfig,
        extension::logic::add_logic_extensions,
        test::{llvm_ctx, TestContext},
    };

    fn test_logic_op(op: LogicOp, arity: usize) -> Hugr {
        SimpleHugrConfig::new()
            .with_ins(vec![BOOL_T; arity])
            .with_outs(vec![BOOL_T])
            .with_extensions(ExtensionRegistry::try_new(vec![logic::EXTENSION.to_owned()]).unwrap())
            .finish(|mut builder| {
                let outputs = builder
                    .add_dataflow_op(op, builder.input_wires())
                    .unwrap()
                    .outputs();
                builder.finish_with_outputs(outputs).unwrap()
            })
    }

    #[rstest]
    fn and(mut llvm_ctx: TestContext) {
        llvm_ctx.add_extensions(add_logic_extensions);
        let hugr = test_logic_op(LogicOp::And, 2);
        check_emission!(hugr, llvm_ctx);
    }

    #[rstest]
    fn or(mut llvm_ctx: TestContext) {
        llvm_ctx.add_extensions(add_logic_extensions);
        let hugr = test_logic_op(LogicOp::Or, 2);
        check_emission!(hugr, llvm_ctx);
    }

    #[rstest]
    fn eq(mut llvm_ctx: TestContext) {
        llvm_ctx.add_extensions(add_logic_extensions);
        let hugr = test_logic_op(LogicOp::Eq, 2);
        check_emission!(hugr, llvm_ctx);
    }
}