Skip to content

Commit 0c06005

Browse files
committed
feat: improve visualization
1 parent e0ed6b5 commit 0c06005

File tree

7 files changed

+71
-24
lines changed

7 files changed

+71
-24
lines changed

zhc_ir/src/visualization/composition/model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ pub use D7::E7 as NodeGroupOutputPortVar;
8383
/// Horizontal row of nodes forming a diagram layer.
8484
pub type Layer = HStack<Node, LayerClass>;
8585

86-
pub type LayerSeparator = Spacer<LayerClass>;
86+
pub type LayerSeparator = Spacer<LayerSpacerClass>;
8787

8888
pub type LayerMember = D2<Layer, LayerSeparator>;
8989
pub use D2::E1 as LayerMemberLayer;
9090
pub use D2::E2 as LayerMemberSeparator;
9191

9292
/// All the diagram layers
93-
pub type Layers = VStack<LayerMember, VerticesClass>;
93+
pub type Layers = VStack<LayerMember, LayersClass>;
9494

9595
/// Content inside a group element (uses smaller padding/spacing than top-level Vertices).
9696
pub type GroupContent = Layers;

zhc_ir/src/visualization/composition/solver.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use std::cmp::{max_by, min_by};
2+
13
use zhc_utils::{
2-
FastMap,
3-
graphics::{Frame, Height, Position},
4-
iter::CollectInSmallVec,
5-
svec,
4+
FastMap, graphics::{Frame, Height, Position}, iter::{CollectInSmallVec, Interleave, Slide, SliderExt}, svec
65
};
76

87
use crate::{
@@ -48,17 +47,49 @@ fn gen_layers<'ir, 'ann>(
4847
order.sort_unstable_by_key(|(place, _)| *place);
4948
let (variable, assocs) = gen_layer(ir.walk_ops_with(order.into_iter().map(|(_, id)| id)));
5049
variables.push(LayerMemberLayer(variable));
51-
let links_out = ir
52-
.walk_ops_with(layer.walker())
53-
.flat_map(|op| op.get_returns_iter().flat_map(|v| v.get_uses_iter()))
54-
.count();
55-
let sep_height = Height::new(10. * links_out as f64);
56-
variables.push(LayerMemberSeparator(LayerSeparator::vertical(sep_height)));
5750
assocs.into_iter().for_each(|(k, v)| {
5851
opmap.insert(k, v);
5952
});
6053
}
61-
variables.pop();
54+
let variables = variables
55+
.into_iter()
56+
.interleave_with(
57+
layers_map
58+
.iter_layers()
59+
.slide::<2>()
60+
.skip_noncompletes()
61+
.map(|layers| {
62+
let [layer_before, layer_after] = layers.unwrap_complete().into_array();
63+
let is_group_input_sep = ir.walk_ops_with(layer_before.walker()).all(|op| {
64+
matches!(
65+
op.get_instruction(),
66+
LayoutInstructionSet::GroupInput { .. }
67+
)
68+
});
69+
let is_group_output_sep = ir.walk_ops_with(layer_after.walker()).all(|op| {
70+
matches!(
71+
op.get_instruction(),
72+
LayoutInstructionSet::GroupOutput { .. }
73+
)
74+
});
75+
let width_before = layer_before.walker().count() as f64;
76+
let width_after = layer_after.walker().count() as f64;
77+
let traffic = ir
78+
.walk_ops_with(layer_after.walker())
79+
.map(|op| op.get_args_arity())
80+
.sum::<usize>() as f64;
81+
let width_min = min_by(width_before, width_after, |a, b| a.partial_cmp(b).unwrap());
82+
let width_max = max_by(width_before, width_after, |a, b| a.partial_cmp(b).unwrap());
83+
let spread = (width_max/width_min.max(1.)).max(1.).sqrt();
84+
let sep_height = if is_group_input_sep | is_group_output_sep {
85+
Height::new(0.)
86+
} else {
87+
Height::new(20.0 + 10.0 * traffic * spread )
88+
};
89+
LayerMemberSeparator(LayerSeparator::vertical(sep_height))
90+
}),
91+
)
92+
.collect();
6293
(
6394
Layers::new(variables),
6495
AnnIR::new(ir.ir, opmap, ir.filled_valmap(())),
@@ -222,10 +253,16 @@ fn gen_group_node<'ir, 'ann>(
222253

223254
for op in ir.walk_ops_linear() {
224255
match (op.get_instruction(), op.get_annotation()) {
225-
(LayoutInstructionSet::GroupInput { .. }, PlacementSolution::NonGroup { op: lp }) => {
256+
(
257+
LayoutInstructionSet::GroupInput { .. },
258+
PlacementSolution::NonGroup { op: lp, .. },
259+
) => {
226260
group_inputs.push((*lp, op.get_id()));
227261
}
228-
(LayoutInstructionSet::GroupOutput { .. }, PlacementSolution::NonGroup { op: lp }) => {
262+
(
263+
LayoutInstructionSet::GroupOutput { .. },
264+
PlacementSolution::NonGroup { op: lp, .. },
265+
) => {
229266
group_outputs.push((*lp, op.get_id()));
230267
}
231268
_ => {}

zhc_ir/src/visualization/composition/stylesheet.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ impl StyleSheet {
139139
});
140140
output.insert::<LayerSpacerClass>(Style {
141141
hjustify: Justify::Space,
142+
padding: Thickness::new(0.),
143+
spacing: Thickness::new(0.),
142144
..Default::default()
143145
});
144-
output.insert::<VerticesClass>(Style {
146+
output.insert::<LayersClass>(Style {
145147
padding: Thickness::new(10.),
146-
spacing: Thickness::new(10.),
148+
spacing: Thickness::new(0.),
147149
..Default::default()
148150
});
149151
output.insert::<LinkClass>(Style {
@@ -255,8 +257,8 @@ impl Class for LayerClass {}
255257
pub struct LayerSpacerClass;
256258
impl Class for LayerSpacerClass {}
257259

258-
pub struct VerticesClass;
259-
impl Class for VerticesClass {}
260+
pub struct LayersClass;
261+
impl Class for LayersClass {}
260262

261263
pub struct LinkClass;
262264
impl Class for LinkClass {}

zhc_ir/src/visualization/layoutlang/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl StackFrame {
160160
let (opid, rets) = self.ir.add_op(
161161
LayoutInstructionSet::Operation {
162162
opid: op.id,
163-
op: OpContent::from_op(op, &FormatContext::new()),
163+
op: OpContent::from_op(op, &FormatContext::new().show_types(true)),
164164
args: op.get_arg_valids().iter().cloned().collect(),
165165
returns: op.get_return_valids().iter().cloned().collect(),
166166
},

zhc_ir/src/visualization/layoutlang/instruction_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl OpContent {
2626
.get_returns_iter()
2727
.map(|a| a.fmt_to_string(ctx))
2828
.collect(),
29-
call: opref.fmt_to_string(ctx),
30-
comment: opref.comment.clone(),
29+
call: opref.fmt_to_string(&ctx.clone().show_comments(false).show_types(false)),
30+
comment: None,
3131
}
3232
}
3333
}

zhc_ir/src/visualization/placement/annotation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub enum PlacementSolution {
194194
impl PlacementSolution {
195195
pub fn get_place(&self) -> Place {
196196
match self {
197-
PlacementSolution::NonGroup { op } | PlacementSolution::Group { op, .. } => *op,
197+
PlacementSolution::NonGroup { op, .. } | PlacementSolution::Group { op, .. } => *op,
198198
}
199199
}
200200
}
@@ -212,7 +212,9 @@ fn positions_to_ranks(positions: SmallVec<Place>) -> SmallVec<Place> {
212212

213213
fn resolve(input: OpMap<PlacementVariable>) -> OpMap<PlacementSolution> {
214214
input.map(|v| match v {
215-
PlacementVariable::NonGroup { op, .. } => PlacementSolution::NonGroup { op: op.get_val() },
215+
PlacementVariable::NonGroup { op, .. } => PlacementSolution::NonGroup {
216+
op: op.get_val(),
217+
},
216218
PlacementVariable::Group {
217219
op,
218220
maps,

zhc_utils/src/dump.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ impl<A: Dumpable, B: Dumpable> Dumpable for (A, B) {
188188
}
189189
}
190190

191+
impl<A: Dumpable, B: Dumpable, C: Dumpable> Dumpable for (A, B, C) {
192+
fn dump_to_string(&self) -> String {
193+
format!("({}, {}, {})", self.0.dump_to_string(), self.1.dump_to_string(), self.2.dump_to_string())
194+
}
195+
}
196+
191197
macro_rules! impl_dumpable_via_display {
192198
($($t:ty),* $(,)?) => {
193199
$(

0 commit comments

Comments
 (0)