011 loop optimization basic

? Unknown Status unknown.

Code

// PERFORMANCE TEST: Loop Optimization - Basic Checker Event Pattern
// Goal: Prove compiler detects checker event pattern and transforms to native loop
// Pattern: Checker event (two branches: continue/done) + label loop with recursive jump
// Baseline: Hand-written Zig for loop
// Threshold: 1.05x (5% overhead max)
//
// This tests the CORE loop optimization:
// - Detect checker event with if/else returning continue vs done
// - Detect label loop with recursive jump that increments counter
// - Transform to NativeLoop IR node
// - Emit as native for loop with inlined body

const std = @import("std");

// Checker event: Two branches (continue/done), proc with if/else
~event loop_step { i: u64, limit: u64, sum: u64 }
| continue { i: u64, sum: u64 }
| done { result: u64 }

~proc loop_step {
    if (i < limit) {
        const new_sum = sum + i;
        return .{ .continue = .{ .i = i, .sum = new_sum } };
    } else {
        return .{ .done = .{ .result = sum } };
    }
}

// Print result
~event print { result: u64 }
| done {}

~proc print {
    std.debug.print("Sum: {}\n", .{result});
    return .{ .done = .{} };
}

// Main flow: Label loop with recursive jump
// Pattern the optimizer should detect:
// - Initial call: loop_step(i: 0, ...)
// - Continue branch: @loop(i: i + 1, ...) - RECURSIVE JUMP with increment
// - Done branch: Exit loop
//
// Expected transformation:
// for (0..10_000_000) |i| {
//     sum += i;
// }

~#main_loop loop_step(i: 0, limit: 10_000_000, sum: 0)
| continue c |> @main_loop(i: c.i + 1, limit: 10_000_000, sum: c.sum)
| done d |> print(result: d.result)
    | done |> _
input.kz

Test Configuration

MUST_RUN

Post-validation Script:

#!/bin/bash
# Post-validation: Check performance is within threshold

set -e

if [ ! -f "results.json" ]; then
    echo "⚠️  No benchmark results found (results.json missing)"
    echo "   Running benchmark..."
    bash benchmark.sh
fi

if [ ! -f "results.json" ]; then
    echo "❌ FAIL: Benchmark did not produce results.json"
    exit 1
fi

# Check if jq is installed
if ! command -v jq &> /dev/null; then
    echo "⚠️  jq not installed (needed to parse benchmark results)"
    echo "   Install with: brew install jq (macOS) or apt install jq (Linux)"
    echo "   Skipping performance validation..."
    exit 0
fi

THRESHOLD=$(cat THRESHOLD)

# Parse results (hyperfine format)
BASELINE_TIME=$(jq -r '.results[0].mean' results.json)
KORU_TIME=$(jq -r '.results[1].mean' results.json)

# Calculate ratio (Koru / Baseline)
RATIO=$(echo "scale=4; $KORU_TIME / $BASELINE_TIME" | bc -l)

echo ""
echo "Performance Results:"
echo "  Baseline (Zig):     ${BASELINE_TIME}s"
echo "  Koru (Optimized):   ${KORU_TIME}s"
echo "  Ratio:              ${RATIO}x"
echo "  Threshold:          ${THRESHOLD}x"
echo ""

# Compare to threshold
if (( $(echo "$RATIO > $THRESHOLD" | bc -l) )); then
    echo "❌ PERFORMANCE REGRESSION!"
    echo "   Koru is ${RATIO}x slower than baseline"
    echo "   Threshold is ${THRESHOLD}x"
    echo "   Regression: $(echo "scale=1; ($RATIO - 1) * 100" | bc -l)%"
    exit 1
elif (( $(echo "$RATIO < 0.95" | bc -l) )); then
    echo "✅ PERFORMANCE IMPROVED!"
    echo "   Koru is FASTER than baseline (${RATIO}x)"
else
    echo "✅ Performance within threshold"
    echo "   Overhead: $(echo "scale=1; ($RATIO - 1) * 100" | bc -l)%"
fi

exit 0