morok-ir 0.1.0-alpha.2

Intermediate representation for the Morok ML compiler
Documentation
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Symbolic Integer (SInt) - dimensions that can be either concrete or symbolic.
//!
//! This module provides the SInt type, which represents a dimension that can be:
//! - A concrete compile-time constant (`usize`)
//! - A symbolic runtime expression (`Arc<UOp>`)
//! - An inference placeholder (`-1` in reshape)
//!
//! Arithmetic via `std::ops` traits matches Tinygrad's approach: concrete values fold
//! inline via native arithmetic, symbolic values produce UOp graph nodes. No
//! simplification at construction time — the pipeline handles it.
//!
//! `SInt::Infer` panics on any arithmetic — it must be resolved at the API boundary
//! (e.g. `try_reshape`) before computing.

use std::fmt;
use std::sync::Arc;

use crate::UOp;

/// Symbolic Integer - either a concrete value or a symbolic UOp expression.
///
/// # Examples
///
/// ```rust
/// # use morok_ir::{SInt, UOp, ConstValue, Op};
/// # use morok_dtype::DType;
/// // Concrete dimension
/// let static_dim = SInt::from(32);
/// assert!(static_dim.is_const());
/// assert_eq!(static_dim.as_const(), Some(32));
///
/// // Symbolic dimension - use DefineVar for truly dynamic dimensions
/// let batch_size = UOp::new(
///     Op::DefineVar { name: "batch".to_string(), min_val: 1, max_val: 1024 },
///     DType::Index,
/// );
/// let dynamic_dim = SInt::from(batch_size);
/// assert!(!dynamic_dim.is_const());
/// ```
#[derive(Debug, Clone)]
pub enum SInt {
    /// Concrete compile-time constant dimension.
    Const(usize),

    /// Symbolic runtime expression (must have dtype Index or Int).
    Symbolic(Arc<UOp>),

    /// Infer this dimension from the total element count (reshape -1 placeholder).
    Infer,
}

// Manual implementations using stable ID equality for Symbolic (consistent with hash consing)
impl PartialEq for SInt {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (SInt::Const(a), SInt::Const(b)) => a == b,
            (SInt::Symbolic(a), SInt::Symbolic(b)) => a.id == b.id,
            (SInt::Infer, SInt::Infer) => true,
            _ => false,
        }
    }
}

impl Eq for SInt {}

impl std::hash::Hash for SInt {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            SInt::Const(v) => v.hash(state),
            SInt::Symbolic(uop) => uop.id.hash(state),
            SInt::Infer => {}
        }
    }
}

impl fmt::Display for SInt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SInt::Const(v) => write!(f, "{v}"),
            SInt::Symbolic(_) => write!(f, "<symbolic>"),
            SInt::Infer => write!(f, "-1"),
        }
    }
}

impl SInt {
    /// Check if this is a concrete constant.
    pub fn is_const(&self) -> bool {
        matches!(self, SInt::Const(_))
    }

    /// Check if this is an infer placeholder (-1).
    pub fn is_infer(&self) -> bool {
        matches!(self, SInt::Infer)
    }

    /// Check if this is a symbolic expression.
    pub fn is_symbolic(&self) -> bool {
        matches!(self, SInt::Symbolic(_))
    }

    /// Get concrete value if this is a constant, None otherwise.
    pub fn as_const(&self) -> Option<usize> {
        match self {
            SInt::Const(v) => Some(*v),
            SInt::Symbolic(_) | SInt::Infer => None,
        }
    }

    /// Get symbolic UOp if this is symbolic, None otherwise.
    pub fn as_symbolic(&self) -> Option<&Arc<UOp>> {
        match self {
            SInt::Symbolic(uop) => Some(uop),
            SInt::Const(_) | SInt::Infer => None,
        }
    }

    /// Convert to UOp (const or passthrough). Panics on Infer.
    pub fn to_uop(&self, dtype: morok_dtype::DType) -> Arc<UOp> {
        match self {
            SInt::Const(v) => UOp::const_(dtype, crate::ConstValue::Int(*v as i64)),
            SInt::Symbolic(uop) => {
                if uop.dtype() != dtype {
                    uop.cast(dtype)
                } else {
                    uop.clone()
                }
            }
            SInt::Infer => panic!("cannot convert SInt::Infer to UOp — resolve -1 first"),
        }
    }

    /// Try to simplify symbolic expression to concrete value if possible.
    ///
    /// For const values, returns self. For symbolic, attempts to evaluate
    /// if it's a constant expression. No full symbolic simplification —
    /// that's deferred to the pipeline (matching Tinygrad).
    pub fn simplify(&self) -> Self {
        match self {
            SInt::Const(_) | SInt::Infer => self.clone(),
            SInt::Symbolic(uop) => {
                if let crate::Op::Const(const_hash) = uop.op() {
                    match const_hash.0 {
                        crate::ConstValue::Int(v) if v >= 0 => SInt::Const(v as usize),
                        crate::ConstValue::UInt(v) => SInt::Const(v as usize),
                        _ => self.clone(),
                    }
                } else {
                    self.clone()
                }
            }
        }
    }

    /// Ceiling division: ceildiv(a, b) = (a + b - 1) / b.
    /// Both operands must be positive. Panics on Infer.
    pub fn ceildiv(&self, rhs: &SInt) -> SInt {
        (self + rhs - 1usize) / rhs
    }

    /// Maximum of two SInt values. Panics on Infer.
    pub fn smax(&self, rhs: &SInt) -> SInt {
        match (self, rhs) {
            (SInt::Infer, _) | (_, SInt::Infer) => {
                panic!("smax on SInt::Infer — resolve -1 before computing")
            }
            (SInt::Const(a), SInt::Const(b)) => SInt::Const(*a.max(b)),
            _ => {
                let a = self.to_uop(morok_dtype::DType::Index);
                let b = rhs.to_uop(morok_dtype::DType::Index);
                SInt::Symbolic(a.try_max(&b).unwrap())
            }
        }
    }

    /// Minimum of two SInt values. Panics on Infer.
    /// Follows Tinygrad: `min(a, b) = -max(-a, -b)`.
    pub fn smin(&self, rhs: &SInt) -> SInt {
        match (self, rhs) {
            (SInt::Infer, _) | (_, SInt::Infer) => {
                panic!("smin on SInt::Infer — resolve -1 before computing")
            }
            (SInt::Const(a), SInt::Const(b)) => SInt::Const(*a.min(b)),
            _ => {
                let a = self.to_uop(morok_dtype::DType::Index);
                let b = rhs.to_uop(morok_dtype::DType::Index);
                // min(a, b) = -max(-a, -b)
                let neg_max = a.neg().try_max(&b.neg()).unwrap();
                SInt::Symbolic(neg_max.neg())
            }
        }
    }
}

// =========================================================================
// std::ops arithmetic
// =========================================================================

macro_rules! impl_sint_binop {
    ($trait:ident, $method:ident, $concrete_op:tt, $uop_method:ident) => {
        // Primary: &SInt op &SInt
        impl std::ops::$trait for &SInt {
            type Output = SInt;
            fn $method(self, rhs: &SInt) -> SInt {
                match (self, rhs) {
                    (SInt::Infer, _) | (_, SInt::Infer) => {
                        panic!("arithmetic on SInt::Infer — resolve -1 before computing")
                    }
                    (SInt::Const(a), SInt::Const(b)) => SInt::Const(a $concrete_op b),
                    _ => {
                        let a = self.to_uop(morok_dtype::DType::Index);
                        let b = rhs.to_uop(morok_dtype::DType::Index);
                        SInt::Symbolic(a.$uop_method(&b).unwrap())
                    }
                }
            }
        }
        // Forward: owned variants
        impl std::ops::$trait for SInt {
            type Output = SInt;
            fn $method(self, rhs: SInt) -> SInt { (&self).$method(&rhs) }
        }
        impl std::ops::$trait<&SInt> for SInt {
            type Output = SInt;
            fn $method(self, rhs: &SInt) -> SInt { (&self).$method(rhs) }
        }
        impl std::ops::$trait<SInt> for &SInt {
            type Output = SInt;
            fn $method(self, rhs: SInt) -> SInt { self.$method(&rhs) }
        }
        // Convenience: SInt op usize
        impl std::ops::$trait<usize> for &SInt {
            type Output = SInt;
            fn $method(self, rhs: usize) -> SInt { self.$method(&SInt::Const(rhs)) }
        }
        impl std::ops::$trait<usize> for SInt {
            type Output = SInt;
            fn $method(self, rhs: usize) -> SInt { (&self).$method(&SInt::Const(rhs)) }
        }
        // Convenience: usize op SInt
        impl std::ops::$trait<SInt> for usize {
            type Output = SInt;
            fn $method(self, rhs: SInt) -> SInt { (&SInt::Const(self)).$method(&rhs) }
        }
        impl std::ops::$trait<&SInt> for usize {
            type Output = SInt;
            fn $method(self, rhs: &SInt) -> SInt { (&SInt::Const(self)).$method(rhs) }
        }
    };
}

impl_sint_binop!(Add, add, +, try_add);
impl_sint_binop!(Mul, mul, *, try_mul);
impl_sint_binop!(Div, div, /, try_div);

// Sub is implemented separately to guard against usize underflow.
// SInt represents non-negative dimension sizes; concrete a - b must have a >= b.
impl std::ops::Sub for &SInt {
    type Output = SInt;
    fn sub(self, rhs: &SInt) -> SInt {
        match (self, rhs) {
            (SInt::Infer, _) | (_, SInt::Infer) => {
                panic!("arithmetic on SInt::Infer — resolve -1 before computing")
            }
            (SInt::Const(a), SInt::Const(b)) => {
                assert!(a >= b, "SInt subtraction underflow: {a} - {b} would be negative");
                SInt::Const(a - b)
            }
            _ => {
                let a = self.to_uop(morok_dtype::DType::Index);
                let b = rhs.to_uop(morok_dtype::DType::Index);
                SInt::Symbolic(a.try_sub(&b).unwrap())
            }
        }
    }
}
impl std::ops::Sub for SInt {
    type Output = SInt;
    fn sub(self, rhs: SInt) -> SInt {
        (&self).sub(&rhs)
    }
}
impl std::ops::Sub<&SInt> for SInt {
    type Output = SInt;
    fn sub(self, rhs: &SInt) -> SInt {
        (&self).sub(rhs)
    }
}
impl std::ops::Sub<SInt> for &SInt {
    type Output = SInt;
    fn sub(self, rhs: SInt) -> SInt {
        self.sub(&rhs)
    }
}
impl std::ops::Sub<usize> for &SInt {
    type Output = SInt;
    fn sub(self, rhs: usize) -> SInt {
        self.sub(&SInt::Const(rhs))
    }
}
impl std::ops::Sub<usize> for SInt {
    type Output = SInt;
    fn sub(self, rhs: usize) -> SInt {
        (&self).sub(&SInt::Const(rhs))
    }
}
impl std::ops::Sub<SInt> for usize {
    type Output = SInt;
    fn sub(self, rhs: SInt) -> SInt {
        (&SInt::Const(self)).sub(&rhs)
    }
}
impl std::ops::Sub<&SInt> for usize {
    type Output = SInt;
    fn sub(self, rhs: &SInt) -> SInt {
        (&SInt::Const(self)).sub(rhs)
    }
}

// =========================================================================
// Conversions
// =========================================================================

impl From<usize> for SInt {
    fn from(value: usize) -> Self {
        SInt::Const(value)
    }
}

impl From<isize> for SInt {
    /// Converts isize to SInt. `-1` becomes `SInt::Infer` (reshape inference placeholder).
    /// Panics on other negative values.
    fn from(value: isize) -> Self {
        if value == -1 {
            SInt::Infer
        } else {
            assert!(value >= 0, "negative dimension {value} is invalid (only -1 for inference is allowed)");
            SInt::Const(value as usize)
        }
    }
}

impl From<&isize> for SInt {
    fn from(value: &isize) -> Self {
        SInt::from(*value)
    }
}

impl From<i32> for SInt {
    fn from(value: i32) -> Self {
        if value == -1 {
            SInt::Infer
        } else {
            assert!(value >= 0, "negative dimension {value} is invalid (only -1 for inference is allowed)");
            SInt::Const(value as usize)
        }
    }
}

impl From<&i32> for SInt {
    fn from(value: &i32) -> Self {
        SInt::from(*value)
    }
}

impl From<i64> for SInt {
    fn from(value: i64) -> Self {
        if value == -1 {
            SInt::Infer
        } else {
            assert!(value >= 0, "negative dimension {value} is invalid (only -1 for inference is allowed)");
            SInt::Const(value as usize)
        }
    }
}

impl From<&i64> for SInt {
    fn from(value: &i64) -> Self {
        SInt::from(*value)
    }
}

impl From<&usize> for SInt {
    fn from(value: &usize) -> Self {
        SInt::Const(*value)
    }
}

impl From<&SInt> for SInt {
    fn from(value: &SInt) -> Self {
        value.clone()
    }
}

impl From<Arc<UOp>> for SInt {
    fn from(value: Arc<UOp>) -> Self {
        // Try to extract constant value if possible
        let sint = SInt::Symbolic(value);
        sint.simplify()
    }
}

impl From<&Arc<UOp>> for SInt {
    fn from(value: &Arc<UOp>) -> Self {
        SInt::from(value.clone())
    }
}

// =========================================================================
// Shrink range conversion
// =========================================================================

/// A shrink range that may be `None` (keep dim), concrete, or symbolic.
///
/// Intermediate representation used by [`IntoShrinkRange`]. isize values
/// are preserved for negative-index resolution at the tensor layer.
#[derive(Debug, Clone)]
pub enum ShrinkRange {
    /// Keep entire dimension (identity).
    None,
    /// Concrete range with possibly-negative indices (resolved by tensor layer).
    Isize(isize, isize),
    /// SInt range (concrete or symbolic, always non-negative).
    Sint(SInt, SInt),
}

/// Trait for types convertible to a shrink range specification.
pub trait IntoShrinkRange {
    fn into_shrink_range(self) -> ShrinkRange;
}

impl IntoShrinkRange for Option<(isize, isize)> {
    fn into_shrink_range(self) -> ShrinkRange {
        match self {
            Some((b, e)) => ShrinkRange::Isize(b, e),
            ::core::option::Option::None => ShrinkRange::None,
        }
    }
}

impl IntoShrinkRange for &Option<(isize, isize)> {
    fn into_shrink_range(self) -> ShrinkRange {
        (*self).into_shrink_range()
    }
}

impl IntoShrinkRange for (isize, isize) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Isize(self.0, self.1)
    }
}

impl IntoShrinkRange for &(isize, isize) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Isize(self.0, self.1)
    }
}

// (i32, i32) → concrete range (ONNX often uses i32)
impl IntoShrinkRange for (i32, i32) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Isize(self.0 as isize, self.1 as isize)
    }
}

impl IntoShrinkRange for &(i32, i32) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Isize(self.0 as isize, self.1 as isize)
    }
}

// (usize, usize) → concrete range
impl IntoShrinkRange for (usize, usize) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Sint(SInt::Const(self.0), SInt::Const(self.1))
    }
}

impl IntoShrinkRange for &(usize, usize) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Sint(SInt::Const(self.0), SInt::Const(self.1))
    }
}

impl IntoShrinkRange for Option<(SInt, SInt)> {
    fn into_shrink_range(self) -> ShrinkRange {
        match self {
            Some((b, e)) => ShrinkRange::Sint(b, e),
            ::core::option::Option::None => ShrinkRange::None,
        }
    }
}

impl IntoShrinkRange for &Option<(SInt, SInt)> {
    fn into_shrink_range(self) -> ShrinkRange {
        self.clone().into_shrink_range()
    }
}

impl IntoShrinkRange for (SInt, SInt) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Sint(self.0, self.1)
    }
}

impl IntoShrinkRange for &(SInt, SInt) {
    fn into_shrink_range(self) -> ShrinkRange {
        ShrinkRange::Sint(self.0.clone(), self.1.clone())
    }
}

// =========================================================================
// Utilities
// =========================================================================

/// Compute product of SInt values.
///
/// If all values are concrete, returns Const(product).
/// Otherwise, constructs symbolic multiplication UOp.
///
/// # Examples
///
/// ```rust
/// # use morok_ir::{SInt, sint_prod};
/// let dims = vec![SInt::from(2), SInt::from(3), SInt::from(4)];
/// let result = sint_prod(&dims);
/// assert_eq!(result.as_const(), Some(24));
/// ```
pub fn sint_prod(values: &[SInt]) -> SInt {
    values.iter().fold(SInt::Const(1), |acc, v| &acc * v)
}

/// Compute maximum of SInt values (symbolic-aware).
///
/// # Examples
///
/// ```rust
/// # use morok_ir::{SInt, sint_max};
/// let a = SInt::from(10);
/// let b = SInt::from(20);
/// let result = sint_max(&[a, b]);
/// assert_eq!(result.as_const(), Some(20));
/// ```
pub fn sint_max(values: &[SInt]) -> SInt {
    assert!(!values.is_empty(), "sint_max requires at least one value");
    values.iter().skip(1).fold(values[0].clone(), |acc, v| acc.smax(v))
}

/// Compute minimum of SInt values (symbolic-aware).
///
/// Note: Currently falls back to first element for symbolic cases since
/// UOp::try_min_op is not yet implemented.
///
/// # Examples
///
/// ```rust
/// # use morok_ir::{SInt, sint_min};
/// let a = SInt::from(10);
/// let b = SInt::from(20);
/// let result = sint_min(&[a, b]);
/// assert_eq!(result.as_const(), Some(10));
/// ```
pub fn sint_min(values: &[SInt]) -> SInt {
    assert!(!values.is_empty(), "sint_min requires at least one value");
    values.iter().skip(1).fold(values[0].clone(), |acc, v| acc.smin(v))
}