@@ -0,0 +1,77 @@ |
| | 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_two_statement_do_body() { |
| | 32 | + let source = fixture("do_loop_vectorize_multi.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 | + |
| | 43 | + // Both statements should land in vector form: at least two VStores |
| | 44 | + // and two VAdds/VMuls in the IR (one for `a(i)=b(i)+scale`, one |
| | 45 | + // for `c(i)=b(i)*6`). |
| | 46 | + let n_vstore = o3_ir.matches("vstore").count(); |
| | 47 | + let n_vbroadcast = o3_ir.matches("vbroadcast").count(); |
| | 48 | + assert!( |
| | 49 | + n_vstore >= 2, |
| | 50 | + "two-store body should produce >=2 VStores, got {}:\n{}", |
| | 51 | + n_vstore, |
| | 52 | + o3_ir |
| | 53 | + ); |
| | 54 | + assert!( |
| | 55 | + n_vbroadcast >= 1, |
| | 56 | + "scale and constant 6 should each become a VBroadcast (>=1 expected):\n{}", |
| | 57 | + o3_ir |
| | 58 | + ); |
| | 59 | + |
| | 60 | + // Runtime check: a(1)=8, a(32)=39, c(1)=6, d(32)=c(32)=192. |
| | 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!( |
| | 72 | + trimmed, |
| | 73 | + vec!["8", "39", "6", "192"], |
| | 74 | + "two-statement vectorized body should produce 8, 39, 6, 192:\n{}", |
| | 75 | + stdout |
| | 76 | + ); |
| | 77 | +} |