from pathlib import Path import hashlib,json,os,subprocess,time R=Path('');D=R/'pgo-c6';os.sched_setaffinity(0,{10}) m=json.loads((D/'metadata.json').read_text());flags=m['flags']+['-C','llvm-args=-hot-callsite-threshold=225','-C','llvm-args=-locally-hot-callsite-threshold=225'];raw=D/'bounded-raw';raw.mkdir(exist_ok=True) subprocess.run(['rustc',*flags,'--crate-name','standard','-C',f'profile-generate={raw}','benchmarks/standard.rs','-o',str(D/'bounded-instrumented')],cwd=D,check=True) rows=[] for n in m['training_cases']: print('Training',n,flush=True);start=time.time();out=subprocess.check_output([D/'bounded-instrumented','48000','256','4',n,'10'],text=True);rows.append(dict(case=n,stdout=out,seconds=time.time()-start)) profdata=Path(subprocess.check_output(['rustc','--print','sysroot'],text=True).strip())/'lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata' subprocess.run([profdata,'merge','-o',str(D/'bounded.profdata'),*[str(p)for p in sorted(raw.glob('*.profraw'))]],check=True) subprocess.run(['rustc',*flags,'--crate-name','standard','-C',f'profile-use={D}/bounded.profdata','-C','llvm-args=-pgo-warn-missing-function','benchmarks/standard.rs','-o',str(D/'bounded-after')],cwd=D,check=True) m.update(after_flags=flags,training=rows,profile_sha256=hashlib.sha256((D/'bounded.profdata').read_bytes()).hexdigest(),after_sha256=hashlib.sha256((D/'bounded-after').read_bytes()).hexdigest(),purpose='Bound PGO hot-callsite inlining to 225 to avoid the 27.6KiB Voice expansion observed in R28; evaluation cases reused for exploration') (D/'bounded-metadata.json').write_text(json.dumps(m,indent=2)+'\n');print('Prepared bounded PGO',flush=True)