.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/creating_and_transforming_sfgs/c_to_sfg.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_creating_and_transforming_sfgs_c_to_sfg.py: ========================== From C to an SFG with LLVM ========================== Clang can turn a C function with fixed-length loops and compile-time branches into static arithmetic. :func:`~b_asic.llvm.c_to_sfg` compiles the file to LLVM IR and maps that arithmetic to B-ASIC operations. Install ``b-asic[llvm]`` and a compatible Clang to run this example. If Clang or llvmlite is unavailable, the example prints a message and skips conversion. See :doc:`/llvm` for the supported subset and C++/``constexpr`` examples. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python import sys from importlib.util import find_spec from pathlib import Path from shutil import which from b_asic import c_to_sfg .. GENERATED FROM PYTHON SOURCE LINES 24-34 The C source ------------ The :download:`C file ` below takes two floating-point inputs and returns one floating-point result. The loop has a constant trip count, and each ``if`` condition becomes constant after unrolling. No runtime branches remain in the optimized LLVM IR. .. literalinclude:: llvm_kernel.c :language: c .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python source_path = Path("llvm_kernel.c") if not source_path.is_file(): source_path = Path(sys.argv[0]).resolve().with_name("llvm_kernel.c") .. GENERATED FROM PYTHON SOURCE LINES 40-46 Compile and import ------------------ The helper requests optimization and full loop unrolling, then checks that the resulting LLVM function contains only supported operations. Unsupported constructs produce warnings and an error instead of a partial SFG. .. GENERATED FROM PYTHON SOURCE LINES 46-59 .. code-block:: Python clang = which("clang") sfg = None if clang is None: print("Skipping C-to-SFG conversion: Clang is not installed or not on PATH.") elif find_spec("llvmlite") is None: print( 'Skipping C-to-SFG conversion: install llvmlite with pip install "b-asic[llvm]".' ) else: sfg = c_to_sfg(source_path, function_name="kernel", clang=clang) print(f"kernel(2.0, 3.0) = {sfg.evaluate(2.0, 3.0)}") .. rst-class:: sphx-glr-script-out .. code-block:: none kernel(2.0, 3.0) = 26.0 .. GENERATED FROM PYTHON SOURCE LINES 60-67 The resulting SFG ----------------- The two inputs follow the C argument order, and the return value becomes the output. Constants and operations use B-ASIC's generic data type, which can be configured later. When compilation is available, the graph is shown below; otherwise this section has no graph. .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python sfg # noqa: B018 - Sphinx-Gallery captures this expression as a graph. .. image-sg:: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_to_sfg_001.png :alt: c to sfg :srcset: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_to_sfg_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.089 seconds) .. _sphx_glr_download_examples_creating_and_transforming_sfgs_c_to_sfg.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: c_to_sfg.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: c_to_sfg.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: c_to_sfg.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_