fortrangoingonforty/armfortas / 356d577

Browse files

Test WHERE/ELSEWHERE with unary and binop on else array load

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
356d57773c0341f86acacf44df1eddc2e989c6d0
Parents
f3fca56
Tree
1cac4d1

4 changed files

StatusFile+-
A test_programs/do_loop_vectorize_where_elsewhere_arr_binop.f90 32 0
A test_programs/do_loop_vectorize_where_elsewhere_arr_unary.f90 31 0
A tests/vectorize_where_elsewhere_arr_binop.rs 76 0
A tests/vectorize_where_elsewhere_arr_unary.rs 71 0
test_programs/do_loop_vectorize_where_elsewhere_arr_binop.f90added
@@ -0,0 +1,32 @@
1
+! WHERE / ELSEWHERE with binop on else array load:
2
+!   where (a > 0) c = a
3
+!   elsewhere    c = d * 10
4
+!
5
+! Lifts to vselect(mask, vload(a), vmul(vload(d), vbcast(10))) → vstore(c).
6
+!
7
+! a(i) = i - 16; d(i) = real(i, 4); c(i) = 0.
8
+! WHERE a > 0 (lanes 17..32): c ← a (= 1..16).
9
+! ELSEWHERE                    : c ← d * 10 (= 10 .. 160 for lanes 1..16).
10
+!   c(1)  = 10.0  (mask=false → 1*10)
11
+!   c(16) = 160.0 (a(16)=0, NOT > 0 → 16*10)
12
+!   c(17) = 1.0   (mask=true → a(17) = 1)
13
+!   c(32) = 16.0  (mask=true → a(32) = 16)
14
+!
15
+! CHECK:    1.0000000E1     1.6000000E2     1.0000000E0     1.6000000E1
16
+program test_do_loop_vectorize_where_elsewhere_arr_binop
17
+  implicit none
18
+  integer :: i
19
+  real(4) :: a(32), c(32), d(32), ten
20
+  ten = 10.0
21
+  do i = 1, 32
22
+    a(i) = real(i - 16, 4)
23
+    d(i) = real(i, 4)
24
+    c(i) = 0.0
25
+  end do
26
+  where (a > 0.0)
27
+    c = a
28
+  elsewhere
29
+    c = d * ten
30
+  end where
31
+  print *, c(1), c(16), c(17), c(32)
32
+end program test_do_loop_vectorize_where_elsewhere_arr_binop
test_programs/do_loop_vectorize_where_elsewhere_arr_unary.f90added
@@ -0,0 +1,31 @@
1
+! WHERE / ELSEWHERE with unary on else array load:
2
+!   where (a > 0) c = a
3
+!   elsewhere    c = -d
4
+!
5
+! Lifts to vselect(mask, vload(a), vneg(vload(d))) → vstore(c).
6
+!
7
+! a(i) = i - 16; d(i) = real(i, 4); c(i) = 0.
8
+! WHERE a > 0 (lanes 17..32): c ← a (= 1..16).
9
+! ELSEWHERE                    : c ← -d (= -1 .. -16 for lanes 1..16).
10
+!   c(1)  = -1.0  (mask=false → -d(1) = -1)
11
+!   c(16) = -16.0 (a(16)=0, NOT > 0 → -d(16) = -16)
12
+!   c(17) = 1.0   (mask=true → a = 1)
13
+!   c(32) = 16.0  (mask=true → a = 16)
14
+!
15
+! CHECK:    -1.0000000E0    -1.6000000E1     1.0000000E0     1.6000000E1
16
+program test_do_loop_vectorize_where_elsewhere_arr_unary
17
+  implicit none
18
+  integer :: i
19
+  real(4) :: a(32), c(32), d(32)
20
+  do i = 1, 32
21
+    a(i) = real(i - 16, 4)
22
+    d(i) = real(i, 4)
23
+    c(i) = 0.0
24
+  end do
25
+  where (a > 0.0)
26
+    c = a
27
+  elsewhere
28
+    c = -d
29
+  end where
30
+  print *, c(1), c(16), c(17), c(32)
31
+end program test_do_loop_vectorize_where_elsewhere_arr_unary
tests/vectorize_where_elsewhere_arr_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_elsewhere_with_binop_on_else_load() {
32
+    let source = fixture("do_loop_vectorize_where_elsewhere_arr_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
+    assert_eq!(
43
+        o3_ir.matches("vselect").count(),
44
+        1,
45
+        "expected one vselect:\n{}",
46
+        o3_ir
47
+    );
48
+    assert!(
49
+        o3_ir.contains("vmul"),
50
+        "expected vmul lifted from `c = d * ten` else arm:\n{}",
51
+        o3_ir
52
+    );
53
+    assert!(
54
+        o3_ir.matches("vbroadcast").count() >= 2,
55
+        "expected ≥2 vbroadcasts (threshold + ten):\n{}",
56
+        o3_ir
57
+    );
58
+
59
+    let stdout = capture_run_stdout(CaptureRequest {
60
+        input: source,
61
+        requested: BTreeSet::from([Stage::Run]),
62
+        opt_level: OptLevel::O3,
63
+    });
64
+    let trimmed: Vec<&str> = stdout
65
+        .lines()
66
+        .map(|l| l.trim())
67
+        .filter(|l| !l.is_empty())
68
+        .collect();
69
+    assert_eq!(trimmed.len(), 1, "expected one output line:\n{}", stdout);
70
+    assert_eq!(
71
+        trimmed[0],
72
+        "1.0000000E1     1.6000000E2     1.0000000E0     1.6000000E1",
73
+        "WHERE/ELSEWHERE-arr-binop wrong: {:?}",
74
+        trimmed[0]
75
+    );
76
+}
tests/vectorize_where_elsewhere_arr_unary.rsadded
@@ -0,0 +1,71 @@
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_unary_on_else_load() {
32
+    let source = fixture("do_loop_vectorize_where_elsewhere_arr_unary.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
+    assert_eq!(
43
+        o3_ir.matches("vselect").count(),
44
+        1,
45
+        "expected one vselect:\n{}",
46
+        o3_ir
47
+    );
48
+    assert!(
49
+        o3_ir.contains("vneg"),
50
+        "expected vneg lifted from `c = -d` else arm:\n{}",
51
+        o3_ir
52
+    );
53
+
54
+    let stdout = capture_run_stdout(CaptureRequest {
55
+        input: source,
56
+        requested: BTreeSet::from([Stage::Run]),
57
+        opt_level: OptLevel::O3,
58
+    });
59
+    let trimmed: Vec<&str> = stdout
60
+        .lines()
61
+        .map(|l| l.trim())
62
+        .filter(|l| !l.is_empty())
63
+        .collect();
64
+    assert_eq!(trimmed.len(), 1, "expected one output line:\n{}", stdout);
65
+    assert_eq!(
66
+        trimmed[0],
67
+        "-1.0000000E0    -1.6000000E1     1.0000000E0     1.6000000E1",
68
+        "WHERE/ELSEWHERE-arr-unary wrong: {:?}",
69
+        trimmed[0]
70
+    );
71
+}