Skip to content

Commit

Permalink
Add new tool to obtain names of groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakayorihiro committed Oct 21, 2024
1 parent 8652faa commit 27a91ac
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"tools/calyx-pass-explorer",
"tools/cider-data-converter",
"tools/component_cells",
"tools/component_groups",
"tools/yxi",
"tools/calyx-writer",
]
Expand Down
28 changes: 28 additions & 0 deletions tools/component_groups/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "component_groups"
authors.workspace = true
license-file.workspace = true
keywords.workspace = true
repository.workspace = true
readme.workspace = true
description.workspace = true
categories.workspace = true
homepage.workspace = true
edition.workspace = true
version.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde.workspace = true
argh.workspace = true
serde_json = "1.0.79"

calyx-utils = { path = "../../calyx-utils" }
calyx-frontend = { path = "../../calyx-frontend" }
calyx-opt = { path = "../../calyx-opt" }

[dependencies.calyx-ir]
path = "../../calyx-ir"
features = ["serialize"]
99 changes: 99 additions & 0 deletions tools/component_groups/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use argh::FromArgs;
use calyx_frontend as frontend;
use calyx_ir::{self as ir, Id};
use calyx_utils::{CalyxResult, OutputFile};
use serde::Serialize;
use std::path::{Path, PathBuf};
use std::{collections::HashSet, io};

#[derive(FromArgs)]
/// Path for library and path for file to read from
struct Args {
/// file path to read data from
#[argh(positional, from_str_fn(read_path))]
file_path: Option<PathBuf>,

/// library path
#[argh(option, short = 'l', default = "Path::new(\".\").into()")]
pub lib_path: PathBuf,

/// output file
#[argh(option, short = 'o', default = "OutputFile::Stdout")]
pub output: OutputFile,
}

fn read_path(path: &str) -> Result<PathBuf, String> {
Ok(Path::new(path).into())
}

fn main() -> CalyxResult<()> {
let p: Args = argh::from_env();

let ws = frontend::Workspace::construct(&p.file_path, &p.lib_path)?;

let ctx: ir::Context = ir::from_ast::ast_to_ir(ws)?;

let main_comp = ctx.entrypoint();

let mut component_info: HashSet<ComponentInfo> = HashSet::new();
gen_component_info(&ctx, main_comp, true, &mut component_info);
write_json(component_info.clone(), p.output)?;
Ok(())
}

fn id_serialize_passthrough<S>(id: &Id, ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
id.to_string().serialize(ser)
}

#[derive(PartialEq, Eq, Hash, Clone, Serialize)]
struct ComponentInfo {
#[serde(serialize_with = "id_serialize_passthrough")]
pub component: Id,
pub is_main_component: bool,
pub groups: Vec<Id>, // list of all groups in the component
}

/// Accumulates a set of components to the groups that they contain
/// in the program with entrypoint `main_comp`.
fn gen_component_info(
ctx: &ir::Context,
comp: &ir::Component,
is_main_comp: bool,
component_info: &mut HashSet<ComponentInfo>,
) {
let curr_comp_info = ComponentInfo {
component: comp.name,
is_main_component: is_main_comp,
groups: comp
.groups
.into_iter()
.map(|g| g.borrow().name())
.collect::<Vec<_>>(),
};
// recurse into any other cells
for cell in comp.cells.iter() {
let cell_ref = cell.borrow();
if let ir::CellType::Component { name } = cell_ref.prototype {
let component = ctx
.components
.iter()
.find(|comp| comp.name == name)
.unwrap();
gen_component_info(ctx, component, false, component_info);
}
}
component_info.insert(curr_comp_info);
}

/// Write the collected set of component information to a JSON file.
fn write_json(
component_info: HashSet<ComponentInfo>,
file: OutputFile,
) -> Result<(), io::Error> {
let created_vec: Vec<ComponentInfo> = component_info.into_iter().collect();
serde_json::to_writer_pretty(file.get_write(), &created_vec)?;
Ok(())
}

0 comments on commit 27a91ac

Please sign in to comment.