2017-09-26 12:37:39 -07:00
|
|
|
//! Runtime support needed for testing the stdsimd crate.
|
2017-09-20 10:28:00 -07:00
|
|
|
//!
|
|
|
|
|
//! This basically just disassembles the current executable and then parses the
|
|
|
|
|
//! output once globally and then provides the `assert` function which makes
|
|
|
|
|
//! assertions about the disassembly of a function.
|
|
|
|
|
|
2018-06-06 00:17:14 +02:00
|
|
|
#![cfg_attr(
|
|
|
|
|
feature = "cargo-clippy",
|
2018-11-10 15:45:16 +01:00
|
|
|
allow(clippy::missing_docs_in_private_items, clippy::print_stdout)
|
2018-06-06 00:17:14 +02:00
|
|
|
)]
|
2017-09-19 14:46:00 -07:00
|
|
|
|
|
|
|
|
extern crate assert_instr_macro;
|
|
|
|
|
extern crate backtrace;
|
|
|
|
|
extern crate cc;
|
|
|
|
|
#[macro_use]
|
|
|
|
|
extern crate lazy_static;
|
2017-10-27 17:55:29 +02:00
|
|
|
extern crate rustc_demangle;
|
|
|
|
|
extern crate simd_test_macro;
|
2018-09-12 11:46:27 +02:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate cfg_if;
|
2018-08-15 18:20:33 +02:00
|
|
|
|
2017-09-19 14:46:00 -07:00
|
|
|
pub use assert_instr_macro::*;
|
2017-09-26 12:37:39 -07:00
|
|
|
pub use simd_test_macro::*;
|
2018-09-12 11:46:27 +02:00
|
|
|
use std::{collections::HashMap, env, str};
|
|
|
|
|
|
2019-01-31 00:36:36 +00:00
|
|
|
// `println!` doesn't work on wasm32 right now, so shadow the compiler's `println!`
|
2018-12-13 20:17:30 -06:00
|
|
|
// macro with our own shim that redirects to `console.log`.
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! println {
|
|
|
|
|
($($args:tt)*) => (wasm::js_console_log(&format!($($args)*)))
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 11:46:27 +02:00
|
|
|
cfg_if! {
|
|
|
|
|
if #[cfg(target_arch = "wasm32")] {
|
|
|
|
|
extern crate wasm_bindgen;
|
2018-10-23 09:10:54 -07:00
|
|
|
extern crate console_error_panic_hook;
|
2018-09-12 11:46:27 +02:00
|
|
|
pub mod wasm;
|
|
|
|
|
use wasm::disassemble_myself;
|
|
|
|
|
} else {
|
|
|
|
|
mod disassembly;
|
|
|
|
|
use disassembly::disassemble_myself;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-19 14:46:00 -07:00
|
|
|
|
|
|
|
|
lazy_static! {
|
2018-12-13 17:26:22 -06:00
|
|
|
static ref DISASSEMBLY: HashMap<String, Vec<Function>> = disassemble_myself();
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Function {
|
2018-08-15 18:20:33 +02:00
|
|
|
addr: Option<usize>,
|
2017-09-19 14:46:00 -07:00
|
|
|
instrs: Vec<Instruction>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Instruction {
|
|
|
|
|
parts: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn normalize(symbol: &str) -> String {
|
|
|
|
|
let symbol = rustc_demangle::demangle(symbol).to_string();
|
2019-01-30 15:11:35 -08:00
|
|
|
let mut ret = match symbol.rfind("::h") {
|
2017-09-19 14:46:00 -07:00
|
|
|
Some(i) => symbol[..i].to_string(),
|
|
|
|
|
None => symbol.to_string(),
|
2019-01-30 15:11:35 -08:00
|
|
|
};
|
|
|
|
|
// Normalize to no leading underscore to handle platforms that may
|
2019-01-31 00:36:36 +00:00
|
|
|
// inject extra ones in symbol names.
|
2019-01-30 15:11:35 -08:00
|
|
|
while ret.starts_with("_") {
|
|
|
|
|
ret.remove(0);
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
2019-01-30 15:11:35 -08:00
|
|
|
return ret;
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-20 10:28:00 -07:00
|
|
|
/// Main entry point for this crate, called by the `#[assert_instr]` macro.
|
|
|
|
|
///
|
|
|
|
|
/// This asserts that the function at `fnptr` contains the instruction
|
|
|
|
|
/// `expected` provided.
|
2017-09-21 07:15:24 -07:00
|
|
|
pub fn assert(fnptr: usize, fnname: &str, expected: &str) {
|
2018-08-15 18:20:33 +02:00
|
|
|
let mut fnname = fnname.to_string();
|
|
|
|
|
let functions = get_functions(fnptr, &mut fnname);
|
2017-09-19 14:46:00 -07:00
|
|
|
assert_eq!(functions.len(), 1);
|
|
|
|
|
let function = &functions[0];
|
2017-09-20 10:28:00 -07:00
|
|
|
|
2018-01-28 20:35:37 -08:00
|
|
|
let mut instrs = &function.instrs[..];
|
2018-06-06 00:17:14 +02:00
|
|
|
while instrs.last().map_or(false, |s| s.parts == ["nop"]) {
|
2018-01-28 20:35:37 -08:00
|
|
|
instrs = &instrs[..instrs.len() - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-20 10:28:00 -07:00
|
|
|
// Look for `expected` as the first part of any instruction in this
|
|
|
|
|
// function, returning if we do indeed find it.
|
2017-09-25 13:55:48 -07:00
|
|
|
let mut found = false;
|
2018-01-28 20:35:37 -08:00
|
|
|
for instr in instrs {
|
2019-01-31 00:36:36 +00:00
|
|
|
// Get the first instruction, e.g., tzcntl in tzcntl %rax,%rax.
|
2017-09-19 14:46:00 -07:00
|
|
|
if let Some(part) = instr.parts.get(0) {
|
2019-01-31 00:36:36 +00:00
|
|
|
// Truncate the instruction with the length of the expected
|
2017-09-21 09:28:02 +02:00
|
|
|
// instruction: tzcntl => tzcnt and compares that.
|
2017-09-21 16:13:46 +02:00
|
|
|
if part.starts_with(expected) {
|
2017-09-25 13:55:48 -07:00
|
|
|
found = true;
|
2017-10-27 17:55:29 +02:00
|
|
|
break;
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 16:37:45 -06:00
|
|
|
// Look for `call` instructions in the disassembly to detect whether
|
|
|
|
|
// inlining failed: all intrinsics are `#[inline(always)]`, so
|
|
|
|
|
// calling one intrinsic from another should not generate `call`
|
|
|
|
|
// instructions.
|
|
|
|
|
let mut inlining_failed = false;
|
2018-01-28 20:35:37 -08:00
|
|
|
for (i, instr) in instrs.iter().enumerate() {
|
2018-01-03 16:37:45 -06:00
|
|
|
let part = match instr.parts.get(0) {
|
|
|
|
|
Some(part) => part,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
if !part.contains("call") {
|
2018-01-04 17:15:23 +01:00
|
|
|
continue;
|
2018-01-03 16:37:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// On 32-bit x86 position independent code will call itself and be
|
|
|
|
|
// immediately followed by a `pop` to learn about the current address.
|
|
|
|
|
// Let's not take that into account when considering whether a function
|
|
|
|
|
// failed inlining something.
|
2018-01-04 17:15:23 +01:00
|
|
|
let followed_by_pop = function
|
|
|
|
|
.instrs
|
|
|
|
|
.get(i + 1)
|
2018-01-03 16:37:45 -06:00
|
|
|
.and_then(|i| i.parts.get(0))
|
2018-01-04 17:15:23 +01:00
|
|
|
.map_or(false, |s| s.contains("pop"));
|
2018-01-03 16:37:45 -06:00
|
|
|
if followed_by_pop && cfg!(target_arch = "x86") {
|
2018-01-04 17:15:23 +01:00
|
|
|
continue;
|
2018-01-03 16:37:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inlining_failed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 11:33:20 +02:00
|
|
|
let instruction_limit = std::env::var("STDSIMD_ASSERT_INSTR_LIMIT")
|
2018-11-10 15:45:16 +01:00
|
|
|
.ok()
|
|
|
|
|
.map_or_else(
|
|
|
|
|
|| match expected {
|
2019-01-31 00:36:36 +00:00
|
|
|
// `cpuid` returns a pretty big aggregate structure, so exempt
|
|
|
|
|
// it from the slightly more restrictive 22 instructions below.
|
2018-11-10 15:45:16 +01:00
|
|
|
"cpuid" => 30,
|
|
|
|
|
|
2019-01-31 00:36:36 +00:00
|
|
|
// Apparently, on Windows, LLVM generates a bunch of
|
|
|
|
|
// saves/restores of xmm registers around these intstructions,
|
|
|
|
|
// which exceeds the limit of 20 below. As it seems dictated by
|
|
|
|
|
// Windows's ABI (I believe?), we probably can't do much
|
|
|
|
|
// about it.
|
2018-11-10 15:45:16 +01:00
|
|
|
"vzeroall" | "vzeroupper" if cfg!(windows) => 30,
|
|
|
|
|
|
|
|
|
|
// Intrinsics using `cvtpi2ps` are typically "composites" and
|
|
|
|
|
// in some cases exceed the limit.
|
|
|
|
|
"cvtpi2ps" => 25,
|
|
|
|
|
|
2019-02-18 18:21:08 +01:00
|
|
|
// core_arch/src/acle/simd32
|
|
|
|
|
"usad8" => 27,
|
|
|
|
|
"qadd8" | "qsub8" | "sadd8" | "sel" | "shadd8" | "shsub8" => 29,
|
|
|
|
|
|
2018-11-10 15:45:16 +01:00
|
|
|
// Original limit was 20 instructions, but ARM DSP Intrinsics
|
2019-01-31 00:36:36 +00:00
|
|
|
// are exactly 20 instructions long. So, bump the limit to 22
|
|
|
|
|
// instead of adding here a long list of exceptions.
|
2018-11-10 15:45:16 +01:00
|
|
|
_ => 22,
|
|
|
|
|
},
|
|
|
|
|
|v| v.parse().unwrap(),
|
|
|
|
|
);
|
2018-02-02 16:08:27 +01:00
|
|
|
let probably_only_one_instruction = instrs.len() < instruction_limit;
|
2017-09-25 13:55:48 -07:00
|
|
|
|
2018-01-03 16:37:45 -06:00
|
|
|
if found && probably_only_one_instruction && !inlining_failed {
|
2017-10-27 17:55:29 +02:00
|
|
|
return;
|
2017-09-25 13:55:48 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-20 10:28:00 -07:00
|
|
|
// Help debug by printing out the found disassembly, and then panic as we
|
|
|
|
|
// didn't find the instruction.
|
2018-08-15 18:20:33 +02:00
|
|
|
println!("disassembly for {}: ", fnname,);
|
2018-01-28 20:35:37 -08:00
|
|
|
for (i, instr) in instrs.iter().enumerate() {
|
2018-08-15 18:20:33 +02:00
|
|
|
let mut s = format!("\t{:2}: ", i);
|
2017-11-22 12:49:15 +01:00
|
|
|
for part in &instr.parts {
|
2018-08-15 18:20:33 +02:00
|
|
|
s.push_str(part);
|
|
|
|
|
s.push_str(" ");
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
2018-08-15 18:20:33 +02:00
|
|
|
println!("{}", s);
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
2017-09-25 13:55:48 -07:00
|
|
|
|
|
|
|
|
if !found {
|
2017-12-14 19:57:53 +01:00
|
|
|
panic!(
|
|
|
|
|
"failed to find instruction `{}` in the disassembly",
|
|
|
|
|
expected
|
|
|
|
|
);
|
2017-09-25 13:55:48 -07:00
|
|
|
} else if !probably_only_one_instruction {
|
2018-01-04 17:15:23 +01:00
|
|
|
panic!(
|
|
|
|
|
"instruction found, but the disassembly contains too many \
|
|
|
|
|
instructions: #instructions = {} >= {} (limit)",
|
2018-01-28 20:35:37 -08:00
|
|
|
instrs.len(),
|
2018-01-04 17:15:23 +01:00
|
|
|
instruction_limit
|
|
|
|
|
);
|
2018-01-03 16:37:45 -06:00
|
|
|
} else if inlining_failed {
|
2018-01-04 17:15:23 +01:00
|
|
|
panic!(
|
|
|
|
|
"instruction found, but the disassembly contains `call` \
|
|
|
|
|
instructions, which hint that inlining failed"
|
|
|
|
|
);
|
2017-09-25 13:55:48 -07:00
|
|
|
}
|
2017-09-19 14:46:00 -07:00
|
|
|
}
|
2017-10-18 11:35:11 -04:00
|
|
|
|
2018-08-15 18:20:33 +02:00
|
|
|
fn get_functions(fnptr: usize, fnname: &mut String) -> &'static [Function] {
|
|
|
|
|
// Translate this function pointer to a symbolic name that we'd have found
|
|
|
|
|
// in the disassembly.
|
|
|
|
|
let mut sym = None;
|
|
|
|
|
backtrace::resolve(fnptr as *mut _, |name| {
|
|
|
|
|
sym = name.name().and_then(|s| s.as_str()).map(normalize);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if let Some(sym) = &sym {
|
|
|
|
|
if let Some(s) = DISASSEMBLY.get(sym) {
|
|
|
|
|
*fnname = sym.to_string();
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let exact_match = DISASSEMBLY
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(_, list)| list.iter().any(|f| f.addr == Some(fnptr)));
|
|
|
|
|
if let Some((name, list)) = exact_match {
|
|
|
|
|
*fnname = name.to_string();
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(sym) = sym {
|
|
|
|
|
println!("assumed symbol name: `{}`", sym);
|
|
|
|
|
}
|
|
|
|
|
println!("maybe related functions");
|
|
|
|
|
for f in DISASSEMBLY.keys().filter(|k| k.contains(&**fnname)) {
|
|
|
|
|
println!("\t- {}", f);
|
|
|
|
|
}
|
|
|
|
|
panic!("failed to find disassembly of {:#x} ({})", fnptr, fnname);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 11:35:11 -04:00
|
|
|
pub fn assert_skip_test_ok(name: &str) {
|
|
|
|
|
if env::var("STDSIMD_TEST_EVERYTHING").is_err() {
|
2017-10-28 22:29:52 +02:00
|
|
|
return;
|
2017-10-18 11:35:11 -04:00
|
|
|
}
|
2018-06-06 00:17:14 +02:00
|
|
|
panic!("skipped test `{}` when it shouldn't be skipped", name);
|
2017-10-18 11:35:11 -04:00
|
|
|
}
|
2018-05-21 20:37:41 +02:00
|
|
|
|
|
|
|
|
// See comment in `assert-instr-macro` crate for why this exists
|
|
|
|
|
pub static mut _DONT_DEDUP: &'static str = "";
|