xlsynth-driver 0.42.0

Binary that integrates XLS capabilities into a driver program
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// SPDX-License-Identifier: Apache-2.0

use std::path::Path;

use clap::ArgMatches;
use serde::Serialize;
use xlsynth_g8r::aig::GateFn;
use xlsynth_g8r::aig_serdes::gate2ir::{repack_gate_fn_inputs_with_schema, GateFnInterfaceSchema};
use xlsynth_g8r::aig_serdes::load_aiger_auto::load_aiger_auto_from_path;
use xlsynth_g8r::gate_builder::GateBuilderOptions;
use xlsynth_g8r::gatify::ir2gate::GatifyOptions;
use xlsynth_g8r::ir2gate_utils::AdderMapping;
use xlsynth_g8r::ir_aig_sharing::{
    get_equivalences, prove_equivalence_candidates_varisat_streaming, CandidateProofResult,
    IrAigCandidateRhs, IrAigSharingOptions,
};
use xlsynth_pir::ir;
use xlsynth_pir::ir_parser;
use xlsynth_pir::ir_utils::is_structural_payload;

use crate::common::parse_bool_flag_or;
use crate::toolchain_config::ToolchainConfig;

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "kind")]
enum JsonBitMapping {
    #[serde(rename = "aig")]
    Aig { id: usize, negated: bool },
    #[serde(rename = "const")]
    Const { value: u8 },
    #[serde(rename = "unknown")]
    Unknown,
}

#[derive(Debug, Clone, Serialize)]
struct JsonNodeEntry {
    pir_node_ref_index: usize,
    pir_text_id: usize,
    pir_name: String,
    width: usize,
    bits_msb_to_lsb: Vec<JsonBitMapping>,
}

#[derive(Debug, Clone, Serialize)]
struct JsonInputs {
    pir_ir_path: String,
    aig_path: String,
    top: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
struct JsonOptions {
    samples: usize,
    seed: u64,
    exclude_structural_pir_nodes: bool,
    max_proofs: usize,
}

#[derive(Debug, Clone, Serialize)]
struct JsonDetectedBitsByKind {
    aig: usize,
    #[serde(rename = "const")]
    const_: usize,
}

#[derive(Debug, Clone, Serialize)]
struct JsonStats {
    candidates: usize,
    proved: usize,
    disproved: usize,
    skipped: usize,
    interesting_nodes: usize,
    interesting_bits: usize,
    detected_bits: usize,
    detected_percent: f64,
    detected_bits_by_kind: JsonDetectedBitsByKind,
}

#[derive(Debug, Clone, Serialize)]
struct JsonReport {
    tool: String,
    version: u32,
    inputs: JsonInputs,
    options: JsonOptions,
    stats: JsonStats,
    nodes: Vec<JsonNodeEntry>,
}

fn rhs_to_json(rhs: IrAigCandidateRhs) -> JsonBitMapping {
    match rhs {
        IrAigCandidateRhs::AigOperand(op) => JsonBitMapping::Aig {
            id: op.node.id,
            negated: op.negated,
        },
        IrAigCandidateRhs::Const(false) => JsonBitMapping::Const { value: 0 },
        IrAigCandidateRhs::Const(true) => JsonBitMapping::Const { value: 1 },
    }
}

fn format_mapping_rhs(rhs: IrAigCandidateRhs) -> String {
    match rhs {
        IrAigCandidateRhs::AigOperand(op) => {
            if op.negated {
                format!("!%{}", op.node.id)
            } else {
                format!("%{}", op.node.id)
            }
        }
        IrAigCandidateRhs::Const(false) => "0".to_string(),
        IrAigCandidateRhs::Const(true) => "1".to_string(),
    }
}

fn choose_preferred_operand(
    a: xlsynth_g8r::aig::gate::AigOperand,
    b: xlsynth_g8r::aig::gate::AigOperand,
) -> xlsynth_g8r::aig::gate::AigOperand {
    // Deterministic choice: smaller node id wins; if tied, non-negated wins.
    match a.node.id.cmp(&b.node.id) {
        std::cmp::Ordering::Less => a,
        std::cmp::Ordering::Greater => b,
        std::cmp::Ordering::Equal => {
            if a.negated == b.negated {
                a
            } else if !a.negated {
                a
            } else {
                b
            }
        }
    }
}

fn choose_preferred_rhs(a: IrAigCandidateRhs, b: IrAigCandidateRhs) -> IrAigCandidateRhs {
    match (a, b) {
        (IrAigCandidateRhs::Const(_), _) => a,
        (_, IrAigCandidateRhs::Const(_)) => b,
        (IrAigCandidateRhs::AigOperand(aop), IrAigCandidateRhs::AigOperand(bop)) => {
            IrAigCandidateRhs::AigOperand(choose_preferred_operand(aop, bop))
        }
    }
}

fn load_aig_gate_fn(path: &Path) -> Result<GateFn, String> {
    load_aiger_auto_from_path(path, GateBuilderOptions::no_opt())
        .map(|res| res.gate_fn)
        .map_err(|e| format!("failed to load {}: {}", path.display(), e))
}

fn load_pir_top_fn(ir_path: &Path, top: Option<&str>) -> Result<(ir::Package, ir::Fn), String> {
    let text = std::fs::read_to_string(ir_path)
        .map_err(|e| format!("failed to read {}: {}", ir_path.display(), e))?;
    let mut parser = ir_parser::Parser::new(&text);
    let pkg = parser.parse_and_validate_package().map_err(|e| {
        format!(
            "failed to parse/validate PIR package {}: {}",
            ir_path.display(),
            e
        )
    })?;
    let f = if let Some(name) = top {
        pkg.get_fn(name)
            .ok_or_else(|| format!("top function '{}' not found in {}", name, ir_path.display()))?
            .clone()
    } else {
        pkg.get_top_fn()
            .ok_or_else(|| format!("no top function found in {}", ir_path.display()))?
            .clone()
    };
    Ok((pkg, f))
}

pub fn handle_ir_aig_sharing(matches: &ArgMatches, _config: &Option<ToolchainConfig>) {
    let ir_path = Path::new(matches.get_one::<String>("pir_ir_file").unwrap());
    let aig_path = Path::new(matches.get_one::<String>("aig_file").unwrap());
    let top = matches.get_one::<String>("ir_top").map(|s| s.as_str());

    let sample_count = matches
        .get_one::<String>("sample_count")
        .map(|s| s.parse::<usize>().unwrap_or(256))
        .unwrap_or(256);
    let sample_seed = matches
        .get_one::<String>("sample_seed")
        .map(|s| s.parse::<u64>().unwrap_or(0))
        .unwrap_or(0);
    let exclude_structural = parse_bool_flag_or(
        matches,
        "exclude_structural_pir_nodes",
        /* default_value= */ true,
    );

    let max_proofs = matches
        .get_one::<String>("max_proofs")
        .map(|s| s.parse::<usize>().unwrap_or(0))
        .unwrap_or(0);
    let print_limit = matches
        .get_one::<String>("print")
        .map(|s| s.parse::<usize>().unwrap_or(0))
        .unwrap_or(0);
    let print_mappings_limit = matches
        .get_one::<String>("print_mappings")
        .map(|s| s.parse::<usize>().unwrap_or(0))
        .unwrap_or(0);
    let output_json_path = matches.get_one::<String>("output_json").map(|s| s.as_str());

    let (pir_pkg, pir_fn) = match load_pir_top_fn(ir_path, top) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };
    let gate_fn = match load_aig_gate_fn(aig_path) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };
    let schema = match GateFnInterfaceSchema::from_pir_fn(&pir_fn) {
        Ok(schema) => schema,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };
    let gate_fn = match repack_gate_fn_inputs_with_schema(gate_fn, &schema) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };

    let options = IrAigSharingOptions {
        sample_count,
        sample_seed,
        exclude_structural_pir_nodes: exclude_structural,
    };
    let mut candidates = match get_equivalences(&pir_pkg, &pir_fn, &gate_fn, &options) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };

    if max_proofs != 0 && candidates.len() > max_proofs {
        candidates.truncate(max_proofs);
    }

    let gatify_opts = GatifyOptions {
        fold: true,
        hash: true,
        check_equivalence: false,
        adder_mapping: AdderMapping::default(),
        array_index_lowering_strategy: Default::default(),
        mul_adder_mapping: None,
        range_info: None,
        enable_rewrite_carry_out: false,
        enable_rewrite_prio_encode: false,
        enable_rewrite_nary_add: false,
        enable_rewrite_mask_low: false,
    };

    // Streaming proof + streaming per-node mapping output (in PIR topo order).
    let mut proved = 0usize;
    let mut disproved = 0usize;
    let mut skipped = 0usize;
    let mut proof_index = 0usize;

    // Coverage stats: "interesting" PIR bits vs detected/proved.
    let mut interesting_total_bits = 0usize;
    let mut interesting_total_nodes = 0usize;
    for node_ref in pir_fn.node_refs() {
        if node_ref.index == 0 {
            continue; // reserved Nil
        }
        if exclude_structural {
            let node = pir_fn.get_node(node_ref);
            if is_structural_payload(&node.payload) {
                continue;
            }
        }
        let ir::Type::Bits(w) = pir_fn.get_node_ty(node_ref) else {
            continue;
        };
        interesting_total_nodes += 1;
        interesting_total_bits += *w;
    }
    let mut detected_bits: std::collections::HashSet<(usize, usize)> =
        std::collections::HashSet::new();
    let mut detected_bits_const = 0usize;
    let mut detected_bits_aig = 0usize;

    // State for streaming node vector printing.
    let mut current_node: Option<ir::NodeRef> = None;
    let mut current_width: usize = 0;
    let mut current_bits: Vec<Option<IrAigCandidateRhs>> = Vec::new(); // LSB=0 indexing
    let mut current_any_proved = false;
    let mut printed_nodes = 0usize;
    let mut json_nodes: Vec<JsonNodeEntry> = Vec::new();

    let flush_current_node = |pir_fn: &ir::Fn,
                              current_node: &mut Option<ir::NodeRef>,
                              current_width: &mut usize,
                              current_bits: &mut Vec<Option<IrAigCandidateRhs>>,
                              current_any_proved: &mut bool,
                              printed_nodes: &mut usize,
                              print_mappings_limit: usize,
                              json_nodes: &mut Vec<JsonNodeEntry>| {
        let Some(nr) = *current_node else {
            return;
        };
        if !*current_any_proved {
            *current_node = None;
            *current_width = 0;
            current_bits.clear();
            return;
        }
        let node = pir_fn.get_node(nr);
        let pir_name = xlsynth_pir::ir::node_textual_id(pir_fn, nr);

        let mut bits_msb_to_lsb: Vec<JsonBitMapping> = Vec::with_capacity(*current_width);
        let mut items: Vec<String> = Vec::with_capacity(*current_width);
        for bit_index in (0..*current_width).rev() {
            match current_bits.get(bit_index).copied().flatten() {
                Some(rhs) => {
                    bits_msb_to_lsb.push(rhs_to_json(rhs));
                    items.push(format_mapping_rhs(rhs));
                }
                None => {
                    bits_msb_to_lsb.push(JsonBitMapping::Unknown);
                    items.push("?".to_string());
                }
            }
        }

        json_nodes.push(JsonNodeEntry {
            pir_node_ref_index: nr.index,
            pir_text_id: node.text_id,
            pir_name: pir_name.clone(),
            width: *current_width,
            bits_msb_to_lsb,
        });

        let should_print = print_mappings_limit == 0 || *printed_nodes < print_mappings_limit;
        if should_print {
            println!(
                "{}: bits[{}] = [{}]",
                pir_name,
                current_width,
                items.join(", ")
            );
            *printed_nodes += 1;
        }

        *current_node = None;
        *current_width = 0;
        current_bits.clear();
        *current_any_proved = false;
    };

    let proofs = match prove_equivalence_candidates_varisat_streaming(
        &pir_fn,
        &gate_fn,
        &candidates,
        &gatify_opts,
        |p| {
            // Proof stats.
            match &p.result {
                CandidateProofResult::Proved => proved += 1,
                CandidateProofResult::Disproved { .. } => disproved += 1,
                CandidateProofResult::Skipped { .. } => skipped += 1,
            }

            // Streaming proof lines (optional).
            if print_limit != 0 && proof_index < print_limit {
                match &p.result {
                    CandidateProofResult::Proved => {
                        println!(
                            "proof[{}]: PROVED pir_text_id={} bit={} <-> {}",
                            proof_index,
                            p.candidate.pir_node_text_id,
                            p.candidate.bit_index,
                            format_mapping_rhs(p.candidate.rhs)
                        );
                    }
                    CandidateProofResult::Disproved {
                        counterexample_inputs,
                    } => {
                        println!(
                            "proof[{}]: DISPROVED pir_text_id={} bit={} <-> {} cex_inputs={:?}",
                            proof_index,
                            p.candidate.pir_node_text_id,
                            p.candidate.bit_index,
                            format_mapping_rhs(p.candidate.rhs),
                            counterexample_inputs
                        );
                    }
                    CandidateProofResult::Skipped { reason } => {
                        println!(
                            "proof[{}]: SKIPPED pir_text_id={} bit={} reason={}",
                            proof_index,
                            p.candidate.pir_node_text_id,
                            p.candidate.bit_index,
                            reason
                        );
                    }
                }
            }
            proof_index += 1;

            let nr = p.candidate.pir_node_ref;
            let ty = pir_fn.get_node_ty(nr);
            let ir::Type::Bits(w) = ty else {
                return;
            };
            if exclude_structural && is_structural_payload(&pir_fn.get_node(nr).payload) {
                return;
            }

            // If we moved to a new node in the candidate stream, flush the previous.
            if current_node.map(|x| x.index) != Some(nr.index) {
                flush_current_node(
                    &pir_fn,
                    &mut current_node,
                    &mut current_width,
                    &mut current_bits,
                    &mut current_any_proved,
                    &mut printed_nodes,
                    print_mappings_limit,
                    &mut json_nodes,
                );

                current_node = Some(nr);
                current_width = *w;
                current_bits = vec![None; *w];
                current_any_proved = false;
            }

            if let CandidateProofResult::Proved = p.result {
                // Coverage bookkeeping: count each (node,bit) once.
                if detected_bits.insert((nr.index, p.candidate.bit_index)) {
                    match p.candidate.rhs {
                        IrAigCandidateRhs::Const(_) => detected_bits_const += 1,
                        IrAigCandidateRhs::AigOperand(_) => detected_bits_aig += 1,
                    }
                }

                current_any_proved = true;
                let idx = p.candidate.bit_index;
                if idx < current_bits.len() {
                    current_bits[idx] = Some(match current_bits[idx] {
                        Some(existing) => choose_preferred_rhs(existing, p.candidate.rhs),
                        None => p.candidate.rhs,
                    });
                }
            }
        },
    ) {
        Ok(p) => p,
        Err(e) => {
            eprintln!("ir-aig-sharing error: {}", e);
            std::process::exit(2);
        }
    };

    // Flush last node in the stream.
    flush_current_node(
        &pir_fn,
        &mut current_node,
        &mut current_width,
        &mut current_bits,
        &mut current_any_proved,
        &mut printed_nodes,
        print_mappings_limit,
        &mut json_nodes,
    );

    // Summary line at the end (since proving/mappings may be streamed).
    println!(
        "ir-aig-sharing: samples={} seed={} candidates={} proved={} disproved={} skipped={}",
        sample_count,
        sample_seed,
        proofs.len(),
        proved,
        disproved,
        skipped
    );
    if interesting_total_bits != 0 {
        let detected_percent =
            (detected_bits.len() as f64) * 100.0 / (interesting_total_bits as f64);
        println!(
            "ir-aig-sharing coverage: detected_bits={}/{} ({:.2}%) (aig={} const={}) interesting_nodes={}",
            detected_bits.len(),
            interesting_total_bits,
            detected_percent,
            detected_bits_aig,
            detected_bits_const,
            interesting_total_nodes
        );
    }

    if let Some(path) = output_json_path {
        let detected_percent = if interesting_total_bits == 0 {
            0.0
        } else {
            (detected_bits.len() as f64) * 100.0 / (interesting_total_bits as f64)
        };
        let report = JsonReport {
            tool: "xlsynth-driver ir-aig-sharing".to_string(),
            version: 1,
            inputs: JsonInputs {
                pir_ir_path: ir_path.display().to_string(),
                aig_path: aig_path.display().to_string(),
                top: top.map(|s| s.to_string()),
            },
            options: JsonOptions {
                samples: sample_count,
                seed: sample_seed,
                exclude_structural_pir_nodes: exclude_structural,
                max_proofs,
            },
            stats: JsonStats {
                candidates: proofs.len(),
                proved,
                disproved,
                skipped,
                interesting_nodes: interesting_total_nodes,
                interesting_bits: interesting_total_bits,
                detected_bits: detected_bits.len(),
                detected_percent,
                detected_bits_by_kind: JsonDetectedBitsByKind {
                    aig: detected_bits_aig,
                    const_: detected_bits_const,
                },
            },
            nodes: json_nodes,
        };

        let s = serde_json::to_string_pretty(&report).expect("JSON serialization should not fail");
        if let Err(e) = std::fs::write(path, s) {
            eprintln!(
                "ir-aig-sharing error: failed to write --output-json {}: {}",
                path, e
            );
            std::process::exit(2);
        }
    }

    if disproved != 0 {
        std::process::exit(1);
    }
}