# `EHLS.MLIR`
[🔗](https://github.com/elixir-hls/elixir-hls/blob/v0.1.0/lib/ehls/mlir.ex#L1)

Translates the body of a `defhls` function (an Elixir AST) into MLIR (func + arith + cf +
memref) for CIRCT `hlstool --dynamic-hw`. Host-only: used by the `defhls` macro at compile
time.

Supported: integer literals, variables and `=` bindings (also of tuples), tuples,
`+ - *`, unary `-`, `&&& ||| ^^^ <<< >>>` (shifts by a constant; `>>>` also by an unsigned value), comparisons,
`and or not`, `if/else`, `signed(x, n)` / `unsigned(x, n)`, a missing output (`{[v], s}` / `{[], s}` in a `defhls`), and for
kernels with memories and tables: `elem(mem, i)`, `mem = put_elem(mem, i, v)` (at the top level
of the body; read / write here, as EHLS.Compiler gives them), `table(@attr, i)`, `for k <- a..b, reduce: init do acc -> ... end` (also with
tuples),
`Enum.sum(for k <- a..b, do: e)`, and `Enum.reduce(a..b, init, fn k, acc -> ... end)`
(unrolled: a pipeline instead of a loop).

Widths: every value carries a width and signedness, derived from its operands so that no
operation can overflow (`+` adds one bit, `*` adds the widths, `&&&` with a non-negative
constant narrows to the constant, ...). The result is the exact integer, which the
function's return type then wraps, like the CPU version. A `reduce` accumulator gets the
width of one step plus log2 of the iteration count (it is meant for sums).

Control flow: an `if` whose branches contain no loop is computed on both sides and
selected (`arith.select`); otherwise it becomes branches (`cf.cond_br`) joining in a block
whose arguments carry the result. A `reduce` is a loop (header, body, exit blocks).
Memory indices wrap (the low bits of the index), like the CPU version.

# `function`

Returns `{mlir_text, space}` for `func @name`. `params` is a list of `{var, type}` where a
type is `{width, signed?}`, `{:tuple, [type]}` or `{:memory, size, {width, signed?}}`;
`tables` a list of `{name, [integer]}` (`table(@name, i)`); `result` the type of the body's
value. Every scalar becomes one port of `lane` bits (32 or 64, the kernel's), in order (tuples flattened). Tables and memories
share one address space, one `memref<2^aw x i32>` argument after the scalars (several
memref arguments crash hlstool of CIRCT 1.160 in some combinations): each region at a
multiple of its (power of two) size. `space` describes it, or is nil:
`%{aw: bits, regions: [%{kind: :rom | :ram, name:, base:, size:, values: (rom)}]}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
