Skip to main content

Numba Compatible RVSDG utilities

Project description

numba-rvsdg

Numba compatible RVSDG (Regionalized Value State Dependence Graph) utilities.

about

This repository contains Numba compatible utilities for working with RVSDGs (Regionalized Value State Dependency Graphs). RVSDGs are a type of Intermediary Representation (IR) suitable for regularizing Python bytecode within Numba.

The code in this repository is an implementation of the CFG restructuring algorithms in Bahmann2015, specifically those from section 4.1 and 4.2: namely "loop restructuring" and "branch restructuring". These are interesting for Numba because they serve to clearly identify regions withing the Python bytecode.

dependencies

  • Python 3.11
  • graphviz
  • pyyaml

As of 2023-03-06 you can create a conda env using the following:

conda env create -n numba-rvsdg python=3.11 python-graphviz
conda activate numba-rvsdg
pip install pyyaml

At the time of writing pyyaml was not available for Python 3.11 via defaults so it had to be installed with pip.

overview

The following files are included in this repository:

numba_rvsdg
├── __init__.py
├── core
│   ├── datastructures
│   │   ├── basic_block.py  # BasicBlock implementation
│   │   ├── block_map.py    # BlockMap implementation, maps labels to blocks
│   │   ├── byte_flow.py    # ByteFlow implementation, BlockMap + bytecode
│   │   ├── flow_info.py    # Converts program to ByteFlow
│   │   └── labels.py       # Collection of Label classes
│   ├── transformations.py  # Algorithms
│   └── utils.py            # Miscellaneous utilities
├── networkx_vendored
│   └── scc.py              # Strongly Connected Componets (loop detection)
├── rendering
│   └── rendering.py        # Graphivz based rendering of BlockMaps
├── tests
│   ├── simulator.py        # Simulator utility for running SCFGs
│   ├── test_byteflow.py    # Testung ByteFlow and others
│   ├── test_fig3.py        # Testing fig. 3 from Bahman2015
│   ├── test_fig4.py        # Testing fig. 4 from Bahman2015
│   ├── test_simulate.py    # Simulator based testing
│   └── test_transforms.py  # Testing graph transformations
└── utils

example

The following will process the given example function and display the four different stages. "initial" is the unprocessed bytecode as produced by cpython. "closed" is simply the closed variant of the initial CFG. "loop restructuring" is the loop-restructured version and "branch-restructured" is the final form which includes closing, loop-restructuring and branch-restructuring.

# Example: for loop with branch and early exit
def foo(n):
    c = 0
    for i in range(n):
        c += 1
        if i == 100:
            break
    return c

flow = ByteFlow.from_bytecode(foo)
ByteFlowRenderer().render_byteflow(flow).view("initial")

cflow = flow._join_returns()
ByteFlowRenderer().render_byteflow(cflow).view("closed")

lflow = cflow._restructure_loop()
ByteFlowRenderer().render_byteflow(lflow).view("loop restructured")

bflow = lflow._restructure_branch()
ByteFlowRenderer().render_byteflow(bflow).view("branch restructured")

initial closed loop-restructured branch-restructured

references

license

Copyright (c) 2022, Anaconda, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numba_rvsdg-0.0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

numba_rvsdg-0.0.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file numba_rvsdg-0.0.1.tar.gz.

File metadata

  • Download URL: numba_rvsdg-0.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for numba_rvsdg-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6dbb526173637722ec583c6b25d3cf848a0665ec86fa687370a1e0cd8f7f0695
MD5 ae6c3d76fd4076849b21730f90d47dba
BLAKE2b-256 c1e7dbc105158b0b774d694dca08107bcc943e535bedc476e2a1bf20688b8210

See more details on using hashes here.

File details

Details for the file numba_rvsdg-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: numba_rvsdg-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for numba_rvsdg-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6fe03b1498e41f71a5d4427514a614b3a9b8da991115633297400b8dbb40a899
MD5 0af7a3ad372f8f8cda560439eb117c4b
BLAKE2b-256 49dec270ea09898dd5f56aa9a1eaf4cd938ed1bacec262f63d1466337eb57f68

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page