Skip to content

Commit 3aeb873

Browse files
committed
[RTG] Add the PyRTG frontend
This commit adds the boiler plate and basic rtgtool with a minimal example to run.
1 parent f6665f1 commit 3aeb873

File tree

14 files changed

+610
-3
lines changed

14 files changed

+610
-3
lines changed

frontends/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
# ===-----------------------------------------------------------------------===//
88

99
set (CIRCT_KNOWN_FRONTENDS
10-
PyCDE)
10+
PyCDE
11+
PyRTG
12+
)
1113
set(CIRCT_ENABLE_FRONTENDS "" CACHE STRING "List of frontends to enable")
1214
foreach(proj ${CIRCT_KNOWN_FRONTENDS})
1315
string(TOUPPER "${proj}" upper_proj)

frontends/PyRTG/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# /src/circt created via
3+
# `ln -s <build dir>/tools/circt/python_packages/pyrtg/pyrtg/circt frontends/PyRTG/src/circt`
4+
# to help PyRTG devs with code completion.
5+
/src/pyrtg/circt

frontends/PyRTG/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ===- CMakeLists.txt - PyRTG top level cmake -----------------*- cmake -*-===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
8+
9+
llvm_canonicalize_cmake_booleans(
10+
CIRCT_BINDINGS_PYTHON_ENABLED
11+
)
12+
13+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
14+
message(FATAL_ERROR "PyRTG cannot be built as separate project")
15+
endif()
16+
17+
if (NOT CIRCT_BINDINGS_PYTHON_ENABLED)
18+
message(FATAL_ERROR "PyRTG requires that CIRCT python bindings be enabled")
19+
endif()
20+
21+
add_subdirectory(src)
22+
add_subdirectory(test)

frontends/PyRTG/pyproject.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=68",
4+
"setuptools_scm>=8.0",
5+
"wheel",
6+
"cmake>=3.12",
7+
8+
# MLIR build depends.
9+
"numpy",
10+
"pybind11>=2.11,<=2.12",
11+
"nanobind==2.4.0",
12+
"PyYAML",
13+
14+
# PyRTG depends
15+
]
16+
build-backend = "setuptools.build_meta"
17+
18+
# Enable version inference from Git.
19+
[tool.setuptools_scm]
20+
root = "../.."
21+
tag_regex = "^pyrtg-(\\d+\\.\\d+\\.\\d+)?$"
22+
local_scheme = "no-local-version"
23+
git_describe_command = "git describe --dirty --tags --long --match pyrtg*"

frontends/PyRTG/src/CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ===- CMakeLists.txt - PyRTG sources -------------------------*- cmake -*-===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
8+
9+
include(AddMLIRPython)
10+
11+
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=pyrtg.circt.")
12+
13+
declare_mlir_python_sources(PyRTGSources
14+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
15+
SOURCES
16+
pyrtg/__init__.py
17+
pyrtg/tests.py
18+
rtgtool/rtgtool.py
19+
)
20+
21+
################################################################################
22+
# Build composite binaries
23+
################################################################################
24+
25+
set(PYRTG_PYTHON_PACKAGE_DIR "${CIRCT_PYTHON_PACKAGES_DIR}/pyrtg/")
26+
27+
# Bundle our own, self-contained CAPI library with all of our deps.
28+
add_mlir_python_common_capi_library(PyRTG_CIRCTPythonCAPI
29+
INSTALL_COMPONENT CIRCTBindingsPythonModules
30+
INSTALL_DESTINATION python_packages/pyrtg/circt/_mlir_libs
31+
OUTPUT_DIRECTORY "${PYRTG_PYTHON_PACKAGE_DIR}/pyrtg/circt/_mlir_libs"
32+
RELATIVE_INSTALL_ROOT "../../.."
33+
DECLARED_SOURCES
34+
MLIRPythonSources.Core
35+
CIRCTBindingsPythonExtension
36+
)
37+
38+
add_mlir_python_modules(PyRTG_CIRCTPythonModules
39+
ROOT_PREFIX "${PYRTG_PYTHON_PACKAGE_DIR}/pyrtg/circt"
40+
INSTALL_PREFIX "python_packages/pyrtg/circt"
41+
DECLARED_SOURCES
42+
MLIRPythonSources.Core
43+
CIRCTBindingsPythonExtension
44+
CIRCTBindingsPythonSources
45+
COMMON_CAPI_LINK_LIBS
46+
PyRTG_CIRCTPythonCAPI
47+
)
48+
49+
add_mlir_python_modules(PyRTG
50+
ROOT_PREFIX "${PYRTG_PYTHON_PACKAGE_DIR}/"
51+
INSTALL_PREFIX "python_packages/"
52+
DECLARED_SOURCES
53+
PyRTGSources
54+
COMMON_CAPI_LINK_LIBS
55+
PyRTG_CIRCTPythonCAPI
56+
)
57+
58+
install(TARGETS PyRTG_CIRCTPythonCAPI
59+
DESTINATION python_packages/pyrtg/circt/_mlir_libs
60+
RUNTIME_DEPENDENCIES
61+
PRE_EXCLUDE_REGEXES ".*"
62+
PRE_INCLUDE_REGEXES ".*zlib.*"
63+
COMPONENT PyRTG
64+
)
65+
add_dependencies(PyRTG PyRTG_CIRCTPythonModules)
66+
add_dependencies(install-PyRTG install-PyRTG_CIRCTPythonModules)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from . import circt
6+
from . import tests
7+
from .tests import test

frontends/PyRTG/src/pyrtg/tests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
import inspect
6+
7+
from .circt import ir
8+
from .circt.dialects import rtg
9+
10+
11+
class Test:
12+
"""
13+
Represents an RTG Test. Stores the test function and location.
14+
"""
15+
16+
type: ir.Type
17+
18+
def __init__(self, test_func):
19+
self.test_func = test_func
20+
21+
sig = inspect.signature(test_func)
22+
assert len(sig.parameters) == 0, "test arguments not supported yet"
23+
24+
self.type = rtg.DictType.get(None, [])
25+
26+
@property
27+
def name(self) -> str:
28+
return self.test_func.__name__
29+
30+
def codegen(self):
31+
test = rtg.TestOp(self.name, ir.TypeAttr.get(self.type))
32+
block = ir.Block.create_at_start(test.bodyRegion, [])
33+
with ir.InsertionPoint(block):
34+
self.test_func(*block.arguments)
35+
36+
37+
def test(func):
38+
"""
39+
Decorator for RTG test functions.
40+
"""
41+
42+
return Test(func)

0 commit comments

Comments
 (0)