pub struct Trace { /* private fields */ }Expand description
Records a run’s events and writes them out as a trace.
Give it to Run::with_observer, then write the run with
to_jsonl:
use bitwright::engine::{Engine, Run, Trace};
use bitwright::{Context, ParseOptions, Width};
let engine = Engine::standard();
let mut cx = Context::new();
let e = cx.parse("((x ^ 0x5a) + 3 - 3) ^ 0x5a", &ParseOptions::width(Width::W32))?;
let mut trace = Trace::new(&engine);
let out = engine.run(&mut cx, &[e], Run::default().with_observer(&mut trace))?;
let text = trace.to_jsonl(&mut cx, &[e], &out)?;
assert!(text.lines().any(|l| l.contains(r#""ev":"applied","pass":"linear""#)));§Format
One JSON object per line, in this order (format 1):
- a header:
{"trace":"bitwright","format":1,"version":…,"strategy":…,"max_rounds":…, "phases":[{"name":"fact_fold"},{"name":"local","groups":[…]},…]}; - each rule and host rewrite the events name, numbered from 0 in order of appearance:
{"rule":0,"name":"core.arith::add_xor_and","id":"…","kind":"identity","proven":true, "doc":…,"examples":[["…","…"]],"source":…,"ledger":…}(source, the rule as written, andledger, the evidence its proof ledger records, when known: always for the built-in rules, and for others fromwith_source),{"rewrite":0,"name":…}; - each node the events, results and assumptions name, with every node below it, operands
first:
{"node":12,"op":"add","w":32,"args":[5,9],"text":"x + y"}.opisconst(withvalue),sym(withname), a unary or binary operator’s name,cmp(withpred, its spelling:<u),zext,sext,extract(withlo),concat,select,fp(withfp_opandformat) orext(withoutput). Node numbers are the context’s own, so a node keeps its number across the whole trace: equal numbers are equal expressions. Only the nodes named directly carrytext, the expression as the printer writes it, and, withwith_facts,facts({"zero":"0x…","one":"0x…","u":["0x…","0x…"], "stride":1,"s":["0x…","0x…"]}: the known zero and one bits, the unsigned interval and its stride, the signed interval as bit patterns) and, under assumptions where they differ,assumed(the same, withrelies); - each constraint of the run’s assumptions (
with_assumptions):{"assume":0,"expr":40}; - the events, in order:
{"ev":"root","index":0,"root":20}(with"again":truefor a root worked on again),{"ev":"stage","stage":"cases","before":…,"after":…},{"ev":"phase_begin","round":0,"phase":1,"root":…},{"ev":"candidate","rule":3,"node":…},{"ev":"no_match","rule":3,"node":…}(alsodegradedandno_change),{"ev":"guard_false","rule":3,"node":…,"pred":"disjoint","args":[5,9]}(argsnull where an operand is no parameter),{"ev":"applied","rule":3,"before":…,"after":…}(or"pass":"linear", or"rewrite":0),{"ev":"relied","on":[0,2]}(with"later":truewhen constraints from 63 on may be among them),{"ev":"explain","pass":"invert", "node":…,"why":{"kind":"invert",…}},{"ev":"not_smaller","pass":"linear","node":…, "candidate":…,"added":4,"freed":3,"shared":false}(candidatenull when it was not built),{"ev":"folded","node":…,"after":…},{"ev":"vetoed",…},{"ev":"rejected",…,"reason":"verify"},{"ev":"phase_end","round":0,"phase":1, "root":…,"spent":{"node_visits":16,…}}(the nonzero work only; with"stopped":"node_visits"when a limit stopped it); - each root’s result:
{"result":0,"input":20,"expr":31,"changed":true,"end":"completed", "relies_on":[]}(endis alsobudget_terminated, withstopped, ordeclined, withcap); - the run’s counters:
{"stats":{…}}, every field ofStatsby name.
The why of an explain event is the pass’s Explanation as an object with its kind;
like the type, its fields may change in any release. A reader should skip events, fields
and kinds it does not know. The same run of the same engine on the same input writes the
same trace.
Implementations§
Source§impl Trace
impl Trace
Sourcepub fn new(engine: &Engine) -> Trace
pub fn new(engine: &Engine) -> Trace
An empty trace of runs of engine (its strategy goes in the header), which knows the
built-in rules’ text and proof ledger.
Sourcepub fn with_explanations(self, yes: bool) -> Trace
pub fn with_explanations(self, yes: bool) -> Trace
Asks the engine for the passes’ reasoning too (Detail::Explain), recorded as
explain events.
Sourcepub fn with_facts(self, yes: bool) -> Trace
pub fn with_facts(self, yes: bool) -> Trace
Writes the facts of every node named directly: known bits and intervals, and under the
assumptions given to with_assumptions where they differ.
Sourcepub fn with_source(self, source: &str, ledger: Option<&Ledger>) -> Trace
pub fn with_source(self, source: &str, ledger: Option<&Ledger>) -> Trace
The source of a rule program the engine links, with the ledger that vouches for it: the trace gives the text and evidence of its rules (those of the groups it declares).
Sourcepub fn with_assumptions(
&mut self,
cx: &mut Context,
a: &Assumptions,
) -> Result<(), Error>
pub fn with_assumptions( &mut self, cx: &mut Context, a: &Assumptions, ) -> Result<(), Error>
Records the run’s assumptions, written as assume lines (and used for the facts under
them). Call it with the assumptions the run is given.
Sourcepub fn to_jsonl(
&self,
cx: &mut Context,
roots: &[Expr],
outcome: &Outcome,
) -> Result<String, Error>
pub fn to_jsonl( &self, cx: &mut Context, roots: &[Expr], outcome: &Outcome, ) -> Result<String, Error>
The run as a trace: the header, the rules and nodes the events name, the assumptions,
the events, the result of each root (roots[i] is answered by outcome.roots[i]) and
the counters. See Trace for the format. Fails when an expression is not of cx.