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

modify compilation strategy to allow for io defs #125

Merged
merged 5 commits into from
Apr 19, 2024

Conversation

enricozb
Copy link
Contributor

fixes #111.

Reorganizes the compilation source so that user-defined defs and stdlib defs are all in the same map, so they can be treated equally when compiling instructions.

Empty defs are created and hoisted to the top of insert_into_host to allow for mutually recursive definitions.

Compiled source looks like this:

#![allow(non_upper_case_globals, unused_imports)]
use crate::{host::{Host, DefRef}, stdlib::{AsHostedDef, HostedDef}, run::*, ops::{TypedOp, Ty::*, Op::*}};

pub fn insert_into_host(host: &mut Host) {
  host.insert_def(r#"sub"#, unsafe { HostedDef::<Def_sub>::new(LabSet::from_bits(&[0x1])) });
  host.insert_def(r#"div"#, unsafe { HostedDef::<Def_div>::new(LabSet::from_bits(&[0x1])) });
  host.insert_def(r#"mul"#, unsafe { HostedDef::<Def_mul>::new(LabSet::from_bits(&[0x1])) });
  host.insert_def(r#"mod"#, unsafe { HostedDef::<Def_mod>::new(LabSet::from_bits(&[0x1])) });
  host.insert_def(r#"main"#, unsafe { HostedDef::<Def_main>::new(LabSet::from_bits(&[0x2b])) });
  host.insert_def(r#"add"#, unsafe { HostedDef::<Def_add>::new(LabSet::from_bits(&[0x1])) });

  let def_div = Port::new_ref(&host.defs[r#"div"#]);
  let def_mod = Port::new_ref(&host.defs[r#"mod"#]);

  host.get_mut::<HostedDef<Def_sub>>(r#"sub"#).data.0 = Def_sub {  };
  host.get_mut::<HostedDef<Def_div>>(r#"div"#).data.0 = Def_div {  };
  host.get_mut::<HostedDef<Def_mul>>(r#"mul"#).data.0 = Def_mul {  };
  host.get_mut::<HostedDef<Def_mod>>(r#"mod"#).data.0 = Def_mod {  };
  host.get_mut::<HostedDef<Def_main>>(r#"main"#).data.0 = Def_main { def_div, def_mod };
  host.get_mut::<HostedDef<Def_add>>(r#"add"#).data.0 = Def_add {  };
}

#[derive(Default)]
struct Def_sub {
  
}

impl AsHostedDef for Def_sub {
  fn call<M: Mode>(slf: &Def<Self>, net: &mut Net<M>, port: Port) {
    let t0 = Trg::port(port);
    let (t1, t2) = net.do_ctr(0, t0);
    let (t3, t4) = net.do_op(TypedOp { ty: U60, op: Sub }, t1);
    let (t5, t6) = net.do_ctr(0, t2);
    net.link_trg(t3, t5);
    net.link_trg(t4, t6);
  }
}

// ...

#[derive(Default)]
struct Def_main {
  def_div: Port,
  def_mod: Port
}

impl AsHostedDef for Def_main {
  fn call<M: Mode>(slf: &Def<Self>, net: &mut Net<M>, port: Port) {
    let t0 = Trg::port(port);
    let (t1, t2) = net.do_ctr(0, t0);
    let (t3, t4) = net.do_ctr(3, t1);
    let (t5, t6) = net.do_ctr(0, t2);
    let (t7, t8) = net.do_ctr(5, t5);
    let (t9, t10) = net.do_ctr(1, t6);
    let t11 = Trg::port(slf.data.def_mod.clone());
    let (t12, t13) = net.do_ctr(0, t11);
    net.link_trg(t4, t12);
    let (t14, t15) = net.do_ctr(0, t13);
    net.link_trg(t8, t14);
    net.link_trg(t10, t15);
    let t16 = Trg::port(slf.data.def_div.clone());
    let (t17, t18) = net.do_ctr(0, t16);
    net.link_trg(t3, t17);
    let (t19, t20) = net.do_ctr(0, t18);
    net.link_trg(t7, t19);
    net.link_trg(t9, t20);
  }
}

reorganizes the compilation source so that user-defined defs
and stdlib defs are all in the same map, so they can be treated
equally when compiling instructions.

empty defs are created and hoisted to the top of `insert_into_host`
to allow for mutually recursive definitions.
@HigherOrderBot

This comment has been minimized.

@enricozb enricozb marked this pull request as ready for review April 19, 2024 14:32
@enricozb enricozb requested a review from tjjfvi April 19, 2024 14:32
@HigherOrderBot

This comment has been minimized.

Copy link
Contributor

@tjjfvi tjjfvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it'd make the codegen code cleaner at all to have something like

struct DefInfo<'h> {
  rust_name: String,
  def: &'h InterpretedDef,
  refs: BTreeSet<&'h str>,
}

and create a defs: BTreeMap<&'h str, DefInfo<'h>> at the beginning instead of the iterator

src/compile.rs Outdated Show resolved Hide resolved
@HigherOrderBot

This comment has been minimized.

src/compile.rs Outdated Show resolved Hide resolved
@HigherOrderBot
Copy link
Collaborator

Perf run for 01481a5:

file              mode        main          01481a582a57
========================================================
sum_tree          intr-singl      10.231 s      10.385 s
                  intr-multi       4.772 s       4.846 s
--------------------------------------------------------
sum_rec           intr-singl      14.658 s      15.348 s
                  intr-multi       7.472 s       7.579 s
--------------------------------------------------------
sum_tail          intr-singl       1.291 s       1.273 s
                  intr-multi       1.226 s       1.233 s
--------------------------------------------------------
c2                intr-singl       0.000 s       0.000 s
                  intr-multi       0.001 s       0.001 s
--------------------------------------------------------
boom              intr-singl       2.233 s       2.315 s
                  intr-multi       2.291 s       3.207 s
--------------------------------------------------------
bitonic_sort_lam  intr-singl      11.955 s      11.980 s
                  intr-multi       5.788 s       5.629 s
--------------------------------------------------------
merge_sort        intr-singl       7.405 s       7.749 s
                  intr-multi       3.818 s       3.869 s
--------------------------------------------------------
radix_sort_lam    intr-singl      10.031 s      10.137 s
                  intr-multi       4.939 s       4.958 s

@enricozb enricozb added this pull request to the merge queue Apr 19, 2024
Merged via the queue into main with commit 90e3b44 Apr 19, 2024
6 checks passed
@enricozb enricozb deleted the enricozb/compile-io-defs branch April 19, 2024 22:07
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 this pull request may close these issues.

support IO in compiled binaries
3 participants