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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use hugr::{
    extension::{simple_op::MakeExtensionOp, ExtensionId},
    ops::{ExtensionOp, Value},
    std_extensions::logic::{self, LogicOp},
    types::{CustomType, SumType},
    HugrView,
};
use inkwell::{types::BasicTypeEnum, IntPredicate};

use crate::{
    emit::{emit_value, func::EmitFuncContext, EmitOp, EmitOpArgs},
    sum::LLVMSumValue,
    types::TypingSession,
};

use super::{CodegenExtension, CodegenExtsMap};
use anyhow::{anyhow, Result};

struct LogicOpEmitter<'c, 'd, H>(&'d mut EmitFuncContext<'c, H>);

impl<'c, H: HugrView> EmitOp<'c, ExtensionOp, H> for LogicOpEmitter<'c, '_, H> {
    fn emit(&mut self, 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 = self.0.builder();
        // Turn bool sum inputs into i1's
        let mut inputs = vec![];
        for inp in args.inputs {
            let bool_ty = self.0.llvm_sum_type(SumType::Unit { size: 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, self.0.iw_context().bool_type(), "")?;
        let true_val = emit_value(self.0, &Value::true_val())?;
        let false_val = emit_value(self.0, &Value::false_val())?;
        let res = self
            .0
            .builder()
            .build_select(res, true_val, false_val, "")?;
        args.outputs.finish(self.0.builder(), vec![res])
    }
}

/// A [CodegenExtension] for the [hugr::std_extensions::logic]
/// extension.
pub struct LogicCodegenExtension;

impl<'c, H: HugrView> CodegenExtension<'c, H> for LogicCodegenExtension {
    fn extension(&self) -> ExtensionId {
        logic::EXTENSION_ID
    }

    fn llvm_type<'d>(
        &self,
        _context: &TypingSession<'c, H>,
        hugr_type: &CustomType,
    ) -> Result<BasicTypeEnum<'c>> {
        Err(anyhow!(
            "LogicCodegenExtension: unsupported type: {}",
            hugr_type
        ))
    }

    fn emitter<'a>(
        &self,
        context: &'a mut EmitFuncContext<'c, H>,
    ) -> Box<dyn EmitOp<'c, ExtensionOp, H> + 'a> {
        Box::new(LogicOpEmitter(context))
    }
}

/// Populates a [CodegenExtsMap] with all extensions needed to lower logic ops,
/// types, and constants.
pub fn add_logic_extensions<H: HugrView>(cem: CodegenExtsMap<'_, H>) -> CodegenExtsMap<'_, H> {
    cem.add_cge(LogicCodegenExtension)
}

impl<H: HugrView> CodegenExtsMap<'_, H> {
    /// Populates a [CodegenExtsMap] with all extensions needed to lower logic ops,
    /// types, and constants.
    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,
        custom::logic::add_logic_extensions,
        emit::test::SimpleHugrConfig,
        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);
    }
}