Skip to content

Commit ba1a347

Browse files
authored
Merge pull request #5370 from donn/pyosys_pybind11
pyosys: rewrite using pybind11
2 parents 8699100 + 440e331 commit ba1a347

38 files changed

+2382
-2722
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ indent_style = space
1111
indent_size = 2
1212
trim_trailing_whitespace = false
1313

14+
[*.rst]
15+
indent_style = space
16+
indent_size = 3
17+
1418
[*.yml]
1519
indent_style = space
1620
indent_size = 2

.github/workflows/wheels.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ jobs:
5555
submodules: true
5656
persist-credentials: false
5757
- uses: actions/setup-python@v5
58-
- name: Get Boost Source
59-
shell: bash
60-
run: |
61-
mkdir -p boost
62-
curl -L https://github.com/boostorg/boost/releases/download/boost-1.86.0/boost-1.86.0-b2-nodocs.tar.gz | tar --strip-components=1 -xzC boost
6358
- name: Get FFI
6459
shell: bash
6560
run: |
@@ -103,21 +98,16 @@ jobs:
10398
CIBW_BEFORE_ALL: bash ./.github/workflows/wheels/cibw_before_all.sh
10499
CIBW_ENVIRONMENT: >
105100
OPTFLAGS=-O3
106-
CXXFLAGS=-I./boost/pfx/include
107-
LINKFLAGS=-L./boost/pfx/lib
108101
PKG_CONFIG_PATH=./ffi/pfx/lib/pkgconfig
109-
makeFlags='BOOST_PYTHON_LIB=./boost/pfx/lib/libboost_python*.a'
110102
PATH="$PWD/bison/src:$PATH"
111103
CIBW_ENVIRONMENT_MACOS: >
112104
OPTFLAGS=-O3
113-
CXXFLAGS=-I./boost/pfx/include
114-
LINKFLAGS=-L./boost/pfx/lib
115105
PKG_CONFIG_PATH=./ffi/pfx/lib/pkgconfig
116106
MACOSX_DEPLOYMENT_TARGET=11
117-
makeFlags='BOOST_PYTHON_LIB=./boost/pfx/lib/libboost_python*.a CONFIG=clang'
107+
makeFlags='CONFIG=clang'
118108
PATH="$PWD/bison/src:$PATH"
119109
CIBW_BEFORE_BUILD: bash ./.github/workflows/wheels/cibw_before_build.sh
120-
CIBW_TEST_COMMAND: python3 {project}/tests/arch/ecp5/add_sub.py
110+
CIBW_TEST_COMMAND: python3 {project}/tests/pyosys/run_tests.py
121111
- uses: actions/upload-artifact@v4
122112
with:
123113
name: python-wheels-${{ matrix.os.runner }}
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
set -e
22
set -x
33

4-
# Don't use objects from previous compiles on Windows/macOS
5-
make clean
4+
# Don't use Python objects from previous compiles
5+
make clean-py
66

77
# DEBUG: show python3 and python3-config outputs
88
if [ "$(uname)" != "Linux" ]; then
@@ -11,24 +11,3 @@ if [ "$(uname)" != "Linux" ]; then
1111
fi
1212
python3 --version
1313
python3-config --includes
14-
15-
# Build boost
16-
cd ./boost
17-
## Delete the artefacts from previous builds (if any)
18-
rm -rf ./pfx
19-
## Bootstrap bjam
20-
./bootstrap.sh --prefix=./pfx
21-
## Build Boost against current version of Python, only for
22-
## static linkage (Boost is statically linked because system boost packages
23-
## wildly vary in versions, including the libboost_python3 version)
24-
./b2\
25-
-j$(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu)\
26-
--prefix=./pfx\
27-
--with-filesystem\
28-
--with-system\
29-
--with-python\
30-
cxxflags="$(python3-config --includes) -std=c++17 -fPIC"\
31-
cflags="$(python3-config --includes) -fPIC"\
32-
link=static\
33-
variant=release\
34-
install

CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ techlibs/gowin/ @pepijndevos
3838
techlibs/gatemate/ @pu-cc
3939

4040
# pyosys
41-
misc/*.py @btut
41+
pyosys/* @donn
42+
setup.py @donn
4243

4344
backends/firrtl @ucbjrl @azidar
4445

Makefile

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ all: top-all
102102
YOSYS_SRC := $(dir $(firstword $(MAKEFILE_LIST)))
103103
VPATH := $(YOSYS_SRC)
104104

105-
CXXSTD ?= c++17
105+
export CXXSTD ?= c++17
106106
CXXFLAGS := $(CXXFLAGS) -Wall -Wextra -ggdb -I. -I"$(YOSYS_SRC)" -MD -MP -D_YOSYS_ -fPIC -I$(PREFIX)/include
107107
LIBS := $(LIBS) -lstdc++ -lm
108108
PLUGIN_LINKFLAGS :=
@@ -133,10 +133,6 @@ LINKFLAGS += -rdynamic
133133
ifneq ($(shell :; command -v brew),)
134134
BREW_PREFIX := $(shell brew --prefix)/opt
135135
$(info $$BREW_PREFIX is [${BREW_PREFIX}])
136-
ifeq ($(ENABLE_PYOSYS),1)
137-
CXXFLAGS += -I$(BREW_PREFIX)/boost/include
138-
LINKFLAGS += -L$(BREW_PREFIX)/boost/lib -L$(BREW_PREFIX)/boost-python3/lib
139-
endif
140136
CXXFLAGS += -I$(BREW_PREFIX)/readline/include -I$(BREW_PREFIX)/flex/include
141137
LINKFLAGS += -L$(BREW_PREFIX)/readline/lib -L$(BREW_PREFIX)/flex/lib
142138
PKG_CONFIG_PATH := $(BREW_PREFIX)/libffi/lib/pkgconfig:$(PKG_CONFIG_PATH)
@@ -348,31 +344,24 @@ ifeq ($(ENABLE_LIBYOSYS),1)
348344
TARGETS += libyosys.so
349345
endif
350346

347+
PY_WRAPPER_FILE = pyosys/wrappers
348+
349+
# running make clean on just those and then recompiling saves a lot of
350+
# time when running cibuildwheel
351+
PYTHON_OBJECTS = pyosys/wrappers.o kernel/drivers.o kernel/yosys.o passes/cmds/plugin.o
352+
351353
ifeq ($(ENABLE_PYOSYS),1)
352354
# python-config --ldflags includes -l and -L, but LINKFLAGS is only -L
353355
LINKFLAGS += $(filter-out -l%,$(shell $(PYTHON_CONFIG) --ldflags))
354356
LIBS += $(shell $(PYTHON_CONFIG) --libs)
355357
EXE_LIBS += $(filter-out $(LIBS),$(shell $(PYTHON_CONFIG_FOR_EXE) --libs))
356-
CXXFLAGS += $(shell $(PYTHON_CONFIG) --includes) -DWITH_PYTHON
357-
358-
# Detect name of boost_python library. Some distros use boost_python-py<version>, other boost_python<version>, some only use the major version number, some a concatenation of major and minor version numbers
359-
CHECK_BOOST_PYTHON = (echo "int main(int argc, char ** argv) {return 0;}" | $(CXX) -xc -o /dev/null $(LINKFLAGS) $(EXE_LIBS) $(LIBS) -l$(1) - > /dev/null 2>&1 && echo "-l$(1)")
360-
BOOST_PYTHON_LIB ?= $(shell \
361-
$(call CHECK_BOOST_PYTHON,boost_python-py$(subst .,,$(PYTHON_VERSION))) || \
362-
$(call CHECK_BOOST_PYTHON,boost_python-py$(PYTHON_MAJOR_VERSION)) || \
363-
$(call CHECK_BOOST_PYTHON,boost_python$(subst .,,$(PYTHON_VERSION))) || \
364-
$(call CHECK_BOOST_PYTHON,boost_python$(PYTHON_MAJOR_VERSION)) \
365-
)
358+
PYBIND11_INCLUDE ?= $(shell $(PYTHON_EXECUTABLE) -m pybind11 --includes)
359+
CXXFLAGS += -I$(PYBIND11_INCLUDE) -DYOSYS_ENABLE_PYTHON
360+
CXXFLAGS += $(shell $(PYTHON_CONFIG) --includes) -DYOSYS_ENABLE_PYTHON
366361

367-
ifeq ($(BOOST_PYTHON_LIB),)
368-
$(error BOOST_PYTHON_LIB could not be detected. Please define manually)
369-
endif
370-
371-
LIBS += $(BOOST_PYTHON_LIB) -lboost_system -lboost_filesystem
372-
PY_WRAPPER_FILE = kernel/python_wrappers
373362
OBJS += $(PY_WRAPPER_FILE).o
374-
PY_GEN_SCRIPT= py_wrap_generator
375-
PY_WRAP_INCLUDES := $(shell $(PYTHON_EXECUTABLE) -c "from misc import $(PY_GEN_SCRIPT); $(PY_GEN_SCRIPT).print_includes()")
363+
PY_GEN_SCRIPT = pyosys/generator.py
364+
PY_WRAP_INCLUDES := $(shell $(PYTHON_EXECUTABLE) $(PY_GEN_SCRIPT) --print-includes)
376365
endif # ENABLE_PYOSYS
377366

378367
ifeq ($(ENABLE_READLINE),1)
@@ -786,9 +775,9 @@ endif
786775
$(P) cat $< | grep -E -v "#[ ]*(include|error)" | $(CXX) $(CXXFLAGS) -x c++ -o $@ -E -P -
787776

788777
ifeq ($(ENABLE_PYOSYS),1)
789-
$(PY_WRAPPER_FILE).cc: misc/$(PY_GEN_SCRIPT).py $(PY_WRAP_INCLUDES)
778+
$(PY_WRAPPER_FILE).cc: $(PY_GEN_SCRIPT) pyosys/wrappers_tpl.cc $(PY_WRAP_INCLUDES) pyosys/hashlib.h
790779
$(Q) mkdir -p $(dir $@)
791-
$(P) $(PYTHON_EXECUTABLE) -c "from misc import $(PY_GEN_SCRIPT); $(PY_GEN_SCRIPT).gen_wrappers(\"$(PY_WRAPPER_FILE).cc\")"
780+
$(P) $(PYTHON_EXECUTABLE) $(PY_GEN_SCRIPT) $(PY_WRAPPER_FILE).cc
792781
endif
793782

794783
%.o: %.cpp
@@ -1035,12 +1024,12 @@ ifeq ($(ENABLE_LIBYOSYS),1)
10351024
if [ -n "$(STRIP)" ]; then $(INSTALL_SUDO) $(STRIP) -S $(DESTDIR)$(LIBDIR)/libyosys.so; fi
10361025
ifeq ($(ENABLE_PYOSYS),1)
10371026
$(INSTALL_SUDO) mkdir -p $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys
1027+
$(INSTALL_SUDO) cp pyosys/__init__.py $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys/__init__.py
10381028
$(INSTALL_SUDO) cp libyosys.so $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys/libyosys.so
10391029
$(INSTALL_SUDO) cp -r share $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys
10401030
ifeq ($(ENABLE_ABC),1)
10411031
$(INSTALL_SUDO) cp yosys-abc $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys/yosys-abc
10421032
endif
1043-
$(INSTALL_SUDO) cp misc/__init__.py $(DESTDIR)$(PYTHON_DESTDIR)/$(subst -,_,$(PROGRAM_PREFIX))pyosys/
10441033
endif
10451034
endif
10461035
ifeq ($(ENABLE_PLUGINS),1)
@@ -1130,12 +1119,10 @@ DOC_TARGET ?= html
11301119
docs: docs/prep
11311120
$(Q) $(MAKE) -C docs $(DOC_TARGET)
11321121

1133-
clean:
1122+
clean: clean-py
11341123
rm -rf share
1135-
rm -rf kernel/*.pyh
1136-
rm -f $(OBJS) $(GENFILES) $(TARGETS) $(EXTRA_TARGETS) $(EXTRA_OBJS) $(PY_WRAP_INCLUDES) $(PY_WRAPPER_FILE).cc
1124+
rm -f $(OBJS) $(GENFILES) $(TARGETS) $(EXTRA_TARGETS) $(EXTRA_OBJS) $(PY_WRAP_INCLUDES)
11371125
rm -f kernel/version_*.o kernel/version_*.cc
1138-
rm -f kernel/python_wrappers.o
11391126
rm -f libs/*/*.d frontends/*/*.d passes/*/*.d backends/*/*.d kernel/*.d techlibs/*/*.d
11401127
rm -rf tests/asicworld/*.out tests/asicworld/*.log
11411128
rm -rf tests/hana/*.out tests/hana/*.log
@@ -1149,8 +1136,14 @@ clean:
11491136
rm -f $(addsuffix /run-test.mk,$(MK_TEST_DIRS))
11501137
-$(MAKE) -C docs clean
11511138
rm -rf docs/util/__pycache__
1139+
rm -f libyosys.so
1140+
1141+
clean-py:
1142+
rm -f $(PY_WRAPPER_FILE).inc.cc $(PY_WRAPPER_FILE).cc
1143+
rm -f $(PYTHON_OBJECTS)
11521144
rm -f *.whl
11531145
rm -f libyosys.so
1146+
rm -rf kernel/*.pyh
11541147

11551148
clean-abc:
11561149
$(MAKE) -C abc DEP= clean

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/source/_images/**/*.svg
77
/source/_images/**/*.dot
88
/source/_images/code_examples
9+
/venv
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pyosys import libyosys as ys
2+
3+
class AllEnablePass(ys.Pass):
4+
def __init__(self):
5+
super().__init__(
6+
"all_enable",
7+
"makes all _DFF_P_ registers require an enable signal"
8+
)
9+
10+
def execute(self, args, design):
11+
ys.log_header(design, "Adding enable signals\n")
12+
ys.log_push()
13+
top_module = design.top_module()
14+
15+
if "\\enable" not in top_module.wires_:
16+
enable_line = top_module.addWire("\\enable")
17+
enable_line.port_input = True
18+
top_module.fixup_ports()
19+
20+
for cell in top_module.cells_.values():
21+
if cell.type != "$_DFF_P_":
22+
continue
23+
cell.type = "$_DFFE_PP_"
24+
cell.setPort("\\E", ys.SigSpec(enable_line))
25+
ys.log_pop()
26+
27+
p = AllEnablePass() # register the pass
28+
29+
# using the pass
30+
31+
design = ys.Design()
32+
ys.run_pass("read_verilog tests/simple/fiedler-cooley.v", design)
33+
ys.run_pass("hierarchy -check -auto-top", design)
34+
ys.run_pass("synth", design)
35+
ys.run_pass("all_enable", design)
36+
ys.run_pass("write_verilog out.v", design)
37+
ys.run_pass("synth_ice40 -json out.json", design)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from pyosys import libyosys as ys
2+
3+
# loading design
4+
design = ys.Design()
5+
6+
ys.run_pass("read_verilog tests/simple/fiedler-cooley.v", design)
7+
ys.run_pass("hierarchy -check -auto-top", design)
8+
9+
# top module inspection
10+
top_module = design.top_module()
11+
12+
for id, wire in top_module.wires_.items():
13+
if not wire.port_input and not wire.port_output:
14+
continue
15+
description = "input" if wire.port_input else "output"
16+
description += " " + wire.name.str()
17+
if wire.width != 1:
18+
frm = wire.start_offset
19+
to = wire.start_offset + wire.width
20+
if wire.upto:
21+
to, frm = frm, to
22+
description += f" [{to}:{frm}]"
23+
print(description)
24+
25+
# synth
26+
27+
ys.run_pass("synth", design)
28+
29+
# adding the enable line
30+
31+
enable_line = top_module.addWire("\\enable")
32+
enable_line.port_input = True
33+
top_module.fixup_ports()
34+
35+
# hooking the enable line to the internal dff cells
36+
37+
for cell in top_module.cells_.values():
38+
if cell.type != "$_DFF_P_":
39+
continue
40+
cell.type = "$_DFFE_PP_"
41+
cell.setPort("\\E", ys.SigSpec(enable_line))
42+
43+
# run check
44+
45+
top_module.check()
46+
ys.run_pass("stat", design)
47+
48+
# write outputs
49+
50+
ys.run_pass("write_verilog out.v", design)
51+
ys.run_pass("synth_ice40 -json out.json", design)

docs/source/using_yosys/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ ways Yosys can interact with designs for a deeper investigation.
1717
more_scripting/index
1818
bugpoint
1919
verilog
20+
pyosys

0 commit comments

Comments
 (0)