fortrangoingonforty/armfortas / 939f7e4

Browse files

Test WHERE with constant-scalar store value vectorizes at O3

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
939f7e443c3fb7b5a310852a2898212a4ac344bf
Parents
131e2f1
Tree
66aaafa

2 changed files

StatusFile+-
A test_programs/do_loop_vectorize_where_const.f90 26 0
A tests/vectorize_where_const.rs 73 0
test_programs/do_loop_vectorize_where_const.f90added
@@ -0,0 +1,26 @@
1
+! WHERE with a literal-constant store value (`b = K`). The matcher
2
+! must accept a store value that is loop-invariant constant — no load,
3
+! no unary, no binop in the then_block other than the Store itself.
4
+! Lifts to vselect(mask, BroadcastK_vec, dest_vec) → store.
5
+!
6
+! a(i) = i - 16 (range -15..16); b(i) = i. Where a > 0 (lanes 17..32),
7
+! set b = -1.0; otherwise leave b = i.
8
+!   b(1)  = 1.0   (a(1)=-15, mask=false, b unchanged)
9
+!   b(16) = 16.0  (a(16)=0, NOT > 0, mask=false)
10
+!   b(17) = -1.0  (a(17)=1, mask=true, b ← -1.0)
11
+!   b(32) = -1.0  (a(32)=16, mask=true, b ← -1.0)
12
+!
13
+! CHECK: 1.0000000E0     1.6000000E1    -1.0000000E0    -1.0000000E0
14
+program test_do_loop_vectorize_where_const
15
+  implicit none
16
+  integer :: i
17
+  real(4) :: a(32), b(32)
18
+  do i = 1, 32
19
+    a(i) = real(i - 16, 4)
20
+    b(i) = real(i, 4)
21
+  end do
22
+  where (a > 0.0)
23
+    b = -1.0
24
+  end where
25
+  print *, b(1), b(16), b(17), b(32)
26
+end program test_do_loop_vectorize_where_const
tests/vectorize_where_const.rsadded
@@ -0,0 +1,73 @@
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_constant_scalar_store() {
32
+    let source = fixture("do_loop_vectorize_where_const.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 constant-scalar WHERE store should lift to a VBroadcast
43
+    // feeding the vselect's true arm.
44
+    assert_eq!(
45
+        o3_ir.matches("vselect").count(),
46
+        1,
47
+        "expected exactly one vselect:\n{}",
48
+        o3_ir
49
+    );
50
+    assert!(
51
+        o3_ir.contains("vbroadcast") || o3_ir.contains("VBroadcast"),
52
+        "expected vbroadcast for const scalar in WHERE body:\n{}",
53
+        o3_ir
54
+    );
55
+
56
+    let stdout = capture_run_stdout(CaptureRequest {
57
+        input: source,
58
+        requested: BTreeSet::from([Stage::Run]),
59
+        opt_level: OptLevel::O3,
60
+    });
61
+    let trimmed: Vec<&str> = stdout
62
+        .lines()
63
+        .map(|l| l.trim())
64
+        .filter(|l| !l.is_empty())
65
+        .collect();
66
+    assert_eq!(trimmed.len(), 1, "expected one output line:\n{}", stdout);
67
+    assert_eq!(
68
+        trimmed[0],
69
+        "1.0000000E0     1.6000000E1    -1.0000000E0    -1.0000000E0",
70
+        "const WHERE wrong: {:?}",
71
+        trimmed[0]
72
+    );
73
+}