fortrangoingonforty/armfortas / a5f0f51

Browse files

Test WHERE/ELSEWHERE composes with then-arm binop and elsewhere const

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
a5f0f514f84663187502831415c4c77f90fa5645
Parents
a8eb0d6
Tree
27c88f5

2 changed files

StatusFile+-
A test_programs/do_loop_vectorize_where_elsewhere_binop.f90 30 0
A tests/vectorize_where_elsewhere_binop.rs 78 0
test_programs/do_loop_vectorize_where_elsewhere_binop.f90added
@@ -0,0 +1,30 @@
1
+! WHERE/ELSEWHERE composing with binop in then-arm:
2
+!   where (a > 0) c = a * 2
3
+!   elsewhere    c = -1
4
+! lowers to vselect(mask, vmul(va, vbcast(2)), vbcast(-1)) → vstore.
5
+!
6
+! a(i) = i - 16; c starts as 0.
7
+! WHERE a > 0 (lanes 17..32): c ← a*2 = 2..32.
8
+! ELSEWHERE                    : c ← -1.
9
+!   c(1)  = -1   (a(1)=-15, mask=false)
10
+!   c(16) = -1   (a(16)=0, NOT > 0)
11
+!   c(17) = 2    (a(17)=1, mask=true → 1*2)
12
+!   c(32) = 32   (a(32)=16, mask=true → 16*2)
13
+!
14
+! CHECK:    -1.0000000E0    -1.0000000E0     2.0000000E0     3.2000000E1
15
+program test_do_loop_vectorize_where_elsewhere_binop
16
+  implicit none
17
+  integer :: i
18
+  real(4) :: a(32), c(32), two
19
+  two = 2.0
20
+  do i = 1, 32
21
+    a(i) = real(i - 16, 4)
22
+    c(i) = 0.0
23
+  end do
24
+  where (a > 0.0)
25
+    c = a * two
26
+  elsewhere
27
+    c = -1.0
28
+  end where
29
+  print *, c(1), c(16), c(17), c(32)
30
+end program test_do_loop_vectorize_where_elsewhere_binop
tests/vectorize_where_elsewhere_binop.rsadded
@@ -0,0 +1,78 @@
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_with_then_binop() {
32
+    let source = fixture("do_loop_vectorize_where_elsewhere_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
+    // Expect a vmul (then-arm a*scale), a vselect, and ≥3 vbroadcasts
43
+    // (threshold + scale + elsewhere const).
44
+    assert_eq!(
45
+        o3_ir.matches("vselect").count(),
46
+        1,
47
+        "expected one vselect:\n{}",
48
+        o3_ir
49
+    );
50
+    assert!(
51
+        o3_ir.contains("vmul"),
52
+        "expected vmul lifted from a*scale in WHERE true arm:\n{}",
53
+        o3_ir
54
+    );
55
+    assert!(
56
+        o3_ir.matches("vbroadcast").count() >= 3,
57
+        "expected ≥3 vbroadcasts (threshold + scale + elsewhere const):\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(), 1, "expected one output line:\n{}", stdout);
72
+    assert_eq!(
73
+        trimmed[0],
74
+        "-1.0000000E0    -1.0000000E0     2.0000000E0     3.2000000E1",
75
+        "WHERE/ELSEWHERE+binop wrong: {:?}",
76
+        trimmed[0]
77
+    );
78
+}