| 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_int_max_and_min_reductions() { |
| 32 | let source = fixture("do_loop_vectorize_reduce_minmax.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!( |
| 43 | o3_ir.contains("vmax") |
| 44 | && o3_ir.contains("vmin") |
| 45 | && o3_ir.contains("vreduce_max") |
| 46 | && o3_ir.contains("vreduce_min"), |
| 47 | "expected NeonVectorize to emit vmax + vmin and vreduce_max + vreduce_min:\n{}", |
| 48 | o3_ir |
| 49 | ); |
| 50 | |
| 51 | let o3_asm = capture_text( |
| 52 | CaptureRequest { |
| 53 | input: source.clone(), |
| 54 | requested: BTreeSet::from([Stage::Asm]), |
| 55 | opt_level: OptLevel::O3, |
| 56 | }, |
| 57 | Stage::Asm, |
| 58 | ); |
| 59 | assert!( |
| 60 | o3_asm.contains("smaxv.4s") && o3_asm.contains("sminv.4s"), |
| 61 | "VReduceMax/VReduceMin on i32 should lower via smaxv.4s / sminv.4s:\n{}", |
| 62 | o3_asm |
| 63 | ); |
| 64 | assert!( |
| 65 | o3_asm.contains("smax.4s") && o3_asm.contains("smin.4s"), |
| 66 | "VMax/VMin in the body should lower via smax.4s / smin.4s:\n{}", |
| 67 | o3_asm |
| 68 | ); |
| 69 | |
| 70 | let stdout = capture_run_stdout(CaptureRequest { |
| 71 | input: source, |
| 72 | requested: BTreeSet::from([Stage::Run]), |
| 73 | opt_level: OptLevel::O3, |
| 74 | }); |
| 75 | let trimmed: Vec<&str> = stdout |
| 76 | .lines() |
| 77 | .map(|l| l.trim()) |
| 78 | .filter(|l| !l.is_empty()) |
| 79 | .collect(); |
| 80 | assert_eq!( |
| 81 | trimmed, |
| 82 | vec!["32", "68"], |
| 83 | "min/max reductions should produce 32 and 68:\n{}", |
| 84 | stdout |
| 85 | ); |
| 86 | } |
| 87 |