Skip to content

Commit

Permalink
Merge c939e06 into 7655fb6
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Durst committed Jun 12, 2019
2 parents 7655fb6 + c939e06 commit 1a9101e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def magma_test():
import magma.config
magma.config.set_compile_dir('callee_file_dir')
clear_cachedFunctions()
magma.backend.coreir_.__reset_context()
magma.backend.coreir_.CoreIRContextSingleton().reset_instance()
8 changes: 8 additions & 0 deletions magma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def cache_definition(fn):
cachedFunctions.append(lru_cache(maxsize=None)(fn))
return cachedFunctions[-1]

def singleton(cls):
instance = [None]
def wrapper(*args, **kwargs):
if instance[0] is None:
instance[0] = cls(*args, **kwargs)
return instance[0]

return wrapper

# wires
from .port import *
Expand Down
22 changes: 14 additions & 8 deletions magma/backend/coreir_.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import inspect
import copy
import json
from .. import singleton

from collections import defaultdict

Expand Down Expand Up @@ -69,21 +70,26 @@ def magma_port_to_coreir(port):

return select.replace("[", ".").replace("]", "")

# Singleton context meant to be used with coreir/magma code
@singleton
class CoreIRContextSingleton:
__instance = None

magma_coreir_context = coreir.Context() # Singleton context meant to be used with coreir/magma code
def __reset_context():
"""
Testing hook so every test has a fresh context
"""
global magma_coreir_context
magma_coreir_context = coreir.Context()
def get_instance(self):
return self.__instance

def reset_instance(self):
self.__instance = coreir.Context()

def __init__(self):
self.__instance = coreir.Context()
CoreIRContextSingleton()

class CoreIRBackend:
context_to_modules_map = {}
def __init__(self, context=None):
if context is None:
context = magma_coreir_context
context = CoreIRContextSingleton().get_instance()
if context not in CoreIRBackend.context_to_modules_map:
CoreIRBackend.context_to_modules_map[context] = {}
self.modules = CoreIRBackend.context_to_modules_map[context]
Expand Down
14 changes: 12 additions & 2 deletions magma/frontend/coreir_.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from magma.backend.coreir_ import CoreIRBackend
from magma import cache_definition
from magma.backend.coreir_ import CoreIRBackend, CoreIRContextSingleton
from magma.circuit import DefineCircuitKind, Circuit
from magma import cache_definition
from coreir.generator import Generator

@cache_definition
def GetCoreIRBackend():
return CoreIRBackend()

@cache_definition
def GetMagmaContext():
return CoreIRContextSingleton().get_instance()

def DefineModuleWrapper(cirb: CoreIRBackend, coreirModule, uniqueName, deps):
class ModuleWrapper(Circuit):
name = uniqueName
Expand All @@ -18,8 +27,9 @@ def definition(cls):

def DefineCircuitFromGeneratorWrapper(cirb: CoreIRBackend, namespace: str, generator: str,
uniqueName: str, dependentNamespaces: list = [],
genargs: dict = {}, runGenerators = True):
genargs: dict = {}, runGenerators = True, print_error=False):
moduleToWrap = cirb.context.import_generator(namespace,generator)(**genargs)

deps = [namespace] + dependentNamespaces
if runGenerators:
cirb.context.run_passes(["rungenerators"], deps)
Expand Down
3 changes: 2 additions & 1 deletion magma/simulator/coreir_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .simulator import CircuitSimulator, ExecutionState
from ..backend import coreir_
from ..frontend.coreir_ import GetMagmaContext
from ..scope import Scope
from ..ref import DefnRef, ArrayRef, TupleRef
from ..array import ArrayType
Expand Down Expand Up @@ -134,7 +135,7 @@ def __init__(self, circuit, clock, coreir_filename=None, context=None,
setup_clocks(circuit)

if context is None:
self.ctx = coreir.Context()
self.ctx = GetMagmaContext()
else:
self.ctx = context
coreir_.compile(circuit, coreir_filename, context=self.ctx)
Expand Down

0 comments on commit 1a9101e

Please sign in to comment.