Skip to content

Commit

Permalink
feat: lower CFGs
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Jun 14, 2024
1 parent b63c619 commit ab26584
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ devenv.local.nix
/target
/.envrc
/result
*.snap.new
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ llvm14 = ["dep:llvm-sys-140", "inkwell/llvm14-0"]
[dependencies]
inkwell = { version = "0.4.0", default-features=false }
llvm-sys-140 = { package = "llvm-sys", version = "140.1.3", features = ["prefer-static"], optional = true}
hugr = "0.4.0"
hugr = "0.5.1"
anyhow = "1.0.83"
itertools = "0.12.1"
delegate = "0.12.0"
Expand Down
4 changes: 2 additions & 2 deletions src/custom/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hugr::{
extension::{simple_op::MakeExtensionOp, ExtensionId},
ops::{constant::CustomConst, CustomOp, NamedOp},
std_extensions::arithmetic::{
int_ops::{self, IntOpType},
int_ops::{self, ConcreteIntOp},
int_types::{self, ConstInt},
},
types::{CustomType, TypeArg},
Expand All @@ -27,7 +27,7 @@ struct IntOpEmitter<'c, 'd, H: HugrView>(&'d mut EmitFuncContext<'c, H>);

impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for IntOpEmitter<'c, '_, H> {
fn emit(&mut self, args: EmitOpArgs<'c, CustomOp, H>) -> Result<()> {
let iot = IntOpType::from_optype(&args.node().generalise())
let iot = ConcreteIntOp::from_optype(&args.node().generalise())
.ok_or(anyhow!("IntOpEmitter from_optype_failed"))?;
match iot.name().as_str() {
"iadd" => {
Expand Down
5 changes: 2 additions & 3 deletions src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ impl<'c, H: HugrView> EmitOpArgs<'c, OpType, H> {
/// Specialise the internal [FatNode].
///
/// Panics if `OT` is not the `get_optype` of the internal [Node].
pub fn into_ot<OTInto: PartialEq + 'c>(self, ot: &'c OTInto) -> EmitOpArgs<'c, OTInto, H>
pub fn into_ot<'b, OTInto: PartialEq + 'c>(self, ot: &'b OTInto) -> EmitOpArgs<'c, OTInto, H>
where
&'c OpType: TryInto<&'c OTInto>,
<&'c OpType as TryInto<&'c OTInto>>::Error: std::fmt::Debug,
for<'a> &'a OpType: TryInto<&'a OTInto>,
{
let EmitOpArgs {
node,
Expand Down
4 changes: 4 additions & 0 deletions src/emit/func/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ impl<'c> ValuePromise<'c> {
pub struct RowMailBox<'c>(Rc<Vec<ValueMailBox<'c>>>, Cow<'static, str>);

impl<'c> RowMailBox<'c> {
pub fn new_empty() -> Self {
Self::new(std::iter::empty(), None)
}

pub(super) fn new(
mbs: impl IntoIterator<Item = ValueMailBox<'c>>,
name: Option<String>,
Expand Down
14 changes: 12 additions & 2 deletions src/emit/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hugr::{
hugr::views::SiblingGraph,
ops::{
Call, Case, Conditional, Const, Input, LoadConstant, MakeTuple, NamedOp, OpTag, OpTrait,
OpType, Output, Tag, UnpackTuple, Value,
OpType, Output, Tag, UnpackTuple, Value, CFG,
},
types::{SumType, Type, TypeEnum},
HugrView, NodeIndex,
Expand All @@ -20,6 +20,8 @@ use super::{
EmitOp, EmitOpArgs,
};

mod cfg;

struct SumOpEmitter<'c, 'd, H: HugrView>(&'d mut EmitFuncContext<'c, H>, LLVMSumType<'c>);

impl<'c, 'd, H: HugrView> SumOpEmitter<'c, 'd, H> {
Expand Down Expand Up @@ -228,7 +230,7 @@ impl<'c, H: HugrView> EmitOp<'c, Conditional, H> for ConditionalEmitter<'c, '_,
})
.collect::<Result<Vec<_>>>()?;

builder.build_switch(tag.into_int_value(), switches[0].1, &switches[1..])?;
builder.build_switch(tag, switches[0].1, &switches[1..])?;
builder.position_at_end(exit_block);
Ok(())
}
Expand Down Expand Up @@ -370,6 +372,13 @@ fn emit_call<'c, H: HugrView>(
args.outputs.finish(builder, rets)
}

fn emit_cfg<'c, H: HugrView>(
context: &mut EmitFuncContext<'c, H>,
args: EmitOpArgs<'c, CFG, H>,
) -> Result<()> {
cfg::CfgEmitter::new(context, args)?.emit_children()
}

fn emit_optype<'c, H: HugrView>(
context: &mut EmitFuncContext<'c, H>,
args: EmitOpArgs<'c, OpType, H>,
Expand All @@ -390,6 +399,7 @@ fn emit_optype<'c, H: HugrView>(
OpType::LoadConstant(ref lc) => emit_load_constant(context, args.into_ot(lc)),
OpType::Call(ref cl) => emit_call(context, args.into_ot(cl)),
OpType::Conditional(ref co) => emit_conditional(context, args.into_ot(co)),
OpType::CFG(ref cfg) => emit_cfg(context, args.into_ot(cfg)),

// OpType::FuncDefn(fd) => self.emit(ot.into_ot(fd), context, inputs, outputs),
_ => todo!("Unimplemented OpTypeEmitter: {}", args.node().name()),
Expand Down
Loading

0 comments on commit ab26584

Please sign in to comment.