| 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_minmax_reductions_with_scalar_tail() { |
| 32 | let source = fixture("do_loop_vectorize_reduce_minmax_tail.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 | // Two vreduce_max (i32 + f32) and two vreduce_min (i32 + f64) |
| 43 | // should fire, each followed by peeled scalar select chains. |
| 44 | assert_eq!( |
| 45 | o3_ir.matches("vreduce_max").count(), |
| 46 | 2, |
| 47 | "expected two vreduce_max:\n{}", |
| 48 | o3_ir |
| 49 | ); |
| 50 | assert_eq!( |
| 51 | o3_ir.matches("vreduce_min").count(), |
| 52 | 2, |
| 53 | "expected two vreduce_min:\n{}", |
| 54 | o3_ir |
| 55 | ); |
| 56 | |
| 57 | let stdout = capture_run_stdout(CaptureRequest { |
| 58 | input: source, |
| 59 | requested: BTreeSet::from([Stage::Run]), |
| 60 | opt_level: OptLevel::O3, |
| 61 | }); |
| 62 | let trimmed: Vec<&str> = stdout |
| 63 | .lines() |
| 64 | .map(|l| l.trim()) |
| 65 | .filter(|l| !l.is_empty()) |
| 66 | .collect(); |
| 67 | assert_eq!(trimmed.len(), 4, "expected four output lines:\n{}", stdout); |
| 68 | // a(i) = 100 - i over i=1..31 → values 99..69. Max=99, Min=69. |
| 69 | assert_eq!(trimmed[0], "99", "i32 max wrong: got {:?}", trimmed[0]); |
| 70 | assert_eq!(trimmed[1], "69", "i32 min wrong: got {:?}", trimmed[1]); |
| 71 | assert!( |
| 72 | trimmed[2].starts_with("9.9"), |
| 73 | "f32 max wrong: got {:?}", |
| 74 | trimmed[2] |
| 75 | ); |
| 76 | assert!( |
| 77 | trimmed[3].starts_with("-9.9"), |
| 78 | "f64 min wrong: got {:?}", |
| 79 | trimmed[3] |
| 80 | ); |
| 81 | } |
| 82 |