Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-kernel

Source of release 0.1.3

0.1.3 / tests / suite / lowering.lucb

## The lowering: snippets as a user types them, wrapped, checked by the Base checker,
## lowered and run over a few points; and the diagnostics of snippets it refuses.
import memory
import strings
from io import path
from luce_std import math32
from luce_kernel import engine, lib, lower
from luce_kernel.engine import Builtin, Call, Column, Request, Uniform, Kind
from luce_kernel.lower import Host, RunOver, Compiled
import common

## The test host's API stubs, as luce-geocore's `code` module will look.
pub let stubs: str = "## Test API.\npub struct Point:\n    pub var P: f32[4]\n    pub var N: f32[4]\n    pub var Cd: f32[4]\n    pub let ptnum: i32\n    pub let numpt: i32\n\n    pub func f32(name: str) -> f32:\n        _ = name\n        return 0.0\n\n    pub func set_f32(name: str, value: f32):\n        _ = name\n        _ = value\n\n    pub func vec3(name: str) -> f32[4]:\n        _ = name\n        return f32[4](0.0)\n\n    pub func area() -> f32:\n        return 0.0\n\npub struct Hit:\n    pub let prim: i32\n    pub let uv: f32[2]\n    pub let position: f32[4]\n    pub let distance: f32\n\npub struct Source:\n    var unused: i32\n\n    pub func count() -> i32:\n        return 0\n\n    pub func value(i: i32, name: str) -> f32:\n        _ = i\n        _ = name\n        return 0.0\n\n    pub func probe(at: f32[4]) -> Hit:\n        _ = at\n        return Hit(prim = 0, uv = [0.0, 0.0], position = [0.0, 0.0, 0.0, 0.0], distance = 0.0)\n\npub struct Kernel:\n    pub let time: f64\n\n    pub func input(index: i64) -> const Source*:\n        _ = index\n        return (const Source*)(void*)0x1000\n\n    pub func f32(name: str, default: f32 = 0.0) -> f32:\n        _ = name\n        return default\n\n    pub func i32(name: str, default: i32 = 0) -> i32:\n        _ = name\n        return default\n"

pub let points: usize = 64

## The stubs as the host texts editor help reads.
pub func stubs_list() -> const str[]:
    return stub_texts[..<1]

let stub_texts: str[1] = [stubs]

pub let run_over_points: RunOver = RunOver(entry = "point", parameters = "p: Point*, k: const Kernel*", imports = "from host.code import Point, Kernel")

func area_lanes(call: Call*):
    let results = call.f32_result(0)
    for lane in 0..<call.lanes:
        results[lane] = (f32)(call.first + lane) * 0.5

pub func no_kinds() -> const Kind[]:
    var none_given: const Kind[]
    return none_given

func source_count(call: Call*):
    let inputs = call.i64_argument(0)
    let counts = call.i32_result(0)
    for lane in 0..<call.lanes: counts[lane] = 100 + (i32)inputs[lane]

func source_value(call: Call*):
    let inputs = call.i64_argument(0)
    let indices = call.i32_argument(1)
    let values = call.f32_result(0)
    let name = call.text(0)
    for lane in 0..<call.lanes:
        values[lane] = (f32)(indices[lane] * 10) + (f32)name.length + (f32)(inputs[lane] * 1000)

func source_probe(call: Call*):
    let inputs = call.i64_argument(0)
    let y = call.f32_argument(2)
    for lane in 0..<call.lanes:
        call.i32_result(0)[lane] = (i32)inputs[lane]
        call.f32_result(2)[lane] = y[lane]
        call.f32_result(7)[lane] = 1.5

pub func make_host() -> Host!:
    var host = Host()
    try host.field("Point", "P", "P", 3)
    try host.field("Point", "N", "N", 3)
    try host.field("Point", "Cd", "Cd", 3)
    try host.element("Point", "ptnum")
    try host.uniform("Point", "numpt", "numpt")
    try host.getter("Point", "f32", "", 1)
    try host.setter("Point", "set_f32", "", 1)
    try host.getter("Point", "vec3", "", 3)
    try host.builtin("Point", "area", Builtin.of("area", no_kinds(), [Kind.f32], area_lanes))
    try host.uniform("Kernel", "time", "time")
    try host.parameter("Kernel", "f32")
    try host.parameter("Kernel", "i32")
    try host.handle("Kernel", "input")
    try host.builtin("Source", "count", Builtin.of("count", [Kind.i64], [Kind.i32], source_count))
    try host.builtin("Source", "value", Builtin.of("value", [Kind.i64, Kind.i32], [Kind.f32], source_value))
    let hit: Kind[8] = [Kind.i32, Kind.f32, Kind.f32, Kind.f32, Kind.f32, Kind.f32, Kind.f32, Kind.f32]
    try host.builtin("Source", "probe", Builtin.of("probe", [Kind.i64, Kind.f32, Kind.f32, Kind.f32, Kind.f32], hit, source_probe))
    try host.column("P", .f32, 3)
    try host.column("N", .f32, 3)
    try host.column("mass", .f32, 1)
    return host

## A fresh snippet root in the temporary directory, with the test host's stubs.
pub func make_root(room: u8[]) -> str!:
    let root = try strings.format(room, f"{path.temp()}/luce-kernel-suite-root")
    try lower.write_root(root)
    try lower.write_text(root, "host/code.lucb", stubs)
    return root

## Points with positions, normals and a mass column; the outputs start as copies.
pub struct Columns:
    pub var positions: f32[]
    pub var normals: f32[]
    pub var masses: f32[]
    pub var moved: f32[]
    pub var colors: f32[]
    pub var weighed: f32[]

    pub static func make() -> Columns!:
        var made = Columns(positions = try new f32[points * 3] in memory.heap, normals = try new f32[points * 3] in memory.heap,
                           masses = try new f32[points] in memory.heap, moved = try new f32[points * 3] in memory.heap,
                           colors = try new f32[points * 3] in memory.heap, weighed = try new f32[points] in memory.heap)
        for at in 0..<points:
            made.positions[at * 3] = (f32)at * 0.0625
            made.positions[at * 3 + 1] = (f32)(at % 5) * 0.25
            made.positions[at * 3 + 2] = 1.0 - (f32)at * 0.03125
            made.normals[at * 3 + 1] = 1.0
            made.masses[at] = (f32)at * 0.5
        return made

    pub func release():
        free(self.positions) in memory.heap
        free(self.normals) in memory.heap
        free(self.masses) in memory.heap
        free(self.moved) in memory.heap
        free(self.colors) in memory.heap
        free(self.weighed) in memory.heap

    ## Run `compiled` at every worker count; the outputs of the last run stay.
    pub func run(compiled: const Compiled*, uniforms: const Uniform[]) -> engine.Fault?!:
        var found: engine.Fault? = none
        for threads in common.thread_counts:
            memory.copy(self.moved, self.positions, self.positions.length)
            memory.copy(self.weighed, self.masses, self.masses.length)
            memory.set(u8[]((u8*)self.colors.data, self.colors.length * 4), 0)
            let inputs = [Column.input_f32("P", self.positions, 3), Column.input_f32("N", self.normals, 3), Column.input_f32("mass", self.masses)]
            let outputs = [Column.output_f32("P", self.moved, 3), Column.output_f32("Cd", self.colors, 3), Column.output_f32("mass", self.weighed)]
            let request = Request(count = points, inputs = inputs, outputs = outputs, uniforms = uniforms, threads = threads, chunk = 7)
            found = try engine.run(&compiled.program, &request)
        return found

## Compile `body` (and `header`), failing the suite with its diagnostics if it does not.
pub func compiled_or_trap(root: str, host: const Host*, header: str, body: str) -> Compiled!:
    var compiled = try lower.compile(root, run_over_points, header, body, host)
    if not compiled.ok():
        for d in compiled.diagnostics():
            print(f"  {d.line}:{d.column}: {d.message}")
        print(compiled.text())
        trap("lowering: the snippet did not compile")
    return compiled

func same(name: str, got: f32, want: f32, at: usize):
    if got.bits() != want.bits():
        print(f"FAIL  lowering {name}: point {at}: {got} for {want}")
        trap("lowering")

pub let no_header: str = ""

pub let wave_body: str = "p.P[1] += math32.sin(p.P[0] * 4.0) * 0.1\n"

pub let displace_body: str = "let amount = k.f32(\"amount\", 0.2)\nlet scale = k.f32(\"scale\", 3.0)\nlet n = snoise(p.P * scale + [0.0, 0.0, (f32)k.time, 0.0])\np.P = p.P + p.N * (n * amount)\n"

pub let colors_body: str = "p.Cd = rand3(p.ptnum)\n"

pub let helpers_header: str = "func wave(x: f32) -> f32:\n    return math32.sin(x * 4.0) * 0.1\n\nfunc steps(count: i32) -> i32:\n    var total: i32 = 0\n    for i in 0..<count:\n        if i == 5:\n            break\n        total += i\n    return total\n"

pub let helpers_body: str = "var y = p.P[1] + wave(p.P[0])\nif p.ptnum % 2 == 0:\n    y = -y\nelif p.ptnum % 3 == 0:\n    y *= 2.0\np.P[1] = y\np.set_f32(\"mass\", (f32)steps(p.ptnum % 9) + p.f32(\"mass\"))\nvar n: i32 = 0\nwhile n < p.ptnum % 4:\n    n += 1\np.Cd = [(f32)n, p.area(), (f32)k.i32(\"count\", 7), 0.0]\n"

## The scalar truth for helpers_body.
func steps_value(count: i32) -> i32:
    var total: i32 = 0
    for i in 0..<count:
        if i == 5:
            break
        total += i
    return total

let constructs_body: str = "var total: f32 = 0.0\nlet weights: f32[4] = [1.0, 2.0, 3.0, 4.0]\nfor w in weights:\n    total += w\nouter: for i in 0..<4:\n    for j in 0..<4:\n        if i * j > p.ptnum % 5:\n            break outer\n        total += 1.0\nmatch p.ptnum % 3:\n    0: total += 10.0\n    1, 2 if p.ptnum > 30: total += 20.0\n    _: total -= 1.0\nlet big = p.P[0] > 2.0 and p.ptnum // 2 > 10\nlet chosen: f32 = 1.0 if big else 2.0\nif p.ptnum > 60:\n    return\np.Cd = [total, chosen, 0.0, 0.0]\n"

## The scalar truth for constructs_body.
func constructs_value(at: usize, x: f32) -> (f32, f32)?:
    var total: f32 = 0.0
    for w in [(f32)1.0, 2.0, 3.0, 4.0]:
        total += w
    let point = (i32)at
    outer: for i in 0..<4:
        for j in 0..<4:
            if (i32)(i * j) > point % 5:
                break outer
            total += 1.0
    match point % 3:
        0: total += 10.0
        1, 2 if point > 30: total += 20.0
        _: total -= 1.0
    let big = x > 2.0 and point // 2 > 10
    let chosen: f32 = 1.0 if big else 2.0
    if point > 60:
        return none
    return (total, chosen)

## Compile `body` and check its first diagnostic: severity, part, line and message start.
func expect_diagnostic(root: str, host: const Host*, header: str, body: str, part: lower.Part, line: u32, starts: str, severity: lower.Severity = .error) -> !:
    var compiled = try lower.compile(root, run_over_points, header, body, host)
    defer compiled.destroy()
    for d in compiled.diagnostics():
        if d.severity != severity:
            continue
        if d.part != part or d.line != line or not strings.starts_with(d.message, starts):
            print(f"FAIL  lowering diagnostic: {d.line}:{d.column}: {d.message} (wanted line {line}: {starts})")
            trap("lowering diagnostics")
        print(f"ok    diagnostic at line {d.line}:{d.column}: {d.message}")
        return
    trap("lowering: no diagnostic")

pub func run_all() -> !:
    var room: u8[1024]
    let root = try make_root(room)
    var host = try make_host()
    defer host.destroy()
    var columns = try Columns.make()
    defer columns.release()

    var wave = try compiled_or_trap(root, &host, no_header, wave_body)
    defer wave.destroy()
    assert(try columns.run(&wave, common.no_uniforms()) == none)
    for at in 0..<points:
        let x = columns.positions[at * 3]
        same("wave", columns.moved[at * 3 + 1], columns.positions[at * 3 + 1] + math32.sin(x * 4.0) * 0.1, at)
        same("wave keeps x", columns.moved[at * 3], x, at)
    print(f"ok    lowering wave: {wave.program.instructions().length} instructions")

    var displace = try compiled_or_trap(root, &host, no_header, displace_body)
    defer displace.destroy()
    let uniforms = displace.program.uniforms()
    assert(uniforms.length == 3 and uniforms[0].name == "amount" and uniforms[1].name == "scale" and uniforms[2].name == "time")
    assert(f32.bits((u32)uniforms[0].default) == 0.2 and f32.bits((u32)uniforms[1].default) == 3.0)
    assert(try columns.run(&displace, [Uniform.f64("time", 0.5)]) == none)
    for at in 0..<points:
        let p = columns.positions[at * 3..<at * 3 + 3]
        let n = lib.snoise([p[0] * 3.0, p[1] * 3.0, p[2] * 3.0 + 0.5, 0.0])
        for k: usize in 0..<3:
            same("noise displacement", columns.moved[at * 3 + k], p[k] + columns.normals[at * 3 + k] * (n * 0.2), at)
    print(f"ok    lowering noise displacement: {displace.program.instructions().length} instructions")

    var colors = try compiled_or_trap(root, &host, no_header, colors_body)
    defer colors.destroy()
    assert(try columns.run(&colors, common.no_uniforms()) == none)
    for at in 0..<points:
        let want = lib.rand3((i64)at)
        for k: usize in 0..<3:
            same("rand3", columns.colors[at * 3 + k], want[k], at)
    print(f"ok    lowering rand3: {colors.program.instructions().length} instructions")

    var helpers = try compiled_or_trap(root, &host, helpers_header, helpers_body)
    defer helpers.destroy()
    assert(try columns.run(&helpers, common.no_uniforms()) == none)
    for at in 0..<points:
        let x = columns.positions[at * 3]
        var y = columns.positions[at * 3 + 1] + math32.sin(x * 4.0) * 0.1
        if at % 2 == 0:
            y = -y
        elif at % 3 == 0:
            y *= 2.0
        same("branches", columns.moved[at * 3 + 1], y, at)
        same("helper with a loop", columns.weighed[at], (f32)steps_value((i32)(at % 9)) + columns.masses[at], at)
        same("masked while", columns.colors[at * 3], (f32)(at % 4), at)
        same("builtin", columns.colors[at * 3 + 1], (f32)at * 0.5, at)
        same("parameter default", columns.colors[at * 3 + 2], 7.0, at)
    print(f"ok    lowering branches, loops, helpers, builtins: {helpers.program.instructions().length} instructions")

    var constructs = try compiled_or_trap(root, &host, no_header, constructs_body)
    defer constructs.destroy()
    assert(try columns.run(&constructs, common.no_uniforms()) == none)
    for at in 0..<points:
        if let found = constructs_value(at, columns.positions[at * 3]):
            let (total, chosen) = found
            same("loops, match, and, conditional", columns.colors[at * 3], total, at)
            same("conditional", columns.colors[at * 3 + 1], chosen, at)
        else:
            same("early return", columns.colors[at * 3], 0.0, at)
    print(f"ok    lowering match, labeled break, early return, lazy and: {constructs.program.instructions().length} instructions")

    # Structs of the snippet: construction, a function returning one (with an early
    # return under a varying condition), a method, a static function and a field write.
    let pair_header = "struct Pair:\n    var a: f32\n    var b: f32 = 2.0\n    func sum() -> f32:\n        return self.a + self.b\n    static func twice(x: f32) -> Pair:\n        return Pair(a = x, b = x * 2.0)\nfunc pick(x: f32) -> Pair:\n    if x > 0.5:\n        return Pair(a = 1.0, b = x)\n    return Pair(a = 0.0, b = -x)\n"
    var structs = try compiled_or_trap(root, &host, pair_header, "var q = Pair(a = p.P[0])\nlet t = Pair.twice(p.P[1])\nlet r = pick(p.P[0])\nif p.ptnum % 2 == 0:\n    q.b = 3.0\np.Cd = [q.sum(), t.b + r.a, r.b, 0.0]\n")
    defer structs.destroy()
    assert(try columns.run(&structs, common.no_uniforms()) == none)
    for at in 0..<points:
        let x = columns.positions[at * 3]
        let y = columns.positions[at * 3 + 1]
        let b: f32 = 3.0 if at % 2 == 0 else 2.0
        same("a method over a constructed struct", columns.colors[at * 3], x + b, at)
        same("a static function and a returned struct", columns.colors[at * 3 + 1], y * 2.0 + (1.0 if x > 0.5 else 0.0), at)
        same("an early return of a struct", columns.colors[at * 3 + 2], x if x > 0.5 else -x, at)
    print("ok    lowering structs: construction, methods, static functions, field writes")

    var branchy = try compiled_or_trap(root, &host, no_header, "if p.ptnum % 2 == 0:\n    p.set_f32(\"mass\", -1.0)\n")
    defer branchy.destroy()
    assert(try columns.run(&branchy, common.no_uniforms()) == none)
    for at in 0..<points:
        same("a setter in a branch", columns.weighed[at], -1.0 if at % 2 == 0 else columns.masses[at], at)
    print("ok    lowering a setter inside a branch")

    var bounded = try compiled_or_trap(root, &host, no_header, "var total: i32 = 0\nfor i in 0..<p.ptnum % 4:\n    total += i + 1\np.set_f32(\"mass\", (f32)total)\n")
    defer bounded.destroy()
    assert(try columns.run(&bounded, common.no_uniforms()) == none)
    for at in 0..<points:
        let n = at % 4
        same("a for loop whose bound differs per element", columns.weighed[at], (f32)(n * (n + 1) // 2), at)
    print("ok    lowering a for loop with an element-dependent bound")

    var handles = try compiled_or_trap(root, &host, no_header, "let g = k.input(1)\nlet h = g.probe(p.P)\np.Cd = [(f32)g.count(), g.value(p.ptnum, \"ab\"), h.uv[1] + h.distance, (f32)h.prim]\np.set_f32(\"mass\", (f32)k.input(0).count())\n")
    defer handles.destroy()
    assert(try columns.run(&handles, common.no_uniforms()) == none)
    for at in 0..<points:
        same("an API value's builtin", columns.colors[at * 3], 101.0, at)
        same("a text argument", columns.colors[at * 3 + 1], (f32)(at * 10 + 2 + 1000), at)
        same("a record result", columns.colors[at * 3 + 2], columns.positions[at * 3 + 1] + 1.5, at)
        same("an API value inline", columns.weighed[at], 100.0, at)
    print("ok    lowering API values, text arguments and record results")
    # What a host can prepare before the run: the inputs each builtin may be called on.
    assert(handles.program.may_pass("probe", 0, 1) and not handles.program.may_pass("probe", 0, 0))
    assert(handles.program.may_pass("count", 0, 0) and handles.program.may_pass("count", 0, 1))
    assert(not handles.program.may_pass("count", 0, 2) and not handles.program.may_pass("absent", 0, 0))
    var varying = try compiled_or_trap(root, &host, no_header, "p.Cd = [(f32)k.input(p.ptnum % 2).count(), 0.0, 0.0, 0.0]\n")
    defer varying.destroy()
    assert(varying.program.may_pass("count", 0, 7))
    print("ok    may_pass: constant and varying API numbers")

    var faulting = try compiled_or_trap(root, &host, no_header, "let x = p.P[0]\nlet q = 100 // (p.ptnum - 10)\np.Cd = [x, (f32)q, 0.0, 0.0]\n")
    defer faulting.destroy()
    let fault = try columns.run(&faulting, common.no_uniforms()) else trap("lowering: no fault")
    let (part, line, column) = faulting.locate(fault.line, fault.column)
    print(f"      fault {fault.message} lane {fault.lane} at {fault.line}:{fault.column} -> {line}:{column}")
    assert(fault.kind == .division and fault.lane == 10 and part == .body and line == 2 and column == 13)
    print(f"ok    lowering fault: body line {line}:{column}: {fault.message} (point {fault.lane})")

    try expect_diagnostic(root, &host, no_header, "let x: f32 = p.ptnum\n", .body, 1, "")
    try expect_diagnostic(root, &host, no_header, "let x = p.P[0]\nprint(\"hello\")\n", .body, 2, "`print` is not available")
    try expect_diagnostic(root, &host, no_header, "p.Cd[0] = p.f32(\"weight\")\n", .body, 1, "the input has no column `weight`")
    try expect_diagnostic(root, &host, no_header, "p.Cd = p.vec3(\"mass\")\n", .body, 1, "`mass` is stored as f32")
    try expect_diagnostic(root, &host, "func f(x: f32) -> f32:\n    return x +\n", "p.Cd[0] = f(1.0)\n", .header, 2, "")
    try expect_diagnostic(root, &host, "func f(x: i32) -> i32:\n    return f(x)\n", "p.Cd[0] = (f32)f(1)\n", .header, 2, "recursion")
    try expect_diagnostic(root, &host, no_header, "let unused_value = 1.0\np.Cd[0] = 1.0\n", .body, 1, "unused", severity = .warning)

    # A header helper and a host helper module (Host.library) take the running point:
    # their bodies read and write it through its fields.
    try lower.write_text(root, "host/tools.lucb", tools)
    try host.library("host.tools")
    let with_tools = RunOver(entry = "point", parameters = "p: Point*, k: const Kernel*", imports = "from host.code import Point, Kernel\nfrom host import tools")
    var helped = try lower.compile(root, with_tools, "func paint(q: Point*, v: f32):\n    q.Cd = [v, q.P[0], 0.0, 0.0]\n", "tools.lift(p, tools.twice(0.25))\npaint(p, 3.0)\n", &host)
    defer helped.destroy()
    if not helped.ok():
        for d in helped.diagnostics(): print(f"  {d.line}:{d.column}: {d.message}")
        trap("lowering: the helper snippet did not compile")
    assert(try columns.run(&helped, common.no_uniforms()) == none)
    for at in 0..<points:
        same("library helper", columns.moved[at * 3 + 1], columns.positions[at * 3 + 1] + 0.5, at)
        same("header helper", columns.colors[at * 3], 3.0, at)
        same("header helper reads", columns.colors[at * 3 + 1], columns.positions[at * 3], at)
    print("ok    lowering helpers given the point: a header helper and a host library")

## A host helper module: inlined like `lib` once the host names it (Host.library).
let tools: str = "from host.code import Point\n\n## Twice `x`.\npub func twice(x: f32) -> f32:\n    return x * 2.0\n\n## Raise the point by `by`.\npub func lift(p: Point*, by: f32):\n    p.P = p.P + [0.0, by, 0.0, 0.0]\n"