Implement const-generics #360

Merged
sb10q merged 1 commits from feat/const-generics into master 2024-08-17 17:37:20 +08:00
Collaborator

Const generics are declared using e.g. T = ConstGeneric("T", int32), and const-generic parameters in classes are declared using class A(Generic[T]).

The ellipsis change is necessary since currently it causes a compilation error, and functions marked as extern are eagerly evaluated.

Const generics are declared using e.g. `T = ConstGeneric("T", int32)`, and const-generic parameters in classes are declared using `class A(Generic[T])`. The ellipsis change is necessary since currently it causes a compilation error, and functions marked as `extern` are eagerly evaluated.
derppening self-assigned this 2023-12-05 16:41:01 +08:00
derppening requested review from sb10q 2023-12-05 16:41:11 +08:00
derppening force-pushed feat/const-generics from 58d457c1f3 to 5266e9b48e 2023-12-05 17:07:39 +08:00 Compare
Author
Collaborator

v2: Add missing documentation

v2: Add missing documentation
derppening changed title from Implement const-generics to WIP: Implement const-generics 2023-12-06 11:37:50 +08:00
derppening force-pushed feat/const-generics from 5266e9b48e to 4fb634aaa7 2023-12-06 15:27:45 +08:00 Compare
derppening changed title from WIP: Implement const-generics to Implement const-generics 2023-12-06 15:27:55 +08:00
Author
Collaborator

v3: Re-introduced ConstGeneric class, implemented codegen for ...

v3: Re-introduced `ConstGeneric` class, implemented codegen for `...`
derppening force-pushed feat/const-generics from 4fb634aaa7 to 181756fb3c 2023-12-06 15:33:01 +08:00 Compare
Author
Collaborator

v4: Remove all references to Const

v4: Remove all references to `Const`
derppening force-pushed feat/const-generics from 181756fb3c to 921aec411b 2023-12-06 18:33:29 +08:00 Compare
Author
Collaborator

v5: Implement const generics for nac3artiq, minor cleanup

ARTIQ implementation is tested and working.

v5: Implement const generics for nac3artiq, minor cleanup ARTIQ implementation is tested and working.
derppening changed title from Implement const-generics to WIP: Implement const-generics 2023-12-06 21:00:29 +08:00
Author
Collaborator

Marked as WIP again due to some missing handling for ellipsis, specifically cases where ... are used in sub-expressions as placeholders, e.g. ... + 1.

Marked as WIP again due to some missing handling for ellipsis, specifically cases where `...` are used in sub-expressions as placeholders, e.g. `... + 1`.
derppening force-pushed feat/const-generics from 921aec411b to e7add9ea0d 2023-12-07 14:41:02 +08:00 Compare
Author
Collaborator

v6: Completed codegen refactoring for ellipsis expression - Ellipsis are now treated as the equivalent of todo!() of Rust

v6: Completed codegen refactoring for ellipsis expression - Ellipsis are now treated as the equivalent of `todo!()` of Rust
derppening force-pushed feat/const-generics from e7add9ea0d to 649874868a 2023-12-08 13:03:26 +08:00 Compare
Author
Collaborator

v7: Fixed some cases where codegen fails for ellipsis due to extraneous/missing terminators, removed all references to NonTypeVar

v7: Fixed some cases where codegen fails for ellipsis due to extraneous/missing terminators, removed all references to `NonTypeVar`
derppening changed title from WIP: Implement const-generics to Implement const-generics 2023-12-08 13:05:35 +08:00
sb10q reviewed 2023-12-08 13:08:35 +08:00
@ -3,3 +3,3 @@
from types import SimpleNamespace
from numpy import int32, int64
from typing import Generic, TypeVar
from typing import Any, Generic, TypeVar
Owner

Where is this used?

Where is this used?
Author
Collaborator

Was a leftover change from before, removed.

Was a leftover change from before, removed.
derppening marked this conversation as resolved
sb10q reviewed 2023-12-08 13:10:09 +08:00
@ -879,1 +880,4 @@
typevar: get_attr_id(typing_mod, "TypeVar"),
const_generic_dummy: id_fn
.call1((
builtins_mod.getattr("globals")
Owner

Won't this break if _ConstGenericDummy is not in scope of the current module?
Also it's not really a "dummy" if it plays such a role.

Won't this break if ``_ConstGenericDummy`` is not in scope of the current module? Also it's not really a "dummy" if it plays such a role.
Author
Collaborator

The intention is that _ConstGenericDummy is from the ARTIQ library and used as a marker that the TypeVar is to be treated as a const generic variable.

The intention is that `_ConstGenericDummy` is from the ARTIQ library and used as a marker that the `TypeVar` is to be treated as a const generic variable.
Owner

I know. The two problems above remain.

I know. The two problems above remain.
Owner

If I understand correctly: After we implement module support, you could just import min_artiq and then this code will break, since it becomes min_artiq._ConstGenericMarker and not _ConstGenericMarker.

If I understand correctly: After we implement module support, you could just ``import min_artiq`` and then this code will break, since it becomes ``min_artiq._ConstGenericMarker`` and not ``_ConstGenericMarker``.
Author
Collaborator

When we implement module support, we will probably have to refactor this entire logic anyways, because none, Option and virtual are all obtained this way. I assume we will do something like this (assuming that the min_artiq module is imported as min_artiq_mod:

id_fn
    .call1((min_artiq_mod.getattr("_ConstGenericMarker").unwrap(),)
    .and_then(|v| v.extract())
    .unwrap()

BTW, this works in the interpreter:

>>> import min_artiq
>>> min_artiq.ConstGeneric("T", int)
~T
When we implement module support, we will probably have to refactor this entire logic anyways, because `none`, `Option` and `virtual` are all obtained this way. I assume we will do something like this (assuming that the `min_artiq` module is imported as `min_artiq_mod`: ```rs id_fn .call1((min_artiq_mod.getattr("_ConstGenericMarker").unwrap(),) .and_then(|v| v.extract()) .unwrap() ``` BTW, this works in the interpreter: ```py >>> import min_artiq >>> min_artiq.ConstGeneric("T", int) ~T ```
sb10q reviewed 2023-12-08 13:11:18 +08:00
@ -27,3 +27,3 @@
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: line {} column {}", self.file.0, self.row, self.column)
write!(f, "{}:{}:{}", self.file.0, self.row, self.column)
Owner

Should be a different PR.

Should be a different PR.
Author
Collaborator

Split this and the builtin-list changes into #361 and #362.

Split this and the builtin-list changes into #361 and #362.
derppening force-pushed feat/const-generics from 649874868a to 6e26c3abb7 2023-12-08 15:59:58 +08:00 Compare
derppening force-pushed feat/const-generics from 6e26c3abb7 to 390b7cc5f4 2023-12-08 16:00:45 +08:00 Compare
Author
Collaborator

v8, v9: Split unrelated changes to other MRs, renamed _ConstGenericDummy to _ConstGenericMarker.

v8, v9: Split unrelated changes to other MRs, renamed `_ConstGenericDummy` to `_ConstGenericMarker`.
derppening force-pushed feat/const-generics from 390b7cc5f4 to 20607934a6 2023-12-08 18:00:02 +08:00 Compare
Author
Collaborator

v10: Fixed missing _ConstGenericMarker changes

v10: Fixed missing `_ConstGenericMarker` changes
derppening force-pushed feat/const-generics from 20607934a6 to 983f080ea7 2023-12-08 18:02:24 +08:00 Compare
Author
Collaborator

v11: Rebased onto master, fixed an incorrect usage of unreachable!

v11: Rebased onto `master`, fixed an incorrect usage of `unreachable!`
sb10q merged commit 983f080ea7 into master 2023-12-08 18:07:32 +08:00
sb10q deleted branch feat/const-generics 2023-12-08 18:07:34 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#360
No description provided.