1#![doc = include_str!("../README.md")]
2
3pub mod lex;
4pub mod parse;
5pub mod print;
6
7pub use parse::parse as parse_text_raw;
8
9use tpt_uir_core::ir::Region;
10
11#[derive(Debug, Clone, PartialEq)]
13pub struct ParseError(pub String);
14
15impl core::fmt::Display for ParseError {
16 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17 write!(f, "TPT-UIR parse error: {}", self.0)
18 }
19}
20
21pub fn to_text(region: &Region) -> String {
23 print::to_text(region)
24}
25
26pub fn parse_text(src: &str) -> Result<Region, ParseError> {
28 parse::parse(src).map_err(ParseError)
29}
30
31pub struct Pretty<'a>(pub &'a Region);
33
34impl<'a> core::fmt::Display for Pretty<'a> {
35 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36 write!(f, "{}", print::to_text(self.0))
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use tpt_uir_core::attr::{Attribute, AttributeValue};
44 use tpt_uir_core::op_name::OpName;
45 use tpt_uir_core::quant::QuantizationParams;
46 use tpt_uir_core::types::{Dimension, ScalarType, ShapeSpec, TensorType, Type};
47 use tpt_uir_core::{Block, Operation};
48
49 fn sample() -> Region {
50 Region {
51 blocks: vec![Block {
52 arguments: vec![
53 (
54 0u32,
55 Type::Tensor(TensorType {
56 dtype: ScalarType::F32,
57 shape: Some(ShapeSpec {
58 dimensions: vec![
59 Dimension::Fixed(1),
60 Dimension::Symbolic("n".into()),
61 Dimension::Bounded {
62 symbol: "m".into(),
63 max_value: 1024,
64 },
65 ],
66 }),
67 }),
68 ),
69 (1u32, Type::Index),
70 ],
71 operations: vec![
72 Operation {
73 id: 1,
74 op_name: OpName::parse("core.add").unwrap(),
75 operands: vec![0, 1],
76 results: vec![2],
77 regions: vec![],
78 attributes: vec![
79 Attribute::i64("axis", -3),
80 Attribute::f64("scale", 1.5),
81 Attribute::string("name", "acc"),
82 Attribute::quantization(
83 "q",
84 QuantizationParams {
85 block_size: 32,
86 bytes_per_block: 18,
87 num_blocks: 4,
88 scales: Some(1),
89 },
90 ),
91 ],
92 },
93 Operation {
94 id: 2,
95 op_name: OpName::parse("tpt_gpu.launch").unwrap(),
96 operands: vec![2],
97 results: vec![],
98 regions: vec![Region {
99 blocks: vec![Block {
100 arguments: vec![],
101 operations: vec![Operation {
102 id: 9,
103 op_name: OpName::parse("core.mul").unwrap(),
104 operands: vec![2],
105 results: vec![3],
106 regions: vec![],
107 attributes: vec![],
108 }],
109 }],
110 }],
111 attributes: vec![],
112 },
113 ],
114 }],
115 }
116 }
117
118 #[test]
119 fn roundtrip_region() {
120 let region = sample();
121 let text = to_text(®ion);
122 let back = parse_text(&text).expect("parse should succeed");
123 assert_eq!(region, back, "round-trip mismatch:\n{}", text);
124 }
125
126 #[test]
127 fn roundtrip_shape_attribute() {
128 let mut region = sample();
129 region.blocks[0].operations[0]
130 .attributes
131 .push(Attribute::shape(
132 "grid",
133 ShapeSpec {
134 dimensions: vec![
135 Dimension::Fixed(128),
136 Dimension::Bounded {
137 symbol: "n".into(),
138 max_value: 4096,
139 },
140 ],
141 },
142 ));
143 let text = to_text(®ion);
144 assert!(text.contains("grid = shape<"), "got: {}", text);
145 let back = parse_text(&text).expect("shape attribute must parse");
146 assert_eq!(region, back, "round-trip mismatch:\n{}", text);
147 }
148
149 #[test]
150 fn empty_region() {
151 let region = Region {
152 blocks: vec![Block {
153 arguments: vec![],
154 operations: vec![],
155 }],
156 };
157 let text = to_text(®ion);
158 assert_eq!(parse_text(&text).unwrap(), region);
159 }
160
161 #[test]
162 fn pretty_display() {
163 let region = sample();
164 let s = format!("{}", Pretty(®ion));
165 assert!(s.contains("^bb0"));
166 assert!(s.contains("core.add"));
167 }
168
169 #[test]
170 fn rejects_garbage() {
171 assert!(parse_text("not a region @@@").is_err());
172 }
173
174 #[test]
178 fn bytes_attribute_roundtrips_through_text() {
179 let region_with = |bytes: Vec<u8>| Region {
180 blocks: vec![Block {
181 arguments: vec![],
182 operations: vec![Operation {
183 id: 1,
184 op_name: OpName::parse("tpt_crucible.const").unwrap(),
185 operands: vec![],
186 results: vec![1],
187 regions: vec![],
188 attributes: vec![Attribute {
189 key: "data".into(),
190 value: AttributeValue::Bytes(bytes),
191 }],
192 }],
193 }],
194 };
195
196 for payload in [
197 vec![0x00, 0x48, 0x79, 0xff], vec![0x12, 0x34, 0x56], vec![], ] {
201 let region = region_with(payload.clone());
202 let text = to_text(®ion);
203 let back = parse_text(&text).expect("parse should succeed");
204 assert_eq!(back, region, "round-trip mismatch for {payload:?}:\n{text}");
205 }
206
207 let text = to_text(®ion_with(vec![0xde, 0xad, 0xbe, 0xef]));
209 assert!(text.contains("bytes<deadbeef>"), "{text}");
210 }
211}