fortrangoingonforty/armfortas / a8eb0d6

Browse files

Test runtime-trip reduction partial unroll runs correctly with remainder

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
a8eb0d64788a002eb1e2f2cc6ac97e28ae22edd5
Parents
0d758b9
Tree
0e91a1b

2 changed files

StatusFile+-
A test_programs/loop_partial_unroll_runtime_red.f90 22 0
A tests/loop_partial_unroll_runtime_red.rs 71 0
test_programs/loop_partial_unroll_runtime_red.f90added
@@ -0,0 +1,22 @@
1
+! Runtime-trip partial unrolling of a sum-reduction loop. The header
2
+! has 2 params (iv + acc); the U-way unrolled main loop threads the
3
+! accumulator across clones; the scalar remainder loop continues the
4
+! accumulation for trailing iterations.
5
+!
6
+! n = 35 + command_argument_count() (no args ⇒ n = 35). Trip = 35.
7
+! With U=4: head_count = (35/4)*4 = 32; remainder = 3.
8
+! sum(i*i, 1..35) = 35*36*71/6 = 14910. a(35) = 35*35 = 1225.
9
+! CHECK: 35 14910 1225
10
+program test_loop_partial_unroll_runtime_red
11
+  implicit none
12
+  integer :: a(64), s, n, i
13
+  n = 35 + command_argument_count()
14
+  do i = 1, n
15
+    a(i) = i * i
16
+  end do
17
+  s = 0
18
+  do i = 1, n
19
+    s = s + a(i)
20
+  end do
21
+  print *, n, s, a(n)
22
+end program test_loop_partial_unroll_runtime_red
tests/loop_partial_unroll_runtime_red.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 o2_partial_unrolls_runtime_trip_reduction_with_remainder() {
32
+    let source = fixture("loop_partial_unroll_runtime_red.f90");
33
+    let opt_ir = capture_text(
34
+        CaptureRequest {
35
+            input: source.clone(),
36
+            requested: BTreeSet::from([Stage::OptIr]),
37
+            opt_level: OptLevel::O2,
38
+        },
39
+        Stage::OptIr,
40
+    );
41
+    // The reduction loop's remainder header takes 2 params (iv +
42
+    // acc); the iv-only store loop's remainder header takes only 1.
43
+    // Both should appear in the IR.
44
+    let two_param_remain = opt_ir
45
+        .lines()
46
+        .filter(|l| l.contains("partial_remain_header"))
47
+        .filter(|l| l.matches(": i32").count() == 2)
48
+        .count();
49
+    assert!(
50
+        two_param_remain >= 1,
51
+        "expected a 2-param partial_remain_header (iv + acc):\n{}",
52
+        opt_ir
53
+    );
54
+
55
+    let stdout = capture_run_stdout(CaptureRequest {
56
+        input: source,
57
+        requested: BTreeSet::from([Stage::Run]),
58
+        opt_level: OptLevel::O2,
59
+    });
60
+    let trimmed: Vec<&str> = stdout
61
+        .lines()
62
+        .map(|l| l.trim())
63
+        .filter(|l| !l.is_empty())
64
+        .collect();
65
+    assert_eq!(trimmed.len(), 1, "expected one print line:\n{}", stdout);
66
+    assert_eq!(
67
+        trimmed[0], "35 14910 1225",
68
+        "runtime reduction partial unroll wrong: {:?}",
69
+        trimmed[0]
70
+    );
71
+}