Skip to content

Commit

Permalink
Merge branch 'main' into profiler-probe-attempt2
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakayorihiro committed Dec 4, 2024
2 parents 1fb49e4 + 445e174 commit f8d14e0
Show file tree
Hide file tree
Showing 51 changed files with 2,134 additions and 755 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
uses: actions/checkout@v4
- name: Install Rust stable
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
- name: Build calyx dev
run: cargo build
- name: Check calyx build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ frontends/queues/tests/**/*.expect

# emacs
*~

!docs/dev/assets/*.png
21 changes: 19 additions & 2 deletions Cargo.lock

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

14 changes: 10 additions & 4 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ impl CalyxParser {
let file = GlobalPositionTable::as_mut()
.add_file(path.to_string_lossy().to_string(), string_content);
let user_data = UserData { file };
let content = GlobalPositionTable::as_ref().get_source(file);
let gpos_ref = GlobalPositionTable::as_ref();
let content = gpos_ref.get_source(file).to_owned();
drop(gpos_ref);

// Parse the file
let inputs =
CalyxParser::parse_with_userdata(Rule::file, content, user_data)
CalyxParser::parse_with_userdata(Rule::file, &content, user_data)
.map_err(|e| e.with_path(&path.to_string_lossy()))
.map_err(|e| {
calyx_utils::Error::parse_error(e.variant.message())
Expand Down Expand Up @@ -96,10 +99,13 @@ impl CalyxParser {
let file =
GlobalPositionTable::as_mut().add_file("<stdin>".to_string(), buf);
let user_data = UserData { file };
let contents = GlobalPositionTable::as_ref().get_source(file);
let gpos_ref = GlobalPositionTable::as_ref();
let content = gpos_ref.get_source(file).to_owned();
drop(gpos_ref);

// Parse the input
let inputs =
CalyxParser::parse_with_userdata(Rule::file, contents, user_data)
CalyxParser::parse_with_userdata(Rule::file, &content, user_data)
.map_err(|e| {
calyx_utils::Error::parse_error(e.variant.message())
.with_pos(&Self::error_span(&e, file))
Expand Down
1 change: 1 addition & 0 deletions calyx-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ string-interner.workspace = true
itertools.workspace = true
petgraph.workspace = true
symbol_table = { version = "0.3", features = ["global"] }
lazy_static.workspace = true
2 changes: 1 addition & 1 deletion calyx-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl Error {
post_msg: None,
}
}
pub fn location(&self) -> (&str, usize, usize) {
pub fn location(&self) -> (String, usize, usize) {
self.pos.get_location()
}
pub fn message(&self) -> String {
Expand Down
63 changes: 36 additions & 27 deletions calyx-utils/src/position.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//! Definitions for tracking source position information of Calyx programs
use itertools::Itertools;
use std::{cmp, fmt::Write, mem, sync};
use lazy_static::lazy_static;
use std::{
cmp,
fmt::Write,
sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
};

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// Handle to a position in a [PositionTable]
Expand Down Expand Up @@ -99,28 +104,28 @@ impl PositionTable {
/// The global position table
pub struct GlobalPositionTable;

lazy_static! {
static ref GPOS_TABLE: RwLock<PositionTable> =
RwLock::new(PositionTable::default());
}

impl GlobalPositionTable {
/// Return reference to a global [PositionTable]
pub fn as_mut() -> &'static mut PositionTable {
static mut SINGLETON: mem::MaybeUninit<PositionTable> =
mem::MaybeUninit::uninit();
static ONCE: sync::Once = sync::Once::new();

// SAFETY:
// - writing to the singleton is OK because we only do it one time
// - the ONCE guarantees that SINGLETON is init'ed before assume_init_ref
unsafe {
ONCE.call_once(|| {
SINGLETON.write(PositionTable::new());
assert!(PositionTable::UNKNOWN == GPosIdx::UNKNOWN.0)
});
SINGLETON.assume_init_mut()
}
/// Return reference to a global [PositionTable].
///
/// # Safety
///
/// You may not call this function after any call to [`Self::as_ref`].
pub fn as_mut() -> RwLockWriteGuard<'static, PositionTable> {
GPOS_TABLE
.try_write()
.expect("failed to get write lock for global position table")
}

/// Return an immutable reference to the global position table
pub fn as_ref() -> &'static PositionTable {
Self::as_mut()
pub fn as_ref() -> RwLockReadGuard<'static, PositionTable> {
GPOS_TABLE
.try_read()
.expect("failed to get read lock for global position table")
}
}

Expand Down Expand Up @@ -152,7 +157,7 @@ impl GPosIdx {
/// 1. lines associated with this span
/// 2. start position of the first line in span
/// 3. line number of the span
fn get_lines(&self) -> (Vec<&str>, usize, usize) {
fn get_lines(&self) -> (Vec<String>, usize, usize) {
let table = GlobalPositionTable::as_ref();
let pos_d = table.get_pos(self.0);
let file = &table.get_file_data(pos_d.file).source;
Expand Down Expand Up @@ -181,16 +186,20 @@ impl GPosIdx {
pos = next_pos + 1;
linum += 1;
}
(buf, out_idx, out_line)
(
buf.into_iter().map(|str| str.to_owned()).collect(),
out_idx,
out_line,
)
}

/// returns:
/// 1. the name of the file the span is in
/// 2. the (inclusive) range of lines within the span
pub fn get_line_num(&self) -> (&String, (usize, usize)) {
pub fn get_line_num(&self) -> (String, (usize, usize)) {
let table = GlobalPositionTable::as_ref();
let pos_data = table.get_pos(self.0);
let file_name = &table.get_file_data(pos_data.file).name;
let file_name = table.get_file_data(pos_data.file).name.clone();
let (buf, _, line_num) = self.get_lines();
//reformat to return the range (inclusive)
let rng = (line_num, line_num + buf.len() - 1);
Expand All @@ -205,7 +214,7 @@ impl GPosIdx {
let (lines, pos, linum) = self.get_lines();
let mut buf = String::new();

let l = lines[0];
let l = lines[0].as_str();
let linum_text = format!("{} ", linum);
let linum_space: String = " ".repeat(linum_text.len());
let mark: String = "^".repeat(cmp::min(
Expand Down Expand Up @@ -238,17 +247,17 @@ impl GPosIdx {
buf
}

pub fn get_location(&self) -> (&str, usize, usize) {
pub fn get_location(&self) -> (String, usize, usize) {
let table = GlobalPositionTable::as_ref();
let pos_d = table.get_pos(self.0);
let name = &table.get_file_data(pos_d.file).name;
let name = table.get_file_data(pos_d.file).name.clone();
(name, pos_d.start, pos_d.end)
}

/// Visualizes the span without any message or marking
pub fn show(&self) -> String {
let (lines, _, linum) = self.get_lines();
let l = lines[0];
let l = lines[0].as_str();
let linum_text = format!("{} ", linum);
format!("{}|{}\n", linum_text, l)
}
Expand Down
9 changes: 8 additions & 1 deletion docs/compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ For example, the alias `all` is an ordered sequence of default passes executed
when the compiler is run from the command-line.

The command-line provides two options to control the execution of passes:

- `-p, --pass`: Execute this pass or alias. Overrides default alias.
- `-d, --disable-pass`: Disable this pass or alias. Takes priority over `-p`.

Expand All @@ -43,6 +44,10 @@ the default execution alias `all`:
cargo run -- examples/futil/simple.futil -p all -d static-timing
```

If you want to work with passes interactively (for instance, you only care about
a pass far into the `all` sequence, and it is impractical to pass 20 `-p`
options), you can [visualize them](./dev/calyx-pass-explorer.md) with the `calyx-pass-explorer` tool.

## Providing Pass Options

Some passes take options to control their behavior. The `--list-passes` command prints out the options for each pass. For example, the `tdcc` pass has the following options:
Expand All @@ -53,23 +58,25 @@ tdcc: <description>
```

The option allows us to change the behavior of the pass. To provide a pass-specific option, we use the `-x` switch:

```
cargo run -- examples/futil/simple.futil -p tdcc -x tdcc:dump-fsm
```

Note that we specify the option of `tdcc` by prefixing it with the pass name and a colon.


## Specifying Primitives Library

The compiler implementation uses a standard library of components to compile
programs.
The only standard library for the compiler is located in:

```
<path to Calyx repository>/primitives
```

Specify the location of the library using the `-l` flag:

```
cargo run -- -l ./primitives
```
Expand Down
Binary file added docs/dev/assets/calyx-missing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/dev/assets/horrific-interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/dev/assets/well-formed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions docs/dev/calyx-pass-explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ is just to run it on code and see what happens.
Enter [`calyx-pass-explorer`](https://github.com/calyxir/calyx/tree/main/tools/calyx-pass-explorer).
It's a command line tool that provides an interactive interface for visualizing
how different passes affect the source code.
It's been used to debug and develop new compiler passes as well as implement new features in the compiler, so I hope you can find it useful too!

> ![Example running of the tool](https://raw.githubusercontent.com/calyxir/calyx/main/tools/calyx-pass-explorer/example_v0.0.0.png)
> _The above image depicts the tool's interface in v0.0.0.
Expand Down Expand Up @@ -67,11 +68,11 @@ tool to help develop it!
We'll first run `calyx-pass-explorer example0.futil`.
You should get something horrific like

![Lots of random text output that doesn't make sense](./assets/horrific-interface.png)
![Lots of random text output that doesn't make sense](assets/horrific-interface.png)

> [!TIP]
> If you get this message:
> ![Calyx executable could not be found](./assets/calyx-missing.png)
> ![Calyx executable could not be found](assets/calyx-missing.png)
> You should setup `fud` or pass the path explicitly with `-e`, as suggested.
> However, we're going to update this later to look at `fud2` as well because
> `fud` is now officially deprecated.
Expand All @@ -82,7 +83,7 @@ What we really want is to focus on what happens to, _e.g._, the `main` component
To do that, we just pass `-c main` (or `--component main`) as a flag:

![Running the tool and visualizing how the well-formed pass affects the main
component](./assets/well-formed.png)
component](assets/well-formed.png)

That's a lot better, but it's still quite a bit of information.
Let's break it down.
Expand Down
27 changes: 25 additions & 2 deletions frontends/queues/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# Queues Library

See the [docs](https://docs.calyxir.org/frontends/queues.html) for more details.
See the [docs][docs] for more details.

## Installation
To use our queues:
1. Install [flit](https://flit.readthedocs.io/en/latest/#install)
1. Install [flit][flit]
2. Install the `queues` package:
```
$ cd frontends/queues/
$ flit install --symlink
```

## Converting Tests to Calyx

To convert any of our [randomized tests][testing-harness] to a single Calyx file and their associated data and expect files:

0. Follow the [installation instructions](#installation)
1. Choose a test by picking a `.py` file in [`tests/`][tests-dir]
2. Convert the test to Calyx:
```
python3 <queue_name>_test.py 20000 --keepgoing > <queue_name>_test.futil
```
3. Run the script [`gen_test_data.sh`][gen_test_data.sh] to generate data and expect files:
```
./gen_test_data.sh
```

The files `<queue_name>_test.py`, `<queue_name>_test.data`, and `<queue_name>_test.expect` contain the Calyx program, input data, and expected outputs for the test.

[docs]: https://docs.calyxir.org/frontends/queues.html
[flit]: https://flit.readthedocs.io/en/latest/#install
[testing-harness]: https://docs.calyxir.org/frontends/queues.html#shared-testing-harness
[tests-dir]: ./tests/
[gen_test_data.sh]: ./test_data_gen/gen_test_data.sh
1 change: 0 additions & 1 deletion interp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ path = "src/main.rs"
[dependencies]
smallvec = { workspace = true, features = ["union", "const_generics"] }
serde = { workspace = true, features = ["derive", "rc"] }
lazy_static.workspace = true
itertools.workspace = true
pest.workspace = true
pest_derive.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions interp/src/as_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ impl<T> AsRaw<T> for *const T {
}
}

impl<'a, T> AsRaw<T> for &Ref<'a, T> {
impl<T> AsRaw<T> for &Ref<'_, T> {
fn as_raw(&self) -> *const T {
self as &T as *const T
}
}
impl<'a, T> AsRaw<T> for Ref<'a, T> {
impl<T> AsRaw<T> for Ref<'_, T> {
fn as_raw(&self) -> *const T {
self as &T as *const T
}
Expand Down
Loading

0 comments on commit f8d14e0

Please sign in to comment.