LLVM Import¶
See Importing C, C++, and LLVM IR for installation, examples, and the supported C/C++ subset.
Import static scalar LLVM functions as signal flow graphs.
The optional llvm extra supplies llvmlite. Compiling C or C++ additionally
requires a separately installed Clang; its LLVM version must be compatible with
llvmlite. See Importing C, C++, and LLVM IR for the supported subset and source-writing guidance.
- class b_asic.llvm.LLVMArray(role: Literal['input', 'output', 'inout', 'state'], size: int, interface: Literal['memory', 'stream'] = 'memory', initial_values: Sequence[float] | None = None, element_type: Literal['float', 'double'] = 'float')¶
Describe an LLVM pointer argument or global as an SFG array.
- Parameters:
- role{“input”, “output”, “inout”, “state”}
Input arrays are supplied anew each iteration. Output arrays expose final stored values. Inout arrays do both without persisting internally. State arrays persist between iterations as delays.
- sizeint
Number of scalar elements. Addresses are zero-based element indices, with nested LLVM arrays flattened in row-major order.
- interface{“memory”, “stream”}, default: “memory”
For input/output arrays, use MemoryRead/MemoryWrite or ordinary Input/Output ports respectively. Streams expose a separate scalar port per element in the same SFG iteration, not a serialized bus protocol. State always uses Delay operations, independent of this setting.
- initial_valuessequence of float, optional
Reset values for state, one per element. Pointer state defaults to zero. Globals require explicit values unless
infer_arrays=Truereads their supported LLVM initializers.- element_type{“float”, “double”}, default: “float”
LLVM scalar element type, used to reject reinterpretation and mixed access widths. SFG constants and reset values are Python floats.
- exception b_asic.llvm.LLVMImportWarning¶
Unsupported LLVM features or diagnostics emitted by Clang.
- exception b_asic.llvm.UnsupportedLLVMError¶
The selected function cannot be translated completely to an SFG.
- b_asic.llvm.c_to_sfg(path: str | PathLike[str], function_name: str | None = None, *, clang: str | PathLike[str] = 'clang', language: str | None = None, extra_args: Sequence[str] = (), arrays: Mapping[str, LLVMArray] | None = None, infer_arrays: bool = False) SFG¶
Compile a C or C++ file to optimized LLVM IR, then attempt SFG import.
- Parameters:
- pathpath-like
Source file.
.cselects C; other suffixes select C++.- function_namestr, optional
Exact LLVM function symbol; C++ entry points should use
extern "C".- clangpath-like, default: “clang”
Clang executable compatible with llvmlite’s LLVM reader.
- language{“c”, “c++”}, optional
Override language detection. Defaults to C11 or C++17 respectively.
- extra_argssequence of str
Additional compiler arguments, for example
["-Iinclude", "-DCOUNT=4"]or["-std=c++20"]. Paths are relative to the current working directory. Passed as individual arguments without a shell.- arraysmapping of str to LLVMArray, optional
Array roles and interfaces, as for
llvm_to_sfg().- infer_arraysbool, default: False
Infer unconfigured array I/O and global/static state, as for
llvm_to_sfg().
- Returns:
- SFG
The translated function. Compilation occurs in a temporary directory.
- Raises:
- RuntimeError
If Clang fails; includes the compiler diagnostics.
- UnsupportedLLVMError
If optimization leaves unsupported constructs in the selected function.
- FileNotFoundError
If the source file or compiler cannot be found.
Notes
Requests O3 optimization, aggressive full loop unrolling and inlining, and disables vectorization and floating-point contraction. These are requests, not guarantees: the importer checks the resulting IR. Compiler diagnostics are surfaced as
LLVMImportWarning. No fast-math flags are enabled.
- b_asic.llvm.cpp_to_sfg(path: str | PathLike[str], function_name: str | None = None, *, clang: str | PathLike[str] = 'clang', extra_args: Sequence[str] = (), arrays: Mapping[str, LLVMArray] | None = None, infer_arrays: bool = False) SFG¶
Compile C++ (including constexpr) using
c_to_sfg()in C++ mode.
- b_asic.llvm.llvm_file_to_sfg(path: str | PathLike[str], function_name: str | None = None, *, arrays: Mapping[str, LLVMArray] | None = None, infer_arrays: bool = False) SFG¶
Read a textual LLVM IR file and import it with
llvm_to_sfg().
- b_asic.llvm.llvm_to_sfg(ir: str, function_name: str | None = None, *, arrays: Mapping[str, LLVMArray] | None = None, infer_arrays: bool = False) SFG¶
Convert textual LLVM IR to an SFG using llvmlite.
- Parameters:
- irstr
LLVM assembly text containing scalar code and acyclic conditionals.
- function_namestr, optional
Exact LLVM symbol to import. Required if several definitions exist.
- arraysmapping of str to LLVMArray, optional
Interpret pointer arguments by name and globals by
@name. Input and output arrays can use memory or ordinary stream ports. State arrays use delays. Different configured names denote disjoint storage.- infer_arraysbool, default: False
Infer unconfigured pointer I/O from constant accesses and defined globals as state using their LLVM initializers. Explicit arrays override defaults.
- Returns:
- SFG
Arguments in declaration order, with input/inout pointers expanded into element ports. A scalar return, if any, precedes output-array writes. Floating-point constants are Python floats; integer constants are signed Python integers. LLVM precision and overflow wrapping are not retained as simulation data types. Unused arguments remain SFG inputs.
- Raises:
- UnsupportedLLVMError
After warning about each unsupported construct in the selected function. No partial graph is returned.
- ValueError
If LLVM parsing/verification or function selection fails.
- ImportError
If llvmlite is not installed.
Notes
Supports scalar
float/double, integer, and booleani1interfaces,fadd,fsub,fmul,fdiv,fneg, andllvm.sqrt/llvm.fabs. Integers useSignalType.INTEGERand support assignment,add, andsubonly, including selection through select/phi nodes. Floating-point comparisons, boolean logic, selects, and acyclic branches/phi nodes become comparisons and selectors. Both arithmetic branches execute; selectors do not provide short-circuit evaluation or guard invalid arithmetic. Configured arrays support constant-address GEPs, scalar loads and stores, and void returns. Array values are merged across acyclic branches. Unwritten inout/state branches retain their input/previous value. Output-only elements must be initialized on every path before a load or return. Cyclic control flow, dynamic addressing, vectors, integer arrays, other integer arithmetic, conversions, and other calls are rejected, including unused instructions. Other functions are not inspected.