okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
805e11c3d
main
1#!/usr/bin/env python3 2"""Which code of a linked strata-vision (or strata-vision-own) holds instructions an older CPU lacks (RESULTS-OWN-3V.md PART H). 3 4The CPU tower compiles each kernel tier in a file of its own with that tier's flags and calls it only after a CPU check (cpu_caps.h); everything else 5must stay at the floor of the build. An instruction-set flag on a file that uses the standard library also reaches its inline and template copies 6(weak / COMDAT symbols), and the linker may keep such a copy for the whole program, so the check looks at the LINKED binary, function by function: 7 8floor none (SSE4.2, Nehalem / Westmere): no VEX / EVEX instruction (no AVX of any kind), no BMI1/2, no MOVBE; 9floor avx (Sandy Bridge): AVX allowed; no AVX2 (256-bit integer ops, broadcasts / permutes / gathers of AVX2), no FMA, no F16C, no EVEX, no BMI; 10floor avx_f16c (Ivy Bridge): as avx, F16C allowed. 11 12A function with such an instruction passes only if it is a LOCAL symbol of a kernel tier (the anonymous namespace `kn` of cpu_kernels_impl.h) or a tier's 13table getter (strata_vit::cpu_ops_<tier>), i.e. code reached only behind the CPU check. Anything else is printed, weak / COMDAT copies marked. 14 15python3 check_floor_isa.py <binary> --floor none|avx|avx_f16c exit 0 clean, 1 a violation, 77 no objdump / nm 16Linux (GNU binutils) only. 17""" 18import argparse 19import re 20import shutil 21import subprocess 22import sys 23 24FMA = re .compile (r"^vf(n?m(add|sub)|maddsub|msubadd)" ) 25F16C = re .compile (r"^vcvt(ph2ps|ps2ph)" ) 26AVX2_ONLY = re .compile (r"^(vpbroadcast|vbroadcasti128|vperm2i128|vpermd|vpermq|vpermps|vpermpd|v(p)?gather|vinserti128|vextracti128|vpmaskmov|vps[lr]lv|vpsrav|vpblendd)" ) 27BMI = re .compile (r"^(andn|bextr|blsi|blsmsk|blsr|bzhi|mulx|pdep|pext|rorx|sarx|shlx|shrx|movbe)\b" ) 28AVX1_YMM_INT_OK = ("vptest" ,"vpermilps" ,"vpermilpd" ,"vperm2f128" )# AVX (1) instructions whose names start with "vp" and take ymm 29 30 31def bad_reason (mn ,ops ,floor ): 32if "zmm" in ops or re .search (r"%k[0-7]|\{%k" ,ops )or re .search (r"[xy]mm(1[6-9]|2\d|3[01])\b" ,ops ): 33return "AVX-512" 34if BMI .match (mn ): 35return "BMI/MOVBE" 36if floor == "none" : 37if mn .startswith ("v" )and mn not in ("verr" ,"verw" ): 38return "AVX" 39return None 40if FMA .match (mn ): 41return "FMA" 42if F16C .match (mn )and floor != "avx_f16c" : 43return "F16C" 44if AVX2_ONLY .match (mn ): 45return "AVX2" 46if mn .startswith ("vp" )and "ymm" in ops and not mn .startswith (AVX1_YMM_INT_OK ): 47return "AVX2" 48return None 49 50 51def main (): 52ap = argparse .ArgumentParser () 53ap .add_argument ("binary" ) 54ap .add_argument ("--floor" ,required = True ,choices = ["none" ,"avx" ,"avx_f16c" ]) 55ap .add_argument ("--show" ,type = int ,default = 3 ,help = "instructions shown per function" ) 56a = ap .parse_args () 57if not shutil .which ("objdump" )or not shutil .which ("nm" ): 58"objdump / nm not found" ) 59return 77 60kind = {} 61for line in subprocess .run (["nm" ,"--defined-only" ,a .binary ],capture_output = True ,text = True ).stdout .splitlines (): 62p = line .split () 63if len (p )>= 3 : 64kind [int (p [0 ],16 )]= p [1 ] 65names = {} 66for line in subprocess .run (["nm" ,"--defined-only" ,"-C" ,a .binary ],capture_output = True ,text = True ).stdout .splitlines (): 67p = line .split (None ,2 ) 68if len (p )== 3 : 69names .setdefault (int (p [0 ],16 ),p [2 ]) 70proc = subprocess .Popen (["objdump" ,"-d" ,"-w" ,"--no-show-raw-insn" ,a .binary ],stdout = subprocess .PIPE ,text = True ,bufsize = 1 << 20 ) 71head = re .compile (r"^([0-9a-f]+) <(.+)>:$" ) 72ins = re .compile (r"^\s*[0-9a-f]+:\s+(\S+)\s*(.*)$" ) 73cur ,cur_addr ,hits = None ,0 , {} 74for line in proc .stdout : 75m = head .match (line ) 76if m : 77cur_addr = int (m .group (1 ),16 ) 78cur = names .get (cur_addr ,m .group (2 )) 79continue 80m = ins .match (line ) 81if not m or cur is None : 82continue 83mn ,ops = m .group (1 ),m .group (2 ) 84if mn in ("data16" ,"rex" ,"lock" ,"rep" ,"repz" ,"repnz" ,"notrack" ,"bnd" )and ops : 85mn ,_ ,ops = ops .partition (" " ) 86r = bad_reason (mn ,ops ,a .floor ) 87if r : 88h = hits .setdefault ((cur_addr ,cur ), {"reasons" :set (),"ex" : []}) 89h ["reasons" ].add (r ) 90if len (h ["ex" ])< a .show : 91h ["ex" ].append (mn + " " + ops .strip ()) 92proc .wait () 93ok_tier ,bad = [], [] 94for (addr ,name ),h in sorted (hits .items (),key = lambda x :x [0 ][1 ]): 95k = kind .get (addr ,"?" ) 96local = k in ("t" ,"d" ,"b" ,"r" ) 97tier = ("(anonymous namespace)::kn::" in name and local )or re .match (r"^strata_vit::cpu_ops_(sse41|avx|avx2|avx512)\(\)$" ,name ) 98 (ok_tier if tier else bad ).append ((name ,k ,h )) 99"floor %s: %d functions with instructions above the floor; %d are kernel-tier code behind the CPU check, %d are NOT" % ( 100a .floor ,len (hits ),len (ok_tier ),len (bad ))) 101for name ,k ,h in bad : 102weak = " WEAK/COMDAT" if k in ("W" ,"w" ,"V" ,"v" ,"u" )else "" 103" NOT GUARDED [%s]%s %s: %s | e.g. %s" % (k ,weak ,name [:200 ],"," .join (sorted (h ["reasons" ]))," ; " .join (h ["ex" ]))) 104return 1 if bad else 0 105 106 107if __name__ == "__main__" : 108sys .exit (main ())