Skip to content
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

[Lang] Support annotated assignment #2610

Open
xumingkuan opened this issue Jul 29, 2021 · 3 comments
Open

[Lang] Support annotated assignment #2610

xumingkuan opened this issue Jul 29, 2021 · 3 comments
Assignees
Labels
feature request Suggest an idea on this project

Comments

@xumingkuan
Copy link
Contributor

xumingkuan commented Jul 29, 2021

Concisely describe the proposed feature
We would like to support annotated assignment in the future, for example,

a: ti.i32 = 1

Since Taichi is statically typed, code snippet like this (which compiles in Python) would not compile in Taichi:

a: int = 1
a: float = 2.0

Describe the solution you'd like (if any)
Add a method build_AnnAssign in stmt_builder.py. The content of the method will be similar to build_Assign. It should also set the type when creating the variable, and check if the type is the same when an already-created variable is annotated assigned again.
The statement a: ti.i32 = 1 should be translated into the following CHI IR:

<i32> $0 = alloca
<i32> $1 = const [1]
<i32> $2 : local store [$0 <- $1]

Additional comments
The statement corresponds to ast.AnnAssign in the Python AST. See https://docs.python.org/3/library/ast.html#ast.AnnAssign for more information.

@xumingkuan xumingkuan added the feature request Suggest an idea on this project label Jul 29, 2021
@woooodyye
Copy link
Contributor

I would like to work on this issue. A simple approach would be cast the value based on the type annotation we provide. In case of already-created variable, we compare the type annotation with the type established in the ctx. If the types are different, we would raise a syntaxError

@strongoier
Copy link
Contributor

strongoier commented Dec 3, 2021

@woooodyye Welcome to the Taichi community! Your proposal looks great. This issue is assigned to you :-)

woooodyye added a commit to woooodyye/taichi that referenced this issue Dec 3, 2021
strongoier added a commit that referenced this issue Dec 7, 2021
* #2610 add typed build assign

* add typed support

* code formated for type assigned

* Auto Format

* wrong tester

* support typed assign with value decl

* Update tests/python/test_assign.py

Co-authored-by: Yi Xu <[email protected]>

* Update python/taichi/lang/ast/ast_transformer.py

Co-authored-by: Yi Xu <[email protected]>

* fixed naming and formatting error

Co-authored-by: Taichi Gardener <[email protected]>
Co-authored-by: Yi Xu <[email protected]>
@YouJiacheng
Copy link

Can we make taichi ignore annotation in annotated assignment when annotation is not a primitive type, or allow ti.template()/typing.Any to serve as a dummy annotation(without any compile-time or runtime effect, only edit-time effect for developer experience)?
With this we can improve developer experience leveraging the existing intelliSense(auto complete, jump to source, semantic highlight) infrastructure for Python.
Use case:

from typing import Generic, TypeVar

import taichi as ti
from taichi.lang.struct import StructType

T = TypeVar('T')

vec3 = ti.types.vector(3, ti.float32)
vec2 = ti.types.vector(2, ti.float32)

# make IntelliSense happy
class TypedStructType(Generic[T], StructType): ...

def ti_dataclass(cls: type[T]) -> TypedStructType[T]:
    return ti.dataclass(cls)

def typing(_: TypedStructType[T], obj) -> type[T]:
    return obj

@ti_dataclass
class Triangle:
    V0: vec3
    V1: vec3
    V2: vec3
TriangleT = typing(Triangle, ti.template())

@ti_dataclass
class TextureTriangle:
    V0: vec2
    V1: vec2
    V2: vec2
TextureTriangleT = typing(TextureTriangle, ti.template())

@ti_dataclass
class Primitive:
    position_triangle: Triangle
    texture_triangle: TextureTriangle
    material_index: ti.i32
PrimitiveT = typing(Primitive, ti.template())

@ti.func
def foo(p: PrimitiveT): # taichi allows ti.template() as argument annotation
    pos = p.position_triangle
    pos.V0
    pos2: TriangleT = p.position_triangle # taichi doesn't allow ti.template() as assignment annotation
    pos2.V0 # intelliSense enabled

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
None yet
Development

No branches or pull requests

4 participants