lift-cli 0.4.3

LIFT compiler CLI: verify, analyse, optimise, print, compile, simulate, and predict from the command line
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
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "lift")]
#[command(version = "0.3.0")]
#[command(about = "LIFT — Language for Intelligent Frameworks and Technologies")]
#[command(
    long_about = "Unified IR for AI and Quantum Computing: Simulate → Predict → Optimise → Compile"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Verbose output
    #[arg(short, long, global = true)]
    verbose: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Verify a .lif file (SSA, types, linearity)
    Verify {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
    },
    /// Analyse a .lif file (FLOP count, memory, noise)
    Analyse {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
        /// Output format (text, json)
        #[arg(short, long, default_value = "text")]
        format: String,
    },
    /// Print the IR in human-readable form
    Print {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
    },
    /// Optimise a .lif file
    Optimise {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
        /// Configuration file (.lith)
        #[arg(short, long)]
        config: Option<PathBuf>,
        /// Output file
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
    /// Simulate and predict performance
    Predict {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
        /// Target device (a100, h100)
        #[arg(short, long, default_value = "a100")]
        device: String,
    },
    /// Export to target backend
    Export {
        /// Path to .lif source file
        #[arg(value_name = "FILE")]
        file: PathBuf,
        /// Target backend (llvm, qasm, onnx)
        #[arg(short, long)]
        backend: String,
        /// Output file
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
}

fn main() {
    let cli = Cli::parse();

    // Initialize tracing
    let filter = if cli.verbose { "debug" } else { "info" };
    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .init();

    let result = match cli.command {
        Commands::Verify { file } => cmd_verify(&file),
        Commands::Analyse { file, format } => cmd_analyse(&file, &format),
        Commands::Print { file } => cmd_print(&file),
        Commands::Optimise {
            file,
            config,
            output,
        } => cmd_optimise(&file, config.as_deref(), output.as_deref()),
        Commands::Predict { file, device } => cmd_predict(&file, &device),
        Commands::Export {
            file,
            backend,
            output,
        } => cmd_export(&file, &backend, output.as_deref()),
    };

    if let Err(e) = result {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}

fn load_and_parse(path: &std::path::Path) -> Result<lift_core::Context, String> {
    let source = std::fs::read_to_string(path)
        .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;

    let mut lexer = lift_ast::Lexer::new(&source);
    let tokens = lexer.tokenize().to_vec();
    if !lexer.errors().is_empty() {
        return Err(format!("Lexer errors: {:?}", lexer.errors()));
    }

    let mut parser = lift_ast::Parser::new(tokens);
    let program = parser
        .parse()
        .map_err(|e| format!("Parse errors: {:?}", e))?;

    let mut ctx = lift_core::Context::new();
    let mut builder = lift_ast::IrBuilder::new();
    builder.build_program(&mut ctx, &program)?;

    Ok(ctx)
}

fn cmd_verify(path: &std::path::Path) -> Result<(), String> {
    let ctx = load_and_parse(path)?;

    // Build the full dialect registry for semantic verification.
    let mut registry = lift_core::DialectRegistry::new();
    lift_core::dialect::register_builtin_dialects(&mut registry);
    lift_tensor::dialect::register_tensor_dialect(&mut registry);
    lift_quantum::dialect::register_quantum_dialect(&mut registry);
    lift_hybrid::dialect::register_hybrid_dialect(&mut registry);

    match lift_core::verifier::verify_with_dialects(&ctx, &registry) {
        Ok(()) => {
            println!("Verification passed: {}", path.display());
            println!("  Values: {}", ctx.values.len());
            println!("  Operations: {}", ctx.ops.len());
            println!("  Blocks: {}", ctx.blocks.len());
            println!("  Regions: {}", ctx.regions.len());
            Ok(())
        }
        Err(errors) => {
            eprintln!("Verification failed with {} error(s):", errors.len());
            for err in &errors {
                eprintln!("  - {}", err);
            }
            Err(format!("{} verification error(s)", errors.len()))
        }
    }
}

fn cmd_analyse(path: &std::path::Path, format: &str) -> Result<(), String> {
    let ctx = load_and_parse(path)?;
    let report = lift_sim::analyze_module(&ctx);
    let quantum = lift_sim::analyze_quantum_ops(&ctx);

    match format {
        "json" => {
            let json =
                serde_json::to_string_pretty(&report).map_err(|e| format!("JSON error: {}", e))?;
            println!("{}", json);
        }
        _ => {
            println!("=== LIFT Analysis Report ===");
            println!("File: {}", path.display());
            println!();
            println!("Operations: {}", report.num_ops);
            println!("  Tensor ops: {}", report.num_tensor_ops);
            println!("  Quantum ops: {}", report.num_quantum_ops);
            println!("  Hybrid ops: {}", report.num_hybrid_ops);
            println!();
            println!("Compute:");
            println!("  Total FLOPs: {}", format_flops(report.total_flops));
            println!(
                "  Total memory: {}",
                format_bytes(report.total_memory_bytes)
            );
            println!("  Peak memory: {}", format_bytes(report.peak_memory_bytes));

            if quantum.gate_count > 0 {
                println!();
                println!("Quantum:");
                println!("  Qubits: {}", quantum.num_qubits_used);
                println!("  Gate count: {}", quantum.gate_count);
                println!("  1Q gates: {}", quantum.one_qubit_gates);
                println!("  2Q gates: {}", quantum.two_qubit_gates);
                println!("  Measurements: {}", quantum.measurements);
                println!("  Estimated fidelity: {:.6}", quantum.estimated_fidelity);
            }

            if !report.op_breakdown.is_empty() {
                println!();
                println!("Op breakdown:");
                let mut ops: Vec<_> = report.op_breakdown.iter().collect();
                ops.sort_by(|a, b| b.1.cmp(a.1));
                for (name, count) in ops {
                    println!("  {}: {}", name, count);
                }
            }
        }
    }

    Ok(())
}

fn cmd_print(path: &std::path::Path) -> Result<(), String> {
    let ctx = load_and_parse(path)?;
    let output = lift_core::printer::print_ir(&ctx);
    println!("{}", output);
    Ok(())
}

fn cmd_optimise(
    path: &std::path::Path,
    config_path: Option<&std::path::Path>,
    output_path: Option<&std::path::Path>,
) -> Result<(), String> {
    let mut ctx = load_and_parse(path)?;

    let config = if let Some(cp) = config_path {
        let src =
            std::fs::read_to_string(cp).map_err(|e| format!("Failed to read config: {}", e))?;
        lift_config::ConfigParser::new()
            .parse(&src)
            .map_err(|e| format!("Config parse error: {}", e))?
    } else {
        lift_config::LithConfig::default()
    };

    let mut pm = lift_core::PassManager::new();

    // Resolve the effective pass pipeline (explicit passes, else by level).
    let effective = config.optimisation.effective_passes();

    tracing::info!(
        "Optimisation level {:?}: running {} passes (default pipeline when no explicit passes)",
        config.optimisation.level,
        effective.len()
    );

    // Warn about unknown passes.
    for unknown in config.optimisation.validate() {
        tracing::warn!(
            "Unknown optimisation pass (skipped): {} (known: {})",
            unknown,
            lift_config::OptimisationConfig::ALL_PASSES.join(", ")
        );
    }

    // Add passes based on config
    for pass_name in &effective {
        match pass_name.as_str() {
            "canonicalize" => pm.add_pass(Box::new(lift_opt::Canonicalize)),
            "constant-folding" => pm.add_pass(Box::new(lift_opt::ConstantFolding)),
            "dce" => pm.add_pass(Box::new(lift_opt::DeadCodeElimination)),
            "tensor-fusion" => pm.add_pass(Box::new(lift_opt::TensorFusion)),
            "gate-cancellation" => pm.add_pass(Box::new(lift_opt::GateCancellation)),
            "rotation-merge" => pm.add_pass(Box::new(lift_opt::RotationMerge)),
            "flash-attention" => pm.add_pass(Box::new(lift_opt::FlashAttentionPass::default())),
            "cse" => pm.add_pass(Box::new(lift_opt::CommonSubexprElimination)),
            "quantisation-pass" => pm.add_pass(Box::new(lift_opt::QuantisationPass::default())),
            "noise-aware-schedule" => pm.add_pass(Box::new(lift_opt::NoiseAwareSchedule)),
            "layout-mapping" => pm.add_pass(Box::new(lift_opt::LayoutMapping)),
            "real-routing" => {
                let qc = config.quantum.as_ref();
                let num_qubits = qc.map(|q| q.num_qubits).unwrap_or(8);
                let topology = match qc.map(|q| q.topology.as_str()) {
                    Some("grid") => lift_quantum::DeviceTopology::grid(4, 4),
                    Some("heavy_hex") => lift_quantum::DeviceTopology::heavy_hex(num_qubits),
                    Some("all_to_all") => lift_quantum::DeviceTopology::all_to_all(num_qubits),
                    Some("tree") => lift_quantum::DeviceTopology::tree(num_qubits),
                    _ => lift_quantum::DeviceTopology::linear(num_qubits),
                };
                pm.add_pass(Box::new(lift_opt::RealRouting::new(topology)))
            }
            "gate-decomposition" => {
                let provider = config
                    .quantum
                    .as_ref()
                    .and_then(|q| q.provider)
                    .map(|p| match p {
                        lift_config::QuantumProvider::Ibm => lift_quantum::Provider::IbmEagle,
                        lift_config::QuantumProvider::IbmKyoto => lift_quantum::Provider::IbmKyoto,
                        lift_config::QuantumProvider::Rigetti => lift_quantum::Provider::Rigetti,
                        lift_config::QuantumProvider::IonQ => lift_quantum::Provider::IonQ,
                        lift_config::QuantumProvider::Quantinuum => {
                            lift_quantum::Provider::Quantinuum
                        }
                        lift_config::QuantumProvider::Simulator => {
                            lift_quantum::Provider::Simulator
                        }
                    })
                    .unwrap_or(lift_quantum::Provider::Simulator);
                pm.add_pass(Box::new(lift_opt::GateDecomposition::new(provider)))
            }
            _ => {
                tracing::warn!("Unknown pass: {}", pass_name);
            }
        }
    }

    let results = pm.run_all(&mut ctx);

    println!("Optimisation results:");
    for (name, result) in &results {
        let status = match result {
            lift_core::PassResult::Changed => "changed",
            lift_core::PassResult::Unchanged => "unchanged",
            lift_core::PassResult::RolledBack => "rolled back",
            lift_core::PassResult::Error(e) => {
                eprintln!("  {} -> error: {}", name, e);
                "error"
            }
        };
        println!("  {} -> {}", name, status);
    }

    if let Some(out) = output_path {
        let ir = lift_core::printer::print_ir(&ctx);
        std::fs::write(out, ir).map_err(|e| format!("Failed to write output: {}", e))?;
        println!("Output written to: {}", out.display());
    }

    Ok(())
}

fn cmd_predict(path: &std::path::Path, device: &str) -> Result<(), String> {
    let ctx = load_and_parse(path)?;
    let report = lift_sim::analyze_module(&ctx);

    let cost_model = match device {
        "a100" => lift_sim::cost::CostModel::a100(),
        "h100" => lift_sim::cost::CostModel::h100(),
        _ => return Err(format!("Unknown device: {}. Use 'a100' or 'h100'", device)),
    };

    let prediction = lift_predict::predict_performance(&report, &cost_model);

    println!("=== LIFT Performance Prediction ===");
    println!("Device: {}", device.to_uppercase());
    println!();
    println!("Compute time: {:.4} ms", prediction.compute_time_ms);
    println!("Memory time: {:.4} ms", prediction.memory_time_ms);
    println!("Predicted time: {:.4} ms", prediction.predicted_time_ms);
    println!(
        "Arithmetic intensity: {:.2} FLOP/byte",
        prediction.arithmetic_intensity
    );
    println!("Bottleneck: {}", prediction.bottleneck);

    Ok(())
}

fn cmd_export(
    path: &std::path::Path,
    backend: &str,
    output_path: Option<&std::path::Path>,
) -> Result<(), String> {
    let ctx = load_and_parse(path)?;

    let output = match backend {
        "llvm" => {
            let exporter = lift_export::LlvmExporter::new();
            exporter.export(&ctx).map_err(|e| format!("{}", e))?
        }
        "qasm" => {
            let exporter = lift_export::QasmExporter::new();
            exporter.export(&ctx).map_err(|e| format!("{}", e))?
        }
        "onnx" => {
            let exporter = lift_export::OnnxExporter::new();
            exporter.export(&ctx).map_err(|e| format!("{}", e))?
        }
        _ => {
            return Err(format!(
                "Unknown backend: {}. Use 'llvm', 'qasm', or 'onnx'",
                backend
            ))
        }
    };

    if let Some(out) = output_path {
        std::fs::write(out, &output).map_err(|e| format!("Failed to write output: {}", e))?;
        println!("Exported to: {}", out.display());
    } else {
        println!("{}", output);
    }

    Ok(())
}

fn format_flops(flops: u64) -> String {
    if flops >= 1_000_000_000_000 {
        format!("{:.2} TFLOP", flops as f64 / 1e12)
    } else if flops >= 1_000_000_000 {
        format!("{:.2} GFLOP", flops as f64 / 1e9)
    } else if flops >= 1_000_000 {
        format!("{:.2} MFLOP", flops as f64 / 1e6)
    } else if flops >= 1_000 {
        format!("{:.2} KFLOP", flops as f64 / 1e3)
    } else {
        format!("{} FLOP", flops)
    }
}

fn format_bytes(bytes: u64) -> String {
    if bytes >= 1_073_741_824 {
        format!("{:.2} GiB", bytes as f64 / 1_073_741_824.0)
    } else if bytes >= 1_048_576 {
        format!("{:.2} MiB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1_024 {
        format!("{:.2} KiB", bytes as f64 / 1_024.0)
    } else {
        format!("{} B", bytes)
    }
}