fortrangoingonforty/armfortas / 26ec0b5

Browse files

Test WHERE/ELSEWHERE two-arm vectorizes at O3

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
26ec0b5a076252787af6b889c319095dcaac86c6
Parents
4c6be2d
Tree
7bd2e2a

2 changed files

StatusFile+-
A test_programs/do_loop_vectorize_where_elsewhere.f90 29 0
A tests/vectorize_where_elsewhere.rs 74 0
test_programs/do_loop_vectorize_where_elsewhere.f90added
@@ -0,0 +1,29 @@
1
+! WHERE / ELSEWHERE two-arm form. Both arms write to `b`. The matcher
2
+! must recognize the diamond's else_block (currently empty in the
3
+! else-less form) being non-empty, with a store to the same dest
4
+! pointer. Lowered to vselect(mask, then_arm, else_arm) → store.
5
+!
6
+! a(i) = i - 16 (range -15..16); b(i) = 0.
7
+! WHERE a > 0: b ← a (so 0..16 stays 0; 17..32 gets a = 1..16).
8
+! ELSEWHERE: b ← -1.0 (so 0..16 becomes -1).
9
+!   b(1)  = -1.0 (a(1)=-15, mask=false, b ← -1)
10
+!   b(16) = -1.0 (a(16)=0, NOT > 0, mask=false, b ← -1)
11
+!   b(17) =  1.0 (a(17)=1, mask=true, b ← a = 1)
12
+!   b(32) = 16.0 (a(32)=16, mask=true, b ← a = 16)
13
+!
14
+! CHECK:    -1.0000000E0    -1.0000000E0     1.0000000E0     1.6000000E1
15
+program test_do_loop_vectorize_where_elsewhere
16
+  implicit none
17
+  integer :: i
18
+  real(4) :: a(32), b(32)
19
+  do i = 1, 32
20
+    a(i) = real(i - 16, 4)
21
+    b(i) = 0.0
22
+  end do
23
+  where (a > 0.0)
24
+    b = a
25
+  elsewhere
26
+    b = -1.0
27
+  end where
28
+  print *, b(1), b(16), b(17), b(32)
29
+end program test_do_loop_vectorize_where_elsewhere
tests/vectorize_where_elsewhere.rsadded
@@ -0,0 +1,74 @@
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_elsewhere_two_arm_form() {
32
+    let source = fixture("do_loop_vectorize_where_elsewhere.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
+    // The two-arm WHERE/ELSEWHERE should produce a vselect whose
43
+    // false arm is the broadcast of the elsewhere constant — not a
44
+    // reload of the dest's prior value.
45
+    assert_eq!(
46
+        o3_ir.matches("vselect").count(),
47
+        1,
48
+        "expected exactly one vselect:\n{}",
49
+        o3_ir
50
+    );
51
+    assert!(
52
+        o3_ir.matches("vbroadcast").count() >= 2,
53
+        "expected at least 2 vbroadcasts (threshold + elsewhere const):\n{}",
54
+        o3_ir
55
+    );
56
+
57
+    let stdout = capture_run_stdout(CaptureRequest {
58
+        input: source,
59
+        requested: BTreeSet::from([Stage::Run]),
60
+        opt_level: OptLevel::O3,
61
+    });
62
+    let trimmed: Vec<&str> = stdout
63
+        .lines()
64
+        .map(|l| l.trim())
65
+        .filter(|l| !l.is_empty())
66
+        .collect();
67
+    assert_eq!(trimmed.len(), 1, "expected one output line:\n{}", stdout);
68
+    assert_eq!(
69
+        trimmed[0],
70
+        "-1.0000000E0    -1.0000000E0     1.0000000E0     1.6000000E1",
71
+        "WHERE/ELSEWHERE wrong: {:?}",
72
+        trimmed[0]
73
+    );
74
+}