Skip to content

Commit a448ec4

Browse files
committed
add compiler library
1 parent fd1c195 commit a448ec4

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ members = [
88
"crates/minisdf-common",
99
"crates/minisdf-optimizer",
1010
"crates/minisdf-backend-spirv",
11+
"crates/msdfc",
1112

1213
"examples/simple"
13-
]
14+
, "crates/msdfc"]
1415

1516

1617
[workspace.dependencies]

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Proof of concept RVSDG based compiler for a simple SignedDistanceField language.
1111

1212
`minisdf-backend-spirv`: simple SPIR-V backend based on [rspirv](https://crates.io/crates/rspirv).
1313

14+
`msdfc`: **M**ini**Sdf** **C**ompiler: Simple library that accepts a miniSDF compiler source file, or AST, and generates, if possible, a SPIR-V Binary.
15+
1416
## Example
1517

1618
The following example defines a field that subtracts smoothed-box from a unit box that repeats in the domain 4³.

crates/minisdf-ast/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod ts_parser;
44
use minisdf_common::Span;
55
pub use ts_parser::parse_file;
6+
pub use ts_parser::TSParseError;
67

78
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
89
pub enum Ty {

crates/minisdf-ast/src/ts_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
Field, Ident, TypedArg,
1212
};
1313

14-
use self::err::TSParseError;
14+
pub use self::err::TSParseError;
1515
use minisdf_common::ErrorPrintBundle;
1616
mod err;
1717
mod tree;

crates/minisdf-backend-spirv/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use ahash::AHashMap;
2-
use err::LoweringError;
32
use minisdf_ast::Ty;
43
use minisdf_optimizer::{
54
rvsdg::{
@@ -15,6 +14,7 @@ use rspirv::{
1514
spirv::{self, Decoration, FunctionControl, LinkageType, Word},
1615
};
1716
mod err;
17+
pub use err::LoweringError;
1818
mod lltraversal;
1919

2020
pub trait SpirvBackend {

crates/minisdf-optimizer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod highlevel;
44
mod lowlevel;
55
mod type_check;
66

7+
pub use err::OptError;
78
pub use highlevel::{hlgraph_from_ast, HLGraph};
89
pub use lowlevel::{LLGraph, LLOp, LLOpTy};
910
pub use rvsdg;

crates/msdfc/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "msdfc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
minisdf-ast = {path = "../../crates/minisdf-ast"}
10+
minisdf-optimizer = {path = "../../crates/minisdf-optimizer"}
11+
minisdf-backend-spirv = {path = "../../crates/minisdf-backend-spirv"}
12+
rvsdg-viewer = {path = "../../../vola/crates/rvsdg-viewer"}
13+
thiserror.workspace = true
14+
bytemuck = "1.14"

crates/msdfc/src/lib.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)