Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TransactionBase into a Protocol #472

Merged
merged 13 commits into from
Oct 20, 2023
18 changes: 18 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ jobs:
# https://github.com/actions/runner/issues/2033
chown -R $(id -u):$(id -g) $PWD

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: "pip"
cache-dependency-path: |
requirements.txt
requirements-dev.txt

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
Expand Down Expand Up @@ -84,6 +93,15 @@ jobs:
# https://github.com/actions/runner/issues/2033
chown -R $(id -u):$(id -g) $PWD

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: "pip"
cache-dependency-path: |
requirements.txt
requirements-dev.txt

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy_gh_pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.11"
cache: "pip"
cache-dependency-path: |
requirements.txt
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: |
requirements.txt
requirements-dev.txt

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
Expand Down Expand Up @@ -76,7 +85,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
cache: 'pip'
cache-dependency-path: |
requirements.txt
Expand Down Expand Up @@ -105,7 +114,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
cache: 'pip'
cache-dependency-path: |
requirements.txt
Expand Down
27 changes: 21 additions & 6 deletions transactron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
from collections.abc import Sequence, Iterable, Callable, Mapping, Iterator
from contextlib import contextmanager
from enum import Enum, auto
from typing import ClassVar, NoReturn, TypeAlias, TypedDict, Union, Optional, Tuple
from typing import (
ClassVar,
NoReturn,
TypeAlias,
TypedDict,
Union,
Optional,
Tuple,
TypeVar,
Protocol,
runtime_checkable,
)
from graphlib import TopologicalSorter
from typing_extensions import Self
from amaranth import *
Expand Down Expand Up @@ -38,6 +49,7 @@
TransactionScheduler: TypeAlias = Callable[["MethodMap", TransactionGraph, TransactionGraphCC, PriorityOrder], Module]
RecordDict: TypeAlias = ValueLike | Mapping[str, "RecordDict"]
TransactionOrMethod: TypeAlias = Union["Transaction", "Method"]
TransactionOrMethodBound = TypeVar("TransactionOrMethodBound", bound=Union["Transaction", "Method"])


class Priority(Enum):
Expand Down Expand Up @@ -670,15 +682,20 @@ def elaborate(self, platform):
return self.main_module


class TransactionBase(Owned):
@runtime_checkable
tilk marked this conversation as resolved.
Show resolved Hide resolved
class TransactionBase(Owned, Protocol):
stack: ClassVar[list[Union["Transaction", "Method"]]] = []
def_counter: ClassVar[count] = count()
def_order: int
defined: bool = False
Kristopher38 marked this conversation as resolved.
Show resolved Hide resolved
name: str
method_uses: dict["Method", Tuple[ValueLike, ValueLike]]
relations: list[RelationBase]
simultaneous_list: list[TransactionOrMethod]
independent_list: list[TransactionOrMethod]
Kristopher38 marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self):
self.method_uses: dict[Method, Tuple[ValueLike, ValueLike]] = dict()
self.method_uses: dict["Method", Tuple[ValueLike, ValueLike]] = dict()
self.relations: list[RelationBase] = []
self.simultaneous_list: list[TransactionOrMethod] = []
self.independent_list: list[TransactionOrMethod] = []
Expand Down Expand Up @@ -769,9 +786,7 @@ def _independent(self, *others: TransactionOrMethod) -> None:
self.independent_list += others

@contextmanager
def context(self, m: TModule) -> Iterator[Self]:
assert isinstance(self, Transaction) or isinstance(self, Method) # for typing

def context(self: TransactionOrMethodBound, m: TModule) -> Iterator[TransactionOrMethodBound]:
tilk marked this conversation as resolved.
Show resolved Hide resolved
parent = TransactionBase.peek()
if parent is not None:
parent.schedule_before(self)
Expand Down
5 changes: 2 additions & 3 deletions transactron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""

from enum import IntFlag
from abc import ABC
from collections import defaultdict
from typing import Literal, Optional
from typing import Literal, Optional, Protocol

from amaranth.hdl.ir import Elaboratable, Fragment
from .tracing import TracingFragment


class Owned(ABC):
class Owned(Protocol):
name: str
owner: Optional[Elaboratable]

Expand Down
Loading