|
| 1 | +// Tests for value-based specialization of constant parameters |
| 2 | +// |
| 3 | +// When ALL constant params are compile-time literals -> specialized version with constants inlined |
| 4 | +// When ANY constant param is a variable -> non-specialized version with all params as arguments |
| 5 | + |
| 6 | +// ============================================================================= |
| 7 | +// Basic constant specialization |
| 8 | +// ============================================================================= |
| 9 | + |
| 10 | +// Generic function with constant parameter |
| 11 | +fun add_n('n : int, x : int) : int { |
| 12 | + return n + x; |
| 13 | +} |
| 14 | + |
| 15 | +// Specialized: n=5 inlined |
| 16 | +fun test_specialized_5() : int { |
| 17 | + return add_n(5, 10); // 5 + 10 = 15 |
| 18 | +} |
| 19 | + |
| 20 | +// Specialized: n=100 inlined (different specialization) |
| 21 | +fun test_specialized_100() : int { |
| 22 | + return add_n(100, 10); // 100 + 10 = 110 |
| 23 | +} |
| 24 | + |
| 25 | +// Non-specialized: variable argument |
| 26 | +fun test_nonspec(n : int) : int { |
| 27 | + return add_n(n, 10); // n + 10 |
| 28 | +} |
| 29 | + |
| 30 | +// ============================================================================= |
| 31 | +// Multiple constant parameters |
| 32 | +// ============================================================================= |
| 33 | + |
| 34 | +fun weighted('w1 : real, 'w2 : real, x : real, y : real) : real { |
| 35 | + return w1 * x + w2 * y; |
| 36 | +} |
| 37 | + |
| 38 | +// Specialized: w1=0.3, w2=0.7 inlined |
| 39 | +fun test_weighted_spec() : real { |
| 40 | + return weighted(0.3, 0.7, 10.0, 20.0); // 0.3*10 + 0.7*20 = 17 |
| 41 | +} |
| 42 | + |
| 43 | +// Non-specialized: first weight is variable |
| 44 | +fun test_weighted_nonspec(w : real) : real { |
| 45 | + return weighted(w, 0.7, 10.0, 20.0); |
| 46 | +} |
| 47 | + |
| 48 | +// ============================================================================= |
| 49 | +// Interleaved constant and regular parameters |
| 50 | +// ============================================================================= |
| 51 | + |
| 52 | +fun interleaved(x : real, 'a : real, y : real, 'b : real) : real { |
| 53 | + return a * x + b * y; |
| 54 | +} |
| 55 | + |
| 56 | +// Specialized: a=2.0, b=3.0 inlined |
| 57 | +fun test_interleaved_spec() : real { |
| 58 | + return interleaved(5.0, 2.0, 4.0, 3.0); // 2*5 + 3*4 = 22 |
| 59 | +} |
| 60 | + |
| 61 | +// Non-specialized: 'a is variable |
| 62 | +fun test_interleaved_nonspec(a : real) : real { |
| 63 | + return interleaved(5.0, a, 4.0, 3.0); |
| 64 | +} |
| 65 | + |
| 66 | +// ============================================================================= |
| 67 | +// Stateful function with constant parameter |
| 68 | +// ============================================================================= |
| 69 | + |
| 70 | +fun accumulate('scale : real, x : real) : real { |
| 71 | + mem sum : real; |
| 72 | + sum = sum + scale * x; |
| 73 | + return sum; |
| 74 | +} |
| 75 | + |
| 76 | +// Specialized: scale=2.0 inlined |
| 77 | +fun test_accumulate_spec() : real { |
| 78 | + return accumulate(2.0, 5.0); // sum = 0 + 2*5 = 10 |
| 79 | +} |
| 80 | + |
| 81 | +// Non-specialized: scale is variable |
| 82 | +fun test_accumulate_nonspec(s : real, x : real) : real { |
| 83 | + return accumulate(s, x); |
| 84 | +} |
| 85 | + |
| 86 | +// ============================================================================= |
| 87 | +// Constant parameter with type generic |
| 88 | +// ============================================================================= |
| 89 | + |
| 90 | +fun scale_typed('factor : int, x : 't) : 't { |
| 91 | + mem last : 't; |
| 92 | + last = x; |
| 93 | + return x; |
| 94 | +} |
| 95 | + |
| 96 | +// Specialized for int type with factor=2 |
| 97 | +fun test_typed_int() : int { |
| 98 | + return scale_typed(2, 42); |
| 99 | +} |
| 100 | + |
| 101 | +// Specialized for real type with factor=2 |
| 102 | +fun test_typed_real() : real { |
| 103 | + return scale_typed(2, 3.14); |
| 104 | +} |
| 105 | + |
| 106 | +// ============================================================================= |
| 107 | +// Main function to exercise all test functions |
| 108 | +// ============================================================================= |
| 109 | + |
| 110 | +fun main() { |
| 111 | + val r1 = test_specialized_5(); |
| 112 | + val r2 = test_specialized_100(); |
| 113 | + val r3 = test_nonspec(50); |
| 114 | + val r4 = test_weighted_spec(); |
| 115 | + val r5 = test_weighted_nonspec(0.5); |
| 116 | + val r6 = test_interleaved_spec(); |
| 117 | + val r7 = test_interleaved_nonspec(1.5); |
| 118 | + val r8 = test_accumulate_spec(); |
| 119 | + val r9 = test_accumulate_nonspec(3.0, 4.0); |
| 120 | + val r10 = test_typed_int(); |
| 121 | + val r11 = test_typed_real(); |
| 122 | +} |
0 commit comments