Skip to content

Commit

Permalink
Cider DAP Metadata Table Generation (#2197)
Browse files Browse the repository at this point in the history
Addresses issue #2183 
This branch implements a new compiler pass to generate the metadata
table for calyx-to-calyx debugging.

---------

Co-authored-by: Serena <[email protected]>
  • Loading branch information
smd21 and Serena authored Jul 15, 2024
1 parent 4956da0 commit 907bcd1
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 2 deletions.
7 changes: 5 additions & 2 deletions calyx-opt/src/default_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::passes::{
CompileSyncWithoutSyncReg, ComponentInliner, DataPathInfer,
DeadAssignmentRemoval, DeadCellRemoval, DeadGroupRemoval, DefaultAssigns,
DiscoverExternal, ExternalToRef, Externalize, GoInsertion, GroupToInvoke,
GroupToSeq, HoleInliner, InferShare, LowerGuards, MergeAssign, Papercut,
ParToSeq, RegisterUnsharing, RemoveIds, ResetInsertion,
GroupToSeq, HoleInliner, InferShare, LowerGuards, MergeAssign, Metadata,
Papercut, ParToSeq, RegisterUnsharing, RemoveIds, ResetInsertion,
SimplifyStaticGuards, SimplifyWithControl, StaticFSMOpts, StaticInference,
StaticInliner, StaticPromotion, SynthesisPapercut, TopDownCompileControl,
UnrollBounded, WellFormed, WireInliner, WrapMain,
Expand Down Expand Up @@ -76,6 +76,9 @@ impl PassManager {
pm.register_pass::<RemoveIds>()?;
pm.register_pass::<ExternalToRef>()?;

//add metadata
pm.register_pass::<Metadata>()?;

register_alias!(pm, "validate", [WellFormed, Papercut, Canonicalize]);
register_alias!(
pm,
Expand Down
91 changes: 91 additions & 0 deletions calyx-opt/src/passes/metadata_table_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir::Id;
use calyx_utils::WithPos;
use linked_hash_map::LinkedHashMap;
use std::fmt;
use std::path::PathBuf;

/// Metadata stores a Map between each group name and data used in the metadata table (specified in PR #2022)
#[derive(Default)]
pub struct Metadata {
groups: LinkedHashMap<Id, (usize, PathBuf)>,
}

impl Metadata {
/// Create an empty metadata table
pub fn new() -> Self {
Self::default()
}

/// Add a new entry to the metadata table
fn add_entry(&mut self, name: Id, line: usize, path: PathBuf) {
let ins = self.groups.insert(name, (line, path));
if let Some(_v) = ins {
panic!("Two of same group name found")
}
}
}

impl fmt::Display for Metadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let grps = &self.groups;

for (name, (line_num, file)) in grps {
let file = file.to_str().unwrap();
writeln!(f, " {name}: {file} {line_num}")?;
}

Ok(())
}
}

impl Named for Metadata {
fn name() -> &'static str {
"metadata-table-generation"
}
fn description() -> &'static str {
"generates metadata table for a file not containing one"
}
}

impl Visitor for Metadata {
//iterate over all groups in all components and collect metadata
fn start_context(&mut self, ctx: &mut calyx_ir::Context) -> VisResult {
if ctx.metadata.is_none() {
let mut table = Metadata::new();
for component in &ctx.components {
let cmpt_iter = component.groups.into_iter();
for rcc_grp in cmpt_iter {
let grp = rcc_grp.borrow_mut();
let pos_data = grp.attributes.copy_span();
let (file, line_num) = pos_data.get_line_num();
table.add_entry(grp.name(), line_num, PathBuf::from(file));
}

ctx.metadata = Some(table.to_string());
}
}
Ok(Action::Stop)
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use calyx_ir::Id;

use crate::passes::metadata_table_gen::Metadata;
#[test]
fn test_metadata() {
let mut data = Metadata::new();
let empt_string = data.to_string();
assert_eq!(empt_string, "");

let path = PathBuf::from("/temp/path/for/testing.futil");
data.add_entry(Id::from("group_1"), 12, path.clone());
data.add_entry(Id::from("group_2"), 23, path);
let test_string = data.to_string();
assert_eq!(test_string, " group_1: /temp/path/for/testing.futil 12\n group_2: /temp/path/for/testing.futil 23\n")
}
}
2 changes: 2 additions & 0 deletions calyx-opt/src/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod add_guard;
mod data_path_infer;
mod default_assigns;
mod discover_external;
mod metadata_table_gen;
mod simplify_with_control;
mod synthesis_papercut;
mod top_down_compile_control;
Expand Down Expand Up @@ -70,6 +71,7 @@ pub use infer_share::InferShare;
pub use lower_guards::LowerGuards;
pub use math_utilities::get_bit_width_from;
pub use merge_assign::MergeAssign;
pub use metadata_table_gen::Metadata;
pub use papercut::Papercut;
pub use par_to_seq::ParToSeq;
pub use register_unsharing::RegisterUnsharing;
Expand Down
11 changes: 11 additions & 0 deletions calyx-utils/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ impl GPosIdx {
(buf, out_idx, out_line)
}

/// returns:
/// 1. the name of the file the span is in
/// 2. the starting line of a span
pub fn get_line_num(&self) -> (&String, 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 (_, _, line_num) = self.get_lines();
(file_name, line_num)
}

/// Format this position with the error message `err_msg`
pub fn format_raw<S: AsRef<str>>(&self, err_msg: S) -> String {
let table = GlobalPositionTable::as_ref();
Expand Down
19 changes: 19 additions & 0 deletions tests/passes/metadata-table-gen.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "primitives/core.futil";
component main(@go go: 1, @clk clk: 1, @reset reset: 1) -> (@done done: 1) {
cells {}
wires {
group g1 {
}
group g2 {
}
group g3 {
}
}
control {}
}
metadata #{
g1: tests/passes/metadata-table-gen.futil 8
g2: tests/passes/metadata-table-gen.futil 9
g3: tests/passes/metadata-table-gen.futil 10

}#
13 changes: 13 additions & 0 deletions tests/passes/metadata-table-gen.futil
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -p metadata-table-generation

import "primitives/core.futil";

component main() -> () {
cells {}
wires {
group g1 {}
group g2 {}
group g3 {}
}
control {}
}

0 comments on commit 907bcd1

Please sign in to comment.