xref: /llvm-project/mlir/docs/Dialects/emitc.md (revision 0bd07652524ebacdee166eb609fef48c50769b09)
1The EmitC dialect allows to convert operations from other MLIR dialects to EmitC
2ops. Those can be translated to C/C++ via the Cpp emitter.
3
4The following convention is followed:
5
6*   If template arguments are passed to an `emitc.call_opaque` operation, C++ is
7    generated.
8*   If tensors are used, C++ is generated.
9*   If multiple return values are used within in a functions or an
10    `emitc.call_opaque` operation, C++11 is required.
11*   If floating-point type template arguments are passed to an `emitc.call_opaque`
12    operation, C++20 is required.
13*   If `ssize_t` is used, then the code requires the POSIX header `sys/types.h`
14    or any of the C++ headers in which the type is defined.
15*   If `_Float16` is used, the code requires the support of C additional
16    floating types.
17*   If `__bf16` is used, the code requires a compiler that supports it, such as
18    GCC or Clang.
19*   If `emitc.array` with a dimension of size zero is used, then the code
20    requires [a GCC extension](https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html).
21*   Else the generated code is compatible with C99.
22
23These restrictions are neither inherent to the EmitC dialect itself nor to the
24Cpp emitter and therefore need to be considered while implementing conversions.
25
26Type conversions are provided for the MLIR type `index` into the unsigned `size_t`
27type and its signed counterpart `ptrdiff_t`. Conversions between these two types
28are only valid if the `index`-typed values are within
29`[PTRDIFF_MIN, PTRDIFF_MAX]`.
30
31After the conversion, C/C++ code can be emitted with `mlir-translate`. The tool
32supports translating MLIR to C/C++ by passing `-mlir-to-cpp`. Furthermore, code
33with variables declared at top can be generated by passing the additional
34argument `-declare-variables-at-top`.
35
36Besides operations part of the EmitC dialect, the Cpp targets supports
37translating the following operations:
38
39*   'cf' Dialect
40    *   `cf.br`
41    *   `cf.cond_br`
42*   'func' Dialect
43    *   `func.call`
44    *   `func.func`
45    *   `func.return`
46