| 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_block_masked_assign() { |
| 32 | let source = fixture("do_loop_vectorize_where.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 | // The diamond should be rewritten into a single body with vload a, |
| 43 | // vload b_old, vfcmp, vselect, vstore. |
| 44 | assert!( |
| 45 | o3_ir.contains("vselect"), |
| 46 | "expected vselect:\n{}", |
| 47 | o3_ir |
| 48 | ); |
| 49 | assert!( |
| 50 | o3_ir.contains("vfcmp"), |
| 51 | "expected vfcmp:\n{}", |
| 52 | o3_ir |
| 53 | ); |
| 54 | // Two vloads in the WHERE body: source `a` and old `b`. |
| 55 | assert!( |
| 56 | o3_ir.matches("vload").count() >= 2, |
| 57 | "expected >= 2 vload:\n{}", |
| 58 | o3_ir |
| 59 | ); |
| 60 | |
| 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!(trimmed.len(), 1, "expected one output line:\n{}", stdout); |
| 72 | // b(1)=1 (mask=false), b(16)=16 (mask=false, a(16)=0), b(17)=1 |
| 73 | // (mask=true, b ← a = 17-16 = 1), b(32)=16 (mask=true, b ← a = 16). |
| 74 | assert!( |
| 75 | trimmed[0].contains("1.0000000E0"), |
| 76 | "missing b(1)=1: {:?}", |
| 77 | trimmed[0] |
| 78 | ); |
| 79 | assert!( |
| 80 | trimmed[0].contains("1.6000000E1"), |
| 81 | "missing b(16)=16 / b(32)=16: {:?}", |
| 82 | trimmed[0] |
| 83 | ); |
| 84 | // Order: b(1)=1, b(16)=16, b(17)=1, b(32)=16. |
| 85 | let expected = "1.0000000E0 1.6000000E1 1.0000000E0 1.6000000E1"; |
| 86 | assert_eq!(trimmed[0], expected, "WHERE result wrong: {:?}", trimmed[0]); |
| 87 | } |
| 88 |