-
Notifications
You must be signed in to change notification settings - Fork 2
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: Add extern symbols #236
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c24a24c
feat: Add extern symbols
mark-koch 5cb16ac
Make symbol required and name optional
mark-koch ea52bac
Revert "Make symbol required and name optional"
mark-koch 58b6303
Stop assigning extern in tests
mark-koch e24d824
Test name to symbol propagation
mark-koch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import ast | ||
from dataclasses import dataclass, field | ||
|
||
from hugr.serialization import ops | ||
|
||
from guppylang.ast_util import AstNode | ||
from guppylang.checker.core import Globals | ||
from guppylang.compiler.core import CompiledGlobals, DFContainer | ||
from guppylang.definition.common import CompilableDef, ParsableDef | ||
from guppylang.definition.value import CompiledValueDef, ValueDef | ||
from guppylang.hugr_builder.hugr import Hugr, Node, OutPortV, VNode | ||
from guppylang.tys.parsing import type_from_ast | ||
|
||
|
||
@dataclass(frozen=True) | ||
class RawExternDef(ParsableDef): | ||
"""A raw extern symbol definition provided by the user.""" | ||
|
||
symbol: str | ||
constant: bool | ||
type_ast: ast.expr | ||
|
||
description: str = field(default="extern", init=False) | ||
|
||
def parse(self, globals: Globals) -> "ExternDef": | ||
"""Parses and checks the user-provided signature of the function.""" | ||
return ExternDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
type_from_ast(self.type_ast, globals, None), | ||
self.symbol, | ||
self.constant, | ||
self.type_ast, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExternDef(RawExternDef, ValueDef, CompilableDef): | ||
"""An extern symbol definition.""" | ||
|
||
def compile_outer(self, graph: Hugr, parent: Node) -> "CompiledExternDef": | ||
"""Adds a Hugr constant node for the extern definition to the provided graph.""" | ||
custom_const = { | ||
"symbol": self.symbol, | ||
"typ": self.ty.to_hugr(), | ||
"constant": self.constant, | ||
} | ||
value = ops.ExtensionValue( | ||
extensions=["prelude"], | ||
typ=self.ty.to_hugr(), | ||
value=ops.CustomConst(c="ConstExternalSymbol", v=custom_const), | ||
) | ||
const_node = graph.add_constant(ops.Value(value), self.ty, parent) | ||
return CompiledExternDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
self.ty, | ||
self.symbol, | ||
self.constant, | ||
self.type_ast, | ||
const_node, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CompiledExternDef(ExternDef, CompiledValueDef): | ||
"""An extern symbol definition that has been compiled to a Hugr constant.""" | ||
|
||
const_node: VNode | ||
|
||
def load( | ||
self, dfg: DFContainer, graph: Hugr, globals: CompiledGlobals, node: AstNode | ||
) -> OutPortV: | ||
"""Loads the extern value into a local Hugr dataflow graph.""" | ||
return graph.add_load_constant(self.const_node.out_port(0)).out_port(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:9 | ||
|
||
7: module = GuppyModule("test") | ||
8: | ||
9: guppy.extern(module, "x", ty="float[int]") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
GuppyError: Type `float` is not parameterized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.prelude.quantum import qubit | ||
|
||
import guppylang.prelude.quantum as quantum | ||
|
||
|
||
module = GuppyModule("test") | ||
|
||
guppy.extern(module, "x", ty="float[int]") | ||
|
||
module.compile() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from hugr.serialization import ops | ||
|
||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
|
||
def test_extern_float(validate): | ||
module = GuppyModule("module") | ||
|
||
guppy.extern(module, "ext", ty="float") | ||
|
||
@guppy(module) | ||
def main() -> float: | ||
return ext + ext # noqa: F821 | ||
|
||
hg = module.compile() | ||
validate(hg) | ||
|
||
[c] = [n.op.root for n in hg.nodes() if isinstance(n.op.root, ops.Const)] | ||
assert isinstance(c.v.root, ops.ExtensionValue) | ||
assert c.v.root.value.v["symbol"] == "ext" | ||
|
||
|
||
def test_extern_alt_symbol(validate): | ||
module = GuppyModule("module") | ||
|
||
guppy.extern(module, "ext", ty="int", symbol="foo") | ||
|
||
@guppy(module) | ||
def main() -> int: | ||
return ext # noqa: F821 | ||
|
||
hg = module.compile() | ||
validate(hg) | ||
|
||
[c] = [n.op.root for n in hg.nodes() if isinstance(n.op.root, ops.Const)] | ||
assert isinstance(c.v.root, ops.ExtensionValue) | ||
assert c.v.root.value.v["symbol"] == "foo" | ||
|
||
def test_extern_tuple(validate): | ||
module = GuppyModule("module") | ||
|
||
guppy.extern(module, "ext", ty="tuple[int, float]") | ||
|
||
@guppy(module) | ||
def main() -> float: | ||
x, y = ext # noqa: F821 | ||
return x + y | ||
|
||
validate(module.compile()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a bit dubious, I think name should and symbol should take it's place?
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.
The motivation behind leaving them distinct was that the LLVM symbol name might not follow Python's naming conventions, so you'd want to pick a different name to refer to it in your program.
But I'm fine with making symbol required and name optional.
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.
I would be fine with symbol optional, in fact I weakly prefer it(now that I understand better). My reading of the original code was that
name
did not propagate into thesymbol
of theConstExternSymbol
. I think you should add tests for how thesymbol
ofConstExternSymbol
is populated with and without the optional param (name or symbol, whichever you decide) here.