-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: Only lower definitions to Hugr if they are used #496
Changes from 34 commits
3c3f157
fcf6984
359c1fc
2b21a44
d25dab1
6f6a564
faa5b64
d99d694
f5cff19
6d869e4
37162f9
9287b39
f24183d
f9aaaa9
b65f837
133888f
3f04c01
fc29e84
2f70db7
17b5647
d4532ac
e3c4453
15e5c48
bafadfe
8f75828
6265442
a2579a2
9944f07
51e7f9b
c2b7489
860c552
a0ce98c
d18b805
a9ca64e
232571c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,52 @@ | |
from typing import cast | ||
|
||
from hugr import Wire, ops | ||
from hugr.build.dfg import DP, DfBase | ||
from hugr.build.dfg import DP, DefinitionBuilder, DfBase | ||
|
||
from guppylang.checker.core import FieldAccess, Place, PlaceId, Variable | ||
from guppylang.definition.common import CompiledDef, DefId | ||
from guppylang.definition.common import CheckedDef, CompilableDef, CompiledDef, DefId | ||
from guppylang.error import InternalGuppyError | ||
from guppylang.tys.ty import StructType | ||
|
||
CompiledGlobals = dict[DefId, CompiledDef] | ||
CompiledLocals = dict[PlaceId, Wire] | ||
|
||
|
||
class CompiledGlobals: | ||
"""Compilation context containing all available definitions. | ||
|
||
Maintains a `worklist` of definitions which have been used by other compiled code | ||
(i.e. `compile_outer` has been called) but have not yet been compiled/lowered | ||
themselves (i.e. `compile_inner` has not yet been called). | ||
""" | ||
|
||
module: DefinitionBuilder[ops.Module] | ||
checked: dict[DefId, CheckedDef] | ||
compiled: dict[DefId, CompiledDef] | ||
worklist: set[DefId] | ||
|
||
def __init__( | ||
self, | ||
checked: dict[DefId, CheckedDef], | ||
module: DefinitionBuilder[ops.Module], | ||
) -> None: | ||
self.module = module | ||
self.checked = checked | ||
self.worklist = set() | ||
self.compiled = {} | ||
|
||
def __getitem__(self, def_id: DefId) -> CompiledDef: | ||
if def_id not in self.compiled: | ||
defn = self.checked[def_id] | ||
self.compiled[def_id] = self._compile(defn) | ||
self.worklist.add(def_id) | ||
return self.compiled[def_id] | ||
|
||
def _compile(self, defn: CheckedDef) -> CompiledDef: | ||
if isinstance(defn, CompilableDef): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For another PR, I reckon we could probably get rid of |
||
return defn.compile_outer(self.module) | ||
return defn | ||
|
||
|
||
@dataclass | ||
class DFContainer: | ||
"""A dataflow graph under construction. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,10 @@ | |
|
||
import guppylang.compiler.hugr_extension | ||
from guppylang.checker.core import Globals, PyScope | ||
from guppylang.compiler.core import CompiledGlobals | ||
from guppylang.definition.common import ( | ||
CheckableDef, | ||
CheckedDef, | ||
CompilableDef, | ||
CompiledDef, | ||
DefId, | ||
Definition, | ||
ParsableDef, | ||
|
@@ -264,18 +263,6 @@ def _check_defs( | |
for def_id, defn in parsed.items() | ||
} | ||
|
||
@staticmethod | ||
def _compile_defs( | ||
checked_defs: Mapping[DefId, CheckedDef], hugr_module: Module | ||
) -> dict[DefId, CompiledDef]: | ||
"""Helper method to compile checked definitions to Hugr.""" | ||
return { | ||
def_id: defn.compile_outer(hugr_module) | ||
if isinstance(defn, CompilableDef) | ||
else defn | ||
for def_id, defn in checked_defs.items() | ||
} | ||
|
||
def check(self) -> None: | ||
"""Type-checks the module.""" | ||
if self.checked: | ||
|
@@ -325,19 +312,20 @@ def compile(self) -> Package: | |
return self._compiled_hugr | ||
|
||
self.check() | ||
checked_defs = self._imported_checked_defs | self._checked_defs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
|
||
# Prepare Hugr for this module | ||
graph = Module() | ||
graph.metadata["name"] = self.name | ||
|
||
# Compile definitions to Hugr | ||
compiled_defs = self._compile_defs(self._imported_checked_defs, graph) | ||
compiled_defs |= self._compile_defs(self._checked_defs, graph) | ||
|
||
# Finally, compile the definition contents to Hugr. For example, this compiles | ||
# the bodies of functions. | ||
for defn in compiled_defs.values(): | ||
defn.compile_inner(compiled_defs) | ||
# Lower definitions to Hugr | ||
required = set(self._checked_defs.keys()) | ||
ctx = CompiledGlobals(checked_defs, graph) | ||
_request_compilation = [ctx[def_id] for def_id in required] | ||
while ctx.worklist: | ||
next_id = ctx.worklist.pop() | ||
next_def = ctx[next_id] | ||
next_def.compile_inner(ctx) | ||
|
||
hugr = graph.hugr | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably rename this, considering the class does way more than just holding the globals. See #497