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

[Circuit] Add get_instance method #1265

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion magma/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
pass

from magma.clock import is_clock_or_nested_clock, Clock, ClockTypes
from magma.common import only, IterableException
from magma.common import (
only,
IterableException,
EmptyIterableException,
NonSingletonIterableException,
)
from magma.config import get_debug_mode, set_debug_mode, config, RuntimeConfig
from magma.definition_context import (
DefinitionContext,
Expand Down Expand Up @@ -758,6 +763,15 @@ def is_definition(self):
def instances(self):
return self._context_.placer.instances()

def get_instance(self, name: str) -> 'AnonymousCircuitType':
instances = self.instances
try:
return only(filter(lambda i: i.name == name, instances))
rsetaluri marked this conversation as resolved.
Show resolved Hide resolved
except EmptyIterableException:
raise KeyError(name) from None
except NonSingletonIterableException:
raise KeyError(f"Found multiple instances with name '{name}'")
rsetaluri marked this conversation as resolved.
Show resolved Hide resolved

@property
def logs(self):
return self._context_.logs
Expand Down
17 changes: 17 additions & 0 deletions tests/test_circuit/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,20 @@ class Foo(m.Circuit):
Foo.bar

assert "has no attribute 'bar'" in str(e)


def test_get_instance_method():

Bar = m.Register(m.Bit) # just any module

class Foo(m.Circuit):
my_placeholder_var = Bar(name="my_instance_name")
Bar(name="duplicated")
Bar(name="duplicated")
my_instance_name = None

assert Foo.get_instance("my_instance_name") is Foo.my_placeholder_var
with pytest.raises(KeyError):
Foo.get_instance("blahblah")
with pytest.raises(KeyError):
Foo.get_instance("duplicated")