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
//! SSA dominance-frontier lookahead scan.
use std::sync::Arc;
use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
use super::ast_ops::AST_ASSIGN;
/// Stable op id for the SSA dominance scan child region.
pub const OP_ID: &str = "vyre-primitives::parsing::ssa_dominance_scan";
/// Emit the bounded lookahead scan that allocates phi nodes when rival
/// assignments to the same variable cross block headers.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn ssa_dominance_scan(
ast_opcodes: &str,
ast_rights: &str,
ast_vals: &str,
block_headers: &str,
num_nodes: Expr,
out_phi_nodes: &str,
out_phi_count: &str,
t: Expr,
) -> Vec<Node> {
vec![
Node::let_bind("var_id", Expr::load(ast_vals, t.clone())),
Node::let_bind("blk", Expr::load(block_headers, t.clone())),
Node::let_bind("active", Expr::bool(true)),
Node::loop_for(
"lookahead",
Expr::add(t.clone(), Expr::u32(1)),
Expr::add(t.clone(), Expr::u32(64)),
vec![
Node::if_then(
Expr::ge(Expr::var("lookahead"), num_nodes.clone()),
vec![Node::assign("active", Expr::bool(false))],
),
Node::if_then(
Expr::and(
Expr::var("active"),
Expr::lt(Expr::var("lookahead"), num_nodes.clone()),
),
vec![
Node::let_bind("fwd_op", Expr::load(ast_opcodes, Expr::var("lookahead"))),
Node::let_bind("fwd_var", Expr::load(ast_vals, Expr::var("lookahead"))),
Node::let_bind(
"fwd_blk",
Expr::load(block_headers, Expr::var("lookahead")),
),
// Combined guard: same variable + rival assignment in a different block.
// Merged from two nested if_thens to stay within MAX_DEPTH=6.
Node::if_then(
Expr::and(
Expr::and(
Expr::eq(Expr::var("fwd_op"), Expr::u32(AST_ASSIGN)),
Expr::eq(Expr::var("fwd_var"), Expr::var("var_id")),
),
Expr::ne(Expr::var("fwd_blk"), Expr::var("blk")),
),
vec![
Node::let_bind(
"phi_idx",
Expr::atomic_add(out_phi_count, Expr::u32(0), Expr::u32(4)),
),
// Gate the 3 phi-record stores against the output capacity.
// `phi_idx` is a SHARED running allocation offset (atomic_add of
// 4 per match across ALL lanes); if total matches exceed
// phi_words/4 the later offsets run past `out_phi_nodes`. The
// reference silently DROPS OOB stores, masking the hazard, but a
// real GPU (CUDA does no bounds-checking) corrupts memory. We
// skip the store when full while STILL counting via the atomic
// above, the canonical GPU append-buffer overflow protocol:
// `out_phi_count` keeps rising past capacity so the caller
// detects the overflow (count > phi_words) and re-runs with a
// larger buffer. NOT a silent fallback (Law 10), the overflow
// is loud in the returned count; only the illegal write is
// removed. Depth 6 == MAX_DEPTH (the last available level, which
// is why the match condition above is merged into one `and`).
Node::if_then(
Expr::lt(
Expr::add(Expr::var("phi_idx"), Expr::u32(2)),
Expr::buf_len(out_phi_nodes),
),
vec![
Node::store(
out_phi_nodes,
Expr::var("phi_idx"),
Expr::var("var_id"),
),
Node::store(
out_phi_nodes,
Expr::add(Expr::var("phi_idx"), Expr::u32(1)),
Expr::load(ast_rights, t.clone()),
),
Node::store(
out_phi_nodes,
Expr::add(Expr::var("phi_idx"), Expr::u32(2)),
Expr::load(ast_rights, Expr::var("lookahead")),
),
],
),
Node::assign("active", Expr::bool(false)),
],
),
],
),
],
),
]
}
/// Build the standalone dominance-scan primitive.
#[must_use]
pub fn ssa_dominance_scan_program(num_nodes: u32, phi_words: u32) -> Program {
let t = Expr::InvocationId { axis: 0 };
// Control-flow nest the lane guard: `t < num_nodes` must gate the `ast_opcodes[t]`
// load via an `if_then`, NOT an `Expr::and`: a data-flow AND evaluates both
// operands, so `load(ast_opcodes, t)` would execute for over-fired lanes
// (t >= num_nodes; GPU dispatch rounds up to full workgroups), reading OOB (UB on
// CUDA; silently zero-filled on the reference). Nesting keeps the load inside the
// in-range branch. This guard is the outermost of the nest that bottoms out at the
// phi-capacity gate (depth 6 == MAX_DEPTH); the match condition below is merged into
// one `and` so that gate fits within the depth budget.
let body = vec![Node::if_then(
Expr::lt(t.clone(), Expr::u32(num_nodes)),
vec![Node::if_then(
Expr::eq(Expr::load("ast_opcodes", t.clone()), Expr::u32(AST_ASSIGN)),
ssa_dominance_scan(
"ast_opcodes",
"ast_rights",
"ast_vals",
"block_headers",
Expr::u32(num_nodes),
"out_phi_nodes",
"out_phi_count",
t,
),
)],
)];
Program::wrapped(
vec![
BufferDecl::storage("ast_opcodes", 0, BufferAccess::ReadOnly, DataType::U32)
.with_count(num_nodes),
BufferDecl::storage("ast_rights", 1, BufferAccess::ReadOnly, DataType::U32)
.with_count(num_nodes),
BufferDecl::storage("ast_vals", 2, BufferAccess::ReadOnly, DataType::U32)
.with_count(num_nodes),
BufferDecl::storage("block_headers", 3, BufferAccess::ReadOnly, DataType::U32)
.with_count(num_nodes),
BufferDecl::storage("out_phi_nodes", 4, BufferAccess::ReadWrite, DataType::U32)
.with_count(phi_words),
BufferDecl::storage("out_phi_count", 5, BufferAccess::ReadWrite, DataType::U32)
.with_count(1),
],
[num_nodes.max(1), 1, 1],
vec![Node::Region {
generator: Ident::from(OP_ID),
source_region: None,
body: Arc::new(body),
}],
)
}
#[cfg(feature = "inventory-registry")]
fn fixture_u32(words: &[u32]) -> Vec<u8> {
crate::wire::pack_u32_slice(words)
}
#[cfg(feature = "inventory-registry")]
inventory::submit! {
crate::harness::OpEntry::new(
OP_ID,
|| ssa_dominance_scan_program(4, 8),
Some(|| vec![vec![
fixture_u32(&[AST_ASSIGN, 0, AST_ASSIGN, 0]),
fixture_u32(&[10, 0, 20, 0]),
fixture_u32(&[7, 0, 7, 0]),
fixture_u32(&[1, 0, 2, 0]),
fixture_u32(&[0; 8]),
fixture_u32(&[0]),
]]),
Some(|| vec![vec![
fixture_u32(&[7, 10, 20, 0, 0, 0, 0, 0]),
fixture_u32(&[4]),
]]),
)
}