fortrangoingonforty/armfortas / 8f2a8dd

Browse files

Test WHERE with binop body emits vadd/vmul + vbroadcast and correct output

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
8f2a8dd06bb80db50136101ad277ca06c4861a61
Parents
0244f08
Tree
2ba8e34

1 changed file

StatusFile+-
A tests/vectorize_where_binop.rs 76 0
tests/vectorize_where_binop.rsadded
@@ -0,0 +1,76 @@
1
+use std::collections::BTreeSet;
2
+use std::path::PathBuf;
3
+
4
+use armfortas::driver::OptLevel;
5
+use armfortas::testing::{capture_from_path, CaptureRequest, CapturedStage, Stage};
6
+
7
+fn fixture(name: &str) -> PathBuf {
8
+    let path = PathBuf::from("test_programs").join(name);
9
+    assert!(path.exists(), "missing test fixture {}", path.display());
10
+    path
11
+}
12
+
13
+fn capture_text(request: CaptureRequest, stage: Stage) -> String {
14
+    let result = capture_from_path(&request).expect("capture should succeed");
15
+    match result.get(stage) {
16
+        Some(CapturedStage::Text(text)) => text.clone(),
17
+        Some(CapturedStage::Run(_)) => panic!("expected text stage for {}", stage.as_str()),
18
+        None => panic!("missing requested stage {}", stage.as_str()),
19
+    }
20
+}
21
+
22
+fn capture_run_stdout(request: CaptureRequest) -> String {
23
+    let result = capture_from_path(&request).expect("capture should succeed");
24
+    match result.get(Stage::Run) {
25
+        Some(CapturedStage::Run(run)) => run.stdout.clone(),
26
+        _ => panic!("missing run stage"),
27
+    }
28
+}
29
+
30
+#[test]
31
+fn o3_vectorizes_where_with_binop_invariant_scalar_body() {
32
+    let source = fixture("do_loop_vectorize_where_binop.f90");
33
+
34
+    let o3_ir = capture_text(
35
+        CaptureRequest {
36
+            input: source.clone(),
37
+            requested: BTreeSet::from([Stage::OptIr]),
38
+            opt_level: OptLevel::O3,
39
+        },
40
+        Stage::OptIr,
41
+    );
42
+    // Two WHERE blocks → two vselects + two vbroadcasts (one for the
43
+    // scalar in each binop) plus the threshold broadcast(s).
44
+    assert_eq!(
45
+        o3_ir.matches("vselect").count(),
46
+        2,
47
+        "expected 2 vselect:\n{}",
48
+        o3_ir
49
+    );
50
+    assert!(
51
+        o3_ir.contains("vadd"),
52
+        "expected vadd lifted from binop:\n{}",
53
+        o3_ir
54
+    );
55
+    assert!(
56
+        o3_ir.contains("vmul"),
57
+        "expected vmul lifted from binop:\n{}",
58
+        o3_ir
59
+    );
60
+
61
+    let stdout = capture_run_stdout(CaptureRequest {
62
+        input: source,
63
+        requested: BTreeSet::from([Stage::Run]),
64
+        opt_level: OptLevel::O3,
65
+    });
66
+    let trimmed: Vec<&str> = stdout
67
+        .lines()
68
+        .map(|l| l.trim())
69
+        .filter(|l| !l.is_empty())
70
+        .collect();
71
+    assert_eq!(trimmed.len(), 2, "expected two output lines:\n{}", stdout);
72
+    let add_line = "1.0000000E0     1.6000000E1     1.0100000E2     1.1600000E2";
73
+    let mul_line = "1.0000000E0     1.6000000E1     1.0000000E1     1.6000000E2";
74
+    assert_eq!(trimmed[0], add_line, "WHERE add wrong: {:?}", trimmed[0]);
75
+    assert_eq!(trimmed[1], mul_line, "WHERE mul wrong: {:?}", trimmed[1]);
76
+}