okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1#!/usr/bin/env python3 2"""msl_text_check.py - are two builds' kernel libraries the same text? 3 4STRATA_METAL_MSL_DUMP=<dir> strata ... (any program that compiles kernels through mtl::Context: the engine, a parity binary) 5python3 metal/tools/msl_text_check.py <dumpdir A> <dumpdir B> [--preprocess "clang -E -P -x c++ -nostdinc -undef"] [--air] 6 7The dump holds one NNN.msl per library (the prelude, <metal_stdlib>, ggml-common, then every kernel file: the exact string handed to 8the compiler) and index.txt (NNN.msl: the kernel files of that library). Libraries are matched by their file list, in order. 9 10For each pair the tool reports, strongest first: 11raw the two strings are byte-identical (sha256 equal) 12pp not identical, but the preprocessed texts are (the preprocessor's output with comments, blank lines and runs of blanks 13dropped): a change of comments, #ifndef guards or a macro that expands to the number it replaced 14air (--air, needs `xcrun metal`) the two compile to byte-identical AIR (same code, whatever the text) 15DIFFERENT none of the above 16Exit code 0 when every pair is raw or pp (or air with --air), 1 otherwise. 17""" 18import argparse 19import hashlib 20import os 21import re 22import shlex 23import subprocess 24import sys 25import tempfile 26 27 28def read_index (d ): 29out = [] 30p = os .path .join (d ,"index.txt" ) 31if not os .path .exists (p ): 32sys .exit ("no index.txt in " + d ) 33for ln in open (p ): 34ln = ln .rstrip ("\n" ) 35if not ln : 36continue 37name ,_ ,files = ln .partition (":" ) 38out .append ((name .strip (),tuple (os .path .basename (f )for f in files .split ())))# (the engine passes absolute paths, the tests relative ones) 39return out 40 41 42def sha (path ): 43return hashlib .sha256 (open (path ,"rb" ).read ()).hexdigest () 44 45 46def preprocess (path ,cmd ): 47# The C preprocessor of clang over the text, the <...> includes left out: none of the kernel sources branches on a Metal macro, so the 48# macros they and the host's prelude define are all there is to expand (`xcrun metal -E` needs the Metal toolchain component). 49with tempfile .NamedTemporaryFile (suffix = ".cpp" ,delete = False )as t : 50data = open (path ,"rb" ).read ().decode ("utf-8" ,"replace" ) 51data = re .sub (r"(?m)^[ ]*#[ ]*include[ ]*<[^>]*>" ,"" ,data )# <metal_stdlib>, <MetalPerformancePrimitives/...> 52t .write (data .encode ()) 53tmp = t .name 54try : 55r = subprocess .run (shlex .split (cmd )+ [tmp ],capture_output = True ,text = True ) 56if r .returncode != 0 : 57return None ,r .stderr [- 400 :] 58txt = r .stdout 59finally : 60os .unlink (tmp ) 61out = [] 62for ln in txt .splitlines (): 63if ln .startswith ("#" ): 64continue # line markers (the file names differ by temp path) 65ln = re .sub (r"//.*$" ,"" ,ln ) 66ln = re .sub (r"\s+" ," " ,ln ).strip () 67if ln : 68out .append (ln ) 69return "\n" .join (out ),"" 70 71 72def air (path ,tool ): 73with tempfile .TemporaryDirectory ()as td : 74src = os .path .join (td ,"x.metal" ) 75open (src ,"wb" ).write (open (path ,"rb" ).read ()) 76o = os .path .join (td ,"x.air" ) 77r = subprocess .run (shlex .split (tool )+ ["-c" ,src ,"-o" ,o ],capture_output = True ,text = True ) 78if r .returncode != 0 : 79return None ,r .stderr [- 400 :] 80return hashlib .sha256 (open (o ,"rb" ).read ()).hexdigest (),"" 81 82 83def main (): 84ap = argparse .ArgumentParser () 85ap .add_argument ("a" ) 86ap .add_argument ("b" ) 87ap .add_argument ("--preprocess" ,default = "clang -E -P -x c++ -nostdinc -undef" ) 88ap .add_argument ("--air" ,action = "store_true" ) 89ap .add_argument ("--air-tool" ,default = "xcrun -sdk macosx metal -std=metal3.2" ) 90a = ap .parse_args () 91ia ,ib = read_index (a .a ),read_index (a .b ) 92used = set () 93bad = 0 94for na ,fa in ia : 95match = None 96for nb ,fb in ib : 97if nb not in used and fb == fa : 98match = nb 99break 100if match is None : 101"%s %s: no library with this file list in B" % (na ," " .join (fa ))) 102bad += 1 103continue 104used .add (match ) 105pa ,pb = os .path .join (a .a ,na ),os .path .join (a .b ,match ) 106if sha (pa )== sha (pb ): 107"raw %s %s (%d bytes)" % (na ," " .join (fa ),os .path .getsize (pa ))) 108continue 109ta ,ea = preprocess (pa ,a .preprocess ) 110tb ,eb = preprocess (pb ,a .preprocess ) 111if ta is None or tb is None : 112"pp-ERROR %s %s: %s %s" % (na ," " .join (fa ),ea ,eb )) 113bad += 1 114continue 115if ta == tb : 116"pp %s %s (raw differs: %d vs %d bytes; preprocessed %d bytes identical)" % 117 (na ," " .join (fa ),os .path .getsize (pa ),os .path .getsize (pb ),len (ta ))) 118continue 119if a .air : 120xa ,_ = air (pa ,a .air_tool ) 121xb ,_ = air (pb ,a .air_tool ) 122if xa and xa == xb : 123"air %s %s (AIR identical)" % (na ," " .join (fa ))) 124continue 125la ,lb = ta .splitlines (),tb .splitlines () 126first = next ((i for i in range (min (len (la ),len (lb )))if la [i ]!= lb [i ]),min (len (la ),len (lb ))) 127"DIFFERENT %s %s: first differing preprocessed line %d\n A: %s\n B: %s" % 128 (na ," " .join (fa ),first ,la [first ][:200 ]if first < len (la )else "<end>" ,lb [first ][:200 ]if first < len (lb )else "<end>" )) 129bad += 1 130for nb ,fb in ib : 131if nb not in used : 132"%s %s: only in B" % (nb ," " .join (fb ))) 133bad += 1 134"libraries: A %d, B %d; %s" % (len (ia ),len (ib ),"all the same text" if bad == 0 else "%d differ" % bad )) 135sys .exit (1 if bad else 0 ) 136 137 138if __name__ == "__main__" : 139main ()