-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Declare WASM modules in guppy #942
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
Draft
croyzor
wants to merge
17
commits into
main
Choose a base branch
from
cr/wasm-wip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
426a5e6
feat: add wasm module (WIP)
qartik 2da0811
Bits and pieces (WIP)
croyzor 219e293
Maintain uniqueness of WASM contexts for a module
croyzor 7361fe2
Add discard; make progress
croyzor d5c570f
Unification of WasmModuleTypes
croyzor af7f7d8
Redundant comment
croyzor 95b1cb2
Oups - add missing file
croyzor cc8aef1
Associate WASM functions with modules
croyzor caf37f4
Let more types be wasmable
croyzor c8a7b71
Add dummy WASM decorators for sphinx
croyzor c6d66d8
Add ConstStringArg
croyzor fe23a6c
checkpoint
croyzor 55805ee
yeee
croyzor 8de275a
Remove array from test file
croyzor c0f70b9
cleanup
croyzor 07e1f62
Merge remote-tracking branch 'origin/main' into 755-add-support-for-wasm
croyzor 9b3bf0b
cleanup
croyzor 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 hidden or 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 hidden or 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 hidden or 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,34 @@ | ||||||||
from dataclasses import dataclass | ||||||||
from typing import ClassVar | ||||||||
|
||||||||
from guppylang.diagnostic import Error | ||||||||
from guppylang.tys.ty import Type | ||||||||
|
||||||||
|
||||||||
class WasmError(Error): | ||||||||
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.
Suggested change
|
||||||||
title: ClassVar[str] = "WASM signature error" | ||||||||
|
||||||||
|
||||||||
@dataclass(frozen=True) | ||||||||
class FirstArgNotModule(WasmError): | ||||||||
span_label: ClassVar[str] = ( | ||||||||
"First argument to WASM function should be a reference to a WASM module" | ||||||||
"Instead, found {ty}" | ||||||||
) | ||||||||
ty: Type | ||||||||
|
||||||||
|
||||||||
@dataclass(frozen=True) | ||||||||
class UnWasmableType(WasmError): | ||||||||
span_label: ClassVar[str] = ( | ||||||||
"WASM function signature contained an unsupported type: {ty}" | ||||||||
) | ||||||||
ty: Type | ||||||||
|
||||||||
|
||||||||
@dataclass(frozen=True) | ||||||||
class NonFunctionWasmType(WasmError): | ||||||||
span_label: ClassVar[str] = ( | ||||||||
"WASM function didn't have a function type, instead found {ty}" | ||||||||
) | ||||||||
ty: Type |
This file contains hidden or 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 |
---|---|---|
|
@@ -15,15 +15,22 @@ | |
|
||
import guppylang | ||
from guppylang.ast_util import annotate_location | ||
from guppylang.checker.core import Globals | ||
from guppylang.compiler.core import GlobalConstId | ||
from guppylang.definition.common import DefId | ||
from guppylang.definition.const import RawConstDef | ||
from guppylang.definition.custom import ( | ||
CustomCallChecker, | ||
CustomFunctionDef, | ||
CustomInoutCallCompiler, | ||
DefaultCallChecker, | ||
NotImplementedCallCompiler, | ||
OpCompiler, | ||
RawCustomFunctionDef, | ||
WasmCallChecker, | ||
WasmModuleCallCompiler, | ||
WasmModuleDiscardCompiler, | ||
WasmModuleInitCompiler, | ||
) | ||
from guppylang.definition.extern import RawExternDef | ||
from guppylang.definition.function import ( | ||
|
@@ -38,7 +45,7 @@ | |
) | ||
from guppylang.definition.struct import RawStructDef | ||
from guppylang.definition.traced import RawTracedFunctionDef | ||
from guppylang.definition.ty import OpaqueTypeDef, TypeDef | ||
from guppylang.definition.ty import OpaqueTypeDef, TypeDef, WasmModule | ||
from guppylang.error import MissingModuleError, pretty_errors | ||
from guppylang.ipython_inspect import ( | ||
get_ipython_globals, | ||
|
@@ -56,9 +63,16 @@ | |
from guppylang.span import Loc, SourceMap, Span | ||
from guppylang.tracing.object import GuppyDefinition | ||
from guppylang.tys.arg import Argument | ||
from guppylang.tys.builtin import option_type | ||
from guppylang.tys.param import Parameter | ||
from guppylang.tys.subst import Inst | ||
from guppylang.tys.ty import NumericType | ||
from guppylang.tys.ty import ( | ||
FuncInput, | ||
FunctionType, | ||
InputFlags, | ||
NoneType, | ||
NumericType, | ||
) | ||
|
||
S = TypeVar("S") | ||
T = TypeVar("T") | ||
|
@@ -99,6 +113,7 @@ class _Guppy: | |
def __init__(self) -> None: | ||
self._modules = {} | ||
self._sources = SourceMap() | ||
self._next_wasm_context = 0 | ||
|
||
@overload | ||
def __call__(self, arg: F) -> F: ... | ||
|
@@ -582,6 +597,77 @@ def load_pytket( | |
mod.register_def(defn) | ||
return GuppyDefinition(defn) | ||
|
||
def wasm_module( | ||
self, filename: str, filehash: int | ||
) -> Decorator[PyClass, GuppyDefinition]: | ||
Comment on lines
+600
to
+602
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. Note that your decorator doesn't work with explicit modules. I think that's fine as long as we merge #983 first |
||
# N.B. Only one module per file and vice-versa | ||
guppy_module = self.get_module() | ||
ctx_id = guppy_module._get_next_wasm_context() | ||
assert guppy_module._instance_func_buffer is None | ||
guppy_module._instance_func_buffer = {} | ||
|
||
def dec(cls: PyClass) -> GuppyDefinition: | ||
wasm_module = WasmModule( | ||
DefId.fresh(guppy_module), | ||
cls.__name__, | ||
None, | ||
filename, | ||
filehash, | ||
ctx_id, | ||
) | ||
wasm_module_ty = wasm_module.check_instantiate([], Globals.default(), None) | ||
guppy_module.register_def(wasm_module) | ||
# Add a __call__ to the class | ||
call_method = CustomFunctionDef( | ||
DefId.fresh(guppy_module), | ||
"__new__", | ||
None, | ||
FunctionType([], option_type(wasm_module_ty)), | ||
DefaultCallChecker(), | ||
WasmModuleInitCompiler(wasm_module), | ||
False, | ||
GlobalConstId.fresh(f"{cls.__name__}.__new__"), | ||
True, | ||
) | ||
discard = CustomFunctionDef( | ||
DefId.fresh(guppy_module), | ||
"discard", | ||
None, | ||
FunctionType([FuncInput(wasm_module_ty, InputFlags.Owned)], NoneType()), | ||
DefaultCallChecker(), | ||
WasmModuleDiscardCompiler(), | ||
False, | ||
GlobalConstId.fresh(f"{cls.__name__}.__discard__"), | ||
True, | ||
) | ||
|
||
assert guppy_module._instance_func_buffer is not None | ||
|
||
guppy_module.register_def(wasm_module) | ||
guppy_module._instance_func_buffer |= { | ||
"__new__": call_method, | ||
"discard": discard, | ||
} | ||
|
||
guppy_module._register_buffered_instance_funcs(wasm_module) | ||
return GuppyDefinition(wasm_module) | ||
|
||
return dec | ||
|
||
def wasm(self, f: PyFunc) -> GuppyDefinition: | ||
guppy_module = self.get_module() | ||
func = RawCustomFunctionDef( | ||
DefId.fresh(guppy_module), | ||
f.__name__, | ||
None, | ||
f, | ||
WasmCallChecker(), | ||
WasmModuleCallCompiler(f.__name__), | ||
True, | ||
) | ||
guppy_module.register_def(func) | ||
return GuppyDefinition(func) | ||
|
||
|
||
class _GuppyDummy: | ||
"""A dummy class with the same interface as `@guppy` that is used during sphinx | ||
|
@@ -638,6 +724,12 @@ def load(self, *args: Any, **kwargs: Any) -> None: | |
def get_module(self, *args: Any, **kwargs: Any) -> Any: | ||
return GuppyModule("dummy", import_builtins=False) | ||
|
||
def wasm_module(self, *args: Any, **kwargs: Any) -> Any: | ||
return lambda cls: cls | ||
|
||
def wasm(self, *args: Any, **kwargs: Any) -> Any: | ||
return lambda cls: cls | ||
|
||
|
||
guppy = cast(_Guppy, _GuppyDummy()) if sphinx_running() else _Guppy() | ||
|
||
|
Oops, something went wrong.
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.
Should this be in
errors/wasm.py
?