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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use crate::ir::{BinOp, Expr, Program, UnOp};
use crate::optimizer::rewrite::rewrite_program;
use crate::optimizer::{fingerprint_program, vyre_pass, PassAnalysis, PassResult};
mod arithmetic;
use arithmetic::{
granlund_montgomery_div, horner_quadratic_u32, power_of_two_shift, reciprocal_constant_fold,
shift_add_decompose, synthesize_fma_add, synthesize_fma_sub,
};
/// Replace multiplication by powers of two with shifts.
#[derive(Debug, Default)]
#[vyre_pass(
name = "strength_reduce",
requires = ["const_fold"],
invalidates = ["value_numbering"]
)]
pub struct StrengthReduce;
impl StrengthReduce {
/// Decide whether this pass should run.
#[must_use]
#[inline]
pub fn analyze(_program: &Program) -> PassAnalysis {
PassAnalysis::RUN
}
/// Rewrite multiply-by-power-of-two expressions into left shifts.
///
/// AUDIT_2026-04-24 F-SR-01 (closed): `rewrite_program` already
/// preserves `non_composable_with_self` via `with_rewritten_entry`
/// (see builder.rs line ~134). No explicit call needed here.
#[must_use]
pub fn transform(program: Program) -> PassResult {
let mut peephole = crate::rewrite_rules! {
// x & 0 -> 0
bitand_zero: Expr::BinOp { op: BinOp::BitAnd, left: _, right } if matches!(right.as_ref(), Expr::LitU32(0)) => Expr::u32(0),
// 0 & x -> 0
bitand_zero_left: Expr::BinOp { op: BinOp::BitAnd, left, right: _ } if matches!(left.as_ref(), Expr::LitU32(0)) => Expr::u32(0),
// x & u32::MAX -> x
bitand_ones: Expr::BinOp { op: BinOp::BitAnd, left, right } if matches!(right.as_ref(), Expr::LitU32(u32::MAX)) => left.as_ref().clone(),
// u32::MAX & x -> x
bitand_ones_left: Expr::BinOp { op: BinOp::BitAnd, left, right } if matches!(left.as_ref(), Expr::LitU32(u32::MAX)) => right.as_ref().clone(),
// x | u32::MAX -> u32::MAX
bitor_ones: Expr::BinOp { op: BinOp::BitOr, left: _, right } if matches!(right.as_ref(), Expr::LitU32(u32::MAX)) => Expr::u32(u32::MAX),
// u32::MAX | x -> u32::MAX
bitor_ones_left: Expr::BinOp { op: BinOp::BitOr, left, right: _ } if matches!(left.as_ref(), Expr::LitU32(u32::MAX)) => Expr::u32(u32::MAX),
// x | 0 -> x
bitor_zero: Expr::BinOp { op: BinOp::BitOr, left, right } if matches!(right.as_ref(), Expr::LitU32(0)) => left.as_ref().clone(),
// 0 | x -> x
bitor_zero_left: Expr::BinOp { op: BinOp::BitOr, left, right } if matches!(left.as_ref(), Expr::LitU32(0)) => right.as_ref().clone(),
// x ^ 0 -> x
bitxor_zero: Expr::BinOp { op: BinOp::BitXor, left, right } if matches!(right.as_ref(), Expr::LitU32(0)) => left.as_ref().clone(),
// 0 ^ x -> x
bitxor_zero_left: Expr::BinOp { op: BinOp::BitXor, left, right } if matches!(left.as_ref(), Expr::LitU32(0)) => right.as_ref().clone(),
};
let (program, changed1) = rewrite_program(program, &mut peephole);
let (program, changed2) = rewrite_program(program, reduce_expr);
PassResult {
program,
changed: changed1 || changed2,
}
}
/// Fingerprint this pass's visible input.
#[must_use]
#[inline]
pub fn fingerprint(program: &Program) -> u64 {
fingerprint_program(program)
}
}
/// Peepholes for UnOp and Select shapes that are independent of BinOp
/// strength reduction.
fn reduce_expr_extra(expr: &Expr) -> Option<Expr> {
match expr {
// Negate(Negate(x)) → x. Two complementary unary ops cancel.
Expr::UnOp {
op: UnOp::Negate,
operand,
} => {
if let Expr::UnOp {
op: UnOp::Negate,
operand: inner,
} = operand.as_ref()
{
return Some(inner.as_ref().clone());
}
None
}
// BitNot(BitNot(x)) → x. Two-fold complement is identity.
Expr::UnOp {
op: UnOp::BitNot,
operand,
} => {
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = operand.as_ref()
{
return Some(inner.as_ref().clone());
}
None
}
// ReverseBits(ReverseBits(x)) → x. Reverse-of-reverse is identity.
Expr::UnOp {
op: UnOp::ReverseBits,
operand,
} => {
if let Expr::UnOp {
op: UnOp::ReverseBits,
operand: inner,
} = operand.as_ref()
{
return Some(inner.as_ref().clone());
}
None
}
// select(c, x, x) → x. When both arms are identical the
// condition is dead. Comes up after CSE merges arms.
Expr::Select {
cond: _,
true_val,
false_val,
} if true_val.as_ref() == false_val.as_ref() => Some(true_val.as_ref().clone()),
// select(true, a, b) → a. Constant-true condition.
Expr::Select {
cond,
true_val,
false_val: _,
} if matches!(cond.as_ref(), Expr::LitBool(true)) => Some(true_val.as_ref().clone()),
// select(false, a, b) → b. Constant-false condition.
Expr::Select {
cond,
true_val: _,
false_val,
} if matches!(cond.as_ref(), Expr::LitBool(false)) => Some(false_val.as_ref().clone()),
_ => None,
}
}
fn reduce_expr(expr: &Expr) -> Option<Expr> {
if let Some(reduced) = reduce_expr_extra(expr) {
return Some(reduced);
}
if let Some(reduced) = horner_quadratic_u32(expr) {
return Some(reduced);
}
let Expr::BinOp { op, left, right } = expr else {
return None;
};
match op {
// Integer Mul-by-2^k → Shl by k.
BinOp::Mul => {
if matches!(right.as_ref(), Expr::LitU32(0)) {
return Some(Expr::u32(0));
}
if matches!(left.as_ref(), Expr::LitU32(0)) {
return Some(Expr::u32(0));
}
if matches!(right.as_ref(), Expr::LitU32(1)) {
return Some(left.as_ref().clone());
}
if matches!(left.as_ref(), Expr::LitU32(1)) {
return Some(right.as_ref().clone());
}
if let Some(shift) = power_of_two_shift(right) {
return Some(Expr::shl(left.as_ref().clone(), Expr::u32(shift)));
}
if let Some(shift) = power_of_two_shift(left) {
return Some(Expr::shl(right.as_ref().clone(), Expr::u32(shift)));
}
// ── Shift-add decomposition for non-power-of-two constants ──
// GPU imul is 4-8 cycles; shift+add/sub is 2 cycles.
// x * C → (x << hi) ± (x << lo) when C = 2^hi ± 2^lo.
// This fires for the most common index/stride multipliers
// found in real GPU kernels (3, 5, 6, 7, 9, 10, 12, 15).
if let Some(decomposed) = shift_add_decompose(left.as_ref(), right.as_ref()) {
return Some(decomposed);
}
if let Some(decomposed) = shift_add_decompose(right.as_ref(), left.as_ref()) {
return Some(decomposed);
}
// Float: x * 2.0 → x + x (saves a mul, uses cheaper add).
if matches!(right.as_ref(), Expr::LitF32(v) if *v == 2.0) {
return Some(Expr::add(left.as_ref().clone(), left.as_ref().clone()));
}
if matches!(left.as_ref(), Expr::LitF32(v) if *v == 2.0) {
return Some(Expr::add(right.as_ref().clone(), right.as_ref().clone()));
}
// Float: x * 1.0 → x (multiplicative identity).
if matches!(right.as_ref(), Expr::LitF32(v) if *v == 1.0) {
return Some(left.as_ref().clone());
}
if matches!(left.as_ref(), Expr::LitF32(v) if *v == 1.0) {
return Some(right.as_ref().clone());
}
None
}
// Unsigned Div-by-2^k → Shr by k. Only fires when rhs is a
// LitU32 power of two — LitI32 paths avoid signed semantics
// mismatch (negative dividend + rounding direction).
BinOp::Div => {
// ROADMAP G2: 1.0 / constant → compile-time reciprocal literal.
if let Some(folded) = reciprocal_constant_fold(left.as_ref(), right.as_ref()) {
return Some(folded);
}
// ROADMAP G2: 1.0 / x → Reciprocal(x). Keeping reciprocal as
// a first-class IR op lets strict backends emit precise rcp and
// ULP-budgeted backends emit approximate rcp without re-discovering
// the expression shape in every driver.
if matches!(left.as_ref(), Expr::LitF32(v) if *v == 1.0)
&& !matches!(right.as_ref(), Expr::LitF32(_))
{
return Some(Expr::reciprocal(right.as_ref().clone()));
}
match right.as_ref() {
Expr::LitU32(value) if value.is_power_of_two() => Some(Expr::shr(
left.as_ref().clone(),
Expr::u32(value.trailing_zeros()),
)),
// Granlund-Montgomery: any non-zero, non-power-of-two u32
// constant → mulhi(n, magic) >> shift.
// Saves 40-90 GPU cycles per division.
Expr::LitU32(d) if *d > 1 && !d.is_power_of_two() => {
granlund_montgomery_div(left.as_ref(), *d)
}
// Float: x / 2.0 → x * 0.5 (mul is cheaper than div).
Expr::LitF32(v) if *v == 2.0 => {
Some(Expr::mul(left.as_ref().clone(), Expr::f32(0.5)))
}
// Float: x / 1.0 → x (identity).
Expr::LitF32(v) if *v == 1.0 => Some(left.as_ref().clone()),
// Float: x / C → x * (1/C) for any non-zero finite constant.
// GPU fdiv is 4-8× slower than fmul; on training workloads
// with per-element normalization (LayerNorm, RMSNorm) this
// turns a ~32-cycle instruction into a ~4-cycle one.
Expr::LitF32(v) if v.is_finite() && *v != 0.0 => {
Some(Expr::mul(left.as_ref().clone(), Expr::f32(1.0 / v)))
}
_ => None,
}
}
// Unsigned Mod-by-2^k → BitAnd (2^k - 1).
BinOp::Mod => {
let Expr::LitU32(value) = right.as_ref() else {
return None;
};
if !value.is_power_of_two() {
return None;
}
Some(Expr::bitand(left.as_ref().clone(), Expr::u32(value - 1)))
}
// Float: x + 0.0 → x (additive identity).
BinOp::Add => {
if let Some(fma) = synthesize_fma_add(left, right) {
return Some(fma);
}
if matches!(right.as_ref(), Expr::LitF32(v) if *v == 0.0) {
return Some(left.as_ref().clone());
}
if matches!(left.as_ref(), Expr::LitF32(v) if *v == 0.0) {
return Some(right.as_ref().clone());
}
// Integer: x + 0 → x.
if matches!(right.as_ref(), Expr::LitU32(0)) {
return Some(left.as_ref().clone());
}
if matches!(left.as_ref(), Expr::LitU32(0)) {
return Some(right.as_ref().clone());
}
// ── Negation fusion ──────────────────────────────────
// x + (-y) → x - y (eliminates 1 negate instruction)
if let Expr::UnOp {
op: UnOp::Negate,
operand: y,
} = right.as_ref()
{
return Some(Expr::sub(left.as_ref().clone(), y.as_ref().clone()));
}
// (-x) + y → y - x
if let Expr::UnOp {
op: UnOp::Negate,
operand: x,
} = left.as_ref()
{
return Some(Expr::sub(right.as_ref().clone(), x.as_ref().clone()));
}
None
}
// Float: x - 0.0 → x (subtractive identity).
BinOp::Sub => {
if let Some(fma) = synthesize_fma_sub(left, right) {
return Some(fma);
}
if matches!(right.as_ref(), Expr::LitF32(v) if *v == 0.0) {
return Some(left.as_ref().clone());
}
if matches!(right.as_ref(), Expr::LitU32(0)) {
return Some(left.as_ref().clone());
}
// x - (-y) → x + y (eliminates 1 negate instruction)
if let Expr::UnOp {
op: UnOp::Negate,
operand: y,
} = right.as_ref()
{
return Some(Expr::add(left.as_ref().clone(), y.as_ref().clone()));
}
None
}
// ── Shift fusion + shift-by-zero elimination ────────────
// (x << a) << b → x << (a + b) when a,b are literal.
// x << 0 → x, x >> 0 → x.
BinOp::Shl | BinOp::Shr => {
// Shift by zero → identity.
if matches!(right.as_ref(), Expr::LitU32(0)) {
return Some(left.as_ref().clone());
}
// Chained shift fusion: (x <<|>> a) <<|>> b → x <<|>> (a+b)
// Only fuse when both shifts are the same direction.
if let Expr::BinOp {
op: inner_op,
left: x,
right: inner_shift,
} = left.as_ref()
{
if inner_op == op {
if let (Expr::LitU32(a), Expr::LitU32(b)) =
(inner_shift.as_ref(), right.as_ref())
{
let fused = a.saturating_add(*b).min(31);
return Some(Expr::BinOp {
op: *op,
left: x.clone(),
right: Box::new(Expr::u32(fused)),
});
}
}
}
None
}
// ── BitAnd mask fusion ──────────────────────────────────
// (x >> k) & mask where mask = (1 << n) - 1
// → extract n bits starting at position k.
// This is a recognition pass; the combined operation is
// already optimal but canonicalizing it aids CSE.
// ── BitAnd complement annihilator ───────────────────────
// x & ~x → 0, ~x & x → 0 (complementary mask cancellation)
BinOp::BitAnd => {
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = right.as_ref()
{
if inner.as_ref() == left.as_ref() {
return Some(Expr::u32(0));
}
}
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = left.as_ref()
{
if inner.as_ref() == right.as_ref() {
return Some(Expr::u32(0));
}
}
None
}
// ── BitOr complement → all-ones ─────────────────────────
// x | ~x → 0xFFFFFFFF, ~x | x → 0xFFFFFFFF
BinOp::BitOr => {
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = right.as_ref()
{
if inner.as_ref() == left.as_ref() {
return Some(Expr::u32(u32::MAX));
}
}
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = left.as_ref()
{
if inner.as_ref() == right.as_ref() {
return Some(Expr::u32(u32::MAX));
}
}
None
}
// ── BitXor complement → all-ones ────────────────────────
// x ^ ~x → 0xFFFFFFFF
BinOp::BitXor => {
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = right.as_ref()
{
if inner.as_ref() == left.as_ref() {
return Some(Expr::u32(u32::MAX));
}
}
if let Expr::UnOp {
op: UnOp::BitNot,
operand: inner,
} = left.as_ref()
{
if inner.as_ref() == right.as_ref() {
return Some(Expr::u32(u32::MAX));
}
}
None
}
// ── Rotate-by-zero → identity ───────────────────────────
BinOp::RotateLeft | BinOp::RotateRight if matches!(right.as_ref(), Expr::LitU32(0)) => {
Some(left.as_ref().clone())
}
// Rotate-by-32 (full width) → identity for u32
BinOp::RotateLeft | BinOp::RotateRight if matches!(right.as_ref(), Expr::LitU32(32)) => {
Some(left.as_ref().clone())
}
// ── AbsDiff self-identity ───────────────────────────────
// absdiff(x, x) → 0
BinOp::AbsDiff if left.as_ref() == right.as_ref() => Some(Expr::u32(0)),
// ── Min/Max with literal extremes ───────────────────────
// min(x, 0) → 0 for unsigned (u32 cannot be negative)
BinOp::Min if matches!(right.as_ref(), Expr::LitU32(0)) => Some(Expr::u32(0)),
BinOp::Min if matches!(left.as_ref(), Expr::LitU32(0)) => Some(Expr::u32(0)),
// max(x, 0) → x for unsigned
BinOp::Max if matches!(right.as_ref(), Expr::LitU32(0)) => Some(left.as_ref().clone()),
BinOp::Max if matches!(left.as_ref(), Expr::LitU32(0)) => Some(right.as_ref().clone()),
// min(x, MAX) → x, max(x, MAX) → MAX
BinOp::Min if matches!(right.as_ref(), Expr::LitU32(u32::MAX)) => {
Some(left.as_ref().clone())
}
BinOp::Min if matches!(left.as_ref(), Expr::LitU32(u32::MAX)) => {
Some(right.as_ref().clone())
}
BinOp::Max if matches!(right.as_ref(), Expr::LitU32(u32::MAX)) => Some(Expr::u32(u32::MAX)),
BinOp::Max if matches!(left.as_ref(), Expr::LitU32(u32::MAX)) => Some(Expr::u32(u32::MAX)),
// ── Comparison strength reduction ───────────────────────
// x < 0 → false for unsigned (u32 can never be negative)
BinOp::Lt if matches!(right.as_ref(), Expr::LitU32(0)) => Some(Expr::bool(false)),
// x >= 0 → true for unsigned
BinOp::Ge if matches!(right.as_ref(), Expr::LitU32(0)) => Some(Expr::bool(true)),
// 0 > x → false for unsigned
BinOp::Gt if matches!(left.as_ref(), Expr::LitU32(0)) => {
// 0 > x is false for all u32 x
Some(Expr::bool(false))
}
// 0 <= x → true for unsigned
BinOp::Le if matches!(left.as_ref(), Expr::LitU32(0)) => Some(Expr::bool(true)),
_ => None,
}
}
#[cfg(test)]
mod tests;