origen_metal 1.4.0

Bare metal APIs for the Origen SDK
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use crate::prog_gen::{FlowCondition, GroupType, PGM};
use crate::Result;
use crate::ast::{Node, Processor, Return};
use std::collections::HashMap;

///  This processor eliminates the use of un-necessary flags between adjacent tests:
///  
/// Input:
///    PGMFlow("f1")
///        PGMTest(1, FlowID("t1"))
///        PGMOnFailed(FlowID("t1"))
///            PGMSetFlag("t1_FAILED", true, true)
///            PGMContinue
///        PGMCondition(IfFlag(["t1_FAILED"]))
///            PGMTest(2, FlowID("t2"))
///
/// Output:
///    PGMFlow("f1")
///        PGMTest(1, FlowID("t1"))
///        PGMOnFailed(FlowID("t1"))
///            PGMTest(2, FlowID("t2"))
///
pub struct FlagOptimizer {
    optimize_when_continue: bool,
    /// The number of times each flag is used
    nodes_to_inline: Vec<Box<Node<PGM>>>,
}

pub fn run(node: &Node<PGM>, optimize_when_continue: Option<bool>) -> Result<Node<PGM>> {
    //node.to_file("pre_flag_optimization.txt")?;
    let optimize_when_continue = match optimize_when_continue {
        Some(x) => x,
        None => true,
    };
    let mut p = FlagOptimizer {
        optimize_when_continue: optimize_when_continue,
        nodes_to_inline: vec![],
    };
    let ast = node.process(&mut p)?.unwrap();

    let mut p = RemoveRedundantSetFlags {
        pass: 0,
        references: HashMap::new(),
    };
    let ast = ast.process(&mut p)?.unwrap();
    p.pass = 1;
    let ast = ast.process(&mut p)?.unwrap();
    //node.to_file("post_flag_optimization.txt")?;
    Ok(ast)
}

/// Removes any auto-generated flags which are set in the flow but which are no longer used/referenced
#[derive(Debug)]
pub struct RemoveRedundantSetFlags {
    pass: usize,
    references: HashMap<String, usize>,
}

/// Extracts the IDs of all tests which have dependents on whether they passed, failed or ran
impl Processor<PGM> for RemoveRedundantSetFlags {
    fn on_node(&mut self, node: &Node<PGM>) -> crate::Result<Return<PGM>> {
        // Count all references the first time around
        if self.pass == 0 {
            Ok(match &node.attrs {
                PGM::Condition(cond) => match cond {
                    FlowCondition::IfFlag(ids) | FlowCondition::UnlessFlag(ids) => {
                        for id in ids {
                            if self.references.contains_key(id) {
                                let x = self.references[id];
                                self.references.insert(id.clone(), x + 1);
                            } else {
                                self.references.insert(id.clone(), 1);
                            }
                        }
                        Return::ProcessChildren
                    }
                    _ => Return::ProcessChildren,
                },
                _ => Return::ProcessChildren,
            })
        } else {
            Ok(match &node.attrs {
                PGM::SetFlag(flag, _, auto_generated) => {
                    if *auto_generated && !self.references.contains_key(flag) {
                        Return::None
                    } else {
                        Return::ProcessChildren
                    }
                }
                _ => Return::ProcessChildren,
            })
        }
    }
}

impl Processor<PGM> for FlagOptimizer {
    fn on_node(&mut self, node: &Node<PGM>) -> crate::Result<Return<PGM>> {
        Ok(match &node.attrs {
            PGM::Group(_name, _, kind, _fid) => match kind {
                GroupType::Flow => {
                    let children = node.process_and_box_children(self)?;
                    Return::Replace(node.updated(None, Some(self.optimize(children)?), None))
                }
                _ => Return::ProcessChildren,
            },
            PGM::Flow(_)
            | PGM::SubFlow(_, _)
            | PGM::Else
            | PGM::Whenever
            | PGM::WheneverAny
            | PGM::WheneverAll => {
                let children = node.process_and_box_children(self)?;
                Return::Replace(node.updated(None, Some(self.optimize(children)?), None))
            }
            PGM::Condition(_) => {
                let children = node.process_and_box_children(self)?;
                Return::Replace(node.updated(None, Some(self.optimize(children)?), None))
            },
            PGM::OnFailed(_) | PGM::OnPassed(_) => {
                let mut flag = None;
                let update = {
                    if let Some(to_inline) = self.nodes_to_inline.last() {
                        let mut i = 0;
                        if let Some(set_flag) = node.children.iter().find(|n| {
                            i += 1;
                            matches!(n.attrs, PGM::SetFlag(_, _, _))
                        }) {
                            if let PGM::SetFlag(flg, true, true) = &set_flag.attrs {
                                flag = Some(flg);
                                is_gated_by_set(flg, to_inline)
                            } else {
                                false
                            }
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                };
                let mut children = node.process_and_box_children(self)?;
                if update {
                    let to_inline = self.nodes_to_inline.pop().unwrap();
                    let to_inline = self.reorder_nested_run_flags(flag.unwrap(), *to_inline)?;
                    for n in to_inline.children {
                        children.push(n);
                    }
                    self.nodes_to_inline.push(Box::new(node!(PGM::Nil))); // This will be popped off and discarded later
                }
                Return::Replace(node.updated(None, Some(self.optimize(children)?), None))
            }
            _ => Return::ProcessChildren,
        })
    }
}

impl FlagOptimizer {
    fn optimize(&mut self, nodes: Vec<Box<Node<PGM>>>) -> Result<Vec<Box<Node<PGM>>>> {
        let mut results: Vec<Box<Node<PGM>>> = vec![];
        let mut node1: Option<Box<Node<PGM>>> = None;
        for node2 in nodes {
            let n2 = node2;
            if let Some(n1) = node1 {
                if self.can_be_combined(&n1, &n2) {
                    node1 = Some(self.combine(n1, n2)?);
                } else {
                    results.push(n1);
                    node1 = Some(n2);
                }
            } else {
                node1 = Some(n2);
            }
        }
        if let Some(n) = node1 {
            results.push(n);
        }
        Ok(results)
    }

    fn can_be_combined(&mut self, node1: &Box<Node<PGM>>, node2: &Box<Node<PGM>>) -> bool {
        // If node1 could have an OnFailed or OnPassed and if node2 is a flag condition
        if (matches!(node1.attrs, PGM::Test(_, _)) || matches!(node1.attrs, PGM::SubFlow(_, _)))
            && (matches!(node2.attrs, PGM::Condition(FlowCondition::IfFlag(_)))
                || matches!(node2.attrs, PGM::Condition(FlowCondition::UnlessFlag(_))))
        {
            // Don't optimize tests which are marked as continue if told not to
            let on_failed = node1
                .children
                .iter()
                .find(|n| matches!(n.attrs, PGM::OnFailed(_)));
            if let Some(on_failed) = on_failed {
                if !self.optimize_when_continue
                    && on_failed
                        .children
                        .iter()
                        .any(|n| matches!(n.attrs, PGM::Continue))
                {
                    return false;
                }
            }

            // Now return true if node 1 sets a flag that is gating node 2
            let on_pass_or_fail: Vec<&Box<Node<PGM>>> = node1
                .children
                .iter()
                .filter(|n| {
                    matches!(n.attrs, PGM::OnFailed(_)) || matches!(n.attrs, PGM::OnPassed(_))
                })
                .collect();

            if on_pass_or_fail.iter().any(|n| {
                let set_flag = n
                    .children
                    .iter()
                    .find(|n| matches!(n.attrs, PGM::SetFlag(_, _, _)));
                if let Some(set_flag) = set_flag {
                    if let PGM::SetFlag(name, val, auto_generated) = &set_flag.attrs {
                        *val && is_gated_by_set(name, node2) && *auto_generated
                    } else {
                        unreachable!()
                    }
                } else {
                    false
                }
            }) {
                return true;
            }
        }
        false
    }

    fn combine(&mut self, node1: Box<Node<PGM>>, node2: Box<Node<PGM>>) -> Result<Box<Node<PGM>>> {
        self.nodes_to_inline.push(node2);
        let node1 = node1.process_and_update_children(self)?;
        self.nodes_to_inline.pop();
        Ok(Box::new(node1))
    }

    // Returns the node with the run_flag clauses re-ordered to have the given flag of interest at the top.
    //
    // The caller guarantees the run_flag clause containing the given flag is present.
    //
    // For example, given this node:
    //
    //   s(:unless_flag, "flag1",
    //     s(:if_flag, "ot_BEA7F3B_FAILED",
    //       s(:test,
    //         s(:object, <TestSuite: inner_test1_BEA7F3B>),
    //         s(:name, "inner_test1_BEA7F3B"),
    //         s(:number, 0),
    //         s(:id, "it1_BEA7F3B"),
    //         s(:on_fail,
    //           s(:render, "multi_bin;")))))
    //
    // Then this node would be returned when the flag of interest is ot_BEA7F3B_FAILED:
    //
    //   s(:if_flag, "ot_BEA7F3B_FAILED",
    //     s(:unless_flag, "flag1",
    //       s(:test,
    //         s(:object, <TestSuite: inner_test1_BEA7F3B>),
    //         s(:name, "inner_test1_BEA7F3B"),
    //         s(:number, 0),
    //         s(:id, "it1_BEA7F3B"),
    //         s(:on_fail,
    //           s(:render, "multi_bin;")))))
    fn reorder_nested_run_flags(&mut self, flag: &str, node: Node<PGM>) -> Result<Node<PGM>> {
        // If the run_flag we care about is already at the top, just return node
        if let PGM::Condition(FlowCondition::IfFlag(flags)) = &node.attrs {
            if flags[0] == flag {
                return Ok(node);
            }
        }
        let mut p = RemoveIfFlag { flag: flag };
        let node = node.process(&mut p)?.unwrap();
        let n = node!(PGM::Condition, FlowCondition::IfFlag(vec![flag.to_owned()]) => node);
        Ok(n)
    }
}

/// Removes any auto-generated flags which are set in the flow but which are no longer used/referenced
#[derive(Debug)]
pub struct RemoveIfFlag<'a> {
    flag: &'a str,
}

/// Extracts the IDs of all tests which have dependents on whether they passed, failed or ran
impl<'a> Processor<PGM> for RemoveIfFlag<'a> {
    fn on_node(&mut self, node: &Node<PGM>) -> crate::Result<Return<PGM>> {
        Ok(match &node.attrs {
            PGM::Condition(cond) => match cond {
                FlowCondition::IfFlag(ids) => {
                    if ids[0] == self.flag {
                        Return::UnwrapWithProcessedChildren
                    } else {
                        Return::ProcessChildren
                    }
                }
                _ => Return::ProcessChildren,
            },
            _ => Return::ProcessChildren,
        })
    }
}

// node will always be an if_flag or unless_flag type node, guaranteed by the caller
//
// Returns true if flag matches the one supplied
//
//   s(:if_flag, flag,
//     s(:test, ...
//
// Also returns true if flag matches the one supplied, but it is nested within other flag conditions:
//
//   s(:unless_flag, other_flag,
//     s(:if_flag, other_flag2,
//       s(:if_flag, flag,
//         s(:test, ...
fn is_gated_by_set(flag: &str, node: &Box<Node<PGM>>) -> bool {
    if let PGM::Condition(FlowCondition::IfFlag(flags)) = &node.attrs {
        if flags[0] == flag {
            return true;
        }
    }
    if node.children.len() == 1
        && (matches!(node.attrs, PGM::Condition(FlowCondition::IfFlag(_)))
            || matches!(node.attrs, PGM::Condition(FlowCondition::UnlessFlag(_))))
    {
        return is_gated_by_set(flag, node.children.last().as_ref().unwrap());
    }
    false
}

#[cfg(test)]
mod tests {
    use super::run;
    use crate::prog_gen::{BinType, FlowCondition, FlowID, GroupType, PGM};
    use crate::Result;

    #[test]
    fn basic_functionality_test() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 2, FlowID::from_int(2))
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
            ),
        );

        assert_eq!(output, run(&input, None)?);
        Ok(())
    }

    #[test]
    fn it_keeps_flags_with_later_references() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 2, FlowID::from_int(2))
            ),
            node!(PGM::Test, 3, FlowID::from_int(3)),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 4, FlowID::from_int(4))
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
            ),
            node!(PGM::Test, 3, FlowID::from_int(3)),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 4, FlowID::from_int(4))
            ),
        );

        assert_eq!(output, run(&input, None)?);
        Ok(())
    }

    #[test]
    fn it_applies_the_optimization_within_nested_groups() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Group, "G1".to_string(), None, GroupType::Flow, Some(FlowID::from_str("g1")) =>
                node!(PGM::Test, 1, FlowID::from_int(1) =>
                    node!(PGM::OnFailed, FlowID::from_int(1) =>
                        node!(PGM::Continue),
                        node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                    ),
                ),
                node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Group, "G1".to_string(), None, GroupType::Flow, Some(FlowID::from_str("g1")) =>
                node!(PGM::Test, 1, FlowID::from_int(1) =>
                    node!(PGM::OnFailed, FlowID::from_int(1) =>
                        node!(PGM::Continue),
                        node!(PGM::Test, 2, FlowID::from_int(2)),
                    ),
                ),
            ),
        );

        assert_eq!(output, run(&input, None)?);
        Ok(())
    }

    #[test]
    fn a_more_complex_case_with_both_pass_and_fail_branches_to_be_optimized() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnPassed, FlowID::from_int(1) =>
                    node!(PGM::SetFlag, "t1_PASSED".to_string(), true, true),
                ),
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_PASSED".to_string()]) =>
                node!(PGM::Test, 2, FlowID::from_int(2))
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 3, FlowID::from_int(3))
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Bin, 10, None, BinType::Bad)
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnPassed, FlowID::from_int(1) =>
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::Test, 3, FlowID::from_int(3)),
                    node!(PGM::Bin, 10, None, BinType::Bad),
                ),
            ),
        );

        assert_eq!(output, run(&input, None)?);
        Ok(())
    }

    #[test]
    fn nested_flags_are_handled() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["my_flag".to_string()]) =>
                node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::Condition, FlowCondition::IfFlag(vec!["my_flag".to_string()]) =>
                        node!(PGM::Test, 2, FlowID::from_int(2))
                    ),
                ),
            ),
        );

        assert_eq!(output, run(&input, None)?);
        Ok(())
    }

    #[test]
    fn optionally_doesnt_eliminate_flags_on_tests_with_a_continue() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 2, FlowID::from_int(2))
            ),
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Test, 1, FlowID::from_int(1) =>
                node!(PGM::OnFailed, FlowID::from_int(1) =>
                    node!(PGM::Continue),
                    node!(PGM::SetFlag, "t1_FAILED".to_string(), true, true),
                ),
            ),
            node!(PGM::Condition, FlowCondition::IfFlag(vec!["t1_FAILED".to_string()]) =>
                node!(PGM::Test, 2, FlowID::from_int(2))
            ),
        );

        assert_eq!(output, run(&input, Some(false))?);
        Ok(())
    }

    #[test]
    fn eliminates_flags_within_a_condition() -> Result<()> {
        let input = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Condition, FlowCondition::IfEnable(vec!["BLAH".to_string()]) =>
                node!(PGM::Test, 1, FlowID::from_int(1) =>
                    node!(PGM::OnFailed, FlowID::from_int(1) =>
                        node!(PGM::SetFlag, "T1_FAILED".to_string(), true, true),
                    ),
                ),
                node!(PGM::Condition, FlowCondition::IfFlag(vec!["T1_FAILED".to_string()]) =>
                    node!(PGM::Test, 2, FlowID::from_int(2))
                ),
            )
        );

        let output = node!(PGM::Flow, "f1".to_string() =>
            node!(PGM::Condition, FlowCondition::IfEnable(vec!["BLAH".to_string()]) =>
                node!(PGM::Test, 1, FlowID::from_int(1) =>
                    node!(PGM::OnFailed, FlowID::from_int(1) =>
                        node!(PGM::Test, 2, FlowID::from_int(2))
                    ),
                ),
            )
        );

        assert_eq!(output, run(&input, Some(false))?);
        Ok(())
    }
}