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
Copy file name to clipboardExpand all lines: README.md
+8-7Lines changed: 8 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -26,12 +26,12 @@ import luisa_lang as lc
26
26
27
27
28
28
### Functions
29
-
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.constexpr()`. (See [Metaprogramming](#metaprogramming) for more details)
29
+
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)
30
30
31
31
```python
32
32
@lc.func
33
33
defadd(a: lc.float, b: lc.float) -> lc.float:
34
-
with lc.constexpr():
34
+
with lc.comptime():
35
35
print('compiliing add function')
36
36
return a + b
37
37
@@ -69,8 +69,8 @@ luisa_lang provides a metaprogramming feature similar to C++ that allows users t
69
69
# Compile time reflection
70
70
@lc.func
71
71
defget_x_or_zero(x: Any):
72
-
t = lc.constexpr(type(x))
73
-
if lc.constexpr(hasattr(t, 'x')):
72
+
t = lc.comptime(type(x))
73
+
if lc.comptime(hasattr(t, 'x')):
74
74
return x.x
75
75
else:
76
76
return0.0
@@ -86,7 +86,7 @@ def apply_func(f: F, x: T):
86
86
# Generate code at compile time
87
87
@lc.func
88
88
defcall_n_times(f: F):
89
-
with lc.constexpr():
89
+
with lc.comptime():
90
90
n =input('how many times to call?')
91
91
for i inrange(n):
92
92
# lc.embed_code(expr) will generate add expr to the DSL code
@@ -95,12 +95,13 @@ def call_n_times(f: F):
95
95
lc.embed_code('apply_func(f, i)')
96
96
97
97
# Hint a parameter is constexpr
98
-
@lc.func(n=lc.constexpr) # without this, n will be treated as a runtime variable and result in an error
98
+
@lc.func(n=lc.comptime) # without this, n will be treated as a runtime variable and result in an error
99
99
defpow(x: lc.float, n: int) -> lc.float:
100
100
p =1.0
101
-
with lc.constexpr():
101
+
with lc.comptime():
102
102
for _ inrange(n):
103
103
lc.embed_code('p *= x')
104
+
return p
104
105
```
105
106
### Limitation & Caveats
106
107
- Lambda and nested function do not support updating nonlocal variables.
0 commit comments