Importing C, C++, and LLVM IR ============================= The :mod:`b_asic.llvm` module converts a static LLVM function into an :class:`~b_asic.sfg.SFG`. Clang can serve as a frontend for both C and C++: constant expressions, templates, local variables, and loops are resolved by the compiler before B-ASIC reads the resulting IR. For a downloadable C file and its rendered SFG, see the :doc:`C-to-SFG gallery example `. Installation ------------ Install the optional LLVM reader:: pip install "b-asic[llvm]" For C/C++, also install `Clang `_ separately and put it on your executable search path, or pass its path as ``clang=...``. Its emitted LLVM IR must be compatible with the LLVM version bundled with llvmlite. Check ``clang --version`` and ``llvmlite.binding.llvm_version_info`` when investigating parser errors; installing llvmlite does not install a C/C++ compiler. A C example ----------- Write a pure function with scalar ``float`` inputs and one ``float`` return. Use ``f`` suffixes on constants to avoid implicit double-precision conversions: .. code-block:: c // kernel.c float kernel(float x, float gain) { float result = x; for (int i = 0; i < 4; ++i) { if (i % 2 == 0) result = result * gain; else result = result + x; } return result; } The loop count and each ``if`` condition depend only on compile-time values. After unrolling and constant folding the function contains only arithmetic: .. code-block:: python from b_asic import c_to_sfg sfg = c_to_sfg("kernel.c", function_name="kernel") assert sfg.evaluate(2.0, 3.0) == 26.0 Inputs follow the original argument order, including arguments optimized out by Clang. The return value becomes the graph's single output. LLVM value names are used as operation names where available. Runtime if/else and conditional expressions ------------------------------------------ Simple runtime conditions can select between values. For example: .. code-block:: c float gate(float x, float threshold) { if (x > threshold) return 2.0f * x; else return x + 1.0f; } .. code-block:: python from b_asic import Comparison, Select, c_to_sfg sfg = c_to_sfg("gate.c", "gate") assert sfg.evaluate(3.0, 2.0) == 6.0 assert sfg.evaluate(1.0, 2.0) == 2.0 assert sfg.find_by_type(Comparison) assert sfg.find_by_type(Select) All six C comparisons (``<``, ``<=``, ``>``, ``>=``, ``==``, ``!=``), boolean conditions, and the ternary operator are supported. Optimized LLVM ``select`` instructions map directly to :class:`~b_asic.comparison_operations.Select`. Acyclic branches and phi nodes are converted to a fixed dataflow graph using path predicates and selectors. This includes if-without-else, nested and successive conditionals, and multiple return statements. Floating-point predicates retain their ordered/unordered NaN comparison behavior. Both arithmetic branches execute in the resulting SFG; selection chooses their result. Conditions do not guard invalid arithmetic or provide short-circuit evaluation. For example, ``x != 0 ? 1 / x : 0`` can still raise division by zero in Python simulation. Runtime loops, switches, exceptions, and other side effects remain unsupported. Constant-address array loads and stores can cross acyclic branches. The importer tracks each element's current value and merges different incoming values using selectors. A store after a merge, such as ``sum[i] = s``, uses the merged scalar directly. Later loads see the latest stored value, including sequential in-place updates such as ``A[i + 1] = A[i] + q * q * q``. On a branch without a store, an ``inout`` element retains its invocation input, or the most recent preceding store; persistent state retains its previous value. An ``output`` element has no initial input value: analysis must prove it has been written on every incoming control-flow path before a load and on every return path if it is exposed as an output. Otherwise the import is rejected; use ``inout`` when preserving an incoming value is intended. Clang may keep branches around larger arithmetic expressions instead of emitting ``select`` instructions. Its branch-folding cost threshold can be raised through ``extra_args`` without changing the C source: .. code-block:: python sfg = c_to_sfg( "kernel.c", "kernel", extra_args=["-mllvm", "-two-entry-phi-node-folding-threshold=100"], infer_arrays=True, ) This compiler-specific option can reduce remaining branches, but is not required for acyclic array updates and does not guarantee branch elimination. As with other selectors, both arithmetic paths execute in the resulting SFG. C++ and constexpr ----------------- C++17 is enabled for C++ files. ``constexpr``, templates, and ``if constexpr`` are useful for generating arithmetic with a fixed structure: .. code-block:: cpp // kernel.cpp constexpr float gain(int n) { return n == 0 ? 1.0f : 2.0f * gain(n - 1); } template float transform(float x) { constexpr float scale = gain(3); if constexpr (Scale) return x * scale; else return x; } extern "C" float kernel(float x) { return transform(x) + 1.0f; } .. code-block:: python from b_asic import cpp_to_sfg sfg = cpp_to_sfg("kernel.cpp", "kernel") assert sfg.evaluate(2.0) == 17.0 ``c_to_sfg`` also detects C++ from any suffix other than ``.c``; ``language="c++"`` overrides this. ``cpp_to_sfg`` always selects C++. Use ``extra_args=["-std=c++20"]`` for a newer language standard. ``extern "C"`` avoids name mangling on entry points. Otherwise, supply the exact mangled LLVM symbol as ``function_name``. The compiler still needs to emit a definition: unused templates and functions evaluated exclusively at compile time may have no standalone LLVM definition. ``constexpr`` permits compile-time evaluation; it does not force every call to be evaluated at compile time. Store computed coefficients in ``constexpr`` variables, as above, to require constant evaluation. Calls that depend on runtime inputs must inline and reduce to supported arithmetic. C++ runtime machinery such as allocation, exceptions, virtual dispatch, and remaining library calls is unsupported. Array interfaces: memory or streams ----------------------------------- Automatic defaults ~~~~~~~~~~~~~~~~~~ Pass ``infer_arrays=True`` to ``c_to_sfg``, ``cpp_to_sfg``, ``llvm_to_sfg``, or ``llvm_file_to_sfg`` to infer state and array I/O without naming each variable: .. code-block:: python sfg = c_to_sfg("accumulate.c", "accumulate", infer_arrays=True) Scalar arguments and return values always become ordinary SFG I/O. With this option, floating-point globals/statics with definitions become persistent state, and their supported LLVM initializers supply delay reset values. Pointer arguments and externally declared globals become input, output, or inout arrays according to their loads and stores, using the default ``"memory"`` interface. State and external array I/O can be used together. LLVM erases C pointer lengths. The inferred pointer extent covers the observed constant element accesses, including holes; it does not recover unused trailing elements of the original declaration. Read/write pointer arrays default to inout, so their contents are supplied anew each iteration. They do not become persistent state automatically. Different pointers/globals are assumed disjoint, as with explicit array mappings. An ``arrays`` entry overrides inference for that variable. Use it to select ``interface="stream"``, supply a larger extent, override reset values, or mark a pointer argument as persistent state. Unused pointers, dynamic addresses, unsupported initializers, and ambiguous element types still require explicit configuration or are rejected. Inference is opt-in to preserve the explicit mapping behavior of existing code. Explicit interfaces ~~~~~~~~~~~~~~~~~~~ Pointer syntax does not specify whether an algorithm's arrays belong in a memory or on ordinary SFG ports. For example, the same FFT can use either interface. Supply an ``arrays`` mapping to any of the import helpers to make the interpretation explicit. Keys are LLVM argument names; prefix global symbols with ``@``. Array sizes count scalar elements, not bytes. Consider a two-point FFT butterfly operating in place: .. code-block:: c void butterfly(float data[2]) { float a = data[0]; float b = data[1]; data[0] = a + b; data[1] = a - b; } .. code-block:: python from b_asic import LLVMArray, c_to_sfg memory_sfg = c_to_sfg( "butterfly.c", "butterfly", arrays={"data": LLVMArray("inout", 2, interface="memory")}, ) stream_sfg = c_to_sfg( "butterfly.c", "butterfly", arrays={"data": LLVMArray("inout", 2, interface="stream")}, ) assert memory_sfg.evaluate(3.0, 1.0) == [4.0, 2.0] assert stream_sfg.evaluate(3.0, 1.0) == [4.0, 2.0] ``interface="memory"`` creates named :class:`~b_asic.special_operations.MemoryRead` and :class:`~b_asic.special_operations.MemoryWrite` operations with constant element addresses. These operations behave as SFG inputs and outputs during simulation; the importer does not allocate a Python RAM or feed outputs back automatically. ``interface="stream"`` creates ordinary :class:`~b_asic.special_operations.Input` and :class:`~b_asic.special_operations.Output` ports. Each array element has a separate scalar port in the same SFG iteration. This is the ordinary B-ASIC dataflow interface; a serialized, one-sample-per-clock protocol is not inferred. Different input/output arrays may independently choose either interface. The ``role`` controls lifetime and access permissions: .. list-table:: :header-rows: 1 :widths: 15 85 * - Role - Interpretation * - ``input`` - Fresh values supplied each iteration. Stores are rejected. * - ``output`` - Final stored values become outputs. Reads require an earlier store to the same location in the current invocation. * - ``inout`` - Fresh input values and final output values share the same array name. This models an in-place transform without introducing persistent state. * - ``state`` - Internal values persist across SFG iterations through Delay operations. There are no external array ports. All input/inout array elements become input ports, in ascending address order. Pointer arguments are expanded at their position in the function signature; scalar arguments keep their original positions. Configured global inputs follow arguments, ordered by global symbol name. A scalar return, when present, is the first output; output/inout arrays then follow argument/global order and ascending written address. Only locations actually written produce output ports. Within one invocation, loads after stores use the latest value, repeated loads share an input, and repeated stores to a location expose only its final value. Thus the graph models the computed transform, not a bus trace of every access. Untouched output locations are not overwritten. C functions with array outputs can return ``void``. All indices must resolve to constant addresses after optimization. Nested arrays are flattened in row-major order. Clang's constant byte-offset GEPs are accepted when they land on whole elements. Set ``element_type="double"`` for arrays of LLVM doubles; it defaults to ``"float"``. The importer validates access widths and bounds against the configured size. Values still use B-ASIC's generic data type, and reset values and constants are Python floats. Different configured names denote disjoint storage. Use C ``restrict`` or C++ ``__restrict`` where appropriate to express the same non-aliasing contract to Clang. An in-place algorithm should use one ``inout`` array rather than two names for overlapping buffers. Dynamic addresses, partial-element accesses, pointer reinterpretation to other element widths, volatile/atomic accesses, and memory-copy intrinsics are unsupported. Local scratch allocations must still be optimized away. Persistent state with delays ---------------------------- Use ``role="state"`` when values belong to the algorithm across calls, rather than arriving in a new input buffer. For example: .. code-block:: c float accumulate(float x, float *state) { *state = *state + x; return *state; } .. code-block:: python from b_asic import LLVMArray, Simulation, c_to_sfg state_sfg = c_to_sfg( "accumulate.c", "accumulate", arrays={"state": LLVMArray("state", 1, initial_values=[0.0])}, ) simulation = Simulation(state_sfg, [2.0]) assert [simulation.step()[0] for _ in range(3)] == [2.0, 4.0, 6.0] The state pointer is removed from the external interface. A :class:`~b_asic.special_operations.Delay` holds each used state element's previous-iteration value, with its input connected to the final store for that element. A location without a store retains its value. Stores are visible to later loads in the same invocation, while delay updates take effect together at the iteration boundary. Use :class:`~b_asic.simulation.Simulation` to retain state across iterations; standalone ``sfg.evaluate`` starts from reset values. Global and static variables use the same state mapping. Conditional assignments retain the previous value on paths without a write: .. code-block:: c // accumulate.c float accumulate(float x) { static float history = 1.5f; if (x > 0.0f) history += x; return history; } .. code-block:: python state_sfg = c_to_sfg( "accumulate.c", "accumulate", arrays={"@accumulate.history": LLVMArray("state", 1, initial_values=[1.5])}, ) simulation = Simulation(state_sfg, [[2.0, -1.0, 3.0]]) assert [simulation.step()[0] for _ in range(3)] == [3.5, 3.5, 6.5] For a global ``float history``, use ``"@history"`` instead. The importer merges state from branch paths through selectors and connects the resulting next value to the delay. Alternatively, ``c_to_sfg("accumulate.c", "accumulate", infer_arrays=True)`` infers the static variable and its 1.5 reset value without an explicit mapping. Pointer state defaults to zero initialization. ``initial_values`` can specify one reset value per element. Without inference, a mapped C global or static variable requires explicit reset values. For example, Clang may emit a local static named ``@kernel.history``; configure it with ``arrays={"@kernel.history": LLVMArray("state", 1, initial_values=[1.5])}``. Use the exact emitted LLVM symbol, including any C++ name mangling. The supplied reset values define the SFG initialization independently of the C initializer. With inference enabled, supported scalar, explicit floating-point array, and all-zero LLVM initializers supply these values automatically. Unknown or partial initializers are never silently treated as zero. State arrays and external input/output/inout arrays can be combined in one imported function. Without inference, unknown or unconfigured pointer accesses produce warnings and are rejected rather than guessed to be state. The :doc:`array gallery example ` shows the same butterfly with memory and ordinary ports, plus a separate stateful accumulator and its delay feedback. Writing code that becomes a static SFG -------------------------------------- * Use scalar floating-point inputs/returns or explicitly configured arrays. Both LLVM ``float`` and ``double`` are accepted. Prefer consistent C ``float`` arithmetic; mixed precision can introduce unsupported conversion instructions. * Loops must have a fixed, compile-time trip count and be fully unrolled by Clang. Integer loop counters are fine if optimization removes them. A constant bound alone does not guarantee successful unrolling. Nested loops can produce very large graphs. * ``if``/``else`` and the conditional operator may use runtime comparisons. Acyclic branches, phi nodes, and selectors become static dataflow. Both arithmetic branches execute, and loops must still be fully unrolled. Switches must resolve at compile time or lower to supported branches. * Configure pointer arguments and globals with ``LLVMArray`` or enable ``infer_arrays=True``. Keep their addresses constant after loop unrolling, and distinguish input/output buffers from persistent state. Local temporary arrays are useful when optimization can eliminate their allocations and accesses. * Small helper functions must inline. Avoid recursion depending on runtime values and external calls. ``sqrtf`` and ``fabsf`` can work when Clang lowers them to the supported LLVM intrinsics; inspect the IR if a call remains. * Keep device/file I/O, assertions that survive optimization, allocation, and other side effects outside the imported function. Use ``extra_args`` for include paths and compile-time definitions, e.g. ``["-Iinclude", "-DCOUNT=4"]``. These paths are relative to the Python process's current working directory. The helper requests ``-O3``, loop unrolling with a high threshold, and aggressive inlining. It disables loop/SLP vectorization and floating-point contraction and allows math intrinsics without preserving C ``errno`` effects. These flags are requests to the optimizer, not a guarantee of acceptable IR. No fast-math flags are enabled by default. Custom compiler options can affect numerical behavior; the importer always checks the final result. Supported LLVM and diagnostics ------------------------------ The selected function must have an acyclic control-flow graph, scalar ``float``/``double``, integer, or boolean ``i1`` arguments or configured pointers, and a scalar floating-point, integer, boolean, or ``void`` return. Supported arithmetic instructions are ``fadd``, ``fsub``, ``fmul``, ``fdiv``, ``fneg``, and calls to ``llvm.sqrt.f32``, ``llvm.sqrt.f64``, ``llvm.fabs.f32``, or ``llvm.fabs.f64``. All scalar ``fcmp`` predicates, ``select``, ``br``, ``phi``, and boolean ``i1`` ``and``/``or``/``xor`` are supported. Unreachable blocks are rejected. Constant-address ``getelementptr``, scalar ``load``, and scalar ``store`` are also supported for configured arrays. Floating-point constants become Python ``float`` constants. Scalar integers (``iN``, with ``i1`` reserved for booleans) use :attr:`~b_asic.signal_type.SignalType.INTEGER` and support setting values, ``add``, and ``sub``, including assignment through ``select`` and ``phi``. Integer constants become signed Python integers, preserving their exact values. LLVM bit widths and overflow wrapping are not reproduced in simulation. Integer arrays, other integer arithmetic, integer comparisons and bitwise logic, casts, vectors, aggregates, undefined/poison values, cyclic control flow, and other calls (including fused multiply-add) are unsupported. Each unsupported construct in the selected function produces an :class:`~b_asic.llvm.LLVMImportWarning`, followed by :class:`~b_asic.llvm.UnsupportedLLVMError`. No partial graph is returned. Instructions with unused results are also checked; side effects cannot silently disappear. Other functions in the module are outside the import scope. Compiler warnings are forwarded, and compiler errors include Clang's diagnostic text. Malformed or unverifiable LLVM raises ``ValueError``. The graph represents arithmetic using B-ASIC operations and its generic data type. LLVM widths, rounding at individual instructions, fast-math assumptions, poison semantics, and C exception/``errno`` behavior are not reproduced as a bit-exact model. Select a B-ASIC simulation data type later as appropriate. Array roles and interfaces follow explicit mappings or the opt-in inference defaults; the frontend does not infer a hardware bus protocol, memory arbitration, or serialization schedule. Importing LLVM directly ----------------------- .. code-block:: python from b_asic import llvm_file_to_sfg, llvm_to_sfg sfg = llvm_file_to_sfg("kernel.ll", function_name="kernel") identity = llvm_to_sfg( "define float @identity(float %x) { ret float %x }" ) Omit ``function_name`` only when the module contains exactly one function definition; declarations of intrinsics or external functions do not count. Direct LLVM import parses and verifies the IR but does not optimize it. Use Clang or LLVM's ``opt`` beforehand to simplify independently generated IR. To inspect compiler output manually, start with:: clang -O3 -S -emit-llvm -fno-vectorize -fno-slp-vectorize -ffp-contract=off -fno-math-errno -funroll-loops -mllvm -unroll-threshold=1000000 -mllvm -inline-threshold=1000000 kernel.c -o kernel.ll Use ``clang -x c++ -std=c++17`` with the same optimization flags for C++. The Python helper keeps its generated IR in a temporary directory that is removed after import.