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
use std::sync::{Arc, OnceLock};
use rustc_hash::FxHashMap;
use crate::ir_inner::model::arena::{ArenaProgram, ExprArena};
use crate::ir_inner::model::node::Node;
use super::{BufferDecl, Program};
impl Program {
/// Synthetic generator id used when callers submit a raw top-level body
/// instead of an explicit `Node::Region`.
pub const ROOT_REGION_GENERATOR: &'static str = "vyre.program.root";
/// Create a complete program from buffer declarations, workgroup size, and
/// entry-point nodes, auto-wrapping the top-level body in a root Region
/// when necessary.
///
/// This is the default construction path for runnable Programs.
///
/// # Examples
///
/// ```
/// use vyre::ir::{BufferAccess, BufferDecl, DataType, Node, Program};
///
/// let program = Program::wrapped(
/// vec![BufferDecl::storage(
/// "output",
/// 0,
/// BufferAccess::ReadWrite,
/// DataType::U32,
/// )],
/// [64, 1, 1],
/// Vec::new(),
/// );
///
/// assert_eq!(program.workgroup_size(), [64, 1, 1]);
/// assert_eq!(program.buffers().len(), 1);
/// assert!(matches!(program.entry(), [Node::Region { .. }]));
/// ```
#[must_use]
#[inline]
pub fn wrapped(buffers: Vec<BufferDecl>, workgroup_size: [u32; 3], entry: Vec<Node>) -> Self {
Self::new_raw(buffers, workgroup_size, Self::wrap_entry(entry))
}
/// Create a complete program from buffer declarations, workgroup size, and
/// entry-point nodes.
///
/// # Examples
///
/// ```
/// use vyre::ir::{BufferAccess, BufferDecl, DataType, Node, Program};
///
/// let program = Program::wrapped(
/// vec![BufferDecl::storage(
/// "output",
/// 0,
/// BufferAccess::ReadWrite,
/// DataType::U32,
/// )],
/// [64, 1, 1],
/// Vec::new(),
/// );
///
/// assert_eq!(program.workgroup_size(), [64, 1, 1]);
/// assert_eq!(program.buffers().len(), 1);
/// assert!(matches!(program.entry(), [Node::Region { .. }]));
/// ```
#[deprecated(
note = "Program::new preserves raw top-level entry nodes. Use Program::wrapped for runnable programs; reserve Program::new for wire decode and negative tests."
)]
#[must_use]
#[inline]
pub fn new(buffers: Vec<BufferDecl>, workgroup_size: [u32; 3], entry: Vec<Node>) -> Self {
Self::new_raw(buffers, workgroup_size, entry)
}
#[must_use]
#[inline]
pub(crate) fn new_raw(
buffers: Vec<BufferDecl>,
workgroup_size: [u32; 3],
entry: Vec<Node>,
) -> Self {
let mut interner = FxHashMap::<Arc<str>, Arc<str>>::default();
interner.reserve(buffers.len());
let buffers: Vec<BufferDecl> = buffers
.into_iter()
.map(|mut b| {
let arc = interner
.entry(Arc::clone(&b.name))
.or_insert_with(|| Arc::clone(&b.name))
.clone();
b.name = arc;
b
})
.collect();
let buffer_index = Self::build_buffer_index(&buffers);
Self {
entry_op_id: None,
buffers: Arc::from(buffers),
buffer_index: Arc::new(buffer_index),
workgroup_size,
entry: Arc::new(entry),
hash: OnceLock::new(),
validation_set: OnceLock::new(),
structural_validated: std::sync::atomic::AtomicBool::new(false),
fingerprint: OnceLock::new(),
output_buffer_index: OnceLock::new(),
has_indirect_dispatch: OnceLock::new(),
stats: OnceLock::new(),
non_composable_with_self: false,
}
}
/// Same as [`Self::with_rewritten_entry`] but wraps the entry first via
/// the runnable-Region root contract (matches [`Self::wrapped`]). Use
/// from passes that produce a fully fresh entry body but want to reuse
/// the existing buffer Arc instead of paying for a full
/// [`Self::wrapped`] (which deep-clones buffers, re-interns names, and
/// rebuilds the buffer index).
#[must_use]
#[inline]
pub fn with_rewritten_wrapped_entry(&self, entry: Vec<Node>) -> Self {
self.with_rewritten_entry(Self::wrap_entry(entry))
}
/// Consume this program and rebuild it with `f` applied to the owned
/// entry vec. Reuses the entry Arc when uniquely owned (the common
/// case under the optimizer fixpoint) - no deep clone of the entry
/// body and no scaffold allocation. Equivalent to:
///
/// ```ignore
/// let scaffold = program.with_rewritten_entry(Vec::new());
/// let entry = f(program.into_entry_vec());
/// scaffold.with_rewritten_entry(entry)
/// ```
///
/// but produces only one new `Program` value instead of two.
#[must_use]
#[inline]
pub fn map_entry<F: FnOnce(Vec<Node>) -> Vec<Node>>(self, f: F) -> Self {
let entry_op_id = self.entry_op_id.clone();
let buffers = Arc::clone(&self.buffers);
let buffer_index = Arc::clone(&self.buffer_index);
let workgroup_size = self.workgroup_size;
let non_composable_with_self = self.non_composable_with_self;
let entry = f(self.into_entry_vec());
Self {
entry_op_id,
buffers,
buffer_index,
workgroup_size,
entry: Arc::new(entry),
hash: OnceLock::new(),
validation_set: OnceLock::new(),
structural_validated: std::sync::atomic::AtomicBool::new(false),
fingerprint: OnceLock::new(),
output_buffer_index: OnceLock::new(),
has_indirect_dispatch: OnceLock::new(),
stats: OnceLock::new(),
non_composable_with_self,
}
}
/// Clone this program with a replacement entry body while preserving the
/// existing buffer table, workgroup size, and optional certified op id.
#[must_use]
#[inline]
pub fn with_rewritten_entry(&self, entry: Vec<Node>) -> Self {
Self {
entry_op_id: self.entry_op_id.clone(),
buffers: Arc::clone(&self.buffers),
buffer_index: Arc::clone(&self.buffer_index),
workgroup_size: self.workgroup_size,
entry: Arc::new(entry),
hash: OnceLock::new(),
validation_set: OnceLock::new(),
structural_validated: std::sync::atomic::AtomicBool::new(false),
fingerprint: OnceLock::new(),
output_buffer_index: OnceLock::new(),
has_indirect_dispatch: OnceLock::new(),
stats: OnceLock::new(),
non_composable_with_self: self.non_composable_with_self,
}
}
/// Clone this program with replacement buffer declarations while
/// preserving the entry body, workgroup size, and metadata flags.
#[must_use]
#[inline]
pub fn with_rewritten_buffers(&self, buffers: Vec<BufferDecl>) -> Self {
let buffer_index = Self::build_buffer_index(&buffers);
Self {
entry_op_id: self.entry_op_id.clone(),
buffers: Arc::from(buffers),
buffer_index: Arc::new(buffer_index),
workgroup_size: self.workgroup_size,
entry: Arc::clone(&self.entry),
hash: OnceLock::new(),
validation_set: OnceLock::new(),
structural_validated: std::sync::atomic::AtomicBool::new(false),
fingerprint: OnceLock::new(),
output_buffer_index: OnceLock::new(),
has_indirect_dispatch: OnceLock::new(),
stats: OnceLock::new(),
non_composable_with_self: self.non_composable_with_self,
}
}
/// Clone this program with replacement dispatch dimensions and entry body
/// while preserving the existing buffer table, indexes, and metadata flags.
#[must_use]
#[inline]
pub fn with_rewritten_workgroup_size_and_entry(
&self,
workgroup_size: [u32; 3],
entry: Vec<Node>,
) -> Self {
Self {
entry_op_id: self.entry_op_id.clone(),
buffers: Arc::clone(&self.buffers),
buffer_index: Arc::clone(&self.buffer_index),
workgroup_size,
entry: Arc::new(entry),
hash: OnceLock::new(),
validation_set: OnceLock::new(),
structural_validated: std::sync::atomic::AtomicBool::new(false),
fingerprint: OnceLock::new(),
output_buffer_index: OnceLock::new(),
has_indirect_dispatch: OnceLock::new(),
stats: OnceLock::new(),
non_composable_with_self: self.non_composable_with_self,
}
}
/// Consume the program and return its entry nodes, reusing the
/// backing vector when this program owns the entry body uniquely.
#[must_use]
#[inline]
pub fn into_entry_vec(self) -> Vec<Node> {
Arc::try_unwrap(self.entry).unwrap_or_else(|entry| entry.as_ref().clone())
}
/// Create an arena-backed program scaffold.
///
/// This constructor is the opt-in migration path for builders that want
/// [`ExprRef`](crate::ir_inner::model::arena::ExprRef) handles instead of boxed
/// expression trees. [`Program::new`] remains the boxed-tree constructor.
#[must_use]
#[inline]
pub fn with_arena(
arena: &ExprArena,
buffers: Vec<BufferDecl>,
workgroup_size: [u32; 3],
) -> ArenaProgram<'_> {
ArenaProgram::new(arena, buffers, workgroup_size)
}
/// Create a minimal program with no buffers and an empty body.
///
/// # Examples
///
/// ```
/// use vyre::ir::Program;
///
/// let program = Program::empty();
///
/// assert!(program.buffers().is_empty());
/// assert_eq!(program.workgroup_size(), [1, 1, 1]);
/// assert!(program.is_explicit_noop());
/// ```
#[must_use]
#[inline]
pub fn empty() -> Self {
Self::wrapped(Vec::new(), [1, 1, 1], Vec::new())
}
/// Attach the stable operation ID whose conform registry entry certifies
/// this program for runtime lowering.
#[must_use]
#[inline]
pub fn with_entry_op_id(mut self, op_id: impl Into<String>) -> Self {
self.entry_op_id = Some(op_id.into());
self.invalidate_caches();
self
}
/// Stable operation ID required by the conform gate.
#[must_use]
#[inline]
pub fn entry_op_id(&self) -> Option<&str> {
self.entry_op_id.as_deref()
}
/// Attach an optional operation ID while preserving anonymous test IR.
#[must_use]
#[inline]
pub(crate) fn with_optional_entry_op_id(mut self, op_id: Option<String>) -> Self {
self.entry_op_id = op_id;
self.invalidate_caches();
self
}
}