1pub mod bench;
2pub mod dispatch;
3pub mod ffi;
4pub mod fusion;
5pub mod ir;
6pub mod passes;
7pub mod provenance;
8pub mod tuning;
9pub mod validate;
10
11pub const VERSION: &str = "0.1.0";
12
13pub fn generate_with_provenance(
17 kernel: &str,
18 elem: crate::ir::ElemType,
19 shape: &[i64],
20 score_gflops: f64,
21) -> Result<String, String> {
22 let region = crate::ir::build_kernel_region(kernel, elem, shape)?;
23 let _changes = crate::passes::default_pipeline().run(®ion);
24 let body = crate::ir::emit_tptir(®ion, kernel, &[]);
25 let prov = crate::provenance::Provenance::new(kernel, score_gflops);
26 Ok(format!("{}{}", prov.to_mlir_comment(), body))
27}
28
29pub fn compile(source: &str, target: &str) -> Result<String, String> {
30 #[cfg(feature = "ffi")]
31 {
32 ffi::compile_via_ffi(source, target)
33 }
34 #[cfg(not(feature = "ffi"))]
35 {
36 compile_native(source, target)
37 }
38}
39
40pub fn compile_native(source: &str, target: &str) -> Result<String, String> {
41 let region = ir::parse_assembly(source)?;
42 let passes = passes::default_pipeline();
43 let _changes = passes.run(®ion);
44 match target {
45 "tptisa" | "text" => Ok(region.to_string()),
46 "llvmir" => Ok(generate_llvm_ir(®ion)),
47 _ => Err(format!("Unknown target: {}", target)),
48 }
49}
50
51fn generate_llvm_ir(region: &ir::Region) -> String {
52 let mut out = String::from("; LLVM IR\ndefine void @kernel() {\n");
53 for block in ®ion.blocks {
54 out.push_str(&format!(" {}:\n", block.label));
55 for op in &block.operations {
56 out.push_str(&format!(" {}\n", op.display()));
57 }
58 }
59 out.push_str("}\n");
60 out
61}
62
63pub fn version() -> String {
64 format!("tptc-rs v{}", VERSION)
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_version() {
73 assert!(version().contains("0.1.0"));
74 }
75 #[test]
76 fn test_ir_types() {
77 let t = ir::Type::primitive("i32");
78 assert_eq!(t.to_string(), "i32");
79 }
80 #[test]
81 fn test_block() {
82 let b = ir::Block::new("entry");
83 assert_eq!(b.label, "entry");
84 }
85 #[test]
86 fn test_generate_with_provenance_header() {
87 let out = generate_with_provenance("vector_add", ir::ElemType::F32, &[1024], 0.0).unwrap();
88 assert!(out.starts_with("# TPTIR Provenance:"));
89 assert!(out.contains("module"));
90 }
91}