Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement codgen extension for int_ops #20

Closed
Tracked by #5
doug-q opened this issue Jun 13, 2024 · 3 comments · Fixed by #87
Closed
Tracked by #5

Implement codgen extension for int_ops #20

doug-q opened this issue Jun 13, 2024 · 3 comments · Fixed by #87
Assignees

Comments

@doug-q
Copy link
Collaborator

doug-q commented Jun 13, 2024

See https://github.com/CQCL/hugr/blob/main/specification/hugr.md#arithmeticint for the spec of these ops.

@doug-q
Copy link
Collaborator Author

doug-q commented Jun 13, 2024

I do have a small amount of progress here

fn emit_icmp<'c, H: HugrView>(
    context: &mut EmitFuncContext<'c, H>,
    args: EmitOpArgs<'c, CustomOp, H>,
    pred: inkwell::IntPredicate,
) -> Result<()> {
    let true_val = emit_value(context, &Value::true_val())?;
    let false_val = emit_value(context, &Value::false_val())?;
    let builder = context.builder();
    let [lhs, rhs] = TryInto::<[_; 2]>::try_into(args.inputs).unwrap();
    let a = builder.build_int_compare(pred, lhs.into_int_value(), rhs.into_int_value(), "")?;
    let a = builder.build_select(a, true_val, false_val, "")?;
    args.outputs.finish(builder, [a.into()])
}

impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for IntOpEmitter<'c, '_, H> {
    fn emit(&mut self, args: EmitOpArgs<'c, CustomOp, H>) -> Result<()> {
        let iot = ConcreteIntOp::from_optype(&args.node().generalise())
            .ok_or(anyhow!("IntOpEmitter from_optype_failed"))?;
        match iot.name().as_str() {
            "iadd" => {
                let builder = self.0.builder();
                let [lhs, rhs] = TryInto::<[_; 2]>::try_into(args.inputs).unwrap();
                let a = builder.build_int_add(lhs.into_int_value(), rhs.into_int_value(), "")?;
                args.outputs.finish(builder, [a.into()])
            }
            "ieq" => {
                emit_icmp(self.0, args, inkwell::IntPredicate::EQ)
            }
            "ilt_s" => {
                emit_icmp(self.0, args, inkwell::IntPredicate::SLT)
            }
            "isub" => {
                let builder = self.0.builder();
                let [lhs, rhs] = TryInto::<[_; 2]>::try_into(args.inputs).unwrap();
                let a = builder.build_int_sub(lhs.into_int_value(), rhs.into_int_value(), "")?;
                args.outputs.finish(builder, [a.into()])
            }
            n => Err(anyhow!("IntOpEmitter: unknown name: {n}")),
        }
    }
}


@doug-q doug-q changed the title The remaining int ops Lowerings for arithmetic.int extension Jun 14, 2024
@doug-q doug-q changed the title Lowerings for arithmetic.int extension Implement codgen extension for int_ops Jun 19, 2024
@aborgna-q
Copy link
Collaborator

I added/moved some helpers in #83, that may come useful here

@croyzor
Copy link
Contributor

croyzor commented Aug 28, 2024

The scope of this issue has been reduced to basic arithmatic for getting guppy programs going, the rest of the ops go in new issue #86

github-merge-queue bot pushed a commit that referenced this issue Aug 28, 2024
Add emission for multiplication, division, remainder and negation ops

Closes #20

---------

Co-authored-by: Agustín Borgna <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants