You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -20,11 +23,16 @@ A new Python DSL frontend for LuisaCompute. Will be integrated into LuisaCompute
20
23
import luisa_lang as lc
21
24
```
22
25
## Basic Syntax
26
+
### Difference from Python
27
+
There are some notable differences between luisa_lang and Python:
28
+
- Variables have value semantics by default. Use `inout` to indicate that an argument that is passed by reference.
29
+
- Generic functions and structs are implemented via monomorphization (a.k.a instantiation) at compile time rather than via type erasure.
30
+
- Overloading subscript operator and attribute access is different from Python. Only `__getitem__` and `__getattr__` are needed, which returns a local reference.
31
+
23
32
### Types
24
33
```python
25
34
```
26
35
27
-
28
36
### Functions
29
37
Functions are defined using the `@lc.func` decorator. The function body can contain any valid LuisaCompute code. You can also include normal Python code that will be executed at DSL comile time using `lc.comptime()`. (See [Metaprogramming](#metaprogramming) for more details)
LuisaCompute uses value semantics, which means that all types are passed by value. You can use `inout` to indicate that a variable can be modified in place.
48
+
49
+
### Value & Reference Semantics
50
+
Variables have value semantics by default. This means that when you assign a variable to another, a copy is made.
51
+
```python
52
+
a = lc.float3(1.0, 2.0, 3.0)
53
+
b = a
54
+
a.x =2.0
55
+
lc.print(f'{a.x}{b.x}') # prints 2.0 1.0
56
+
```
57
+
58
+
You can use `inout` to indicate that a variable is passed as a *local reference*. Assigning to an `inout` variable will update the original variable.
41
59
```python
42
60
@luisa.func(a=inout, b=inout)
43
61
defswap(a: int, b: int):
44
62
a, b = b, a
63
+
64
+
a = lc.float3(1.0, 2.0, 3.0)
65
+
b = lc.float3(4.0, 5.0, 6.0)
66
+
swap(a.x, b.x)
67
+
lc.print(f'{a.x}{b.x}') # prints 4.0 1.0
45
68
```
46
69
70
+
When overloading subscript operator or attribute access, you actually return a local reference to the object.
71
+
72
+
#### Local References
73
+
Local references are like pointers in C++. However, they cannot escape the expression boundary. This means that you cannot store a local reference in a variable and use it later. While you can return a local reference from a function, it must be returned from a uniform path. That is you cannot return different local references based on a condition.
74
+
75
+
76
+
```python
77
+
@lc.struct
78
+
classInfiniteArray:
79
+
def__getitem__(self, index: int) -> int:
80
+
returnself.data[index] # returns a local reference
81
+
82
+
# this method will be ignored by the compiler. but you can still put it here for linting
0 commit comments