|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use minisdf_ast::{Field, TSParseError}; |
| 4 | +use minisdf_backend_spirv::{rspirv::binary::Assemble, LoweringError, SpirvBackend}; |
| 5 | +use minisdf_optimizer::OptError; |
| 6 | +use thiserror::Error; |
| 7 | + |
| 8 | +#[derive(Error, Debug)] |
| 9 | +pub enum CompileError { |
| 10 | + #[error("Failed to parse file into AST(s): {0}")] |
| 11 | + AstParsingFailed(#[from] TSParseError), |
| 12 | + #[error("High level type check failed")] |
| 13 | + HLTypeCheckFailed, |
| 14 | + #[error("Optimizer error: {0}")] |
| 15 | + OptErr(#[from] OptError), |
| 16 | + #[error("Failed to lower LLGraph to SPIR-V: {0}")] |
| 17 | + LoweringErr(#[from] LoweringError), |
| 18 | +} |
| 19 | + |
| 20 | +///Compiles fields in `file`. If successful, returns all SPIR-V modules and their field names. |
| 21 | +pub fn compile_file(file: impl AsRef<Path>) -> Result<Vec<(String, Vec<u32>)>, CompileError> { |
| 22 | + let p = file.as_ref(); |
| 23 | + |
| 24 | + let fields = minisdf_ast::parse_file(p)?; |
| 25 | + |
| 26 | + let mut asts = Vec::new(); |
| 27 | + for ast in fields { |
| 28 | + let name = ast.name.0.clone(); |
| 29 | + let spv = compile_ast(ast, file.as_ref())?; |
| 30 | + |
| 31 | + asts.push((name, spv)); |
| 32 | + } |
| 33 | + |
| 34 | + Ok(asts) |
| 35 | +} |
| 36 | + |
| 37 | +///Compiles the `ast` based on `file` into a SPIR-V buffer |
| 38 | +pub fn compile_ast(ast: Field, file: impl AsRef<Path>) -> Result<Vec<u32>, CompileError> { |
| 39 | + let hltree = minisdf_optimizer::hlgraph_from_ast(ast, file).unwrap(); |
| 40 | + |
| 41 | + //rvsdg_viewer::into_svg(&hltree.graph, &format!("hl_{}.svg", name)); |
| 42 | + |
| 43 | + if !hltree.type_check() { |
| 44 | + println!("Type check failed 😭"); |
| 45 | + return Err(CompileError::HLTypeCheckFailed); |
| 46 | + } |
| 47 | + let mut ll_graph = hltree.into_ll_graph(); |
| 48 | + |
| 49 | + //rvsdg_viewer::into_svg(&ll_graph.graph, &format!("ll_{}.svg", name)); |
| 50 | + |
| 51 | + ll_graph.verify()?; |
| 52 | + |
| 53 | + ll_graph.inline(); |
| 54 | + //rvsdg_viewer::into_svg(&ll_graph.graph, &format!("ll_{}_post_inline.svg", name)); |
| 55 | + |
| 56 | + ll_graph.cne(); |
| 57 | + //rvsdg_viewer::into_svg(&ll_graph.graph, &format!("ll_{}_post_cne.svg", name)); |
| 58 | + |
| 59 | + ll_graph.type_resolve()?; |
| 60 | + |
| 61 | + let spvmod = ll_graph.lower()?; |
| 62 | + let dta = spvmod.assemble(); |
| 63 | + Ok(dta) |
| 64 | +} |
0 commit comments