Elixir HLS 发布 用 Elixir 函数直接生成 FPGA 电路
Elixir HLS 0.1.0 发布,将高层综合(HLS)引入 Elixir 生态,开发者用 defhls 定义的函数既能在 CPU 上作为普通 Elixir 代码运行,也能被综合为 FPGA 电路。每个电路都会与对应的 Elixir 函数进行仿真比对,结果一致才被采用,使 Elixir 函数本身成为电路的规格说明。首个目标平台为搭载 AtomVM 的 Sipeed Tang Primer 25K,示例项目为 FM 收音机。
报道正文
本文目录5 个章节
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
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.
mix ehls.compile --out build lib/kernel_demo.exmix 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)
| Elixir | Elixir HLS | |
|---|---|---|
| Integers | integer() (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) |
| Floats | float() | ✗ |
| Atoms, strings, maps | atom(), String.t(), map() | ✗ |
| Lists | [integer()] | ✗ (from the CPU an instance is called with a list) |
| Tables of constants | Enum.at(@t, i) | The same (a table in the circuit) |
Defining a function (defhls)
| Elixir | Elixir HLS | |
|---|---|---|
| Definition | def f(x) do ... end | defhls f(x :: u8) :: u8 do ... end (an Elixir function too) |
| Operators | + - *, Bitwise, == <, and or not | The same |
| Division | div, rem, / | ✗ |
| Choices | if, case, cond, clauses and guards | The same (the last clause matches everything) |
| Calls | g(x) | Of defhls of the same module only (a circuit for each place of a call) |
| for loops | for k <- 0..15, reduce: acc do ... end | The same (a constant range; one circuit used again) |
| reduce | Enum.reduce(0..15, acc, fn k, acc -> ... end) | The same (a constant range; as many circuits side by side) |
| Tuples in the state | elem(t, i), put_elem(t, i, v) | The same (registers in the circuit; the FPGA's memory with transform's memory:) |
| Recursion | def f(n), do: f(n - 1) | ✗ |
| Anonymous functions | fn x -> x + 1 end | ✗ |
| Exceptions, messages, I/O | raise, receive, IO.puts | ✗ |
Functions on streams
| Elixir | Elixir HLS | |
|---|---|---|
| map | Stream.map(&f/1) | EHLS.Stream.map(&f/1) (the module may be left out: map(&f/1)) |
| filter | Stream.filter(&f/1) | EHLS.Stream.filter(&f/1) |
| transform | Stream.transform(0, &f/2) | EHLS.Stream.transform(0, &f/2) (f a defhls function) |
| zip_with | Stream.zip_with(a, b, &f/2) | EHLS.Stream.zip_with(a, b, &f/2) |
| chunk_every | Stream.chunk_every(32, 4, :discard) | EHLS.Stream.chunk_every(32, 4, :discard) (gives tuples of 32; Elixir gives lists, so the types differ) |
| run | Stream.run() | EHLS.Stream.run() |
| At an interval | Stream.interval(1) | ticks(1000) (times per second) |
| More elements | Stream.flat_map(fn x -> [x, x] end) | ✗ |
| The whole collection | Enum.sort(), Enum.take(n) | ✗ |
| One stream after another | Stream.concat(a, b) | ✗ |
Shared state and events (definstance)
| Elixir | Elixir HLS | |
|---|---|---|
| A shared state | Agent | EHLS.Agent (how it differs: EHLS.Agent) |
| Events | Phoenix.PubSub | EHLS.PubSub (how it differs: EHLS.PubSub) |
| Making processes | spawn, Task | ✗ |
(注:更多技术实现细节与完整 API 文档请查阅原项目 README)