From 15b0e620f8c8aeb8d87fbb5c94cf0e394ec9c731 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:26:29 +0000 Subject: [PATCH] feat(dev): compile throughput under every-access masking and four-level placement, emitting each block --- crates/stela-dev/src/compile/throughput.rs | 77 +++++++++++++++++++++- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/crates/stela-dev/src/compile/throughput.rs b/crates/stela-dev/src/compile/throughput.rs index d0bf479c53..5473820b70 100644 --- a/crates/stela-dev/src/compile/throughput.rs +++ b/crates/stela-dev/src/compile/throughput.rs @@ -6,7 +6,7 @@ use crate::args::*; use anyhow::{Context, Result, ensure}; use clap::Args; use std::{path::PathBuf, time::Instant}; -use stela_compiler::x86_64::{MemoryChecks, NativeSystem}; +use stela_compiler::x86_64::{MemoryChecks, NativeSystem, RangeCheck}; use stela_contracts::Chip; use stela_engine::machine::engine::{Description, Preparation, Prepared, window}; @@ -27,6 +27,19 @@ pub(crate) struct CompileThroughput { /// Compile the region this many times. #[arg(long, default_value_t = 1)] repeat: u32, + /// Mask every access's address, as blocks of a guest that discloses + /// FEAT_CSV3 do. + #[arg(long)] + every_access_masked: bool, + /// Check guest ranges for a four-level placement of Stela's window in + /// this many 2 TiB slices, rather than for five-level paging. + #[arg(long)] + four_level_slices: Option, + /// Write each block of the last pass as a JSON line to this new file: + /// its PC, consumed source words, native code and source map, for + /// inspecting what real code compiles to. + #[arg(long)] + emit: Option, } impl CompileThroughput { @@ -37,7 +50,16 @@ impl CompileThroughput { !code.is_empty() && code.len().is_multiple_of(4), "the input holds whole instructions" ); - let checks = MemoryChecks::system(true, false, true, false); + ensure!( + self.four_level_slices + .is_none_or(|slices| (2..64).contains(&slices)), + "a four-level placement takes 2 to 63 slices" + ); + let checks = MemoryChecks::system(true, false, true, self.every_access_masked); + let checks = match self.four_level_slices { + Some(slices) => checks.with_range(RangeCheck::four_level(slices)), + None => checks, + }; let preparation = Preparation::from_description(&Description { chip: Chip::AppleM2, target: self.cpu_target, @@ -52,9 +74,11 @@ impl CompileThroughput { withheld: 0, }) .map_err(|refusal| anyhow::anyhow!("{refusal}"))?; + let mut emitted = Vec::new(); let mut totals = Totals::default(); let started = Instant::now(); - for _ in 0..self.repeat { + for pass in 0..self.repeat { + let last = pass + 1 == self.repeat; totals = Totals::default(); let mut offset = 0; while offset < code.len() { @@ -65,6 +89,13 @@ impl CompileThroughput { totals.blocks += 1; totals.instructions += compiled.source_bytes / 4; totals.native_bytes += compiled.native.x86_64_code.len(); + if last && self.emit.is_some() { + emitted.push(Emitted::of( + pc, + &source[..compiled.source_bytes], + &compiled, + )); + } compiled.source_bytes.max(4) } Ok(Prepared::Checked(_) | Prepared::FloatingPointTrap) => 4, @@ -76,6 +107,20 @@ impl CompileThroughput { } } let nanoseconds = started.elapsed().as_nanos() / u128::from(self.repeat.max(1)); + if let Some(path) = &self.emit { + use std::io::Write; + let file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(path) + .with_context(|| format!("creating {}", path.display()))?; + let mut writer = std::io::BufWriter::new(file); + for block in &emitted { + serde_json::to_writer(&mut writer, block)?; + writer.write_all(b"\n")?; + } + writer.flush()?; + } println!( "blocks={} instructions={} refused={} native_bytes={} ns={nanoseconds} ns/block={} ns/instruction={}", totals.blocks, @@ -89,6 +134,32 @@ impl CompileThroughput { } } +/// One emitted block: its PC, consumed source and native code in hex, and +/// the native offset of each source instruction. +#[derive(serde::Serialize)] +struct Emitted { + pc: u64, + source: String, + native: String, + map: Vec, +} + +impl Emitted { + fn of(pc: u64, source: &[u8], compiled: &stela_compiler::block::CompiledBlock) -> Self { + Self { + pc, + source: hex::encode(source), + native: hex::encode(&compiled.native.x86_64_code), + map: compiled + .native + .source_map + .iter() + .map(|&(_, offset)| offset) + .collect(), + } + } +} + /// One pass's blocks, their instructions and native code, and the words no /// block could be compiled from. #[derive(Default)]