Skip to content

Commit

Permalink
[RTG] Add the PyRTG frontend
Browse files Browse the repository at this point in the history
This commit adds the boiler plate and basic rtgtool with a minimal example to run.
  • Loading branch information
maerhart committed Feb 5, 2025
1 parent f6665f1 commit 3aeb873
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 3 deletions.
4 changes: 3 additions & 1 deletion frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# ===-----------------------------------------------------------------------===//

set (CIRCT_KNOWN_FRONTENDS
PyCDE)
PyCDE
PyRTG
)
set(CIRCT_ENABLE_FRONTENDS "" CACHE STRING "List of frontends to enable")
foreach(proj ${CIRCT_KNOWN_FRONTENDS})
string(TOUPPER "${proj}" upper_proj)
Expand Down
5 changes: 5 additions & 0 deletions frontends/PyRTG/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# /src/circt created via
# `ln -s <build dir>/tools/circt/python_packages/pyrtg/pyrtg/circt frontends/PyRTG/src/circt`
# to help PyRTG devs with code completion.
/src/pyrtg/circt
22 changes: 22 additions & 0 deletions frontends/PyRTG/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ===- CMakeLists.txt - PyRTG top level cmake -----------------*- cmake -*-===//
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===----------------------------------------------------------------------===//

llvm_canonicalize_cmake_booleans(
CIRCT_BINDINGS_PYTHON_ENABLED
)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(FATAL_ERROR "PyRTG cannot be built as separate project")
endif()

if (NOT CIRCT_BINDINGS_PYTHON_ENABLED)
message(FATAL_ERROR "PyRTG requires that CIRCT python bindings be enabled")
endif()

add_subdirectory(src)
add_subdirectory(test)
23 changes: 23 additions & 0 deletions frontends/PyRTG/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[build-system]
requires = [
"setuptools>=68",
"setuptools_scm>=8.0",
"wheel",
"cmake>=3.12",

# MLIR build depends.
"numpy",
"pybind11>=2.11,<=2.12",
"nanobind==2.4.0",
"PyYAML",

# PyRTG depends
]
build-backend = "setuptools.build_meta"

# Enable version inference from Git.
[tool.setuptools_scm]
root = "../.."
tag_regex = "^pyrtg-(\\d+\\.\\d+\\.\\d+)?$"
local_scheme = "no-local-version"
git_describe_command = "git describe --dirty --tags --long --match pyrtg*"
66 changes: 66 additions & 0 deletions frontends/PyRTG/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# ===- CMakeLists.txt - PyRTG sources -------------------------*- cmake -*-===//
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===----------------------------------------------------------------------===//

include(AddMLIRPython)

add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=pyrtg.circt.")

declare_mlir_python_sources(PyRTGSources
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
SOURCES
pyrtg/__init__.py
pyrtg/tests.py
rtgtool/rtgtool.py
)

################################################################################
# Build composite binaries
################################################################################

set(PYRTG_PYTHON_PACKAGE_DIR "${CIRCT_PYTHON_PACKAGES_DIR}/pyrtg/")

# Bundle our own, self-contained CAPI library with all of our deps.
add_mlir_python_common_capi_library(PyRTG_CIRCTPythonCAPI
INSTALL_COMPONENT CIRCTBindingsPythonModules
INSTALL_DESTINATION python_packages/pyrtg/circt/_mlir_libs
OUTPUT_DIRECTORY "${PYRTG_PYTHON_PACKAGE_DIR}/pyrtg/circt/_mlir_libs"
RELATIVE_INSTALL_ROOT "../../.."
DECLARED_SOURCES
MLIRPythonSources.Core
CIRCTBindingsPythonExtension
)

add_mlir_python_modules(PyRTG_CIRCTPythonModules
ROOT_PREFIX "${PYRTG_PYTHON_PACKAGE_DIR}/pyrtg/circt"
INSTALL_PREFIX "python_packages/pyrtg/circt"
DECLARED_SOURCES
MLIRPythonSources.Core
CIRCTBindingsPythonExtension
CIRCTBindingsPythonSources
COMMON_CAPI_LINK_LIBS
PyRTG_CIRCTPythonCAPI
)

add_mlir_python_modules(PyRTG
ROOT_PREFIX "${PYRTG_PYTHON_PACKAGE_DIR}/"
INSTALL_PREFIX "python_packages/"
DECLARED_SOURCES
PyRTGSources
COMMON_CAPI_LINK_LIBS
PyRTG_CIRCTPythonCAPI
)

install(TARGETS PyRTG_CIRCTPythonCAPI
DESTINATION python_packages/pyrtg/circt/_mlir_libs
RUNTIME_DEPENDENCIES
PRE_EXCLUDE_REGEXES ".*"
PRE_INCLUDE_REGEXES ".*zlib.*"
COMPONENT PyRTG
)
add_dependencies(PyRTG PyRTG_CIRCTPythonModules)
add_dependencies(install-PyRTG install-PyRTG_CIRCTPythonModules)
7 changes: 7 additions & 0 deletions frontends/PyRTG/src/pyrtg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

from . import circt
from . import tests
from .tests import test
42 changes: 42 additions & 0 deletions frontends/PyRTG/src/pyrtg/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import inspect

from .circt import ir
from .circt.dialects import rtg


class Test:
"""
Represents an RTG Test. Stores the test function and location.
"""

type: ir.Type

def __init__(self, test_func):
self.test_func = test_func

sig = inspect.signature(test_func)
assert len(sig.parameters) == 0, "test arguments not supported yet"

self.type = rtg.DictType.get(None, [])

@property
def name(self) -> str:
return self.test_func.__name__

def codegen(self):
test = rtg.TestOp(self.name, ir.TypeAttr.get(self.type))
block = ir.Block.create_at_start(test.bodyRegion, [])
with ir.InsertionPoint(block):
self.test_func(*block.arguments)


def test(func):
"""
Decorator for RTG test functions.
"""

return Test(func)
Loading

0 comments on commit 3aeb873

Please sign in to comment.