-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathbuilt.rs
More file actions
362 lines (323 loc) · 10.5 KB
/
built.rs
File metadata and controls
362 lines (323 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use std::marker::PhantomData;
use dfir_lang::graph::{
DfirGraph, FlatGraphBuilderOutput, eliminate_extra_unions_tees, partition_graph,
};
use slotmap::{SecondaryMap, SlotMap, SparseSecondaryMap};
use super::compiled::CompiledFlow;
use super::deploy::{DeployFlow, DeployResult};
use super::deploy_provider::{ClusterSpec, Deploy, ExternalSpec, IntoProcessSpec};
use super::ir::{HydroRoot, emit};
use crate::location::{Cluster, External, LocationKey, LocationType, Process};
#[cfg(stageleft_runtime)]
#[cfg(feature = "sim")]
use crate::sim::{flow::SimFlow, graph::SimNode};
use crate::staging_util::Invariant;
#[cfg(stageleft_runtime)]
#[cfg(feature = "viz")]
use crate::viz::api::GraphApi;
pub struct BuiltFlow<'a> {
pub(super) ir: Vec<HydroRoot>,
pub(super) locations: SlotMap<LocationKey, LocationType>,
pub(super) location_names: SecondaryMap<LocationKey, String>,
/// Application name used in telemetry.
pub(super) flow_name: String,
pub(super) _phantom: Invariant<'a>,
}
pub(crate) fn build_inner<'a, D: Deploy<'a>>(
ir: &mut Vec<HydroRoot>,
) -> SecondaryMap<LocationKey, DfirGraph> {
emit::<D>(ir)
.into_iter()
.map(|(k, v)| {
let FlatGraphBuilderOutput { mut flat_graph, .. } =
v.build().expect("Failed to build DFIR flat graph.");
eliminate_extra_unions_tees(&mut flat_graph);
let partitioned_graph =
partition_graph(flat_graph).expect("Failed to partition (cycle detected).");
(k, partitioned_graph)
})
.collect()
}
impl<'a> BuiltFlow<'a> {
/// Returns all [`HydroRoot`]s in the IR.
pub fn ir(&self) -> &[HydroRoot] {
&self.ir
}
/// Returns all raw location ID -> location name mappings.
pub fn location_names(&self) -> &SecondaryMap<LocationKey, String> {
&self.location_names
}
/// Get a GraphApi instance for this built flow
#[cfg(stageleft_runtime)]
#[cfg(feature = "viz")]
pub fn graph_api(&self) -> GraphApi<'_> {
GraphApi::new(&self.ir, self.location_names())
}
// String generation methods
#[cfg(feature = "viz")]
pub fn mermaid_string(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> String {
self.graph_api()
.mermaid_to_string(show_metadata, show_location_groups, use_short_labels)
}
#[cfg(feature = "viz")]
pub fn dot_string(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> String {
self.graph_api()
.dot_to_string(show_metadata, show_location_groups, use_short_labels)
}
#[cfg(feature = "viz")]
pub fn hydroscope_string(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> String {
self.graph_api()
.hydroscope_to_string(show_metadata, show_location_groups, use_short_labels)
}
// File generation methods
#[cfg(feature = "viz")]
pub fn mermaid_to_file(
&self,
filename: &str,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().mermaid_to_file(
filename,
show_metadata,
show_location_groups,
use_short_labels,
)
}
#[cfg(feature = "viz")]
pub fn dot_to_file(
&self,
filename: &str,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().dot_to_file(
filename,
show_metadata,
show_location_groups,
use_short_labels,
)
}
#[cfg(feature = "viz")]
pub fn hydroscope_to_file(
&self,
filename: &str,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().hydroscope_to_file(
filename,
show_metadata,
show_location_groups,
use_short_labels,
)
}
// Browser generation methods
#[cfg(feature = "viz")]
pub fn mermaid_to_browser(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
message_handler: Option<&dyn Fn(&str)>,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().mermaid_to_browser(
show_metadata,
show_location_groups,
use_short_labels,
message_handler,
)
}
#[cfg(feature = "viz")]
pub fn dot_to_browser(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
message_handler: Option<&dyn Fn(&str)>,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().dot_to_browser(
show_metadata,
show_location_groups,
use_short_labels,
message_handler,
)
}
#[cfg(feature = "viz")]
pub fn hydroscope_to_browser(
&self,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
message_handler: Option<&dyn Fn(&str)>,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().hydroscope_to_browser(
show_metadata,
show_location_groups,
use_short_labels,
message_handler,
)
}
pub fn optimize_with(mut self, f: impl FnOnce(&mut [HydroRoot])) -> Self {
f(&mut self.ir);
self
}
pub fn with_default_optimize<D: Deploy<'a>>(self) -> DeployFlow<'a, D> {
self.into_deploy()
}
#[cfg(feature = "sim")]
/// Creates a simulation for this builder, which can be used to run deterministic simulations
/// of the Hydro program.
pub fn sim(self) -> SimFlow<'a> {
use std::cell::RefCell;
use std::rc::Rc;
use slotmap::SparseSecondaryMap;
use crate::sim::graph::SimNodePort;
let shared_port_counter = Rc::new(RefCell::new(SimNodePort::default()));
let mut processes = SparseSecondaryMap::new();
let mut clusters = SparseSecondaryMap::new();
let externals = SparseSecondaryMap::new();
for (key, loc) in self.locations.iter() {
match loc {
LocationType::Process => {
processes.insert(
key,
SimNode {
shared_port_counter: shared_port_counter.clone(),
},
);
}
LocationType::Cluster => {
clusters.insert(
key,
SimNode {
shared_port_counter: shared_port_counter.clone(),
},
);
}
LocationType::External => {
panic!("Sim cannot have externals");
}
}
}
SimFlow {
ir: self.ir,
processes,
clusters,
externals,
cluster_max_sizes: SparseSecondaryMap::new(),
externals_port_registry: Default::default(),
_phantom: PhantomData,
}
}
pub fn into_deploy<D: Deploy<'a>>(self) -> DeployFlow<'a, D> {
let (processes, clusters, externals) = Default::default();
DeployFlow {
ir: self.ir,
locations: self.locations,
location_names: self.location_names,
processes,
clusters,
externals,
sidecars: SparseSecondaryMap::new(),
flow_name: self.flow_name,
_phantom: PhantomData,
}
}
pub fn with_process<P, D: Deploy<'a>>(
self,
process: &Process<P>,
spec: impl IntoProcessSpec<'a, D>,
) -> DeployFlow<'a, D> {
self.into_deploy().with_process(process, spec)
}
pub fn with_remaining_processes<D: Deploy<'a>, S: IntoProcessSpec<'a, D> + 'a>(
self,
spec: impl Fn() -> S,
) -> DeployFlow<'a, D> {
self.into_deploy().with_remaining_processes(spec)
}
pub fn with_external<P, D: Deploy<'a>>(
self,
process: &External<P>,
spec: impl ExternalSpec<'a, D>,
) -> DeployFlow<'a, D> {
self.into_deploy().with_external(process, spec)
}
pub fn with_remaining_externals<D: Deploy<'a>, S: ExternalSpec<'a, D> + 'a>(
self,
spec: impl Fn() -> S,
) -> DeployFlow<'a, D> {
self.into_deploy().with_remaining_externals(spec)
}
pub fn with_cluster<C, D: Deploy<'a>>(
self,
cluster: &Cluster<C>,
spec: impl ClusterSpec<'a, D>,
) -> DeployFlow<'a, D> {
self.into_deploy().with_cluster(cluster, spec)
}
pub fn with_remaining_clusters<D: Deploy<'a>, S: ClusterSpec<'a, D> + 'a>(
self,
spec: impl Fn() -> S,
) -> DeployFlow<'a, D> {
self.into_deploy().with_remaining_clusters(spec)
}
pub fn compile<D: Deploy<'a, InstantiateEnv = ()>>(self) -> CompiledFlow<'a> {
self.into_deploy::<D>().compile()
}
pub fn deploy<D: Deploy<'a>>(self, env: &mut D::InstantiateEnv) -> DeployResult<'a, D> {
self.into_deploy::<D>().deploy(env)
}
#[cfg(feature = "viz")]
pub fn generate_all_files(
&self,
prefix: &str,
show_metadata: bool,
show_location_groups: bool,
use_short_labels: bool,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api().generate_all_files(
prefix,
show_metadata,
show_location_groups,
use_short_labels,
)
}
#[cfg(feature = "viz")]
pub fn generate_graph_with_config(
&self,
config: &crate::viz::config::GraphConfig,
message_handler: Option<&dyn Fn(&str)>,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api()
.generate_graph_with_config(config, message_handler)
}
#[cfg(feature = "viz")]
pub fn generate_all_files_with_config(
&self,
config: &crate::viz::config::GraphConfig,
prefix: &str,
) -> Result<(), Box<dyn std::error::Error>> {
self.graph_api()
.generate_all_files_with_config(config, prefix)
}
}