gstaichi 0.1.18.dev1__cp310-cp310-macosx_15_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. gstaichi-0.1.18.dev1.data/data/include/GLFW/glfw3.h +6389 -0
  2. gstaichi-0.1.18.dev1.data/data/include/GLFW/glfw3native.h +594 -0
  3. gstaichi-0.1.18.dev1.data/data/include/spirv-tools/instrument.hpp +268 -0
  4. gstaichi-0.1.18.dev1.data/data/include/spirv-tools/libspirv.h +907 -0
  5. gstaichi-0.1.18.dev1.data/data/include/spirv-tools/libspirv.hpp +375 -0
  6. gstaichi-0.1.18.dev1.data/data/include/spirv-tools/linker.hpp +97 -0
  7. gstaichi-0.1.18.dev1.data/data/include/spirv-tools/optimizer.hpp +970 -0
  8. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/GLSL.std.450.h +114 -0
  9. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv.h +2568 -0
  10. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv.hpp +2579 -0
  11. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cfg.hpp +168 -0
  12. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_common.hpp +1920 -0
  13. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cpp.hpp +93 -0
  14. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross.hpp +1171 -0
  15. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_c.h +1074 -0
  16. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_containers.hpp +754 -0
  17. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_error_handling.hpp +94 -0
  18. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_parsed_ir.hpp +256 -0
  19. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_util.hpp +37 -0
  20. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_glsl.hpp +1001 -0
  21. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_hlsl.hpp +406 -0
  22. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_msl.hpp +1273 -0
  23. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_parser.hpp +103 -0
  24. gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_reflect.hpp +91 -0
  25. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsConfig.cmake +5 -0
  26. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget-release.cmake +29 -0
  27. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget.cmake +114 -0
  28. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffConfig.cmake +5 -0
  29. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets-release.cmake +19 -0
  30. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets.cmake +123 -0
  31. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkConfig.cmake +5 -0
  32. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets-release.cmake +19 -0
  33. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets.cmake +123 -0
  34. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintConfig.cmake +5 -0
  35. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets-release.cmake +19 -0
  36. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets.cmake +123 -0
  37. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optConfig.cmake +5 -0
  38. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets-release.cmake +19 -0
  39. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets.cmake +123 -0
  40. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceConfig.cmake +5 -0
  41. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget-release.cmake +19 -0
  42. gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget.cmake +123 -0
  43. gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Config.cmake +3 -0
  44. gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3ConfigVersion.cmake +65 -0
  45. gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Targets-release.cmake +19 -0
  46. gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Targets.cmake +107 -0
  47. gstaichi-0.1.18.dev1.data/data/lib/libSPIRV-Tools-shared.dylib +0 -0
  48. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig-release.cmake +19 -0
  49. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig.cmake +123 -0
  50. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig-release.cmake +19 -0
  51. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig.cmake +106 -0
  52. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig-release.cmake +19 -0
  53. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig.cmake +123 -0
  54. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig-release.cmake +19 -0
  55. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig.cmake +123 -0
  56. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig-release.cmake +19 -0
  57. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig.cmake +123 -0
  58. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig-release.cmake +19 -0
  59. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig.cmake +123 -0
  60. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig-release.cmake +19 -0
  61. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig.cmake +106 -0
  62. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig-release.cmake +19 -0
  63. gstaichi-0.1.18.dev1.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig.cmake +123 -0
  64. gstaichi-0.1.18.dev1.dist-info/METADATA +108 -0
  65. gstaichi-0.1.18.dev1.dist-info/RECORD +219 -0
  66. gstaichi-0.1.18.dev1.dist-info/WHEEL +5 -0
  67. gstaichi-0.1.18.dev1.dist-info/entry_points.txt +2 -0
  68. gstaichi-0.1.18.dev1.dist-info/licenses/LICENSE +201 -0
  69. gstaichi-0.1.18.dev1.dist-info/top_level.txt +1 -0
  70. taichi/__init__.py +44 -0
  71. taichi/__main__.py +5 -0
  72. taichi/_funcs.py +706 -0
  73. taichi/_kernels.py +420 -0
  74. taichi/_lib/__init__.py +3 -0
  75. taichi/_lib/c_api/include/taichi/cpp/taichi.hpp +1401 -0
  76. taichi/_lib/c_api/include/taichi/taichi.h +29 -0
  77. taichi/_lib/c_api/include/taichi/taichi_core.h +1111 -0
  78. taichi/_lib/c_api/include/taichi/taichi_cpu.h +29 -0
  79. taichi/_lib/c_api/include/taichi/taichi_metal.h +72 -0
  80. taichi/_lib/c_api/include/taichi/taichi_platform.h +55 -0
  81. taichi/_lib/c_api/include/taichi/taichi_unity.h +64 -0
  82. taichi/_lib/c_api/include/taichi/taichi_vulkan.h +151 -0
  83. taichi/_lib/c_api/lib/libtaichi_c_api.dylib +0 -0
  84. taichi/_lib/c_api/runtime/libMoltenVK.dylib +0 -0
  85. taichi/_lib/c_api/runtime/runtime_arm64.bc +0 -0
  86. taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiConfig.cmake +29 -0
  87. taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiConfigVersion.cmake +65 -0
  88. taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiTargets.cmake +121 -0
  89. taichi/_lib/core/__init__.py +0 -0
  90. taichi/_lib/core/py.typed +0 -0
  91. taichi/_lib/core/taichi_python.cpython-310-darwin.so +0 -0
  92. taichi/_lib/core/taichi_python.pyi +3077 -0
  93. taichi/_lib/runtime/libMoltenVK.dylib +0 -0
  94. taichi/_lib/runtime/runtime_arm64.bc +0 -0
  95. taichi/_lib/utils.py +249 -0
  96. taichi/_logging.py +131 -0
  97. taichi/_main.py +552 -0
  98. taichi/_snode/__init__.py +5 -0
  99. taichi/_snode/fields_builder.py +189 -0
  100. taichi/_snode/snode_tree.py +34 -0
  101. taichi/_ti_module/__init__.py +3 -0
  102. taichi/_ti_module/cppgen.py +309 -0
  103. taichi/_ti_module/module.py +145 -0
  104. taichi/_version.py +1 -0
  105. taichi/_version_check.py +100 -0
  106. taichi/ad/__init__.py +3 -0
  107. taichi/ad/_ad.py +530 -0
  108. taichi/algorithms/__init__.py +3 -0
  109. taichi/algorithms/_algorithms.py +117 -0
  110. taichi/aot/__init__.py +12 -0
  111. taichi/aot/_export.py +28 -0
  112. taichi/aot/conventions/__init__.py +3 -0
  113. taichi/aot/conventions/gfxruntime140/__init__.py +38 -0
  114. taichi/aot/conventions/gfxruntime140/dr.py +244 -0
  115. taichi/aot/conventions/gfxruntime140/sr.py +613 -0
  116. taichi/aot/module.py +253 -0
  117. taichi/aot/utils.py +151 -0
  118. taichi/assets/.git +1 -0
  119. taichi/assets/Go-Regular.ttf +0 -0
  120. taichi/assets/static/imgs/ti_gallery.png +0 -0
  121. taichi/examples/minimal.py +28 -0
  122. taichi/experimental.py +16 -0
  123. taichi/graph/__init__.py +3 -0
  124. taichi/graph/_graph.py +292 -0
  125. taichi/lang/__init__.py +50 -0
  126. taichi/lang/_ndarray.py +348 -0
  127. taichi/lang/_ndrange.py +152 -0
  128. taichi/lang/_texture.py +172 -0
  129. taichi/lang/_wrap_inspect.py +189 -0
  130. taichi/lang/any_array.py +99 -0
  131. taichi/lang/argpack.py +411 -0
  132. taichi/lang/ast/__init__.py +5 -0
  133. taichi/lang/ast/ast_transformer.py +1806 -0
  134. taichi/lang/ast/ast_transformer_utils.py +328 -0
  135. taichi/lang/ast/checkers.py +106 -0
  136. taichi/lang/ast/symbol_resolver.py +57 -0
  137. taichi/lang/ast/transform.py +9 -0
  138. taichi/lang/common_ops.py +310 -0
  139. taichi/lang/exception.py +80 -0
  140. taichi/lang/expr.py +180 -0
  141. taichi/lang/field.py +464 -0
  142. taichi/lang/impl.py +1246 -0
  143. taichi/lang/kernel_arguments.py +157 -0
  144. taichi/lang/kernel_impl.py +1415 -0
  145. taichi/lang/matrix.py +1877 -0
  146. taichi/lang/matrix_ops.py +341 -0
  147. taichi/lang/matrix_ops_utils.py +190 -0
  148. taichi/lang/mesh.py +687 -0
  149. taichi/lang/misc.py +807 -0
  150. taichi/lang/ops.py +1489 -0
  151. taichi/lang/runtime_ops.py +13 -0
  152. taichi/lang/shell.py +35 -0
  153. taichi/lang/simt/__init__.py +5 -0
  154. taichi/lang/simt/block.py +94 -0
  155. taichi/lang/simt/grid.py +7 -0
  156. taichi/lang/simt/subgroup.py +191 -0
  157. taichi/lang/simt/warp.py +96 -0
  158. taichi/lang/snode.py +487 -0
  159. taichi/lang/source_builder.py +150 -0
  160. taichi/lang/struct.py +855 -0
  161. taichi/lang/util.py +381 -0
  162. taichi/linalg/__init__.py +8 -0
  163. taichi/linalg/matrixfree_cg.py +310 -0
  164. taichi/linalg/sparse_cg.py +59 -0
  165. taichi/linalg/sparse_matrix.py +303 -0
  166. taichi/linalg/sparse_solver.py +123 -0
  167. taichi/math/__init__.py +11 -0
  168. taichi/math/_complex.py +204 -0
  169. taichi/math/mathimpl.py +886 -0
  170. taichi/profiler/__init__.py +6 -0
  171. taichi/profiler/kernel_metrics.py +260 -0
  172. taichi/profiler/kernel_profiler.py +592 -0
  173. taichi/profiler/memory_profiler.py +15 -0
  174. taichi/profiler/scoped_profiler.py +36 -0
  175. taichi/shaders/Circles_vk.frag +29 -0
  176. taichi/shaders/Circles_vk.vert +45 -0
  177. taichi/shaders/Circles_vk_frag.spv +0 -0
  178. taichi/shaders/Circles_vk_vert.spv +0 -0
  179. taichi/shaders/Lines_vk.frag +9 -0
  180. taichi/shaders/Lines_vk.vert +11 -0
  181. taichi/shaders/Lines_vk_frag.spv +0 -0
  182. taichi/shaders/Lines_vk_vert.spv +0 -0
  183. taichi/shaders/Mesh_vk.frag +71 -0
  184. taichi/shaders/Mesh_vk.vert +68 -0
  185. taichi/shaders/Mesh_vk_frag.spv +0 -0
  186. taichi/shaders/Mesh_vk_vert.spv +0 -0
  187. taichi/shaders/Particles_vk.frag +95 -0
  188. taichi/shaders/Particles_vk.vert +73 -0
  189. taichi/shaders/Particles_vk_frag.spv +0 -0
  190. taichi/shaders/Particles_vk_vert.spv +0 -0
  191. taichi/shaders/SceneLines2quad_vk_comp.spv +0 -0
  192. taichi/shaders/SceneLines_vk.frag +9 -0
  193. taichi/shaders/SceneLines_vk.vert +12 -0
  194. taichi/shaders/SceneLines_vk_frag.spv +0 -0
  195. taichi/shaders/SceneLines_vk_vert.spv +0 -0
  196. taichi/shaders/SetImage_vk.frag +21 -0
  197. taichi/shaders/SetImage_vk.vert +15 -0
  198. taichi/shaders/SetImage_vk_frag.spv +0 -0
  199. taichi/shaders/SetImage_vk_vert.spv +0 -0
  200. taichi/shaders/Triangles_vk.frag +16 -0
  201. taichi/shaders/Triangles_vk.vert +29 -0
  202. taichi/shaders/Triangles_vk_frag.spv +0 -0
  203. taichi/shaders/Triangles_vk_vert.spv +0 -0
  204. taichi/shaders/lines2quad_vk_comp.spv +0 -0
  205. taichi/sparse/__init__.py +3 -0
  206. taichi/sparse/_sparse_grid.py +77 -0
  207. taichi/tools/__init__.py +12 -0
  208. taichi/tools/diagnose.py +124 -0
  209. taichi/tools/np2ply.py +364 -0
  210. taichi/tools/vtk.py +38 -0
  211. taichi/types/__init__.py +19 -0
  212. taichi/types/annotations.py +47 -0
  213. taichi/types/compound_types.py +90 -0
  214. taichi/types/enums.py +49 -0
  215. taichi/types/ndarray_type.py +147 -0
  216. taichi/types/primitive_types.py +203 -0
  217. taichi/types/quant.py +88 -0
  218. taichi/types/texture_type.py +85 -0
  219. taichi/types/utils.py +13 -0
@@ -0,0 +1,328 @@
1
+ # type: ignore
2
+
3
+ import ast
4
+ import builtins
5
+ import traceback
6
+ from enum import Enum
7
+ from sys import version_info
8
+ from textwrap import TextWrapper
9
+ from typing import TYPE_CHECKING, Any, List
10
+
11
+ from taichi._lib.core.taichi_python import ASTBuilder
12
+ from taichi.lang import impl
13
+ from taichi.lang.exception import (
14
+ TaichiCompilationError,
15
+ TaichiNameError,
16
+ TaichiSyntaxError,
17
+ handle_exception_from_cpp,
18
+ )
19
+
20
+ if TYPE_CHECKING:
21
+ from taichi.lang.kernel_impl import (
22
+ Func,
23
+ Kernel,
24
+ )
25
+
26
+
27
+ class Builder:
28
+ def __call__(self, ctx, node):
29
+ method = getattr(self, "build_" + node.__class__.__name__, None)
30
+ try:
31
+ if method is None:
32
+ error_msg = f'Unsupported node "{node.__class__.__name__}"'
33
+ raise TaichiSyntaxError(error_msg)
34
+ info = ctx.get_pos_info(node) if isinstance(node, (ast.stmt, ast.expr)) else ""
35
+ with impl.get_runtime().src_info_guard(info):
36
+ return method(ctx, node)
37
+ except Exception as e:
38
+ if impl.get_runtime().print_full_traceback:
39
+ raise e
40
+ if ctx.raised or not isinstance(node, (ast.stmt, ast.expr)):
41
+ raise e.with_traceback(None)
42
+ ctx.raised = True
43
+ e = handle_exception_from_cpp(e)
44
+ if not isinstance(e, TaichiCompilationError):
45
+ msg = ctx.get_pos_info(node) + traceback.format_exc()
46
+ raise TaichiCompilationError(msg) from None
47
+ msg = ctx.get_pos_info(node) + str(e)
48
+ raise type(e)(msg) from None
49
+
50
+
51
+ class VariableScopeGuard:
52
+ def __init__(self, scopes):
53
+ self.scopes = scopes
54
+
55
+ def __enter__(self):
56
+ self.scopes.append({})
57
+
58
+ def __exit__(self, exc_type, exc_val, exc_tb):
59
+ self.scopes.pop()
60
+
61
+
62
+ class StaticScopeStatus:
63
+ def __init__(self):
64
+ self.is_in_static_scope = False
65
+
66
+
67
+ class StaticScopeGuard:
68
+ def __init__(self, status):
69
+ self.status = status
70
+
71
+ def __enter__(self):
72
+ self.prev = self.status.is_in_static_scope
73
+ self.status.is_in_static_scope = True
74
+
75
+ def __exit__(self, exc_type, exc_val, exc_tb):
76
+ self.status.is_in_static_scope = self.prev
77
+
78
+
79
+ class NonStaticControlFlowStatus:
80
+ def __init__(self):
81
+ self.is_in_non_static_control_flow = False
82
+
83
+
84
+ class NonStaticControlFlowGuard:
85
+ def __init__(self, status: NonStaticControlFlowStatus):
86
+ self.status = status
87
+
88
+ def __enter__(self):
89
+ self.prev = self.status.is_in_non_static_control_flow
90
+ self.status.is_in_non_static_control_flow = True
91
+
92
+ def __exit__(self, exc_type, exc_val, exc_tb):
93
+ self.status.is_in_non_static_control_flow = self.prev
94
+
95
+
96
+ class LoopStatus(Enum):
97
+ Normal = 0
98
+ Break = 1
99
+ Continue = 2
100
+
101
+
102
+ class LoopScopeAttribute:
103
+ def __init__(self, is_static: bool):
104
+ self.is_static = is_static
105
+ self.status: LoopStatus = LoopStatus.Normal
106
+ self.nearest_non_static_if: ast.If | None = None
107
+
108
+
109
+ class LoopScopeGuard:
110
+ def __init__(self, scopes, non_static_guard=None):
111
+ self.scopes = scopes
112
+ self.non_static_guard = non_static_guard
113
+
114
+ def __enter__(self):
115
+ self.scopes.append(LoopScopeAttribute(self.non_static_guard is None))
116
+ if self.non_static_guard:
117
+ self.non_static_guard.__enter__()
118
+
119
+ def __exit__(self, exc_type, exc_val, exc_tb):
120
+ self.scopes.pop()
121
+ if self.non_static_guard:
122
+ self.non_static_guard.__exit__(exc_type, exc_val, exc_tb)
123
+
124
+
125
+ class NonStaticIfGuard:
126
+ def __init__(
127
+ self,
128
+ if_node: ast.If,
129
+ loop_attribute: LoopScopeAttribute,
130
+ non_static_status: NonStaticControlFlowStatus,
131
+ ):
132
+ self.loop_attribute = loop_attribute
133
+ self.if_node = if_node
134
+ self.non_static_guard = NonStaticControlFlowGuard(non_static_status)
135
+
136
+ def __enter__(self):
137
+ if self.loop_attribute:
138
+ self.old_non_static_if = self.loop_attribute.nearest_non_static_if
139
+ self.loop_attribute.nearest_non_static_if = self.if_node
140
+ self.non_static_guard.__enter__()
141
+
142
+ def __exit__(self, exc_type, exc_val, exc_tb):
143
+ if self.loop_attribute:
144
+ self.loop_attribute.nearest_non_static_if = self.old_non_static_if
145
+ self.non_static_guard.__exit__(exc_type, exc_val, exc_tb)
146
+
147
+
148
+ class ReturnStatus(Enum):
149
+ NoReturn = 0
150
+ ReturnedVoid = 1
151
+ ReturnedValue = 2
152
+
153
+
154
+ class ASTTransformerContext:
155
+ def __init__(
156
+ self,
157
+ excluded_parameters=(),
158
+ is_kernel: bool = True,
159
+ func: "Func | Kernel | None" = None,
160
+ arg_features=None,
161
+ global_vars: dict[str, Any] | None = None,
162
+ argument_data=None,
163
+ file: str | None = None,
164
+ src: list[str] | None = None,
165
+ start_lineno: int | None = None,
166
+ ast_builder: ASTBuilder | None = None,
167
+ is_real_function: bool = False,
168
+ ):
169
+ self.func = func
170
+ self.local_scopes = []
171
+ self.loop_scopes: List[LoopScopeAttribute] = []
172
+ self.excluded_parameters = excluded_parameters
173
+ self.is_kernel = is_kernel
174
+ self.arg_features = arg_features
175
+ self.returns = None
176
+ self.global_vars = global_vars
177
+ self.argument_data = argument_data
178
+ self.return_data = None
179
+ self.file = file
180
+ self.src = src
181
+ self.indent = 0
182
+ for c in self.src[0]:
183
+ if c == " ":
184
+ self.indent += 1
185
+ else:
186
+ break
187
+ self.lineno_offset = start_lineno - 1
188
+ self.raised = False
189
+ self.non_static_control_flow_status = NonStaticControlFlowStatus()
190
+ self.static_scope_status = StaticScopeStatus()
191
+ self.returned = ReturnStatus.NoReturn
192
+ self.ast_builder = ast_builder
193
+ self.visited_funcdef = False
194
+ self.is_real_function = is_real_function
195
+ self.kernel_args = []
196
+
197
+ # e.g.: FunctionDef, Module, Global
198
+ def variable_scope_guard(self):
199
+ return VariableScopeGuard(self.local_scopes)
200
+
201
+ # e.g.: For, While
202
+ def loop_scope_guard(self, is_static=False):
203
+ if is_static:
204
+ return LoopScopeGuard(self.loop_scopes)
205
+ return LoopScopeGuard(self.loop_scopes, self.non_static_control_flow_guard())
206
+
207
+ def non_static_if_guard(self, if_node: ast.If):
208
+ return NonStaticIfGuard(
209
+ if_node,
210
+ self.current_loop_scope() if self.loop_scopes else None,
211
+ self.non_static_control_flow_status,
212
+ )
213
+
214
+ def non_static_control_flow_guard(self):
215
+ return NonStaticControlFlowGuard(self.non_static_control_flow_status)
216
+
217
+ def static_scope_guard(self):
218
+ return StaticScopeGuard(self.static_scope_status)
219
+
220
+ def current_scope(self):
221
+ return self.local_scopes[-1]
222
+
223
+ def current_loop_scope(self):
224
+ return self.loop_scopes[-1]
225
+
226
+ def loop_status(self):
227
+ if self.loop_scopes:
228
+ return self.loop_scopes[-1].status
229
+ return LoopStatus.Normal
230
+
231
+ def set_loop_status(self, status):
232
+ self.loop_scopes[-1].status = status
233
+
234
+ def is_in_static_for(self):
235
+ if self.loop_scopes:
236
+ return self.loop_scopes[-1].is_static
237
+ return False
238
+
239
+ def is_in_non_static_control_flow(self):
240
+ return self.non_static_control_flow_status.is_in_non_static_control_flow
241
+
242
+ def is_in_static_scope(self):
243
+ return self.static_scope_status.is_in_static_scope
244
+
245
+ def is_var_declared(self, name):
246
+ for s in self.local_scopes:
247
+ if name in s:
248
+ return True
249
+ return False
250
+
251
+ def create_variable(self, name, var):
252
+ if name in self.current_scope():
253
+ raise TaichiSyntaxError("Recreating variables is not allowed")
254
+ self.current_scope()[name] = var
255
+
256
+ def check_loop_var(self, loop_var):
257
+ if self.is_var_declared(loop_var):
258
+ raise TaichiSyntaxError(
259
+ f"Variable '{loop_var}' is already declared in the outer scope and cannot be used as loop variable"
260
+ )
261
+
262
+ def get_var_by_name(self, name: str):
263
+ for s in reversed(self.local_scopes):
264
+ if name in s:
265
+ return s[name]
266
+ if name in self.global_vars:
267
+ var = self.global_vars[name]
268
+ from taichi.lang.matrix import ( # pylint: disable-msg=C0415
269
+ Matrix,
270
+ make_matrix,
271
+ )
272
+
273
+ if isinstance(var, Matrix):
274
+ return make_matrix(var.to_list())
275
+ return var
276
+ try:
277
+ return getattr(builtins, name)
278
+ except AttributeError:
279
+ raise TaichiNameError(f'Name "{name}" is not defined')
280
+
281
+ def get_pos_info(self, node) -> str:
282
+ msg = f'File "{self.file}", line {node.lineno + self.lineno_offset}, in {self.func.func.__name__}:\n'
283
+ if version_info < (3, 8):
284
+ msg += self.src[node.lineno - 1] + "\n"
285
+ return msg
286
+ col_offset = self.indent + node.col_offset
287
+ end_col_offset = self.indent + node.end_col_offset
288
+
289
+ wrapper = TextWrapper(width=80)
290
+
291
+ def gen_line(code, hint):
292
+ hint += " " * (len(code) - len(hint))
293
+ code = wrapper.wrap(code)
294
+ hint = wrapper.wrap(hint)
295
+ if not len(code):
296
+ return "\n\n"
297
+ return "".join([c + "\n" + h + "\n" for c, h in zip(code, hint)])
298
+
299
+ if node.lineno == node.end_lineno:
300
+ hint = " " * col_offset + "^" * (end_col_offset - col_offset)
301
+ msg += gen_line(self.src[node.lineno - 1], hint)
302
+ else:
303
+ node_type = node.__class__.__name__
304
+
305
+ if node_type in ["For", "While", "FunctionDef", "If"]:
306
+ end_lineno = max(node.body[0].lineno - 1, node.lineno)
307
+ else:
308
+ end_lineno = node.end_lineno
309
+
310
+ for i in range(node.lineno - 1, end_lineno):
311
+ last = len(self.src[i])
312
+ while last > 0 and (self.src[i][last - 1].isspace() or not self.src[i][last - 1].isprintable()):
313
+ last -= 1
314
+ first = 0
315
+ while first < len(self.src[i]) and (
316
+ self.src[i][first].isspace() or not self.src[i][first].isprintable()
317
+ ):
318
+ first += 1
319
+ if i == node.lineno - 1:
320
+ hint = " " * col_offset + "^" * (last - col_offset)
321
+ elif i == node.end_lineno - 1:
322
+ hint = " " * first + "^" * (end_col_offset - first)
323
+ elif first < last:
324
+ hint = " " * first + "^" * (last - first)
325
+ else:
326
+ hint = ""
327
+ msg += gen_line(self.src[i], hint)
328
+ return msg
@@ -0,0 +1,106 @@
1
+ # type: ignore
2
+
3
+ import ast
4
+
5
+ from taichi.lang._wrap_inspect import getsourcefile, getsourcelines
6
+ from taichi.lang.exception import TaichiSyntaxError
7
+
8
+
9
+ class KernelSimplicityASTChecker(ast.NodeVisitor):
10
+ class ScopeGuard:
11
+ def __init__(self, checker):
12
+ self.c = checker
13
+ self._allows_for_loop = True
14
+ self._allows_more_stmt = True
15
+
16
+ @property
17
+ def allows_for_loop(self):
18
+ return self._allows_for_loop
19
+
20
+ @property
21
+ def allows_more_stmt(self):
22
+ return self._allows_more_stmt
23
+
24
+ def mark_no_more_for_loop(self):
25
+ self._allows_for_loop = False
26
+
27
+ def mark_no_more_stmt(self):
28
+ self._allows_for_loop = False
29
+ self._allows_more_stmt = False
30
+
31
+ def __enter__(self):
32
+ self.c._scope_guards.append(self)
33
+
34
+ def __exit__(self, exc_type, exc_val, exc_tb):
35
+ self.c._scope_guards.pop()
36
+
37
+ def __init__(self, func):
38
+ super().__init__()
39
+ self._func_file = getsourcefile(func)
40
+ self._func_lineno = getsourcelines(func)[1]
41
+ self._func_name = func.__name__
42
+ self._scope_guards = []
43
+
44
+ def new_scope(self):
45
+ return KernelSimplicityASTChecker.ScopeGuard(self)
46
+
47
+ @property
48
+ def current_scope(self):
49
+ return self._scope_guards[-1]
50
+
51
+ @property
52
+ def top_level(self):
53
+ return len(self._scope_guards) == 0
54
+
55
+ def get_error_location(self, node):
56
+ # -1 because ast's lineno is 1-based.
57
+ lineno = self._func_lineno + node.lineno - 1
58
+ return f"file={self._func_file} kernel={self._func_name} line={lineno}"
59
+
60
+ @staticmethod
61
+ def should_check(node):
62
+ if not isinstance(node, ast.stmt):
63
+ return False
64
+ # TODO(#536): Frontend pass should help make sure |func| is a valid AST for
65
+ # Taichi.
66
+ ignored = [ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef]
67
+ return not any(map(lambda t: isinstance(node, t), ignored))
68
+
69
+ def generic_visit(self, node):
70
+ if not self.should_check(node):
71
+ super().generic_visit(node)
72
+ return
73
+
74
+ if not (self.top_level or self.current_scope.allows_more_stmt):
75
+ raise TaichiSyntaxError(f"No more statements allowed, at {self.get_error_location(node)}")
76
+ old_top_level = self.top_level
77
+ if old_top_level:
78
+ self._scope_guards.append(self.new_scope())
79
+ # Marking here before the visit has the effect of disallow for-loops in
80
+ # nested blocks. E.g. if |node| is a IfStmt, then the checker would disallow
81
+ # for-loops inside it.
82
+ self.current_scope.mark_no_more_for_loop()
83
+ super().generic_visit(node)
84
+ if old_top_level:
85
+ self._scope_guards.pop()
86
+
87
+ @staticmethod
88
+ def visit_for(node):
89
+ # TODO: since autodiff is enhanced, AST checker rules should be relaxed. This part should be updated.
90
+ # original code is #def visit_For(self, node) without #@staticmethod before fix pylint R0201
91
+ return
92
+ # is_static = (isinstance(node.iter, ast.Call)
93
+ # and isinstance(node.iter.func, ast.Attribute)
94
+ # and isinstance(node.iter.func.value, ast.Name)
95
+ # and node.iter.func.value.id == 'ti'
96
+ # and node.iter.func.attr == 'static')
97
+ # if not (self.top_level or self.current_scope.allows_for_loop
98
+ # or is_static):
99
+ # raise TaichiSyntaxError(
100
+ # f'No more for loops allowed, at {self.get_error_location(node)}'
101
+ # )
102
+ # with self.new_scope():
103
+ # super().generic_visit(node)
104
+ #
105
+ # if not (self.top_level or is_static):
106
+ # self.current_scope.mark_no_more_stmt()
@@ -0,0 +1,57 @@
1
+ # type: ignore
2
+
3
+ """Provides helpers to resolve AST nodes."""
4
+
5
+ import ast
6
+
7
+
8
+ class ASTResolver:
9
+ """Provides helper methods to resolve AST nodes."""
10
+
11
+ @staticmethod
12
+ def resolve_to(node, wanted, scope):
13
+ """Check if symbol ``node`` resolves to ``wanted`` object.
14
+
15
+ This is only intended to check if a given AST node resolves to a symbol
16
+ under some namespaces, e.g. the ``a.b.c.foo`` pattern, but not meant for
17
+ more complicated expressions like ``(a + b).foo``.
18
+
19
+ Args:
20
+ node (Union[ast.Attribute, ast.Name]): an AST node to be resolved.
21
+ wanted (Any): The expected python object.
22
+ scope (Dict[str, Any]): Maps from symbol names to objects, for
23
+ example, globals()
24
+
25
+ Returns:
26
+ bool: The checked result.
27
+ """
28
+ if isinstance(node, ast.Name):
29
+ return scope.get(node.id) is wanted
30
+
31
+ if not isinstance(node, ast.Attribute):
32
+ return False
33
+
34
+ v = node.value
35
+ chain = [node.attr]
36
+ while isinstance(v, ast.Attribute):
37
+ chain.append(v.attr)
38
+ v = v.value
39
+ if not isinstance(v, ast.Name):
40
+ # Example cases that fall under this branch:
41
+ #
42
+ # x[i].attr: ast.Subscript
43
+ # (a + b).attr: ast.BinOp
44
+ # ...
45
+ return False
46
+ chain.append(v.id)
47
+
48
+ for attr in reversed(chain):
49
+ try:
50
+ if isinstance(scope, dict):
51
+ scope = scope[attr]
52
+ else:
53
+ scope = getattr(scope, attr)
54
+ except (KeyError, AttributeError):
55
+ return False
56
+ # The name ``scope`` here could be a bit confusing
57
+ return scope is wanted
@@ -0,0 +1,9 @@
1
+ # type: ignore
2
+
3
+ from taichi.lang.ast.ast_transformer import ASTTransformer
4
+ from taichi.lang.ast.ast_transformer_utils import ASTTransformerContext
5
+
6
+
7
+ def transform_tree(tree, ctx: ASTTransformerContext):
8
+ ASTTransformer()(ctx, tree)
9
+ return ctx.return_data