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
//! `motif` — intersect edge witnesses for a small graph pattern.
//!
//! Each motif edge is checked independently against the canonical
//! ProgramGraph CSR. If every requested motif edge exists, every
//! endpoint participating in the motif is marked in the final witness.
use std::sync::Arc;
use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
use crate::graph::program_graph::{
ProgramGraphShape, BINDING_PRIMITIVE_START, NAME_EDGE_KIND_MASK, NAME_EDGE_OFFSETS,
NAME_EDGE_TARGETS,
};
/// Canonical op id.
pub const OP_ID: &str = "vyre-primitives::graph::motif";
/// One directed motif edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MotifEdge {
/// Source node id.
pub from: u32,
/// Edge-kind mask that must match.
pub kind_mask: u32,
/// Destination node id.
pub to: u32,
}
/// Build a Program: one invocation checks every motif edge, records
/// participating endpoint bits only for matched edges, and publishes
/// the participant union if the whole motif matched.
///
/// Invalid motif sizes lower to an explicit trap program. Prior code
/// silently truncated `edges.len() as u32`; this path keeps the failure
/// executable without crashing the host process.
#[must_use]
pub fn motif(shape: ProgramGraphShape, edges: &[MotifEdge], witness_out: &str) -> Program {
let Ok(edge_count) = u32::try_from(edges.len()) else {
return crate::invalid_output_program(
OP_ID,
witness_out,
DataType::U32,
"Fix: motif edges.len() exceeds u32::MAX; split the motif or redesign the caller."
.to_string(),
);
};
let mut buffers = shape.read_only_buffers();
buffers.push(
BufferDecl::storage(
"motif_from",
BINDING_PRIMITIVE_START,
BufferAccess::ReadOnly,
DataType::U32,
)
.with_count(edge_count.max(1)),
);
buffers.push(
BufferDecl::storage(
"motif_kind",
BINDING_PRIMITIVE_START + 1,
BufferAccess::ReadOnly,
DataType::U32,
)
.with_count(edge_count.max(1)),
);
buffers.push(
BufferDecl::storage(
"motif_to",
BINDING_PRIMITIVE_START + 2,
BufferAccess::ReadOnly,
DataType::U32,
)
.with_count(edge_count.max(1)),
);
buffers.push(
BufferDecl::storage(
"motif_hits",
BINDING_PRIMITIVE_START + 3,
BufferAccess::ReadWrite,
DataType::U32,
)
.with_count(shape.node_count.max(1)),
);
buffers.push(
BufferDecl::storage(
witness_out,
BINDING_PRIMITIVE_START + 4,
BufferAccess::ReadWrite,
DataType::U32,
)
.with_count(shape.node_count.max(1)),
);
let clear_outputs = vec![
Node::store("motif_hits", Expr::var("node"), Expr::u32(0)),
Node::store(witness_out, Expr::var("node"), Expr::u32(0)),
];
// AUDIT_2026-04-24 F-MOT-02: guard `src < node_count` before
// loading `NAME_EDGE_OFFSETS[src]` and `NAME_EDGE_OFFSETS[src+1]`
// so a hand-crafted motif with `from >= node_count` cannot read
// past the graph offsets buffer on the GPU.
let scan_edge = vec![
Node::let_bind("src", Expr::load("motif_from", Expr::var("m"))),
Node::let_bind("dst", Expr::load("motif_to", Expr::var("m"))),
Node::let_bind("want_kind", Expr::load("motif_kind", Expr::var("m"))),
Node::let_bind("edge_found", Expr::u32(0)),
Node::if_then(
Expr::lt(Expr::var("src"), Expr::u32(shape.node_count)),
vec![
Node::let_bind(
"edge_start",
Expr::load(NAME_EDGE_OFFSETS, Expr::var("src")),
),
Node::let_bind(
"edge_end",
Expr::load(NAME_EDGE_OFFSETS, Expr::add(Expr::var("src"), Expr::u32(1))),
),
Node::loop_for(
"e",
Expr::var("edge_start"),
Expr::var("edge_end"),
vec![
Node::let_bind("actual_dst", Expr::load(NAME_EDGE_TARGETS, Expr::var("e"))),
Node::let_bind(
"actual_kind",
Expr::load(NAME_EDGE_KIND_MASK, Expr::var("e")),
),
Node::if_then(
Expr::and(
Expr::eq(Expr::var("actual_dst"), Expr::var("dst")),
Expr::ne(
Expr::bitand(Expr::var("actual_kind"), Expr::var("want_kind")),
Expr::u32(0),
),
),
vec![Node::assign("edge_found", Expr::u32(1))],
),
],
),
],
),
Node::if_then(
Expr::ne(Expr::var("edge_found"), Expr::u32(0)),
vec![
Node::assign(
"matched_edges",
Expr::add(Expr::var("matched_edges"), Expr::u32(1)),
),
Node::store("motif_hits", Expr::var("src"), Expr::u32(1)),
Node::store("motif_hits", Expr::var("dst"), Expr::u32(1)),
],
),
];
let materialize = vec![Node::store(
witness_out,
Expr::var("node"),
Expr::load("motif_hits", Expr::var("node")),
)];
// PHASE7_GRAPH C2: motif is fundamentally serial — one thread loops
// over every motif edge in order and accumulates `matched_edges`.
// Using a [256,1,1] workgroup with a `gid_x() == 0` gate burns 255
// idle lanes per workgroup. Dispatch a single 1-lane workgroup
// instead so the wasted parallelism is gone, and drop the redundant
// gate.
Program::wrapped(
buffers,
[1, 1, 1],
vec![Node::Region {
generator: Ident::from(OP_ID),
source_region: None,
body: Arc::new(vec![
Node::loop_for(
"node",
Expr::u32(0),
Expr::u32(shape.node_count),
clear_outputs,
),
Node::let_bind("matched_edges", Expr::u32(0)),
Node::loop_for("m", Expr::u32(0), Expr::u32(edge_count), scan_edge),
Node::if_then(
Expr::eq(Expr::var("matched_edges"), Expr::u32(edge_count)),
vec![Node::loop_for(
"node",
Expr::u32(0),
Expr::u32(shape.node_count),
materialize,
)],
),
]),
}],
)
}
/// CPU reference: return one byte-per-node witness set where `1`
/// means the node participates in a complete motif match.
#[must_use]
pub fn cpu_ref(
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
motif_edges: &[MotifEdge],
) -> Vec<u32> {
let mut participants = vec![0u32; node_count as usize];
let mut matched_edges = 0u32;
for motif_edge in motif_edges {
let mut found = false;
// AUDIT_2026-04-24 F-MOTIF-01/02/03: silent fall-through
// previously masked malformed CSR. Fail loudly.
let Some(start) = edge_offsets.get(motif_edge.from as usize).copied() else {
continue;
};
let Some(end) = edge_offsets.get(motif_edge.from as usize + 1).copied() else {
continue;
};
let start = start as usize;
let end = end as usize;
for edge_idx in start..end {
let Some(dst) = edge_targets.get(edge_idx).copied() else {
break;
};
let Some(kind) = edge_kind_mask.get(edge_idx).copied() else {
break;
};
if dst == motif_edge.to && (kind & motif_edge.kind_mask) != 0 {
found = true;
}
}
if !found {
return vec![0; node_count as usize];
}
matched_edges += 1;
if let Some(hit) = participants.get_mut(motif_edge.from as usize) {
*hit = 1;
}
if let Some(hit) = participants.get_mut(motif_edge.to as usize) {
*hit = 1;
}
}
if matched_edges == motif_edges.len() as u32 {
participants
} else {
vec![0; node_count as usize]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn three_node_chain_motif_marks_every_participant() {
let witness = cpu_ref(
3,
&[0, 1, 2, 2],
&[1, 2],
&[1, 1],
&[
MotifEdge {
from: 0,
kind_mask: 1,
to: 1,
},
MotifEdge {
from: 1,
kind_mask: 1,
to: 2,
},
],
);
assert_eq!(witness, vec![1, 1, 1]);
}
#[test]
fn missing_motif_edge_clears_all_participants() {
let witness = cpu_ref(
3,
&[0, 1, 1, 1],
&[1],
&[1],
&[
MotifEdge {
from: 0,
kind_mask: 1,
to: 1,
},
MotifEdge {
from: 1,
kind_mask: 1,
to: 2,
},
],
);
assert_eq!(witness, vec![0, 0, 0]);
}
}