Skip to content

Commit 450c398

Browse files
author
Joe Hellerstein
committed
feat: add --graph ir-json flag to serialize raw IR from examples
Add IrJson variant to GraphType enum so paxos and map_reduce examples can output serialized IR via --graph ir-json. Extract ir_to_json() helper in compile::ir for shared use by BuiltFlow::ir_json() and GraphApi::render(). Also adds a paxos_ir_json snapshot test in paxos_bench.
1 parent 4b66057 commit 450c398

6 files changed

Lines changed: 29608 additions & 2 deletions

File tree

hydro_lang/src/compile/built.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ impl<'a> BuiltFlow<'a> {
5050
}
5151

5252
/// Serialize the IR as JSON.
53-
#[cfg(feature = "serde_json")]
53+
#[cfg(feature = "viz")]
5454
pub fn ir_json(&self) -> Result<String, serde_json::Error> {
55-
super::ir::serialize_dedup_shared(|| serde_json::to_string_pretty(&self.ir))
55+
super::ir::ir_to_json(&self.ir)
5656
}
5757

5858
/// Returns all raw location ID -> location name mappings.

hydro_lang/src/compile/ir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,12 @@ pub fn serialize_dedup_shared<T>(f: impl FnOnce() -> T) -> T {
17161716
f()
17171717
}
17181718

1719+
/// Serialize a slice of [`HydroRoot`]s to a JSON string with shared-node deduplication.
1720+
#[cfg(feature = "viz")]
1721+
pub fn ir_to_json(ir: &[HydroRoot]) -> Result<String, serde_json::Error> {
1722+
serialize_dedup_shared(|| serde_json::to_string(ir))
1723+
}
1724+
17191725
/// RAII guard that saves/restores the `SERIALIZED_SHARED` thread-local,
17201726
/// making `serialize_dedup_shared` re-entrant and panic-safe.
17211727
struct SerializedSharedGuard {

hydro_lang/src/viz/api.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl<'a> GraphApi<'a> {
4040
crate::viz::config::GraphType::Mermaid => render_hydro_ir_mermaid(self.ir, config),
4141
crate::viz::config::GraphType::Dot => render_hydro_ir_dot(self.ir, config),
4242
crate::viz::config::GraphType::Json => render_hydro_ir_json(self.ir, config),
43+
crate::viz::config::GraphType::IrJson => {
44+
crate::compile::ir::ir_to_json(self.ir)
45+
.expect("failed to serialize IR to JSON")
46+
}
4347
}
4448
}
4549

hydro_lang/src/viz/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub enum GraphType {
99
Dot,
1010
/// JSON format for Hydroscope interactive viewer.
1111
Json,
12+
/// Raw serialized IR as JSON (full intermediate representation).
13+
IrJson,
1214
}
1315

1416
impl GraphType {
@@ -18,6 +20,7 @@ impl GraphType {
1820
GraphType::Mermaid => "mmd",
1921
GraphType::Dot => "dot",
2022
GraphType::Json => "json",
23+
GraphType::IrJson => "ir.json",
2124
}
2225
}
2326
}

hydro_test/src/cluster/paxos_bench.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,40 @@ mod tests {
240240
});
241241
}
242242

243+
#[test]
244+
fn paxos_ir_json() {
245+
let mut builder = hydro_lang::compile::builder::FlowBuilder::new();
246+
let proposers = builder.cluster();
247+
let acceptors = builder.cluster();
248+
let clients = builder.cluster();
249+
let client_aggregator = builder.process();
250+
let replicas = builder.cluster();
251+
252+
create_paxos(
253+
&proposers,
254+
&acceptors,
255+
&clients,
256+
&client_aggregator,
257+
&replicas,
258+
);
259+
let built = builder.finalize();
260+
261+
let json = hydro_lang::compile::ir::serialize_dedup_shared(|| {
262+
serde_json::to_string_pretty(built.ir()).unwrap()
263+
});
264+
265+
// Redact absolute paths for CI portability.
266+
let workspace_root = env!("CARGO_MANIFEST_DIR")
267+
.strip_suffix("/hydro_test")
268+
.or_else(|| env!("CARGO_MANIFEST_DIR").strip_suffix("\\hydro_test"))
269+
.unwrap_or(env!("CARGO_MANIFEST_DIR"));
270+
let json_escaped_root = workspace_root.replace('\\', "\\\\");
271+
let json = json.replace(&json_escaped_root, "[workspace]");
272+
let json = json.replace("\\\\", "/");
273+
274+
hydro_build_utils::assert_snapshot!(json);
275+
}
276+
243277
#[tokio::test]
244278
async fn paxos_some_throughput() {
245279
let mut builder = hydro_lang::compile::builder::FlowBuilder::new();

0 commit comments

Comments
 (0)