ALL INTELLIGENCE
Hacker News AIT2·Tools

Elixir HLS Turns Elixir Functions into FPGA Circuits

Original title: Elixir HLS makes FPGA circuits from functions written in Elixir
62AI Score

Elixir HLS 0.1.0 debuts, bringing high-level synthesis to the Elixir ecosystem: functions defined with defhls run as ordinary Elixir on the CPU and also compile into FPGA circuits. Each circuit is simulated against its Elixir function and accepted only on a match, making the Elixir function the circuit's specification. The first target is the Sipeed Tang Primer 25K with AtomVM, with an FM radio as the working example.

Full Report

Contents5 sections

elixir-hls

Elixir HLS makes FPGA circuits from functions written in Elixir: high-level synthesis (HLS).

High-level synthesis

FPGA circuits are usually written in a hardware description language such as Verilog or VHDL, register by register: which value goes into which register at which clock. This is called RTL (register transfer level). High-level synthesis generates that RTL from code in a software language: the author says what to compute, and the tool decides the clocks and the registers.

In Elixir HLS that language is Elixir. A function defined with defhls stays an ordinary Elixir function that runs on the CPU, and also becomes an FPGA circuit. Each circuit is simulated against its Elixir function and used only if the results match, so the Elixir function is the circuit's specification. Why it is built this way: vision.

Status

Elixir HLS 0.1.0 is its first release, an early one: it synthesizes integer functions, with state, tables, memories and fixed loops, and instances joining them with streams, levels and events, in the FPGA or on the CPU; its first target is the Sipeed Tang Primer 25K with AtomVM, and its working example the FM radio (atomvm-fm-radio), whose whole demodulator runs in the FPGA. Not there yet, among others: division, floats, recursion, and of Elixir's streams what needs the whole collection.

Example

ELIXIR
defmodule KernelDemo do
  use EHLS

  defhls stream_kernel(x :: u32) :: u32 do
    a = signed(x &&& 0xFFFF, 16)
    b = signed(x >>> 16, 16)
    if a > b, do: a * 3 + b, else: a + b * 2
  end
end

KernelDemo.stream_kernel(123)        # => 369 (computed on the CPU)

stream_kernel takes a 32-bit value, compares its low and high 16 bits as signed numbers and computes its result depending on which is larger. Given an integer, it computes on the CPU, as an Elixir function. How the same function runs as a circuit in the FPGA: On a board.

A type such as u32 (unsigned, 32 bits) is the circuit's bit width. A circuit spends wiring and logic on every bit, so narrower widths make it smaller and faster. On the CPU the values are cut to the same widths, so the CPU and the circuit compute the same results.

BASH
mix ehls.compile --out build lib/kernel_demo.ex

mix ehls.compile turns each defhls function of the files into a SystemVerilog circuit and then simulates every circuit against its function; it stops at a circuit that does not match.

Elixir and Elixir HLS

What can be written

What can be written with defhls (a function, which runs both on the CPU and as a circuit) and definstance (an instance: functions placed as circuits), beside Elixir. ✗: not in Elixir HLS. Details: How Elixir HLS works, EHLS.defhls/2, EHLS.Pipeline, EHLS.Stream.

Values and types (defhls)

ElixirElixir HLS
Integersinteger() (of any size)u8, s16 and so on (u unsigned, s signed, the number the bits, 1 to 64); beyond the width they wrap
Types@spec f(integer()) :: integer() (may be left out)defhls f(x :: u8) :: u16 (always written)
Tuples{integer(), integer()}{u8, s16}; of one type also tuple(s16, 32)
Floatsfloat()✗
Atoms, strings, mapsatom(), String.t(), map()✗
Lists[integer()]✗ (from the CPU an instance is called with a list)
Tables of constantsEnum.at(@t, i)The same (a table in the circuit)

Defining a function (defhls)

ElixirElixir HLS
Definitiondef f(x) do ... enddefhls f(x :: u8) :: u8 do ... end (an Elixir function too)
Operators+ - *, Bitwise, == <, and or notThe same
Divisiondiv, rem, /✗
Choicesif, case, cond, clauses and guardsThe same (the last clause matches everything)
Callsg(x)Of defhls of the same module only (a circuit for each place of a call)
for loopsfor k <- 0..15, reduce: acc do ... endThe same (a constant range; one circuit used again)
reduceEnum.reduce(0..15, acc, fn k, acc -> ... end)The same (a constant range; as many circuits side by side)
Tuples in the stateelem(t, i), put_elem(t, i, v)The same (registers in the circuit; the FPGA's memory with transform's memory:)
Recursiondef f(n), do: f(n - 1)✗
Anonymous functionsfn x -> x + 1 end✗
Exceptions, messages, I/Oraise, receive, IO.puts✗

Functions on streams

ElixirElixir HLS
mapStream.map(&f/1)EHLS.Stream.map(&f/1) (the module may be left out: map(&f/1))
filterStream.filter(&f/1)EHLS.Stream.filter(&f/1)
transformStream.transform(0, &f/2)EHLS.Stream.transform(0, &f/2) (f a defhls function)
zip_withStream.zip_with(a, b, &f/2)EHLS.Stream.zip_with(a, b, &f/2)
chunk_everyStream.chunk_every(32, 4, :discard)EHLS.Stream.chunk_every(32, 4, :discard) (gives tuples of 32; Elixir gives lists, so the types differ)
runStream.run()EHLS.Stream.run()
At an intervalStream.interval(1)ticks(1000) (times per second)
More elementsStream.flat_map(fn x -> [x, x] end)✗
The whole collectionEnum.sort(), Enum.take(n)✗
One stream after anotherStream.concat(a, b)✗

Shared state and events (definstance)

ElixirElixir HLS
A shared stateAgentEHLS.Agent (how it differs: EHLS.Agent)
EventsPhoenix.PubSubEHLS.PubSub (how it differs: EHLS.PubSub)
Making processesspawn, Task✗

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)