|
| 1 | +//@ run-pass |
| 2 | +//! Test information regarding intrinsics and ensure we can retrieve the fallback body if it exists. |
| 3 | +//! |
| 4 | +//! This tests relies on the intrinsics implementation, and requires one intrinsic with and one |
| 5 | +//! without a body. It doesn't matter which intrinsic is called here, and feel free to update that |
| 6 | +//! if needed. |
| 7 | +
|
| 8 | +//@ ignore-stage1 |
| 9 | +//@ ignore-cross-compile |
| 10 | +//@ ignore-remote |
| 11 | +//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837 |
| 12 | + |
| 13 | +#![feature(rustc_private)] |
| 14 | + |
| 15 | +extern crate rustc_hir; |
| 16 | +#[macro_use] |
| 17 | +extern crate rustc_smir; |
| 18 | +extern crate rustc_driver; |
| 19 | +extern crate rustc_interface; |
| 20 | +extern crate stable_mir; |
| 21 | + |
| 22 | +use rustc_smir::rustc_internal; |
| 23 | +use stable_mir::mir::mono::{Instance, InstanceKind}; |
| 24 | +use stable_mir::mir::visit::{Location, MirVisitor}; |
| 25 | +use stable_mir::mir::{LocalDecl, Terminator, TerminatorKind}; |
| 26 | +use stable_mir::ty::{RigidTy, TyKind}; |
| 27 | +use std::collections::HashSet; |
| 28 | +use std::convert::TryFrom; |
| 29 | +use std::io::Write; |
| 30 | +use std::ops::ControlFlow; |
| 31 | + |
| 32 | +/// This function tests that we can correctly get type information from binary operations. |
| 33 | +fn test_intrinsics() -> ControlFlow<()> { |
| 34 | + // Find items in the local crate. |
| 35 | + let main_def = stable_mir::all_local_items()[0]; |
| 36 | + let main_instance = Instance::try_from(main_def).unwrap(); |
| 37 | + let main_body = main_instance.body().unwrap(); |
| 38 | + let mut visitor = CallsVisitor { locals: main_body.locals(), calls: Default::default() }; |
| 39 | + visitor.visit_body(&main_body); |
| 40 | + |
| 41 | + let calls = visitor.calls; |
| 42 | + assert_eq!(calls.len(), 2, "Expected 2 calls, but found: {calls:?}"); |
| 43 | + for intrinsic in &calls { |
| 44 | + check_intrinsic(intrinsic) |
| 45 | + } |
| 46 | + |
| 47 | + ControlFlow::Continue(()) |
| 48 | +} |
| 49 | + |
| 50 | +/// This check is unfortunately tight to the implementation of intrinsics. |
| 51 | +/// |
| 52 | +/// We want to ensure that StableMIR can handle intrinsics with and without fallback body. |
| 53 | +/// |
| 54 | +/// If by any chance this test breaks because you changed how an intrinsic is implemented, please |
| 55 | +/// update the test to invoke a different intrinsic. |
| 56 | +fn check_intrinsic(intrinsic: &Instance) { |
| 57 | + assert_eq!(intrinsic.kind, InstanceKind::Intrinsic); |
| 58 | + let name = intrinsic.intrinsic_name().unwrap(); |
| 59 | + if intrinsic.has_body() { |
| 60 | + let Some(body) = intrinsic.body() else { unreachable!("Expected a body") }; |
| 61 | + assert!(!body.blocks.is_empty()); |
| 62 | + assert_eq!(&name, "likely"); |
| 63 | + } else { |
| 64 | + assert!(intrinsic.body().is_none()); |
| 65 | + assert_eq!(&name, "size_of_val"); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +struct CallsVisitor<'a> { |
| 70 | + locals: &'a [LocalDecl], |
| 71 | + calls: HashSet<Instance>, |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a> MirVisitor for CallsVisitor<'a> { |
| 75 | + fn visit_terminator(&mut self, term: &Terminator, _loc: Location) { |
| 76 | + match &term.kind { |
| 77 | + TerminatorKind::Call { func, .. } => { |
| 78 | + let TyKind::RigidTy(RigidTy::FnDef(def, args)) = |
| 79 | + func.ty(self.locals).unwrap().kind() |
| 80 | + else { |
| 81 | + return; |
| 82 | + }; |
| 83 | + self.calls.insert(Instance::resolve(def, &args).unwrap()); |
| 84 | + } |
| 85 | + _ => {} |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 91 | +/// For that, it will first write the dummy crate into a file. |
| 92 | +/// Then it will create a `StableMir` using custom arguments and then |
| 93 | +/// it will run the compiler. |
| 94 | +fn main() { |
| 95 | + let path = "binop_input.rs"; |
| 96 | + generate_input(&path).unwrap(); |
| 97 | + let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()]; |
| 98 | + run!(args, test_intrinsics).unwrap(); |
| 99 | +} |
| 100 | + |
| 101 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 102 | + let mut file = std::fs::File::create(path)?; |
| 103 | + write!( |
| 104 | + file, |
| 105 | + r#" |
| 106 | + #![feature(core_intrinsics)] |
| 107 | + use std::intrinsics::*; |
| 108 | + pub fn use_intrinsics(init: bool) -> bool {{ |
| 109 | + let sz = unsafe {{ size_of_val("hi") }}; |
| 110 | + likely(init && sz == 2) |
| 111 | + }} |
| 112 | + "# |
| 113 | + )?; |
| 114 | + Ok(()) |
| 115 | +} |
0 commit comments