Skip to content

Commit

Permalink
Preliminary implementation for globals
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWeber42 committed Jan 18, 2021
1 parent 77a07b7 commit 9b5e4a7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
8 changes: 6 additions & 2 deletions dusk/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,12 @@ def constant(self, value):
_type,
)

@transform(Name(id=Capture(str).to("name"), ctx=AnyContext))
def var(self, name: str, index: expr = None):
@transform(Capture(Name(id=Capture(str).to("name"), ctx=AnyContext)).to("node"))
def var(self, node: Name, name: str, index: expr = None):

if hasattr(node, "sir"):
# FIXME: remove once symbol resolution has been properly unified
return node.sir

if not self.ctx.scope.current_scope.contains(name):
raise SemanticError(f"Undeclared variable '{name}'!", name)
Expand Down
1 change: 1 addition & 0 deletions dusk/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StencilObject:
callable: Callable
filename: str
pyast: Optional[ast.FunctionDef] = None
globals: Optional[sir.GlobalVariableMap]
sir_node: Optional[sir.SIR] = None

def __init__(self, callable: Callable, filename: str = "<unknown>"):
Expand Down
7 changes: 6 additions & 1 deletion dusk/passes/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from dusk.integration import StencilObject
from dusk.grammar import Grammar
from dusk.passes.symbol_resolution import resolve_symbols
from dusk.passes.resolve_globals import resolve_globals


def stencil_object_to_sir(stencil_object: StencilObject) -> sir.Stencil:

add_filename(stencil_object)
add_pyast(stencil_object)
resolve_symbols(stencil_object)
resolve_globals(stencil_object)
add_sir(stencil_object)

return stencil_object.sir_node
Expand Down Expand Up @@ -42,5 +44,8 @@ def add_sir(stencil_object: StencilObject) -> None:

sir_stencil = Grammar().stencil(stencil_object.pyast)
stencil_object.sir_node = ser.make_sir(
stencil_object.filename, sir.GridType.Value("Unstructured"), [sir_stencil]
stencil_object.filename,
sir.GridType.Value("Unstructured"),
[sir_stencil],
global_variables=stencil_object.globals,
)
8 changes: 8 additions & 0 deletions dusk/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

__all__ = [
"stencil",
"Global",
"Edge",
"Cell",
"Vertex",
Expand Down Expand Up @@ -38,6 +39,13 @@ def stencil(stencil: typing.Callable) -> typing.Callable:
return stencil


class Global:
name: str

def __init__(self, name: str):
self.name = name


class Edge(metaclass=internal.LocationType):
pass

Expand Down
39 changes: 39 additions & 0 deletions tests/stencils/test_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dusk.script import *
from dusk.test import stencil_test


dt = Global("dt")


@stencil_test()
def test_simple_global1(a: Field[Edge]):
with levels_downward:
a = dt


g = Global("some_global")


@stencil_test()
def test_simple_global2(a: Field[Edge], b: Field[Cell]):
with levels_downward:
a = sum_over(Edge > Cell, g * b)


one = Global("one")
two = Global("two")


@stencil_test()
def test_two_globals(a: Field[Edge]):
with levels_downward:
a = one + two


aliased = dt


@stencil_test()
def test_aliased_global(a: Field[Cell]):
with levels_upward:
a = aliased / (one - two)

0 comments on commit 9b5e4a7

Please sign in to comment.