-
Notifications
You must be signed in to change notification settings - Fork 174
backend: (llvm) add ctypes conversion registry [STACKED PR] #6183
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
sueszli
wants to merge
6
commits into
xdslproject:main
Choose a base branch
from
sueszli:convert-ctypes
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
6 commits
Select commit
Hold shift + click to select a range
fede964
backend: (llvm) add ctypes conversion registry
sueszli e835614
backend: (llvm) move convert_ctypes to xdsl.jit.llvm
sueszli 22c493f
testing: (pyjit) look up entry FuncOp via SymbolTable
sueszli ec4753d
testing: (pyjit) TODO for future Python-type registry
sueszli 257a62c
jit: (llvm) rename convert_ctypes to ctype_context
sueszli 4034d1b
jit: (llvm) rename ctype_context to c_type_context
sueszli 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import ctypes | ||
|
|
||
| import pytest | ||
|
|
||
| from xdsl.dialects.builtin import ( | ||
| Float16Type, | ||
| Float32Type, | ||
| Float64Type, | ||
| IndexType, | ||
| IntAttr, | ||
| IntegerType, | ||
| NoneType, | ||
| Signedness, | ||
| ) | ||
| from xdsl.dialects.llvm import LLVMPointerType, LLVMVoidType | ||
| from xdsl.ir import Attribute | ||
| from xdsl.jit.llvm.c_type_context import CTypeContext, register_builtin_ctypes | ||
| from xdsl.utils.exceptions import LLVMTranslationException | ||
|
|
||
|
|
||
| def test_register_and_resolve(): | ||
| ctx = CTypeContext() | ||
| ctx.register_ctype(Float32Type, lambda _: ctypes.c_float) | ||
| assert ctx.to_ctype(Float32Type()) is ctypes.c_float | ||
|
|
||
|
|
||
| def test_converter_receives_the_attribute(): | ||
| ctx = CTypeContext() | ||
| seen: list[IntegerType] = [] | ||
|
|
||
| def converter(t: IntegerType): | ||
| seen.append(t) | ||
| return ctypes.c_int32 | ||
|
|
||
| ctx.register_ctype(IntegerType, converter) | ||
| int_t = IntegerType(32) | ||
| assert ctx.to_ctype(int_t) is ctypes.c_int32 | ||
| assert seen == [int_t] | ||
|
|
||
|
|
||
| def test_resolve_unregistered_raises(): | ||
| ctx = CTypeContext() | ||
| with pytest.raises(LLVMTranslationException, match="No ctypes mapping"): | ||
| ctx.to_ctype(Float32Type()) | ||
|
|
||
|
|
||
| def test_re_register_overwrites(): | ||
| ctx = CTypeContext() | ||
| ctx.register_ctype(Float32Type, lambda _: ctypes.c_float) | ||
| ctx.register_ctype(Float32Type, lambda _: ctypes.c_double) | ||
| assert ctx.to_ctype(Float32Type()) is ctypes.c_double | ||
|
|
||
|
|
||
| def test_two_contexts_are_independent(): | ||
| a = CTypeContext() | ||
| b = CTypeContext() | ||
| a.register_ctype(Float32Type, lambda _: ctypes.c_float) | ||
| with pytest.raises(LLVMTranslationException): | ||
| b.to_ctype(Float32Type()) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def ctx() -> CTypeContext: | ||
| c = CTypeContext() | ||
| register_builtin_ctypes(c) | ||
| return c | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "type_attr, expected", | ||
| [ | ||
| (IntegerType(1), ctypes.c_bool), | ||
| (IntegerType(8), ctypes.c_int8), | ||
| (IntegerType(16), ctypes.c_int16), | ||
| (IntegerType(32), ctypes.c_int32), | ||
| (IntegerType(64), ctypes.c_int64), | ||
| (IntegerType(32, Signedness.SIGNED), ctypes.c_int32), | ||
| (IntegerType(32, Signedness.UNSIGNED), ctypes.c_int32), | ||
| (Float32Type(), ctypes.c_float), | ||
| (Float64Type(), ctypes.c_double), | ||
| (LLVMPointerType(), ctypes.c_void_p), | ||
| (LLVMPointerType(IntAttr(1)), ctypes.c_void_p), | ||
| (LLVMVoidType(), None), | ||
| (NoneType(), None), | ||
| ], | ||
| ) | ||
| def test_builtin_resolve(ctx: CTypeContext, type_attr: Attribute, expected: object): | ||
| assert ctx.to_ctype(type_attr) is expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "type_attr, match", | ||
| [ | ||
| (IntegerType(0), "integer of width 0"), | ||
| (IntegerType(17), "integer of width 17"), | ||
| (IntegerType(128), "integer of width 128"), | ||
| (Float16Type(), "No ctypes mapping for type"), | ||
| (IndexType(), "No ctypes mapping for type"), | ||
| ], | ||
| ) | ||
| def test_builtin_unsupported(ctx: CTypeContext, type_attr: Attribute, match: str): | ||
| with pytest.raises(LLVMTranslationException, match=match): | ||
| ctx.to_ctype(type_attr) |
Empty file.
Empty file.
|
superlopuh marked this conversation as resolved.
|
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,60 @@ | ||
| import ctypes | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from xdsl.dialects.builtin import Float32Type, Float64Type, IntegerType, NoneType | ||
| from xdsl.dialects.llvm import LLVMPointerType, LLVMVoidType | ||
| from xdsl.ir import Attribute | ||
| from xdsl.utils.exceptions import LLVMTranslationException | ||
|
|
||
|
|
||
| class CTypeContext: | ||
| """Registry of xDSL attribute classes to ctypes converters.""" | ||
|
|
||
| registry: dict[type[Attribute], Callable[[Any], Any]] | ||
| """Map from an xDSL attribute class to a converter producing its ctypes type.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self.registry = {} | ||
|
|
||
| def register_ctype( | ||
| self, | ||
| attr_type: type[Attribute], | ||
| converter: Callable[[Any], Any], | ||
| ) -> None: | ||
| self.registry[attr_type] = converter | ||
|
|
||
| def to_ctype(self, type_attr: Attribute) -> Any: | ||
| try: | ||
| converter = self.registry[type(type_attr)] | ||
| except KeyError: | ||
| raise LLVMTranslationException(f"No ctypes mapping for type: {type_attr}") | ||
| return converter(type_attr) | ||
|
|
||
|
|
||
| _INT_CTYPE_BY_WIDTH: dict[int, Any] = { | ||
| 1: ctypes.c_bool, | ||
| 8: ctypes.c_int8, | ||
| 16: ctypes.c_int16, | ||
| 32: ctypes.c_int32, | ||
| 64: ctypes.c_int64, | ||
| } | ||
|
|
||
|
|
||
| def _int_to_ctype(type_attr: IntegerType) -> Any: | ||
|
superlopuh marked this conversation as resolved.
|
||
| width = type_attr.width.data | ||
| try: | ||
| return _INT_CTYPE_BY_WIDTH[width] | ||
| except KeyError: | ||
| raise LLVMTranslationException( | ||
| f"No ctypes mapping for integer of width {width}" | ||
| ) | ||
|
|
||
|
|
||
| def register_builtin_ctypes(ctx: CTypeContext) -> None: | ||
| ctx.register_ctype(Float32Type, lambda _: ctypes.c_float) | ||
| ctx.register_ctype(Float64Type, lambda _: ctypes.c_double) | ||
| ctx.register_ctype(IntegerType, _int_to_ctype) | ||
| ctx.register_ctype(LLVMPointerType, lambda _: ctypes.c_void_p) | ||
| ctx.register_ctype(LLVMVoidType, lambda _: None) | ||
| ctx.register_ctype(NoneType, lambda _: None) | ||
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.
no, let's do this in this PR, but keep it minimal