Skip to content

Infer whether a function or snippet is deterministic #53

@royalicing

Description

@royalicing

A key feature of WebAssembly is its determinism: the same instructions will produce the same output for a given input across any host platform.

It would be nice to detect when a function is deterministic.

Or, it might be nice for the programmer to declare that a function is deterministic.

This would allow patterns to be unlocked:

  • You could optimise by caching since you know a given input will always produce an expected output. This would benefit distributed applications especially, as they could save a network hop by reusing a previously generated output value stored in a local cache.
  • Your cached function could also inherit determinism. This would allow optimisations to be kept.

A compiler could optimise by stashing identical expressions in a local variable. So the identity of an expression could be the AST of that expression.

A compiler could error when a function (or snippet) has been declared to be pure but is accessing memory or calling out to imports, breaking deterministic guarantees.

A function could be bound to a strictly controlled subrange of memory, which would increase confidence of its determinism again.

region Base64 do
  Memory.data LookupTable# def component
  defc encode_ascii(ascii: I32.U8), I32 do
    LookupTable.read!(ascii)
  end
end

Orb.Component.memory_range_used(Base64) # Range<…>
Orb.Component.exports_used(Base64) # []
Orb.Component.imports_used(Base64) # []
Orb.Component.deterministic?(Base64) # true
defmodule Math do
  defc vec3(x: F32, y: F32, z: F32), {F32, F32, F32} do
    {x, y, z}
  end

  defc vec3(value: F32), {F32, F32, F32} do
    {x, x, x}
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions