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

IO/FFI Standard #336

Merged
merged 5 commits into from
May 23, 2024
Merged

IO/FFI Standard #336

merged 5 commits into from
May 23, 2024

Conversation

VictorTaelin
Copy link
Member

@VictorTaelin VictorTaelin commented May 22, 2024

This adds a much simpler IO functionality, based on external FFI calls. The commit message describes how it works.

@developedby This also has a new demo_io file which should replace the current built-in IO on Bend. I'm calling it IO_T to avoid conflict, but it should be just IO. I've designed it in a way that won't need to be changed as we add more IOs (and should be extensive later). There is also the issue of Bend consuming the output. Perhaps that's a moment for us to rework the output into something cleaner?

@enricozb Could you kindly review and implement on CUDA? The old commit has most of the core functions needed (like reading needed data from GPU memory to CPU). So you can use these to implement the new standard.

IO works by a simple a monadic FFI interface:

type IO:
  Done { magic, expr }
  Call { magic, func, argm, cont }

It provides a minimal mechanism to call an external function ('func')
with a given Port argument ('argm') and a Port->Port continuation
('cont'). The function is just a name, which links to a native function
with type:

Port (*func)(Net*, Book*, Port);

This is called from inside `do_run_io`, which is now called on main,
instead of `normalize`. The 'magic' field is a constant, which we use to
tell whether a given program is meant to do IO, without needing external
flags or pragmas. If the normal form of the program is an ADT with the
magic word, we treat it is an IO. Otherwise, we just normalize it.

void do_run_io(Net* net, Book* book, Port port) {
  // IO loop
  while (TRUE) {
    // Normalizes the net
    normalize(net, book);

    // Reads the λ-Encoded Ctr
    Ctr ctr = read_ctr(net, book, peek(net, port));

    // Checks if IO Magic Number is a CON
    ...

    // Dispatches to the foreign function
    switch (ctr.tag) {
      case IO_CALL: ... call ...
      case IO_DONE: ... stop ...
    }
  }
}

Right now, we provide some builtin IOs, but this is meant to be
extensible with user-defined fns in a future extension.

This hasn't been implemented in CUDA yet.

SDL (and graphics) is still disabled.
@developedby
Copy link
Member

What I've thought of doing to print IO but not the hvm result output is having HVM end with a very unique magic keyword.

On Bend's side I can buffer stdout a little bit to check for a new line followed by that magic keyword and capture only from that line onwards (to also capture any additional information like runtime stats).

It could be for example "\nHVM Result:", but it has to be something a user would never accidentally type.

@VictorTaelin VictorTaelin merged commit fe8cbe7 into main May 23, 2024
1 of 2 checks passed
@developedby developedby mentioned this pull request Aug 20, 2024
2 tasks
@kings177 kings177 added this to the HVM IO lib v0 milestone Aug 20, 2024
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.

3 participants