numba-cuda 0.22.0__cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.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 (487) hide show
  1. _numba_cuda_redirector.pth +4 -0
  2. _numba_cuda_redirector.py +89 -0
  3. numba_cuda/VERSION +1 -0
  4. numba_cuda/__init__.py +6 -0
  5. numba_cuda/_version.py +11 -0
  6. numba_cuda/numba/cuda/__init__.py +70 -0
  7. numba_cuda/numba/cuda/_internal/cuda_bf16.py +16394 -0
  8. numba_cuda/numba/cuda/_internal/cuda_fp16.py +8112 -0
  9. numba_cuda/numba/cuda/api.py +580 -0
  10. numba_cuda/numba/cuda/api_util.py +76 -0
  11. numba_cuda/numba/cuda/args.py +72 -0
  12. numba_cuda/numba/cuda/bf16.py +397 -0
  13. numba_cuda/numba/cuda/cache_hints.py +287 -0
  14. numba_cuda/numba/cuda/cext/__init__.py +2 -0
  15. numba_cuda/numba/cuda/cext/_devicearray.cpp +159 -0
  16. numba_cuda/numba/cuda/cext/_devicearray.cpython-313-aarch64-linux-gnu.so +0 -0
  17. numba_cuda/numba/cuda/cext/_devicearray.h +29 -0
  18. numba_cuda/numba/cuda/cext/_dispatcher.cpp +1098 -0
  19. numba_cuda/numba/cuda/cext/_dispatcher.cpython-313-aarch64-linux-gnu.so +0 -0
  20. numba_cuda/numba/cuda/cext/_hashtable.cpp +532 -0
  21. numba_cuda/numba/cuda/cext/_hashtable.h +135 -0
  22. numba_cuda/numba/cuda/cext/_helperlib.c +71 -0
  23. numba_cuda/numba/cuda/cext/_helperlib.cpython-313-aarch64-linux-gnu.so +0 -0
  24. numba_cuda/numba/cuda/cext/_helpermod.c +82 -0
  25. numba_cuda/numba/cuda/cext/_pymodule.h +38 -0
  26. numba_cuda/numba/cuda/cext/_typeconv.cpp +206 -0
  27. numba_cuda/numba/cuda/cext/_typeconv.cpython-313-aarch64-linux-gnu.so +0 -0
  28. numba_cuda/numba/cuda/cext/_typeof.cpp +1159 -0
  29. numba_cuda/numba/cuda/cext/_typeof.h +19 -0
  30. numba_cuda/numba/cuda/cext/capsulethunk.h +111 -0
  31. numba_cuda/numba/cuda/cext/mviewbuf.c +385 -0
  32. numba_cuda/numba/cuda/cext/mviewbuf.cpython-313-aarch64-linux-gnu.so +0 -0
  33. numba_cuda/numba/cuda/cext/typeconv.cpp +212 -0
  34. numba_cuda/numba/cuda/cext/typeconv.hpp +101 -0
  35. numba_cuda/numba/cuda/cg.py +67 -0
  36. numba_cuda/numba/cuda/cgutils.py +1294 -0
  37. numba_cuda/numba/cuda/cloudpickle/__init__.py +21 -0
  38. numba_cuda/numba/cuda/cloudpickle/cloudpickle.py +1598 -0
  39. numba_cuda/numba/cuda/cloudpickle/cloudpickle_fast.py +17 -0
  40. numba_cuda/numba/cuda/codegen.py +541 -0
  41. numba_cuda/numba/cuda/compiler.py +1396 -0
  42. numba_cuda/numba/cuda/core/analysis.py +758 -0
  43. numba_cuda/numba/cuda/core/annotations/__init__.py +0 -0
  44. numba_cuda/numba/cuda/core/annotations/pretty_annotate.py +288 -0
  45. numba_cuda/numba/cuda/core/annotations/type_annotations.py +305 -0
  46. numba_cuda/numba/cuda/core/base.py +1332 -0
  47. numba_cuda/numba/cuda/core/boxing.py +1411 -0
  48. numba_cuda/numba/cuda/core/bytecode.py +728 -0
  49. numba_cuda/numba/cuda/core/byteflow.py +2346 -0
  50. numba_cuda/numba/cuda/core/caching.py +744 -0
  51. numba_cuda/numba/cuda/core/callconv.py +392 -0
  52. numba_cuda/numba/cuda/core/codegen.py +171 -0
  53. numba_cuda/numba/cuda/core/compiler.py +199 -0
  54. numba_cuda/numba/cuda/core/compiler_lock.py +85 -0
  55. numba_cuda/numba/cuda/core/compiler_machinery.py +497 -0
  56. numba_cuda/numba/cuda/core/config.py +650 -0
  57. numba_cuda/numba/cuda/core/consts.py +124 -0
  58. numba_cuda/numba/cuda/core/controlflow.py +989 -0
  59. numba_cuda/numba/cuda/core/entrypoints.py +57 -0
  60. numba_cuda/numba/cuda/core/environment.py +66 -0
  61. numba_cuda/numba/cuda/core/errors.py +917 -0
  62. numba_cuda/numba/cuda/core/event.py +511 -0
  63. numba_cuda/numba/cuda/core/funcdesc.py +330 -0
  64. numba_cuda/numba/cuda/core/generators.py +387 -0
  65. numba_cuda/numba/cuda/core/imputils.py +509 -0
  66. numba_cuda/numba/cuda/core/inline_closurecall.py +1787 -0
  67. numba_cuda/numba/cuda/core/interpreter.py +3617 -0
  68. numba_cuda/numba/cuda/core/ir.py +1812 -0
  69. numba_cuda/numba/cuda/core/ir_utils.py +2638 -0
  70. numba_cuda/numba/cuda/core/optional.py +129 -0
  71. numba_cuda/numba/cuda/core/options.py +262 -0
  72. numba_cuda/numba/cuda/core/postproc.py +249 -0
  73. numba_cuda/numba/cuda/core/pythonapi.py +1859 -0
  74. numba_cuda/numba/cuda/core/registry.py +46 -0
  75. numba_cuda/numba/cuda/core/removerefctpass.py +123 -0
  76. numba_cuda/numba/cuda/core/rewrites/__init__.py +26 -0
  77. numba_cuda/numba/cuda/core/rewrites/ir_print.py +91 -0
  78. numba_cuda/numba/cuda/core/rewrites/registry.py +104 -0
  79. numba_cuda/numba/cuda/core/rewrites/static_binop.py +41 -0
  80. numba_cuda/numba/cuda/core/rewrites/static_getitem.py +189 -0
  81. numba_cuda/numba/cuda/core/rewrites/static_raise.py +100 -0
  82. numba_cuda/numba/cuda/core/sigutils.py +68 -0
  83. numba_cuda/numba/cuda/core/ssa.py +498 -0
  84. numba_cuda/numba/cuda/core/targetconfig.py +330 -0
  85. numba_cuda/numba/cuda/core/tracing.py +231 -0
  86. numba_cuda/numba/cuda/core/transforms.py +956 -0
  87. numba_cuda/numba/cuda/core/typed_passes.py +867 -0
  88. numba_cuda/numba/cuda/core/typeinfer.py +1950 -0
  89. numba_cuda/numba/cuda/core/unsafe/__init__.py +0 -0
  90. numba_cuda/numba/cuda/core/unsafe/bytes.py +67 -0
  91. numba_cuda/numba/cuda/core/unsafe/eh.py +67 -0
  92. numba_cuda/numba/cuda/core/unsafe/refcount.py +98 -0
  93. numba_cuda/numba/cuda/core/untyped_passes.py +1979 -0
  94. numba_cuda/numba/cuda/cpython/builtins.py +1153 -0
  95. numba_cuda/numba/cuda/cpython/charseq.py +1218 -0
  96. numba_cuda/numba/cuda/cpython/cmathimpl.py +560 -0
  97. numba_cuda/numba/cuda/cpython/enumimpl.py +103 -0
  98. numba_cuda/numba/cuda/cpython/iterators.py +167 -0
  99. numba_cuda/numba/cuda/cpython/listobj.py +1326 -0
  100. numba_cuda/numba/cuda/cpython/mathimpl.py +499 -0
  101. numba_cuda/numba/cuda/cpython/numbers.py +1475 -0
  102. numba_cuda/numba/cuda/cpython/rangeobj.py +289 -0
  103. numba_cuda/numba/cuda/cpython/slicing.py +322 -0
  104. numba_cuda/numba/cuda/cpython/tupleobj.py +456 -0
  105. numba_cuda/numba/cuda/cpython/unicode.py +2865 -0
  106. numba_cuda/numba/cuda/cpython/unicode_support.py +1597 -0
  107. numba_cuda/numba/cuda/cpython/unsafe/__init__.py +0 -0
  108. numba_cuda/numba/cuda/cpython/unsafe/numbers.py +64 -0
  109. numba_cuda/numba/cuda/cpython/unsafe/tuple.py +92 -0
  110. numba_cuda/numba/cuda/cuda_paths.py +691 -0
  111. numba_cuda/numba/cuda/cudadecl.py +543 -0
  112. numba_cuda/numba/cuda/cudadrv/__init__.py +14 -0
  113. numba_cuda/numba/cuda/cudadrv/devicearray.py +954 -0
  114. numba_cuda/numba/cuda/cudadrv/devices.py +249 -0
  115. numba_cuda/numba/cuda/cudadrv/driver.py +3238 -0
  116. numba_cuda/numba/cuda/cudadrv/drvapi.py +435 -0
  117. numba_cuda/numba/cuda/cudadrv/dummyarray.py +562 -0
  118. numba_cuda/numba/cuda/cudadrv/enums.py +613 -0
  119. numba_cuda/numba/cuda/cudadrv/error.py +48 -0
  120. numba_cuda/numba/cuda/cudadrv/libs.py +220 -0
  121. numba_cuda/numba/cuda/cudadrv/linkable_code.py +184 -0
  122. numba_cuda/numba/cuda/cudadrv/mappings.py +14 -0
  123. numba_cuda/numba/cuda/cudadrv/ndarray.py +26 -0
  124. numba_cuda/numba/cuda/cudadrv/nvrtc.py +193 -0
  125. numba_cuda/numba/cuda/cudadrv/nvvm.py +756 -0
  126. numba_cuda/numba/cuda/cudadrv/rtapi.py +13 -0
  127. numba_cuda/numba/cuda/cudadrv/runtime.py +34 -0
  128. numba_cuda/numba/cuda/cudaimpl.py +983 -0
  129. numba_cuda/numba/cuda/cudamath.py +149 -0
  130. numba_cuda/numba/cuda/datamodel/__init__.py +7 -0
  131. numba_cuda/numba/cuda/datamodel/cuda_manager.py +66 -0
  132. numba_cuda/numba/cuda/datamodel/cuda_models.py +1446 -0
  133. numba_cuda/numba/cuda/datamodel/cuda_packer.py +224 -0
  134. numba_cuda/numba/cuda/datamodel/cuda_registry.py +22 -0
  135. numba_cuda/numba/cuda/datamodel/cuda_testing.py +153 -0
  136. numba_cuda/numba/cuda/datamodel/manager.py +11 -0
  137. numba_cuda/numba/cuda/datamodel/models.py +9 -0
  138. numba_cuda/numba/cuda/datamodel/packer.py +9 -0
  139. numba_cuda/numba/cuda/datamodel/registry.py +11 -0
  140. numba_cuda/numba/cuda/datamodel/testing.py +11 -0
  141. numba_cuda/numba/cuda/debuginfo.py +997 -0
  142. numba_cuda/numba/cuda/decorators.py +294 -0
  143. numba_cuda/numba/cuda/descriptor.py +35 -0
  144. numba_cuda/numba/cuda/device_init.py +155 -0
  145. numba_cuda/numba/cuda/deviceufunc.py +1021 -0
  146. numba_cuda/numba/cuda/dispatcher.py +2463 -0
  147. numba_cuda/numba/cuda/errors.py +72 -0
  148. numba_cuda/numba/cuda/extending.py +697 -0
  149. numba_cuda/numba/cuda/flags.py +178 -0
  150. numba_cuda/numba/cuda/fp16.py +357 -0
  151. numba_cuda/numba/cuda/include/12/cuda_bf16.h +5118 -0
  152. numba_cuda/numba/cuda/include/12/cuda_bf16.hpp +3865 -0
  153. numba_cuda/numba/cuda/include/12/cuda_fp16.h +5363 -0
  154. numba_cuda/numba/cuda/include/12/cuda_fp16.hpp +3483 -0
  155. numba_cuda/numba/cuda/include/13/cuda_bf16.h +5118 -0
  156. numba_cuda/numba/cuda/include/13/cuda_bf16.hpp +3865 -0
  157. numba_cuda/numba/cuda/include/13/cuda_fp16.h +5363 -0
  158. numba_cuda/numba/cuda/include/13/cuda_fp16.hpp +3483 -0
  159. numba_cuda/numba/cuda/initialize.py +24 -0
  160. numba_cuda/numba/cuda/intrinsics.py +531 -0
  161. numba_cuda/numba/cuda/itanium_mangler.py +214 -0
  162. numba_cuda/numba/cuda/kernels/__init__.py +2 -0
  163. numba_cuda/numba/cuda/kernels/reduction.py +265 -0
  164. numba_cuda/numba/cuda/kernels/transpose.py +65 -0
  165. numba_cuda/numba/cuda/libdevice.py +3386 -0
  166. numba_cuda/numba/cuda/libdevicedecl.py +20 -0
  167. numba_cuda/numba/cuda/libdevicefuncs.py +1060 -0
  168. numba_cuda/numba/cuda/libdeviceimpl.py +88 -0
  169. numba_cuda/numba/cuda/locks.py +19 -0
  170. numba_cuda/numba/cuda/lowering.py +1980 -0
  171. numba_cuda/numba/cuda/mathimpl.py +374 -0
  172. numba_cuda/numba/cuda/memory_management/__init__.py +4 -0
  173. numba_cuda/numba/cuda/memory_management/memsys.cu +99 -0
  174. numba_cuda/numba/cuda/memory_management/memsys.cuh +22 -0
  175. numba_cuda/numba/cuda/memory_management/nrt.cu +212 -0
  176. numba_cuda/numba/cuda/memory_management/nrt.cuh +48 -0
  177. numba_cuda/numba/cuda/memory_management/nrt.py +390 -0
  178. numba_cuda/numba/cuda/memory_management/nrt_context.py +438 -0
  179. numba_cuda/numba/cuda/misc/appdirs.py +594 -0
  180. numba_cuda/numba/cuda/misc/cffiimpl.py +24 -0
  181. numba_cuda/numba/cuda/misc/coverage_support.py +43 -0
  182. numba_cuda/numba/cuda/misc/dump_style.py +41 -0
  183. numba_cuda/numba/cuda/misc/findlib.py +75 -0
  184. numba_cuda/numba/cuda/misc/firstlinefinder.py +96 -0
  185. numba_cuda/numba/cuda/misc/gdb_hook.py +240 -0
  186. numba_cuda/numba/cuda/misc/literal.py +28 -0
  187. numba_cuda/numba/cuda/misc/llvm_pass_timings.py +412 -0
  188. numba_cuda/numba/cuda/misc/special.py +94 -0
  189. numba_cuda/numba/cuda/models.py +56 -0
  190. numba_cuda/numba/cuda/np/arraymath.py +5130 -0
  191. numba_cuda/numba/cuda/np/arrayobj.py +7635 -0
  192. numba_cuda/numba/cuda/np/extensions.py +11 -0
  193. numba_cuda/numba/cuda/np/linalg.py +3087 -0
  194. numba_cuda/numba/cuda/np/math/__init__.py +0 -0
  195. numba_cuda/numba/cuda/np/math/cmathimpl.py +558 -0
  196. numba_cuda/numba/cuda/np/math/mathimpl.py +487 -0
  197. numba_cuda/numba/cuda/np/math/numbers.py +1461 -0
  198. numba_cuda/numba/cuda/np/npdatetime.py +969 -0
  199. numba_cuda/numba/cuda/np/npdatetime_helpers.py +217 -0
  200. numba_cuda/numba/cuda/np/npyfuncs.py +1808 -0
  201. numba_cuda/numba/cuda/np/npyimpl.py +1027 -0
  202. numba_cuda/numba/cuda/np/numpy_support.py +798 -0
  203. numba_cuda/numba/cuda/np/polynomial/__init__.py +4 -0
  204. numba_cuda/numba/cuda/np/polynomial/polynomial_core.py +242 -0
  205. numba_cuda/numba/cuda/np/polynomial/polynomial_functions.py +380 -0
  206. numba_cuda/numba/cuda/np/ufunc/__init__.py +4 -0
  207. numba_cuda/numba/cuda/np/ufunc/decorators.py +203 -0
  208. numba_cuda/numba/cuda/np/ufunc/sigparse.py +68 -0
  209. numba_cuda/numba/cuda/np/ufunc/ufuncbuilder.py +65 -0
  210. numba_cuda/numba/cuda/np/ufunc_db.py +1282 -0
  211. numba_cuda/numba/cuda/np/unsafe/__init__.py +0 -0
  212. numba_cuda/numba/cuda/np/unsafe/ndarray.py +84 -0
  213. numba_cuda/numba/cuda/nvvmutils.py +254 -0
  214. numba_cuda/numba/cuda/printimpl.py +126 -0
  215. numba_cuda/numba/cuda/random.py +308 -0
  216. numba_cuda/numba/cuda/reshape_funcs.cu +156 -0
  217. numba_cuda/numba/cuda/serialize.py +267 -0
  218. numba_cuda/numba/cuda/simulator/__init__.py +63 -0
  219. numba_cuda/numba/cuda/simulator/_internal/__init__.py +4 -0
  220. numba_cuda/numba/cuda/simulator/_internal/cuda_bf16.py +2 -0
  221. numba_cuda/numba/cuda/simulator/api.py +179 -0
  222. numba_cuda/numba/cuda/simulator/bf16.py +4 -0
  223. numba_cuda/numba/cuda/simulator/compiler.py +38 -0
  224. numba_cuda/numba/cuda/simulator/cudadrv/__init__.py +11 -0
  225. numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +462 -0
  226. numba_cuda/numba/cuda/simulator/cudadrv/devices.py +122 -0
  227. numba_cuda/numba/cuda/simulator/cudadrv/driver.py +66 -0
  228. numba_cuda/numba/cuda/simulator/cudadrv/drvapi.py +7 -0
  229. numba_cuda/numba/cuda/simulator/cudadrv/dummyarray.py +7 -0
  230. numba_cuda/numba/cuda/simulator/cudadrv/error.py +10 -0
  231. numba_cuda/numba/cuda/simulator/cudadrv/libs.py +10 -0
  232. numba_cuda/numba/cuda/simulator/cudadrv/linkable_code.py +61 -0
  233. numba_cuda/numba/cuda/simulator/cudadrv/nvrtc.py +11 -0
  234. numba_cuda/numba/cuda/simulator/cudadrv/nvvm.py +32 -0
  235. numba_cuda/numba/cuda/simulator/cudadrv/runtime.py +22 -0
  236. numba_cuda/numba/cuda/simulator/dispatcher.py +11 -0
  237. numba_cuda/numba/cuda/simulator/kernel.py +320 -0
  238. numba_cuda/numba/cuda/simulator/kernelapi.py +509 -0
  239. numba_cuda/numba/cuda/simulator/memory_management/__init__.py +4 -0
  240. numba_cuda/numba/cuda/simulator/memory_management/nrt.py +21 -0
  241. numba_cuda/numba/cuda/simulator/reduction.py +19 -0
  242. numba_cuda/numba/cuda/simulator/tests/support.py +4 -0
  243. numba_cuda/numba/cuda/simulator/vector_types.py +65 -0
  244. numba_cuda/numba/cuda/simulator_init.py +18 -0
  245. numba_cuda/numba/cuda/stubs.py +624 -0
  246. numba_cuda/numba/cuda/target.py +505 -0
  247. numba_cuda/numba/cuda/testing.py +347 -0
  248. numba_cuda/numba/cuda/tests/__init__.py +62 -0
  249. numba_cuda/numba/cuda/tests/benchmarks/__init__.py +0 -0
  250. numba_cuda/numba/cuda/tests/benchmarks/test_kernel_launch.py +119 -0
  251. numba_cuda/numba/cuda/tests/cloudpickle_main_class.py +9 -0
  252. numba_cuda/numba/cuda/tests/core/serialize_usecases.py +113 -0
  253. numba_cuda/numba/cuda/tests/core/test_itanium_mangler.py +83 -0
  254. numba_cuda/numba/cuda/tests/core/test_serialize.py +371 -0
  255. numba_cuda/numba/cuda/tests/cudadrv/__init__.py +9 -0
  256. numba_cuda/numba/cuda/tests/cudadrv/test_array_attr.py +147 -0
  257. numba_cuda/numba/cuda/tests/cudadrv/test_context_stack.py +161 -0
  258. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_array_slicing.py +397 -0
  259. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_auto_context.py +24 -0
  260. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +180 -0
  261. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_driver.py +313 -0
  262. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_memory.py +191 -0
  263. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +621 -0
  264. numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +247 -0
  265. numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +100 -0
  266. numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +200 -0
  267. numba_cuda/numba/cuda/tests/cudadrv/test_events.py +53 -0
  268. numba_cuda/numba/cuda/tests/cudadrv/test_host_alloc.py +72 -0
  269. numba_cuda/numba/cuda/tests/cudadrv/test_init.py +138 -0
  270. numba_cuda/numba/cuda/tests/cudadrv/test_inline_ptx.py +43 -0
  271. numba_cuda/numba/cuda/tests/cudadrv/test_is_fp16.py +15 -0
  272. numba_cuda/numba/cuda/tests/cudadrv/test_linkable_code.py +58 -0
  273. numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +348 -0
  274. numba_cuda/numba/cuda/tests/cudadrv/test_managed_alloc.py +128 -0
  275. numba_cuda/numba/cuda/tests/cudadrv/test_module_callbacks.py +301 -0
  276. numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py +174 -0
  277. numba_cuda/numba/cuda/tests/cudadrv/test_nvrtc.py +28 -0
  278. numba_cuda/numba/cuda/tests/cudadrv/test_nvvm_driver.py +185 -0
  279. numba_cuda/numba/cuda/tests/cudadrv/test_pinned.py +39 -0
  280. numba_cuda/numba/cuda/tests/cudadrv/test_profiler.py +23 -0
  281. numba_cuda/numba/cuda/tests/cudadrv/test_reset_device.py +38 -0
  282. numba_cuda/numba/cuda/tests/cudadrv/test_runtime.py +48 -0
  283. numba_cuda/numba/cuda/tests/cudadrv/test_select_device.py +44 -0
  284. numba_cuda/numba/cuda/tests/cudadrv/test_streams.py +127 -0
  285. numba_cuda/numba/cuda/tests/cudapy/__init__.py +9 -0
  286. numba_cuda/numba/cuda/tests/cudapy/cache_usecases.py +231 -0
  287. numba_cuda/numba/cuda/tests/cudapy/cache_with_cpu_usecases.py +50 -0
  288. numba_cuda/numba/cuda/tests/cudapy/cg_cache_usecases.py +36 -0
  289. numba_cuda/numba/cuda/tests/cudapy/complex_usecases.py +116 -0
  290. numba_cuda/numba/cuda/tests/cudapy/enum_usecases.py +59 -0
  291. numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +62 -0
  292. numba_cuda/numba/cuda/tests/cudapy/jitlink.ptx +28 -0
  293. numba_cuda/numba/cuda/tests/cudapy/overload_usecases.py +33 -0
  294. numba_cuda/numba/cuda/tests/cudapy/recursion_usecases.py +104 -0
  295. numba_cuda/numba/cuda/tests/cudapy/test_alignment.py +47 -0
  296. numba_cuda/numba/cuda/tests/cudapy/test_analysis.py +1122 -0
  297. numba_cuda/numba/cuda/tests/cudapy/test_array.py +344 -0
  298. numba_cuda/numba/cuda/tests/cudapy/test_array_alignment.py +268 -0
  299. numba_cuda/numba/cuda/tests/cudapy/test_array_args.py +203 -0
  300. numba_cuda/numba/cuda/tests/cudapy/test_array_methods.py +63 -0
  301. numba_cuda/numba/cuda/tests/cudapy/test_array_reductions.py +360 -0
  302. numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1815 -0
  303. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +599 -0
  304. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +377 -0
  305. numba_cuda/numba/cuda/tests/cudapy/test_blackscholes.py +160 -0
  306. numba_cuda/numba/cuda/tests/cudapy/test_boolean.py +27 -0
  307. numba_cuda/numba/cuda/tests/cudapy/test_byteflow.py +98 -0
  308. numba_cuda/numba/cuda/tests/cudapy/test_cache_hints.py +210 -0
  309. numba_cuda/numba/cuda/tests/cudapy/test_caching.py +683 -0
  310. numba_cuda/numba/cuda/tests/cudapy/test_casting.py +265 -0
  311. numba_cuda/numba/cuda/tests/cudapy/test_cffi.py +42 -0
  312. numba_cuda/numba/cuda/tests/cudapy/test_compiler.py +718 -0
  313. numba_cuda/numba/cuda/tests/cudapy/test_complex.py +370 -0
  314. numba_cuda/numba/cuda/tests/cudapy/test_complex_kernel.py +23 -0
  315. numba_cuda/numba/cuda/tests/cudapy/test_const_string.py +142 -0
  316. numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +178 -0
  317. numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +193 -0
  318. numba_cuda/numba/cuda/tests/cudapy/test_copy_propagate.py +131 -0
  319. numba_cuda/numba/cuda/tests/cudapy/test_cuda_array_interface.py +438 -0
  320. numba_cuda/numba/cuda/tests/cudapy/test_cuda_jit_no_types.py +94 -0
  321. numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +101 -0
  322. numba_cuda/numba/cuda/tests/cudapy/test_debug.py +105 -0
  323. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +978 -0
  324. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +476 -0
  325. numba_cuda/numba/cuda/tests/cudapy/test_device_func.py +500 -0
  326. numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +820 -0
  327. numba_cuda/numba/cuda/tests/cudapy/test_enums.py +152 -0
  328. numba_cuda/numba/cuda/tests/cudapy/test_errors.py +111 -0
  329. numba_cuda/numba/cuda/tests/cudapy/test_exception.py +170 -0
  330. numba_cuda/numba/cuda/tests/cudapy/test_extending.py +1088 -0
  331. numba_cuda/numba/cuda/tests/cudapy/test_extending_types.py +71 -0
  332. numba_cuda/numba/cuda/tests/cudapy/test_fastmath.py +265 -0
  333. numba_cuda/numba/cuda/tests/cudapy/test_flow_control.py +1433 -0
  334. numba_cuda/numba/cuda/tests/cudapy/test_forall.py +57 -0
  335. numba_cuda/numba/cuda/tests/cudapy/test_freevar.py +34 -0
  336. numba_cuda/numba/cuda/tests/cudapy/test_frexp_ldexp.py +69 -0
  337. numba_cuda/numba/cuda/tests/cudapy/test_globals.py +62 -0
  338. numba_cuda/numba/cuda/tests/cudapy/test_gufunc.py +474 -0
  339. numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scalar.py +167 -0
  340. numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scheduling.py +92 -0
  341. numba_cuda/numba/cuda/tests/cudapy/test_idiv.py +39 -0
  342. numba_cuda/numba/cuda/tests/cudapy/test_inline.py +170 -0
  343. numba_cuda/numba/cuda/tests/cudapy/test_inspect.py +255 -0
  344. numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +1219 -0
  345. numba_cuda/numba/cuda/tests/cudapy/test_ipc.py +263 -0
  346. numba_cuda/numba/cuda/tests/cudapy/test_ir.py +598 -0
  347. numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +276 -0
  348. numba_cuda/numba/cuda/tests/cudapy/test_iterators.py +101 -0
  349. numba_cuda/numba/cuda/tests/cudapy/test_lang.py +68 -0
  350. numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +123 -0
  351. numba_cuda/numba/cuda/tests/cudapy/test_libdevice.py +194 -0
  352. numba_cuda/numba/cuda/tests/cudapy/test_lineinfo.py +220 -0
  353. numba_cuda/numba/cuda/tests/cudapy/test_localmem.py +173 -0
  354. numba_cuda/numba/cuda/tests/cudapy/test_make_function_to_jit_function.py +364 -0
  355. numba_cuda/numba/cuda/tests/cudapy/test_mandel.py +47 -0
  356. numba_cuda/numba/cuda/tests/cudapy/test_math.py +842 -0
  357. numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +76 -0
  358. numba_cuda/numba/cuda/tests/cudapy/test_minmax.py +78 -0
  359. numba_cuda/numba/cuda/tests/cudapy/test_montecarlo.py +25 -0
  360. numba_cuda/numba/cuda/tests/cudapy/test_multigpu.py +145 -0
  361. numba_cuda/numba/cuda/tests/cudapy/test_multiprocessing.py +39 -0
  362. numba_cuda/numba/cuda/tests/cudapy/test_multithreads.py +82 -0
  363. numba_cuda/numba/cuda/tests/cudapy/test_nondet.py +53 -0
  364. numba_cuda/numba/cuda/tests/cudapy/test_operator.py +504 -0
  365. numba_cuda/numba/cuda/tests/cudapy/test_optimization.py +93 -0
  366. numba_cuda/numba/cuda/tests/cudapy/test_overload.py +402 -0
  367. numba_cuda/numba/cuda/tests/cudapy/test_powi.py +128 -0
  368. numba_cuda/numba/cuda/tests/cudapy/test_print.py +193 -0
  369. numba_cuda/numba/cuda/tests/cudapy/test_py2_div_issue.py +37 -0
  370. numba_cuda/numba/cuda/tests/cudapy/test_random.py +117 -0
  371. numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +614 -0
  372. numba_cuda/numba/cuda/tests/cudapy/test_recursion.py +130 -0
  373. numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +94 -0
  374. numba_cuda/numba/cuda/tests/cudapy/test_retrieve_autoconverted_arrays.py +83 -0
  375. numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +86 -0
  376. numba_cuda/numba/cuda/tests/cudapy/test_slicing.py +40 -0
  377. numba_cuda/numba/cuda/tests/cudapy/test_sm.py +457 -0
  378. numba_cuda/numba/cuda/tests/cudapy/test_sm_creation.py +233 -0
  379. numba_cuda/numba/cuda/tests/cudapy/test_ssa.py +454 -0
  380. numba_cuda/numba/cuda/tests/cudapy/test_stream_api.py +56 -0
  381. numba_cuda/numba/cuda/tests/cudapy/test_sync.py +277 -0
  382. numba_cuda/numba/cuda/tests/cudapy/test_tracing.py +200 -0
  383. numba_cuda/numba/cuda/tests/cudapy/test_transpose.py +90 -0
  384. numba_cuda/numba/cuda/tests/cudapy/test_typeconv.py +333 -0
  385. numba_cuda/numba/cuda/tests/cudapy/test_typeinfer.py +538 -0
  386. numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +585 -0
  387. numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +42 -0
  388. numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +485 -0
  389. numba_cuda/numba/cuda/tests/cudapy/test_vectorize.py +312 -0
  390. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_complex.py +23 -0
  391. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +183 -0
  392. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_device.py +40 -0
  393. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_scalar_arg.py +40 -0
  394. numba_cuda/numba/cuda/tests/cudapy/test_warning.py +206 -0
  395. numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +446 -0
  396. numba_cuda/numba/cuda/tests/cudasim/__init__.py +9 -0
  397. numba_cuda/numba/cuda/tests/cudasim/support.py +9 -0
  398. numba_cuda/numba/cuda/tests/cudasim/test_cudasim_issues.py +111 -0
  399. numba_cuda/numba/cuda/tests/data/__init__.py +2 -0
  400. numba_cuda/numba/cuda/tests/data/cta_barrier.cu +28 -0
  401. numba_cuda/numba/cuda/tests/data/cuda_include.cu +10 -0
  402. numba_cuda/numba/cuda/tests/data/error.cu +12 -0
  403. numba_cuda/numba/cuda/tests/data/include/add.cuh +8 -0
  404. numba_cuda/numba/cuda/tests/data/jitlink.cu +28 -0
  405. numba_cuda/numba/cuda/tests/data/jitlink.ptx +49 -0
  406. numba_cuda/numba/cuda/tests/data/warn.cu +12 -0
  407. numba_cuda/numba/cuda/tests/doc_examples/__init__.py +9 -0
  408. numba_cuda/numba/cuda/tests/doc_examples/ffi/__init__.py +2 -0
  409. numba_cuda/numba/cuda/tests/doc_examples/ffi/functions.cu +54 -0
  410. numba_cuda/numba/cuda/tests/doc_examples/ffi/include/mul.cuh +8 -0
  411. numba_cuda/numba/cuda/tests/doc_examples/ffi/saxpy.cu +14 -0
  412. numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +86 -0
  413. numba_cuda/numba/cuda/tests/doc_examples/test_cpointer.py +68 -0
  414. numba_cuda/numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py +81 -0
  415. numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +141 -0
  416. numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +160 -0
  417. numba_cuda/numba/cuda/tests/doc_examples/test_matmul.py +180 -0
  418. numba_cuda/numba/cuda/tests/doc_examples/test_montecarlo.py +119 -0
  419. numba_cuda/numba/cuda/tests/doc_examples/test_random.py +66 -0
  420. numba_cuda/numba/cuda/tests/doc_examples/test_reduction.py +80 -0
  421. numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +206 -0
  422. numba_cuda/numba/cuda/tests/doc_examples/test_ufunc.py +53 -0
  423. numba_cuda/numba/cuda/tests/doc_examples/test_vecadd.py +76 -0
  424. numba_cuda/numba/cuda/tests/nocuda/__init__.py +9 -0
  425. numba_cuda/numba/cuda/tests/nocuda/test_dummyarray.py +452 -0
  426. numba_cuda/numba/cuda/tests/nocuda/test_function_resolution.py +48 -0
  427. numba_cuda/numba/cuda/tests/nocuda/test_import.py +63 -0
  428. numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +252 -0
  429. numba_cuda/numba/cuda/tests/nocuda/test_nvvm.py +59 -0
  430. numba_cuda/numba/cuda/tests/nrt/__init__.py +9 -0
  431. numba_cuda/numba/cuda/tests/nrt/test_nrt.py +387 -0
  432. numba_cuda/numba/cuda/tests/nrt/test_nrt_refct.py +124 -0
  433. numba_cuda/numba/cuda/tests/support.py +900 -0
  434. numba_cuda/numba/cuda/typeconv/__init__.py +4 -0
  435. numba_cuda/numba/cuda/typeconv/castgraph.py +137 -0
  436. numba_cuda/numba/cuda/typeconv/rules.py +63 -0
  437. numba_cuda/numba/cuda/typeconv/typeconv.py +121 -0
  438. numba_cuda/numba/cuda/types/__init__.py +233 -0
  439. numba_cuda/numba/cuda/types/__init__.pyi +167 -0
  440. numba_cuda/numba/cuda/types/abstract.py +9 -0
  441. numba_cuda/numba/cuda/types/common.py +9 -0
  442. numba_cuda/numba/cuda/types/containers.py +9 -0
  443. numba_cuda/numba/cuda/types/cuda_abstract.py +533 -0
  444. numba_cuda/numba/cuda/types/cuda_common.py +110 -0
  445. numba_cuda/numba/cuda/types/cuda_containers.py +971 -0
  446. numba_cuda/numba/cuda/types/cuda_function_type.py +230 -0
  447. numba_cuda/numba/cuda/types/cuda_functions.py +798 -0
  448. numba_cuda/numba/cuda/types/cuda_iterators.py +120 -0
  449. numba_cuda/numba/cuda/types/cuda_misc.py +569 -0
  450. numba_cuda/numba/cuda/types/cuda_npytypes.py +690 -0
  451. numba_cuda/numba/cuda/types/cuda_scalars.py +280 -0
  452. numba_cuda/numba/cuda/types/ext_types.py +101 -0
  453. numba_cuda/numba/cuda/types/function_type.py +11 -0
  454. numba_cuda/numba/cuda/types/functions.py +9 -0
  455. numba_cuda/numba/cuda/types/iterators.py +9 -0
  456. numba_cuda/numba/cuda/types/misc.py +9 -0
  457. numba_cuda/numba/cuda/types/npytypes.py +9 -0
  458. numba_cuda/numba/cuda/types/scalars.py +9 -0
  459. numba_cuda/numba/cuda/typing/__init__.py +19 -0
  460. numba_cuda/numba/cuda/typing/arraydecl.py +939 -0
  461. numba_cuda/numba/cuda/typing/asnumbatype.py +130 -0
  462. numba_cuda/numba/cuda/typing/bufproto.py +70 -0
  463. numba_cuda/numba/cuda/typing/builtins.py +1209 -0
  464. numba_cuda/numba/cuda/typing/cffi_utils.py +219 -0
  465. numba_cuda/numba/cuda/typing/cmathdecl.py +47 -0
  466. numba_cuda/numba/cuda/typing/collections.py +138 -0
  467. numba_cuda/numba/cuda/typing/context.py +782 -0
  468. numba_cuda/numba/cuda/typing/ctypes_utils.py +125 -0
  469. numba_cuda/numba/cuda/typing/dictdecl.py +63 -0
  470. numba_cuda/numba/cuda/typing/enumdecl.py +74 -0
  471. numba_cuda/numba/cuda/typing/listdecl.py +147 -0
  472. numba_cuda/numba/cuda/typing/mathdecl.py +158 -0
  473. numba_cuda/numba/cuda/typing/npdatetime.py +322 -0
  474. numba_cuda/numba/cuda/typing/npydecl.py +749 -0
  475. numba_cuda/numba/cuda/typing/setdecl.py +115 -0
  476. numba_cuda/numba/cuda/typing/templates.py +1446 -0
  477. numba_cuda/numba/cuda/typing/typeof.py +301 -0
  478. numba_cuda/numba/cuda/ufuncs.py +746 -0
  479. numba_cuda/numba/cuda/utils.py +724 -0
  480. numba_cuda/numba/cuda/vector_types.py +214 -0
  481. numba_cuda/numba/cuda/vectorizers.py +260 -0
  482. numba_cuda-0.22.0.dist-info/METADATA +109 -0
  483. numba_cuda-0.22.0.dist-info/RECORD +487 -0
  484. numba_cuda-0.22.0.dist-info/WHEEL +6 -0
  485. numba_cuda-0.22.0.dist-info/licenses/LICENSE +26 -0
  486. numba_cuda-0.22.0.dist-info/licenses/LICENSE.numba +24 -0
  487. numba_cuda-0.22.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1859 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ from collections import namedtuple
5
+ import contextlib
6
+ import hashlib
7
+ import sys
8
+
9
+ from llvmlite import ir
10
+ from llvmlite.ir import Constant
11
+
12
+ import ctypes
13
+ from numba.cuda.cext import _helperlib
14
+ from numba.cuda.core import errors
15
+ from numba.cuda.core import imputils
16
+ from numba.cuda.utils import PYVERSION
17
+ from numba.cuda import config, types, lowering, cgutils, serialize
18
+
19
+ PY_UNICODE_1BYTE_KIND = _helperlib.py_unicode_1byte_kind
20
+ PY_UNICODE_2BYTE_KIND = _helperlib.py_unicode_2byte_kind
21
+ PY_UNICODE_4BYTE_KIND = _helperlib.py_unicode_4byte_kind
22
+ if PYVERSION in ((3, 10), (3, 11)):
23
+ PY_UNICODE_WCHAR_KIND = _helperlib.py_unicode_wchar_kind
24
+
25
+
26
+ class _Registry(object):
27
+ def __init__(self):
28
+ self.functions = {}
29
+
30
+ def register(self, typeclass):
31
+ assert issubclass(typeclass, types.Type)
32
+
33
+ def decorator(func):
34
+ if typeclass in self.functions:
35
+ raise KeyError("duplicate registration for %s" % (typeclass,))
36
+ self.functions[typeclass] = func
37
+ return func
38
+
39
+ return decorator
40
+
41
+ def lookup(self, typeclass, default=None):
42
+ assert issubclass(typeclass, types.Type)
43
+ for cls in typeclass.__mro__:
44
+ func = self.functions.get(cls)
45
+ if func is not None:
46
+ return func
47
+ return default
48
+
49
+
50
+ # Registries of boxing / unboxing implementations
51
+ _boxers = _Registry()
52
+ _unboxers = _Registry()
53
+ _reflectors = _Registry()
54
+
55
+ box = _boxers.register
56
+ unbox = _unboxers.register
57
+ reflect = _reflectors.register
58
+
59
+
60
+ class _BoxContext(
61
+ namedtuple("_BoxContext", ("context", "builder", "pyapi", "env_manager"))
62
+ ):
63
+ """
64
+ The facilities required by boxing implementations.
65
+ """
66
+
67
+ __slots__ = ()
68
+
69
+ def box(self, typ, val):
70
+ return self.pyapi.from_native_value(typ, val, self.env_manager)
71
+
72
+
73
+ class _UnboxContext(
74
+ namedtuple("_UnboxContext", ("context", "builder", "pyapi"))
75
+ ):
76
+ """
77
+ The facilities required by unboxing implementations.
78
+ """
79
+
80
+ __slots__ = ()
81
+
82
+ def unbox(self, typ, obj):
83
+ return self.pyapi.to_native_value(typ, obj)
84
+
85
+
86
+ class _ReflectContext(
87
+ namedtuple(
88
+ "_ReflectContext",
89
+ ("context", "builder", "pyapi", "env_manager", "is_error"),
90
+ )
91
+ ):
92
+ """
93
+ The facilities required by reflection implementations.
94
+ """
95
+
96
+ __slots__ = ()
97
+
98
+ # XXX the error bit is currently unused by consumers (e.g. PyCallWrapper)
99
+ def set_error(self):
100
+ self.builder.store(self.is_error, cgutils.true_bit)
101
+
102
+ def box(self, typ, val):
103
+ return self.pyapi.from_native_value(typ, val, self.env_manager)
104
+
105
+ def reflect(self, typ, val):
106
+ return self.pyapi.reflect_native_value(typ, val, self.env_manager)
107
+
108
+
109
+ class NativeValue(object):
110
+ """
111
+ Encapsulate the result of converting a Python object to a native value,
112
+ recording whether the conversion was successful and how to cleanup.
113
+ """
114
+
115
+ def __init__(self, value, is_error=None, cleanup=None):
116
+ self.value = value
117
+ self.is_error = is_error if is_error is not None else cgutils.false_bit
118
+ self.cleanup = cleanup
119
+
120
+
121
+ class EnvironmentManager(object):
122
+ def __init__(self, pyapi, env, env_body, env_ptr):
123
+ assert isinstance(env, lowering.Environment)
124
+ self.pyapi = pyapi
125
+ self.env = env
126
+ self.env_body = env_body
127
+ self.env_ptr = env_ptr
128
+
129
+ def add_const(self, const):
130
+ """
131
+ Add a constant to the environment, return its index.
132
+ """
133
+ # All constants are frozen inside the environment
134
+ if isinstance(const, str):
135
+ const = sys.intern(const)
136
+ for index, val in enumerate(self.env.consts):
137
+ if val is const:
138
+ break
139
+ else:
140
+ index = len(self.env.consts)
141
+ self.env.consts.append(const)
142
+ return index
143
+
144
+ def read_const(self, index):
145
+ """
146
+ Look up constant number *index* inside the environment body.
147
+ A borrowed reference is returned.
148
+
149
+ The returned LLVM value may have NULL value at runtime which indicates
150
+ an error at runtime.
151
+ """
152
+ assert index < len(self.env.consts)
153
+
154
+ builder = self.pyapi.builder
155
+ consts = self.env_body.consts
156
+ ret = cgutils.alloca_once(builder, self.pyapi.pyobj, zfill=True)
157
+ with builder.if_else(cgutils.is_not_null(builder, consts)) as (
158
+ br_not_null,
159
+ br_null,
160
+ ):
161
+ with br_not_null:
162
+ getitem = self.pyapi.list_getitem(consts, index)
163
+ builder.store(getitem, ret)
164
+ with br_null:
165
+ # This can happen when the Environment is accidentally released
166
+ # and has subsequently been garbage collected.
167
+ self.pyapi.err_set_string(
168
+ "PyExc_RuntimeError",
169
+ "`env.consts` is NULL in `read_const`",
170
+ )
171
+ return builder.load(ret)
172
+
173
+
174
+ _IteratorLoop = namedtuple("_IteratorLoop", ("value", "do_break"))
175
+
176
+
177
+ class PythonAPI(object):
178
+ """
179
+ Code generation facilities to call into the CPython C API (and related
180
+ helpers).
181
+ """
182
+
183
+ def __init__(self, context, builder):
184
+ """
185
+ Note: Maybe called multiple times when lowering a function
186
+ """
187
+ self.context = context
188
+ self.builder = builder
189
+
190
+ self.module = builder.basic_block.function.module
191
+ # A unique mapping of serialized objects in this module
192
+ try:
193
+ self.module.__serialized
194
+ except AttributeError:
195
+ self.module.__serialized = {}
196
+
197
+ # Initialize types
198
+ self.pyobj = self.context.get_argument_type(types.pyobject)
199
+ self.pyobjptr = self.pyobj.as_pointer()
200
+ self.voidptr = ir.PointerType(ir.IntType(8))
201
+ self.long = ir.IntType(ctypes.sizeof(ctypes.c_long) * 8)
202
+ self.ulong = self.long
203
+ self.longlong = ir.IntType(ctypes.sizeof(ctypes.c_ulonglong) * 8)
204
+ self.ulonglong = self.longlong
205
+ self.double = ir.DoubleType()
206
+ self.py_ssize_t = self.context.get_value_type(types.intp)
207
+ self.cstring = ir.PointerType(ir.IntType(8))
208
+ self.gil_state = ir.IntType(_helperlib.py_gil_state_size * 8)
209
+ self.py_buffer_t = ir.ArrayType(
210
+ ir.IntType(8), _helperlib.py_buffer_size
211
+ )
212
+ self.py_hash_t = self.py_ssize_t
213
+ self.py_unicode_1byte_kind = _helperlib.py_unicode_1byte_kind
214
+ self.py_unicode_2byte_kind = _helperlib.py_unicode_2byte_kind
215
+ self.py_unicode_4byte_kind = _helperlib.py_unicode_4byte_kind
216
+
217
+ def get_env_manager(self, env, env_body, env_ptr):
218
+ return EnvironmentManager(self, env, env_body, env_ptr)
219
+
220
+ def emit_environment_sentry(
221
+ self, envptr, return_pyobject=False, debug_msg=""
222
+ ):
223
+ """Emits LLVM code to ensure the `envptr` is not NULL"""
224
+ is_null = cgutils.is_null(self.builder, envptr)
225
+ with cgutils.if_unlikely(self.builder, is_null):
226
+ if return_pyobject:
227
+ fnty = self.builder.function.type.pointee
228
+ assert fnty.return_type == self.pyobj
229
+ self.err_set_string(
230
+ "PyExc_RuntimeError",
231
+ f"missing Environment: {debug_msg}",
232
+ )
233
+ self.builder.ret(self.get_null_object())
234
+ else:
235
+ self.context.call_conv.return_user_exc(
236
+ self.builder,
237
+ RuntimeError,
238
+ (f"missing Environment: {debug_msg}",),
239
+ )
240
+
241
+ # ------ Python API -----
242
+
243
+ #
244
+ # Basic object API
245
+ #
246
+
247
+ def incref(self, obj):
248
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
249
+ fn = self._get_function(fnty, name="Py_IncRef")
250
+ self.builder.call(fn, [obj])
251
+
252
+ def decref(self, obj):
253
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
254
+ fn = self._get_function(fnty, name="Py_DecRef")
255
+ self.builder.call(fn, [obj])
256
+
257
+ def get_type(self, obj):
258
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
259
+ fn = self._get_function(fnty, name="numba_py_type")
260
+ return self.builder.call(fn, [obj])
261
+
262
+ #
263
+ # Argument unpacking
264
+ #
265
+
266
+ def parse_tuple_and_keywords(self, args, kws, fmt, keywords, *objs):
267
+ charptr = ir.PointerType(ir.IntType(8))
268
+ charptrary = ir.PointerType(charptr)
269
+ argtypes = [self.pyobj, self.pyobj, charptr, charptrary]
270
+ fnty = ir.FunctionType(ir.IntType(32), argtypes, var_arg=True)
271
+ fn = self._get_function(fnty, name="PyArg_ParseTupleAndKeywords")
272
+ return self.builder.call(fn, [args, kws, fmt, keywords] + list(objs))
273
+
274
+ def parse_tuple(self, args, fmt, *objs):
275
+ charptr = ir.PointerType(ir.IntType(8))
276
+ argtypes = [self.pyobj, charptr]
277
+ fnty = ir.FunctionType(ir.IntType(32), argtypes, var_arg=True)
278
+ fn = self._get_function(fnty, name="PyArg_ParseTuple")
279
+ return self.builder.call(fn, [args, fmt] + list(objs))
280
+
281
+ def unpack_tuple(self, args, name, n_min, n_max, *objs):
282
+ charptr = ir.PointerType(ir.IntType(8))
283
+ argtypes = [self.pyobj, charptr, self.py_ssize_t, self.py_ssize_t]
284
+ fnty = ir.FunctionType(ir.IntType(32), argtypes, var_arg=True)
285
+ fn = self._get_function(fnty, name="PyArg_UnpackTuple")
286
+ n_min = Constant(self.py_ssize_t, int(n_min))
287
+ n_max = Constant(self.py_ssize_t, int(n_max))
288
+ if isinstance(name, str):
289
+ name = self.context.insert_const_string(self.builder.module, name)
290
+ return self.builder.call(fn, [args, name, n_min, n_max] + list(objs))
291
+
292
+ #
293
+ # Exception and errors
294
+ #
295
+
296
+ def err_occurred(self):
297
+ fnty = ir.FunctionType(self.pyobj, ())
298
+ fn = self._get_function(fnty, name="PyErr_Occurred")
299
+ return self.builder.call(fn, ())
300
+
301
+ def err_clear(self):
302
+ fnty = ir.FunctionType(ir.VoidType(), ())
303
+ fn = self._get_function(fnty, name="PyErr_Clear")
304
+ return self.builder.call(fn, ())
305
+
306
+ def err_set_string(self, exctype, msg):
307
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj, self.cstring])
308
+ fn = self._get_function(fnty, name="PyErr_SetString")
309
+ if isinstance(exctype, str):
310
+ exctype = self.get_c_object(exctype)
311
+ if isinstance(msg, str):
312
+ msg = self.context.insert_const_string(self.module, msg)
313
+ return self.builder.call(fn, (exctype, msg))
314
+
315
+ def err_format(self, exctype, msg, *format_args):
316
+ fnty = ir.FunctionType(
317
+ ir.VoidType(), [self.pyobj, self.cstring], var_arg=True
318
+ )
319
+ fn = self._get_function(fnty, name="PyErr_Format")
320
+ if isinstance(exctype, str):
321
+ exctype = self.get_c_object(exctype)
322
+ if isinstance(msg, str):
323
+ msg = self.context.insert_const_string(self.module, msg)
324
+ return self.builder.call(fn, (exctype, msg) + tuple(format_args))
325
+
326
+ def raise_object(self, exc=None):
327
+ """
328
+ Raise an arbitrary exception (type or value or (type, args)
329
+ or None - if reraising). A reference to the argument is consumed.
330
+ """
331
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
332
+ fn = self._get_function(fnty, name="numba_do_raise")
333
+ if exc is None:
334
+ exc = self.make_none()
335
+ return self.builder.call(fn, (exc,))
336
+
337
+ def err_set_object(self, exctype, excval):
338
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj, self.pyobj])
339
+ fn = self._get_function(fnty, name="PyErr_SetObject")
340
+ if isinstance(exctype, str):
341
+ exctype = self.get_c_object(exctype)
342
+ return self.builder.call(fn, (exctype, excval))
343
+
344
+ def err_set_none(self, exctype):
345
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
346
+ fn = self._get_function(fnty, name="PyErr_SetNone")
347
+ if isinstance(exctype, str):
348
+ exctype = self.get_c_object(exctype)
349
+ return self.builder.call(fn, (exctype,))
350
+
351
+ def err_write_unraisable(self, obj):
352
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
353
+ fn = self._get_function(fnty, name="PyErr_WriteUnraisable")
354
+ return self.builder.call(fn, (obj,))
355
+
356
+ def err_fetch(self, pty, pval, ptb):
357
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobjptr] * 3)
358
+ fn = self._get_function(fnty, name="PyErr_Fetch")
359
+ return self.builder.call(fn, (pty, pval, ptb))
360
+
361
+ def err_restore(self, ty, val, tb):
362
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj] * 3)
363
+ fn = self._get_function(fnty, name="PyErr_Restore")
364
+ return self.builder.call(fn, (ty, val, tb))
365
+
366
+ @contextlib.contextmanager
367
+ def err_push(self, keep_new=False):
368
+ """
369
+ Temporarily push the current error indicator while the code
370
+ block is executed. If *keep_new* is True and the code block
371
+ raises a new error, the new error is kept, otherwise the old
372
+ error indicator is restored at the end of the block.
373
+ """
374
+ pty, pval, ptb = [
375
+ cgutils.alloca_once(self.builder, self.pyobj) for i in range(3)
376
+ ]
377
+ self.err_fetch(pty, pval, ptb)
378
+ yield
379
+ ty = self.builder.load(pty)
380
+ val = self.builder.load(pval)
381
+ tb = self.builder.load(ptb)
382
+ if keep_new:
383
+ new_error = cgutils.is_not_null(self.builder, self.err_occurred())
384
+ with self.builder.if_else(new_error, likely=False) as (
385
+ if_error,
386
+ if_ok,
387
+ ):
388
+ with if_error:
389
+ # Code block raised an error, keep it
390
+ self.decref(ty)
391
+ self.decref(val)
392
+ self.decref(tb)
393
+ with if_ok:
394
+ # Restore previous error
395
+ self.err_restore(ty, val, tb)
396
+ else:
397
+ self.err_restore(ty, val, tb)
398
+
399
+ def get_c_object(self, name):
400
+ """
401
+ Get a Python object through its C-accessible *name*
402
+ (e.g. "PyExc_ValueError"). The underlying variable must be
403
+ a `PyObject *`, and the value of that pointer is returned.
404
+ """
405
+ # A LLVM global variable is implicitly a pointer to the declared
406
+ # type, so fix up by using pyobj.pointee.
407
+ return self.context.get_c_value(
408
+ self.builder, self.pyobj.pointee, name, dllimport=True
409
+ )
410
+
411
+ def raise_missing_global_error(self, name):
412
+ msg = "global name '%s' is not defined" % name
413
+ cstr = self.context.insert_const_string(self.module, msg)
414
+ self.err_set_string("PyExc_NameError", cstr)
415
+
416
+ def raise_missing_name_error(self, name):
417
+ msg = "name '%s' is not defined" % name
418
+ cstr = self.context.insert_const_string(self.module, msg)
419
+ self.err_set_string("PyExc_NameError", cstr)
420
+
421
+ def fatal_error(self, msg):
422
+ fnty = ir.FunctionType(ir.VoidType(), [self.cstring])
423
+ fn = self._get_function(fnty, name="Py_FatalError")
424
+ fn.attributes.add("noreturn")
425
+ cstr = self.context.insert_const_string(self.module, msg)
426
+ self.builder.call(fn, (cstr,))
427
+
428
+ #
429
+ # Concrete dict API
430
+ #
431
+
432
+ def dict_getitem_string(self, dic, name):
433
+ """Lookup name inside dict
434
+
435
+ Returns a borrowed reference
436
+ """
437
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.cstring])
438
+ fn = self._get_function(fnty, name="PyDict_GetItemString")
439
+ cstr = self.context.insert_const_string(self.module, name)
440
+ return self.builder.call(fn, [dic, cstr])
441
+
442
+ def dict_getitem(self, dic, name):
443
+ """Lookup name inside dict
444
+
445
+ Returns a borrowed reference
446
+ """
447
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.pyobj])
448
+ fn = self._get_function(fnty, name="PyDict_GetItem")
449
+ return self.builder.call(fn, [dic, name])
450
+
451
+ def dict_new(self, presize=0):
452
+ if presize == 0:
453
+ fnty = ir.FunctionType(self.pyobj, ())
454
+ fn = self._get_function(fnty, name="PyDict_New")
455
+ return self.builder.call(fn, ())
456
+ else:
457
+ fnty = ir.FunctionType(self.pyobj, [self.py_ssize_t])
458
+ fn = self._get_function(fnty, name="_PyDict_NewPresized")
459
+ return self.builder.call(
460
+ fn, [Constant(self.py_ssize_t, int(presize))]
461
+ )
462
+
463
+ def dict_setitem(self, dictobj, nameobj, valobj):
464
+ fnty = ir.FunctionType(
465
+ ir.IntType(32), (self.pyobj, self.pyobj, self.pyobj)
466
+ )
467
+ fn = self._get_function(fnty, name="PyDict_SetItem")
468
+ return self.builder.call(fn, (dictobj, nameobj, valobj))
469
+
470
+ def dict_setitem_string(self, dictobj, name, valobj):
471
+ fnty = ir.FunctionType(
472
+ ir.IntType(32), (self.pyobj, self.cstring, self.pyobj)
473
+ )
474
+ fn = self._get_function(fnty, name="PyDict_SetItemString")
475
+ cstr = self.context.insert_const_string(self.module, name)
476
+ return self.builder.call(fn, (dictobj, cstr, valobj))
477
+
478
+ def dict_pack(self, keyvalues):
479
+ """
480
+ Args
481
+ -----
482
+ keyvalues: iterable of (str, llvm.Value of PyObject*)
483
+ """
484
+ dictobj = self.dict_new()
485
+ with self.if_object_ok(dictobj):
486
+ for k, v in keyvalues:
487
+ self.dict_setitem_string(dictobj, k, v)
488
+ return dictobj
489
+
490
+ #
491
+ # Concrete number APIs
492
+ #
493
+
494
+ def float_from_double(self, fval):
495
+ fnty = ir.FunctionType(self.pyobj, [self.double])
496
+ fn = self._get_function(fnty, name="PyFloat_FromDouble")
497
+ return self.builder.call(fn, [fval])
498
+
499
+ def number_as_ssize_t(self, numobj):
500
+ fnty = ir.FunctionType(self.py_ssize_t, [self.pyobj, self.pyobj])
501
+ fn = self._get_function(fnty, name="PyNumber_AsSsize_t")
502
+ # We don't want any clipping, so pass OverflowError as the 2nd arg
503
+ exc_class = self.get_c_object("PyExc_OverflowError")
504
+ return self.builder.call(fn, [numobj, exc_class])
505
+
506
+ def number_long(self, numobj):
507
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
508
+ fn = self._get_function(fnty, name="PyNumber_Long")
509
+ return self.builder.call(fn, [numobj])
510
+
511
+ def long_as_ulonglong(self, numobj):
512
+ fnty = ir.FunctionType(self.ulonglong, [self.pyobj])
513
+ fn = self._get_function(fnty, name="PyLong_AsUnsignedLongLong")
514
+ return self.builder.call(fn, [numobj])
515
+
516
+ def long_as_longlong(self, numobj):
517
+ fnty = ir.FunctionType(self.ulonglong, [self.pyobj])
518
+ fn = self._get_function(fnty, name="PyLong_AsLongLong")
519
+ return self.builder.call(fn, [numobj])
520
+
521
+ def long_as_voidptr(self, numobj):
522
+ """
523
+ Convert the given Python integer to a void*. This is recommended
524
+ over number_as_ssize_t as it isn't affected by signedness.
525
+ """
526
+ fnty = ir.FunctionType(self.voidptr, [self.pyobj])
527
+ fn = self._get_function(fnty, name="PyLong_AsVoidPtr")
528
+ return self.builder.call(fn, [numobj])
529
+
530
+ def _long_from_native_int(self, ival, func_name, native_int_type, signed):
531
+ fnty = ir.FunctionType(self.pyobj, [native_int_type])
532
+ fn = self._get_function(fnty, name=func_name)
533
+ resptr = cgutils.alloca_once(self.builder, self.pyobj)
534
+ fn = self._get_function(fnty, name=func_name)
535
+ self.builder.store(self.builder.call(fn, [ival]), resptr)
536
+
537
+ return self.builder.load(resptr)
538
+
539
+ def long_from_long(self, ival):
540
+ func_name = "PyLong_FromLong"
541
+ fnty = ir.FunctionType(self.pyobj, [self.long])
542
+ fn = self._get_function(fnty, name=func_name)
543
+ return self.builder.call(fn, [ival])
544
+
545
+ def long_from_ulong(self, ival):
546
+ return self._long_from_native_int(
547
+ ival, "PyLong_FromUnsignedLong", self.long, signed=False
548
+ )
549
+
550
+ def long_from_ssize_t(self, ival):
551
+ return self._long_from_native_int(
552
+ ival, "PyLong_FromSsize_t", self.py_ssize_t, signed=True
553
+ )
554
+
555
+ def long_from_longlong(self, ival):
556
+ return self._long_from_native_int(
557
+ ival, "PyLong_FromLongLong", self.longlong, signed=True
558
+ )
559
+
560
+ def long_from_ulonglong(self, ival):
561
+ return self._long_from_native_int(
562
+ ival, "PyLong_FromUnsignedLongLong", self.ulonglong, signed=False
563
+ )
564
+
565
+ def long_from_signed_int(self, ival):
566
+ """
567
+ Return a Python integer from any native integer value.
568
+ """
569
+ bits = ival.type.width
570
+ if bits <= self.long.width:
571
+ return self.long_from_long(self.builder.sext(ival, self.long))
572
+ elif bits <= self.longlong.width:
573
+ return self.long_from_longlong(
574
+ self.builder.sext(ival, self.longlong)
575
+ )
576
+ else:
577
+ raise OverflowError("integer too big (%d bits)" % (bits))
578
+
579
+ def long_from_unsigned_int(self, ival):
580
+ """
581
+ Same as long_from_signed_int, but for unsigned values.
582
+ """
583
+ bits = ival.type.width
584
+ if bits <= self.ulong.width:
585
+ return self.long_from_ulong(self.builder.zext(ival, self.ulong))
586
+ elif bits <= self.ulonglong.width:
587
+ return self.long_from_ulonglong(
588
+ self.builder.zext(ival, self.ulonglong)
589
+ )
590
+ else:
591
+ raise OverflowError("integer too big (%d bits)" % (bits))
592
+
593
+ def _get_number_operator(self, name):
594
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.pyobj])
595
+ fn = self._get_function(fnty, name="PyNumber_%s" % name)
596
+ return fn
597
+
598
+ def _call_number_operator(self, name, lhs, rhs, inplace=False):
599
+ if inplace:
600
+ name = "InPlace" + name
601
+ fn = self._get_number_operator(name)
602
+ return self.builder.call(fn, [lhs, rhs])
603
+
604
+ def number_add(self, lhs, rhs, inplace=False):
605
+ return self._call_number_operator("Add", lhs, rhs, inplace=inplace)
606
+
607
+ def number_subtract(self, lhs, rhs, inplace=False):
608
+ return self._call_number_operator("Subtract", lhs, rhs, inplace=inplace)
609
+
610
+ def number_multiply(self, lhs, rhs, inplace=False):
611
+ return self._call_number_operator("Multiply", lhs, rhs, inplace=inplace)
612
+
613
+ def number_truedivide(self, lhs, rhs, inplace=False):
614
+ return self._call_number_operator(
615
+ "TrueDivide", lhs, rhs, inplace=inplace
616
+ )
617
+
618
+ def number_floordivide(self, lhs, rhs, inplace=False):
619
+ return self._call_number_operator(
620
+ "FloorDivide", lhs, rhs, inplace=inplace
621
+ )
622
+
623
+ def number_remainder(self, lhs, rhs, inplace=False):
624
+ return self._call_number_operator(
625
+ "Remainder", lhs, rhs, inplace=inplace
626
+ )
627
+
628
+ def number_matrix_multiply(self, lhs, rhs, inplace=False):
629
+ return self._call_number_operator(
630
+ "MatrixMultiply", lhs, rhs, inplace=inplace
631
+ )
632
+
633
+ def number_lshift(self, lhs, rhs, inplace=False):
634
+ return self._call_number_operator("Lshift", lhs, rhs, inplace=inplace)
635
+
636
+ def number_rshift(self, lhs, rhs, inplace=False):
637
+ return self._call_number_operator("Rshift", lhs, rhs, inplace=inplace)
638
+
639
+ def number_and(self, lhs, rhs, inplace=False):
640
+ return self._call_number_operator("And", lhs, rhs, inplace=inplace)
641
+
642
+ def number_or(self, lhs, rhs, inplace=False):
643
+ return self._call_number_operator("Or", lhs, rhs, inplace=inplace)
644
+
645
+ def number_xor(self, lhs, rhs, inplace=False):
646
+ return self._call_number_operator("Xor", lhs, rhs, inplace=inplace)
647
+
648
+ def number_power(self, lhs, rhs, inplace=False):
649
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj] * 3)
650
+ fname = "PyNumber_InPlacePower" if inplace else "PyNumber_Power"
651
+ fn = self._get_function(fnty, fname)
652
+ return self.builder.call(fn, [lhs, rhs, self.borrow_none()])
653
+
654
+ def number_negative(self, obj):
655
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
656
+ fn = self._get_function(fnty, name="PyNumber_Negative")
657
+ return self.builder.call(fn, (obj,))
658
+
659
+ def number_positive(self, obj):
660
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
661
+ fn = self._get_function(fnty, name="PyNumber_Positive")
662
+ return self.builder.call(fn, (obj,))
663
+
664
+ def number_float(self, val):
665
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
666
+ fn = self._get_function(fnty, name="PyNumber_Float")
667
+ return self.builder.call(fn, [val])
668
+
669
+ def number_invert(self, obj):
670
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
671
+ fn = self._get_function(fnty, name="PyNumber_Invert")
672
+ return self.builder.call(fn, (obj,))
673
+
674
+ def float_as_double(self, fobj):
675
+ fnty = ir.FunctionType(self.double, [self.pyobj])
676
+ fn = self._get_function(fnty, name="PyFloat_AsDouble")
677
+ return self.builder.call(fn, [fobj])
678
+
679
+ def bool_from_bool(self, bval):
680
+ """
681
+ Get a Python bool from a LLVM boolean.
682
+ """
683
+ longval = self.builder.zext(bval, self.long)
684
+ return self.bool_from_long(longval)
685
+
686
+ def bool_from_long(self, ival):
687
+ fnty = ir.FunctionType(self.pyobj, [self.long])
688
+ fn = self._get_function(fnty, name="PyBool_FromLong")
689
+ return self.builder.call(fn, [ival])
690
+
691
+ def complex_from_doubles(self, realval, imagval):
692
+ fnty = ir.FunctionType(self.pyobj, [ir.DoubleType(), ir.DoubleType()])
693
+ fn = self._get_function(fnty, name="PyComplex_FromDoubles")
694
+ return self.builder.call(fn, [realval, imagval])
695
+
696
+ def complex_real_as_double(self, cobj):
697
+ fnty = ir.FunctionType(ir.DoubleType(), [self.pyobj])
698
+ fn = self._get_function(fnty, name="PyComplex_RealAsDouble")
699
+ return self.builder.call(fn, [cobj])
700
+
701
+ def complex_imag_as_double(self, cobj):
702
+ fnty = ir.FunctionType(ir.DoubleType(), [self.pyobj])
703
+ fn = self._get_function(fnty, name="PyComplex_ImagAsDouble")
704
+ return self.builder.call(fn, [cobj])
705
+
706
+ #
707
+ # Concrete slice API
708
+ #
709
+ def slice_as_ints(self, obj):
710
+ """
711
+ Read the members of a slice of integers.
712
+
713
+ Returns a (ok, start, stop, step) tuple where ok is a boolean and
714
+ the following members are pointer-sized ints.
715
+ """
716
+ pstart = cgutils.alloca_once(self.builder, self.py_ssize_t)
717
+ pstop = cgutils.alloca_once(self.builder, self.py_ssize_t)
718
+ pstep = cgutils.alloca_once(self.builder, self.py_ssize_t)
719
+ fnty = ir.FunctionType(
720
+ ir.IntType(32), [self.pyobj] + [self.py_ssize_t.as_pointer()] * 3
721
+ )
722
+ fn = self._get_function(fnty, name="numba_unpack_slice")
723
+ res = self.builder.call(fn, (obj, pstart, pstop, pstep))
724
+ start = self.builder.load(pstart)
725
+ stop = self.builder.load(pstop)
726
+ step = self.builder.load(pstep)
727
+ return cgutils.is_null(self.builder, res), start, stop, step
728
+
729
+ #
730
+ # List and sequence APIs
731
+ #
732
+
733
+ def sequence_getslice(self, obj, start, stop):
734
+ fnty = ir.FunctionType(
735
+ self.pyobj, [self.pyobj, self.py_ssize_t, self.py_ssize_t]
736
+ )
737
+ fn = self._get_function(fnty, name="PySequence_GetSlice")
738
+ return self.builder.call(fn, (obj, start, stop))
739
+
740
+ def sequence_tuple(self, obj):
741
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
742
+ fn = self._get_function(fnty, name="PySequence_Tuple")
743
+ return self.builder.call(fn, [obj])
744
+
745
+ def sequence_concat(self, obj1, obj2):
746
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.pyobj])
747
+ fn = self._get_function(fnty, name="PySequence_Concat")
748
+ return self.builder.call(fn, [obj1, obj2])
749
+
750
+ def list_new(self, szval):
751
+ fnty = ir.FunctionType(self.pyobj, [self.py_ssize_t])
752
+ fn = self._get_function(fnty, name="PyList_New")
753
+ return self.builder.call(fn, [szval])
754
+
755
+ def list_size(self, lst):
756
+ fnty = ir.FunctionType(self.py_ssize_t, [self.pyobj])
757
+ fn = self._get_function(fnty, name="PyList_Size")
758
+ return self.builder.call(fn, [lst])
759
+
760
+ def list_append(self, lst, val):
761
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.pyobj])
762
+ fn = self._get_function(fnty, name="PyList_Append")
763
+ return self.builder.call(fn, [lst, val])
764
+
765
+ def list_setitem(self, lst, idx, val):
766
+ """
767
+ Warning: Steals reference to ``val``
768
+ """
769
+ fnty = ir.FunctionType(
770
+ ir.IntType(32), [self.pyobj, self.py_ssize_t, self.pyobj]
771
+ )
772
+ fn = self._get_function(fnty, name="PyList_SetItem")
773
+ return self.builder.call(fn, [lst, idx, val])
774
+
775
+ def list_getitem(self, lst, idx):
776
+ """
777
+ Returns a borrowed reference.
778
+ """
779
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.py_ssize_t])
780
+ fn = self._get_function(fnty, name="PyList_GetItem")
781
+ if isinstance(idx, int):
782
+ idx = self.context.get_constant(types.intp, idx)
783
+ return self.builder.call(fn, [lst, idx])
784
+
785
+ def list_setslice(self, lst, start, stop, obj):
786
+ if obj is None:
787
+ obj = self.get_null_object()
788
+ fnty = ir.FunctionType(
789
+ ir.IntType(32),
790
+ [self.pyobj, self.py_ssize_t, self.py_ssize_t, self.pyobj],
791
+ )
792
+ fn = self._get_function(fnty, name="PyList_SetSlice")
793
+ return self.builder.call(fn, (lst, start, stop, obj))
794
+
795
+ #
796
+ # Concrete tuple API
797
+ #
798
+
799
+ def tuple_getitem(self, tup, idx):
800
+ """
801
+ Borrow reference
802
+ """
803
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.py_ssize_t])
804
+ fn = self._get_function(fnty, name="PyTuple_GetItem")
805
+ idx = self.context.get_constant(types.intp, idx)
806
+ return self.builder.call(fn, [tup, idx])
807
+
808
+ def tuple_pack(self, items):
809
+ fnty = ir.FunctionType(self.pyobj, [self.py_ssize_t], var_arg=True)
810
+ fn = self._get_function(fnty, name="PyTuple_Pack")
811
+ n = self.context.get_constant(types.intp, len(items))
812
+ args = [n]
813
+ args.extend(items)
814
+ return self.builder.call(fn, args)
815
+
816
+ def tuple_size(self, tup):
817
+ fnty = ir.FunctionType(self.py_ssize_t, [self.pyobj])
818
+ fn = self._get_function(fnty, name="PyTuple_Size")
819
+ return self.builder.call(fn, [tup])
820
+
821
+ def tuple_new(self, count):
822
+ fnty = ir.FunctionType(self.pyobj, [self.py_ssize_t])
823
+ fn = self._get_function(fnty, name="PyTuple_New")
824
+ return self.builder.call(fn, [self.py_ssize_t(count)])
825
+
826
+ def tuple_setitem(self, tuple_val, index, item):
827
+ """
828
+ Steals a reference to `item`.
829
+ """
830
+ fnty = ir.FunctionType(
831
+ ir.IntType(32), [self.pyobj, self.py_ssize_t, self.pyobj]
832
+ )
833
+ setitem_fn = self._get_function(fnty, name="PyTuple_SetItem")
834
+ index = self.py_ssize_t(index)
835
+ self.builder.call(setitem_fn, [tuple_val, index, item])
836
+
837
+ #
838
+ # Concrete set API
839
+ #
840
+
841
+ def set_new(self, iterable=None):
842
+ if iterable is None:
843
+ iterable = self.get_null_object()
844
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
845
+ fn = self._get_function(fnty, name="PySet_New")
846
+ return self.builder.call(fn, [iterable])
847
+
848
+ def set_add(self, set, value):
849
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.pyobj])
850
+ fn = self._get_function(fnty, name="PySet_Add")
851
+ return self.builder.call(fn, [set, value])
852
+
853
+ def set_clear(self, set):
854
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj])
855
+ fn = self._get_function(fnty, name="PySet_Clear")
856
+ return self.builder.call(fn, [set])
857
+
858
+ def set_size(self, set):
859
+ fnty = ir.FunctionType(self.py_ssize_t, [self.pyobj])
860
+ fn = self._get_function(fnty, name="PySet_Size")
861
+ return self.builder.call(fn, [set])
862
+
863
+ def set_update(self, set, iterable):
864
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.pyobj])
865
+ fn = self._get_function(fnty, name="_PySet_Update")
866
+ return self.builder.call(fn, [set, iterable])
867
+
868
+ def set_next_entry(self, set, posptr, keyptr, hashptr):
869
+ fnty = ir.FunctionType(
870
+ ir.IntType(32),
871
+ [
872
+ self.pyobj,
873
+ self.py_ssize_t.as_pointer(),
874
+ self.pyobj.as_pointer(),
875
+ self.py_hash_t.as_pointer(),
876
+ ],
877
+ )
878
+ fn = self._get_function(fnty, name="_PySet_NextEntry")
879
+ return self.builder.call(fn, (set, posptr, keyptr, hashptr))
880
+
881
+ @contextlib.contextmanager
882
+ def set_iterate(self, set):
883
+ builder = self.builder
884
+
885
+ hashptr = cgutils.alloca_once(builder, self.py_hash_t, name="hashptr")
886
+ keyptr = cgutils.alloca_once(builder, self.pyobj, name="keyptr")
887
+ posptr = cgutils.alloca_once_value(
888
+ builder, Constant(self.py_ssize_t, 0), name="posptr"
889
+ )
890
+
891
+ bb_body = builder.append_basic_block("bb_body")
892
+ bb_end = builder.append_basic_block("bb_end")
893
+
894
+ builder.branch(bb_body)
895
+
896
+ def do_break():
897
+ builder.branch(bb_end)
898
+
899
+ with builder.goto_block(bb_body):
900
+ r = self.set_next_entry(set, posptr, keyptr, hashptr)
901
+ finished = cgutils.is_null(builder, r)
902
+ with builder.if_then(finished, likely=False):
903
+ builder.branch(bb_end)
904
+ yield _IteratorLoop(builder.load(keyptr), do_break)
905
+ builder.branch(bb_body)
906
+
907
+ builder.position_at_end(bb_end)
908
+
909
+ #
910
+ # GIL APIs
911
+ #
912
+
913
+ def gil_ensure(self):
914
+ """
915
+ Ensure the GIL is acquired.
916
+ The returned value must be consumed by gil_release().
917
+ """
918
+ gilptrty = ir.PointerType(self.gil_state)
919
+ fnty = ir.FunctionType(ir.VoidType(), [gilptrty])
920
+ fn = self._get_function(fnty, "numba_gil_ensure")
921
+ gilptr = cgutils.alloca_once(self.builder, self.gil_state)
922
+ self.builder.call(fn, [gilptr])
923
+ return gilptr
924
+
925
+ def gil_release(self, gil):
926
+ """
927
+ Release the acquired GIL by gil_ensure().
928
+ Must be paired with a gil_ensure().
929
+ """
930
+ gilptrty = ir.PointerType(self.gil_state)
931
+ fnty = ir.FunctionType(ir.VoidType(), [gilptrty])
932
+ fn = self._get_function(fnty, "numba_gil_release")
933
+ return self.builder.call(fn, [gil])
934
+
935
+ def save_thread(self):
936
+ """
937
+ Release the GIL and return the former thread state
938
+ (an opaque non-NULL pointer).
939
+ """
940
+ fnty = ir.FunctionType(self.voidptr, [])
941
+ fn = self._get_function(fnty, name="PyEval_SaveThread")
942
+ return self.builder.call(fn, [])
943
+
944
+ def restore_thread(self, thread_state):
945
+ """
946
+ Restore the given thread state by reacquiring the GIL.
947
+ """
948
+ fnty = ir.FunctionType(ir.VoidType(), [self.voidptr])
949
+ fn = self._get_function(fnty, name="PyEval_RestoreThread")
950
+ self.builder.call(fn, [thread_state])
951
+
952
+ #
953
+ # Generic object private data (a way of associating an arbitrary void *
954
+ # pointer to an arbitrary Python object).
955
+ #
956
+
957
+ def object_get_private_data(self, obj):
958
+ fnty = ir.FunctionType(self.voidptr, [self.pyobj])
959
+ fn = self._get_function(fnty, name="numba_get_pyobject_private_data")
960
+ return self.builder.call(fn, (obj,))
961
+
962
+ def object_set_private_data(self, obj, ptr):
963
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj, self.voidptr])
964
+ fn = self._get_function(fnty, name="numba_set_pyobject_private_data")
965
+ return self.builder.call(fn, (obj, ptr))
966
+
967
+ def object_reset_private_data(self, obj):
968
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
969
+ fn = self._get_function(fnty, name="numba_reset_pyobject_private_data")
970
+ return self.builder.call(fn, (obj,))
971
+
972
+ #
973
+ # Other APIs (organize them better!)
974
+ #
975
+
976
+ def import_module(self, modname):
977
+ fnty = ir.FunctionType(self.pyobj, [self.cstring])
978
+ fn = self._get_function(fnty, name="PyImport_ImportModule")
979
+ return self.builder.call(fn, [modname])
980
+
981
+ def call_function_objargs(self, callee, objargs):
982
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj], var_arg=True)
983
+ fn = self._get_function(fnty, name="PyObject_CallFunctionObjArgs")
984
+ args = [callee] + list(objargs)
985
+ args.append(self.context.get_constant_null(types.pyobject))
986
+ return self.builder.call(fn, args)
987
+
988
+ def call_method(self, callee, method, objargs=()):
989
+ cname = self.context.insert_const_string(self.module, method)
990
+ fnty = ir.FunctionType(
991
+ self.pyobj, [self.pyobj, self.cstring, self.cstring], var_arg=True
992
+ )
993
+ fn = self._get_function(fnty, name="PyObject_CallMethod")
994
+ fmt = "O" * len(objargs)
995
+ cfmt = self.context.insert_const_string(self.module, fmt)
996
+ args = [callee, cname, cfmt]
997
+ if objargs:
998
+ args.extend(objargs)
999
+ args.append(self.context.get_constant_null(types.pyobject))
1000
+ return self.builder.call(fn, args)
1001
+
1002
+ def call(self, callee, args=None, kws=None):
1003
+ if args_was_none := args is None:
1004
+ args = self.tuple_new(0)
1005
+ if kws is None:
1006
+ kws = self.get_null_object()
1007
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj] * 3)
1008
+ fn = self._get_function(fnty, name="PyObject_Call")
1009
+ result = self.builder.call(fn, (callee, args, kws))
1010
+ if args_was_none:
1011
+ self.decref(args)
1012
+ return result
1013
+
1014
+ def object_type(self, obj):
1015
+ """Emit a call to ``PyObject_Type(obj)`` to get the type of ``obj``."""
1016
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
1017
+ fn = self._get_function(fnty, name="PyObject_Type")
1018
+ return self.builder.call(fn, (obj,))
1019
+
1020
+ def object_istrue(self, obj):
1021
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj])
1022
+ fn = self._get_function(fnty, name="PyObject_IsTrue")
1023
+ return self.builder.call(fn, [obj])
1024
+
1025
+ def object_not(self, obj):
1026
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj])
1027
+ fn = self._get_function(fnty, name="PyObject_Not")
1028
+ return self.builder.call(fn, [obj])
1029
+
1030
+ def object_richcompare(self, lhs, rhs, opstr):
1031
+ """
1032
+ Refer to Python source Include/object.h for macros definition
1033
+ of the opid.
1034
+ """
1035
+ ops = ["<", "<=", "==", "!=", ">", ">="]
1036
+ if opstr in ops:
1037
+ opid = ops.index(opstr)
1038
+ fnty = ir.FunctionType(
1039
+ self.pyobj, [self.pyobj, self.pyobj, ir.IntType(32)]
1040
+ )
1041
+ fn = self._get_function(fnty, name="PyObject_RichCompare")
1042
+ lopid = self.context.get_constant(types.int32, opid)
1043
+ return self.builder.call(fn, (lhs, rhs, lopid))
1044
+ elif opstr == "is":
1045
+ bitflag = self.builder.icmp_unsigned("==", lhs, rhs)
1046
+ return self.bool_from_bool(bitflag)
1047
+ elif opstr == "is not":
1048
+ bitflag = self.builder.icmp_unsigned("!=", lhs, rhs)
1049
+ return self.bool_from_bool(bitflag)
1050
+ elif opstr in ("in", "not in"):
1051
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.pyobj])
1052
+ fn = self._get_function(fnty, name="PySequence_Contains")
1053
+ status = self.builder.call(fn, (rhs, lhs))
1054
+ negone = self.context.get_constant(types.int32, -1)
1055
+ is_good = self.builder.icmp_unsigned("!=", status, negone)
1056
+ # Stack allocate output and initialize to Null
1057
+ outptr = cgutils.alloca_once_value(
1058
+ self.builder, Constant(self.pyobj, None)
1059
+ )
1060
+ # If PySequence_Contains returns non-error value
1061
+ with cgutils.if_likely(self.builder, is_good):
1062
+ if opstr == "not in":
1063
+ status = self.builder.not_(status)
1064
+ # Store the status as a boolean object
1065
+ truncated = self.builder.trunc(status, ir.IntType(1))
1066
+ self.builder.store(self.bool_from_bool(truncated), outptr)
1067
+
1068
+ return self.builder.load(outptr)
1069
+ else:
1070
+ raise NotImplementedError(
1071
+ "Unknown operator {op!r}".format(op=opstr)
1072
+ )
1073
+
1074
+ def iter_next(self, iterobj):
1075
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
1076
+ fn = self._get_function(fnty, name="PyIter_Next")
1077
+ return self.builder.call(fn, [iterobj])
1078
+
1079
+ def object_getiter(self, obj):
1080
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
1081
+ fn = self._get_function(fnty, name="PyObject_GetIter")
1082
+ return self.builder.call(fn, [obj])
1083
+
1084
+ def object_getattr_string(self, obj, attr):
1085
+ cstr = self.context.insert_const_string(self.module, attr)
1086
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.cstring])
1087
+ fn = self._get_function(fnty, name="PyObject_GetAttrString")
1088
+ return self.builder.call(fn, [obj, cstr])
1089
+
1090
+ def object_getattr(self, obj, attr):
1091
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.pyobj])
1092
+ fn = self._get_function(fnty, name="PyObject_GetAttr")
1093
+ return self.builder.call(fn, [obj, attr])
1094
+
1095
+ def object_setattr_string(self, obj, attr, val):
1096
+ cstr = self.context.insert_const_string(self.module, attr)
1097
+ fnty = ir.FunctionType(
1098
+ ir.IntType(32), [self.pyobj, self.cstring, self.pyobj]
1099
+ )
1100
+ fn = self._get_function(fnty, name="PyObject_SetAttrString")
1101
+ return self.builder.call(fn, [obj, cstr, val])
1102
+
1103
+ def object_setattr(self, obj, attr, val):
1104
+ fnty = ir.FunctionType(
1105
+ ir.IntType(32), [self.pyobj, self.pyobj, self.pyobj]
1106
+ )
1107
+ fn = self._get_function(fnty, name="PyObject_SetAttr")
1108
+ return self.builder.call(fn, [obj, attr, val])
1109
+
1110
+ def object_delattr_string(self, obj, attr):
1111
+ # PyObject_DelAttrString() is actually a C macro calling
1112
+ # PyObject_SetAttrString() with value == NULL.
1113
+ return self.object_setattr_string(obj, attr, self.get_null_object())
1114
+
1115
+ def object_delattr(self, obj, attr):
1116
+ # PyObject_DelAttr() is actually a C macro calling
1117
+ # PyObject_SetAttr() with value == NULL.
1118
+ return self.object_setattr(obj, attr, self.get_null_object())
1119
+
1120
+ def object_getitem(self, obj, key):
1121
+ """
1122
+ Return obj[key]
1123
+ """
1124
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj, self.pyobj])
1125
+ fn = self._get_function(fnty, name="PyObject_GetItem")
1126
+ return self.builder.call(fn, (obj, key))
1127
+
1128
+ def object_setitem(self, obj, key, val):
1129
+ """
1130
+ obj[key] = val
1131
+ """
1132
+ fnty = ir.FunctionType(
1133
+ ir.IntType(32), [self.pyobj, self.pyobj, self.pyobj]
1134
+ )
1135
+ fn = self._get_function(fnty, name="PyObject_SetItem")
1136
+ return self.builder.call(fn, (obj, key, val))
1137
+
1138
+ def object_delitem(self, obj, key):
1139
+ """
1140
+ del obj[key]
1141
+ """
1142
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.pyobj])
1143
+ fn = self._get_function(fnty, name="PyObject_DelItem")
1144
+ return self.builder.call(fn, (obj, key))
1145
+
1146
+ def string_as_string(self, strobj):
1147
+ fnty = ir.FunctionType(self.cstring, [self.pyobj])
1148
+ fname = "PyUnicode_AsUTF8"
1149
+ fn = self._get_function(fnty, name=fname)
1150
+ return self.builder.call(fn, [strobj])
1151
+
1152
+ def string_as_string_and_size(self, strobj):
1153
+ """
1154
+ Returns a tuple of ``(ok, buffer, length)``.
1155
+ The ``ok`` is i1 value that is set if ok.
1156
+ The ``buffer`` is a i8* of the output buffer.
1157
+ The ``length`` is a i32/i64 (py_ssize_t) of the length of the buffer.
1158
+ """
1159
+
1160
+ p_length = cgutils.alloca_once(self.builder, self.py_ssize_t)
1161
+ fnty = ir.FunctionType(
1162
+ self.cstring, [self.pyobj, self.py_ssize_t.as_pointer()]
1163
+ )
1164
+ fname = "PyUnicode_AsUTF8AndSize"
1165
+ fn = self._get_function(fnty, name=fname)
1166
+
1167
+ buffer = self.builder.call(fn, [strobj, p_length])
1168
+ ok = self.builder.icmp_unsigned(
1169
+ "!=", Constant(buffer.type, None), buffer
1170
+ )
1171
+ return (ok, buffer, self.builder.load(p_length))
1172
+
1173
+ def string_as_string_size_and_kind(self, strobj):
1174
+ """
1175
+ Returns a tuple of ``(ok, buffer, length, kind)``.
1176
+ The ``ok`` is i1 value that is set if ok.
1177
+ The ``buffer`` is a i8* of the output buffer.
1178
+ The ``length`` is a i32/i64 (py_ssize_t) of the length of the buffer.
1179
+ The ``kind`` is a i32 (int32) of the Unicode kind constant
1180
+ The ``hash`` is a long/uint64_t (py_hash_t) of the Unicode constant hash
1181
+ """
1182
+ p_length = cgutils.alloca_once(self.builder, self.py_ssize_t)
1183
+ p_kind = cgutils.alloca_once(self.builder, ir.IntType(32))
1184
+ p_ascii = cgutils.alloca_once(self.builder, ir.IntType(32))
1185
+ p_hash = cgutils.alloca_once(self.builder, self.py_hash_t)
1186
+ fnty = ir.FunctionType(
1187
+ self.cstring,
1188
+ [
1189
+ self.pyobj,
1190
+ self.py_ssize_t.as_pointer(),
1191
+ ir.IntType(32).as_pointer(),
1192
+ ir.IntType(32).as_pointer(),
1193
+ self.py_hash_t.as_pointer(),
1194
+ ],
1195
+ )
1196
+ fname = "numba_extract_unicode"
1197
+ fn = self._get_function(fnty, name=fname)
1198
+
1199
+ buffer = self.builder.call(
1200
+ fn, [strobj, p_length, p_kind, p_ascii, p_hash]
1201
+ )
1202
+ ok = self.builder.icmp_unsigned(
1203
+ "!=", Constant(buffer.type, None), buffer
1204
+ )
1205
+ return (
1206
+ ok,
1207
+ buffer,
1208
+ self.builder.load(p_length),
1209
+ self.builder.load(p_kind),
1210
+ self.builder.load(p_ascii),
1211
+ self.builder.load(p_hash),
1212
+ )
1213
+
1214
+ def string_from_string_and_size(self, string, size):
1215
+ fnty = ir.FunctionType(self.pyobj, [self.cstring, self.py_ssize_t])
1216
+ fname = "PyString_FromStringAndSize"
1217
+ fn = self._get_function(fnty, name=fname)
1218
+ return self.builder.call(fn, [string, size])
1219
+
1220
+ def string_from_string(self, string):
1221
+ fnty = ir.FunctionType(self.pyobj, [self.cstring])
1222
+ fname = "PyUnicode_FromString"
1223
+ fn = self._get_function(fnty, name=fname)
1224
+ return self.builder.call(fn, [string])
1225
+
1226
+ def string_from_kind_and_data(self, kind, string, size):
1227
+ fnty = ir.FunctionType(
1228
+ self.pyobj, [ir.IntType(32), self.cstring, self.py_ssize_t]
1229
+ )
1230
+ fname = "PyUnicode_FromKindAndData"
1231
+ fn = self._get_function(fnty, name=fname)
1232
+ return self.builder.call(fn, [kind, string, size])
1233
+
1234
+ def bytes_as_string(self, obj):
1235
+ fnty = ir.FunctionType(self.cstring, [self.pyobj])
1236
+ fname = "PyBytes_AsString"
1237
+ fn = self._get_function(fnty, name=fname)
1238
+ return self.builder.call(fn, [obj])
1239
+
1240
+ def bytes_as_string_and_size(self, obj, p_buffer, p_length):
1241
+ fnty = ir.FunctionType(
1242
+ ir.IntType(32),
1243
+ [
1244
+ self.pyobj,
1245
+ self.cstring.as_pointer(),
1246
+ self.py_ssize_t.as_pointer(),
1247
+ ],
1248
+ )
1249
+ fname = "PyBytes_AsStringAndSize"
1250
+ fn = self._get_function(fnty, name=fname)
1251
+ result = self.builder.call(fn, [obj, p_buffer, p_length])
1252
+ ok = self.builder.icmp_signed("!=", Constant(result.type, -1), result)
1253
+ return ok
1254
+
1255
+ def bytes_from_string_and_size(self, string, size):
1256
+ fnty = ir.FunctionType(self.pyobj, [self.cstring, self.py_ssize_t])
1257
+ fname = "PyBytes_FromStringAndSize"
1258
+ fn = self._get_function(fnty, name=fname)
1259
+ return self.builder.call(fn, [string, size])
1260
+
1261
+ def object_hash(self, obj):
1262
+ fnty = ir.FunctionType(
1263
+ self.py_hash_t,
1264
+ [
1265
+ self.pyobj,
1266
+ ],
1267
+ )
1268
+ fname = "PyObject_Hash"
1269
+ fn = self._get_function(fnty, name=fname)
1270
+ return self.builder.call(
1271
+ fn,
1272
+ [
1273
+ obj,
1274
+ ],
1275
+ )
1276
+
1277
+ def object_str(self, obj):
1278
+ fnty = ir.FunctionType(self.pyobj, [self.pyobj])
1279
+ fn = self._get_function(fnty, name="PyObject_Str")
1280
+ return self.builder.call(fn, [obj])
1281
+
1282
+ def make_none(self):
1283
+ obj = self.borrow_none()
1284
+ self.incref(obj)
1285
+ return obj
1286
+
1287
+ def borrow_none(self):
1288
+ return self.get_c_object("_Py_NoneStruct")
1289
+
1290
+ def sys_write_stdout(self, fmt, *args):
1291
+ fnty = ir.FunctionType(ir.VoidType(), [self.cstring], var_arg=True)
1292
+ fn = self._get_function(fnty, name="PySys_FormatStdout")
1293
+ return self.builder.call(fn, (fmt,) + args)
1294
+
1295
+ def object_dump(self, obj):
1296
+ """
1297
+ Dump a Python object on C stderr. For debugging purposes.
1298
+ """
1299
+ fnty = ir.FunctionType(ir.VoidType(), [self.pyobj])
1300
+ fn = self._get_function(fnty, name="_PyObject_Dump")
1301
+ return self.builder.call(fn, (obj,))
1302
+
1303
+ #
1304
+ # NRT (Numba runtime) APIs
1305
+ #
1306
+
1307
+ def nrt_adapt_ndarray_to_python(self, aryty, ary, dtypeptr):
1308
+ assert self.context.enable_nrt, "NRT required"
1309
+
1310
+ intty = ir.IntType(32)
1311
+ # Embed the Python type of the array (maybe subclass) in the LLVM IR.
1312
+ serial_aryty_pytype = self.unserialize(
1313
+ self.serialize_object(aryty.box_type)
1314
+ )
1315
+
1316
+ fnty = ir.FunctionType(
1317
+ self.pyobj, [self.voidptr, self.pyobj, intty, intty, self.pyobj]
1318
+ )
1319
+ fn = self._get_function(fnty, name="NRT_adapt_ndarray_to_python_acqref")
1320
+ fn.args[0].add_attribute("nocapture")
1321
+
1322
+ ndim = self.context.get_constant(types.int32, aryty.ndim)
1323
+ writable = self.context.get_constant(types.int32, int(aryty.mutable))
1324
+
1325
+ aryptr = cgutils.alloca_once_value(self.builder, ary)
1326
+ return self.builder.call(
1327
+ fn,
1328
+ [
1329
+ self.builder.bitcast(aryptr, self.voidptr),
1330
+ serial_aryty_pytype,
1331
+ ndim,
1332
+ writable,
1333
+ dtypeptr,
1334
+ ],
1335
+ )
1336
+
1337
+ def nrt_meminfo_new_from_pyobject(self, data, pyobj):
1338
+ """
1339
+ Allocate a new MemInfo with data payload borrowed from a python
1340
+ object.
1341
+ """
1342
+ mod = self.builder.module
1343
+ fnty = ir.FunctionType(
1344
+ cgutils.voidptr_t,
1345
+ [cgutils.voidptr_t, cgutils.voidptr_t],
1346
+ )
1347
+ fn = cgutils.get_or_insert_function(
1348
+ mod,
1349
+ fnty,
1350
+ "NRT_meminfo_new_from_pyobject",
1351
+ )
1352
+ fn.args[0].add_attribute("nocapture")
1353
+ fn.args[1].add_attribute("nocapture")
1354
+ fn.return_value.add_attribute("noalias")
1355
+ return self.builder.call(fn, [data, pyobj])
1356
+
1357
+ def nrt_meminfo_as_pyobject(self, miptr):
1358
+ mod = self.builder.module
1359
+ fnty = ir.FunctionType(self.pyobj, [cgutils.voidptr_t])
1360
+ fn = cgutils.get_or_insert_function(
1361
+ mod,
1362
+ fnty,
1363
+ "NRT_meminfo_as_pyobject",
1364
+ )
1365
+ fn.return_value.add_attribute("noalias")
1366
+ return self.builder.call(fn, [miptr])
1367
+
1368
+ def nrt_meminfo_from_pyobject(self, miobj):
1369
+ mod = self.builder.module
1370
+ fnty = ir.FunctionType(cgutils.voidptr_t, [self.pyobj])
1371
+ fn = cgutils.get_or_insert_function(
1372
+ mod,
1373
+ fnty,
1374
+ "NRT_meminfo_from_pyobject",
1375
+ )
1376
+ fn.return_value.add_attribute("noalias")
1377
+ return self.builder.call(fn, [miobj])
1378
+
1379
+ def nrt_adapt_ndarray_from_python(self, ary, ptr):
1380
+ assert self.context.enable_nrt
1381
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.voidptr])
1382
+ fn = self._get_function(fnty, name="NRT_adapt_ndarray_from_python")
1383
+ fn.args[0].add_attribute("nocapture")
1384
+ fn.args[1].add_attribute("nocapture")
1385
+ return self.builder.call(fn, (ary, ptr))
1386
+
1387
+ def nrt_adapt_buffer_from_python(self, buf, ptr):
1388
+ assert self.context.enable_nrt
1389
+ fnty = ir.FunctionType(
1390
+ ir.VoidType(), [ir.PointerType(self.py_buffer_t), self.voidptr]
1391
+ )
1392
+ fn = self._get_function(fnty, name="NRT_adapt_buffer_from_python")
1393
+ fn.args[0].add_attribute("nocapture")
1394
+ fn.args[1].add_attribute("nocapture")
1395
+ return self.builder.call(fn, (buf, ptr))
1396
+
1397
+ # ------ utils -----
1398
+
1399
+ def _get_function(self, fnty, name):
1400
+ return cgutils.get_or_insert_function(self.module, fnty, name)
1401
+
1402
+ def alloca_obj(self):
1403
+ return self.builder.alloca(self.pyobj)
1404
+
1405
+ def alloca_buffer(self):
1406
+ """
1407
+ Return a pointer to a stack-allocated, zero-initialized Py_buffer.
1408
+ """
1409
+ # Treat the buffer as an opaque array of bytes
1410
+ ptr = cgutils.alloca_once_value(
1411
+ self.builder, Constant(self.py_buffer_t, None)
1412
+ )
1413
+ return ptr
1414
+
1415
+ @contextlib.contextmanager
1416
+ def if_object_ok(self, obj):
1417
+ with cgutils.if_likely(
1418
+ self.builder, cgutils.is_not_null(self.builder, obj)
1419
+ ):
1420
+ yield
1421
+
1422
+ def print_object(self, obj):
1423
+ strobj = self.object_str(obj)
1424
+ cstr = self.string_as_string(strobj)
1425
+ fmt = self.context.insert_const_string(self.module, "%s")
1426
+ self.sys_write_stdout(fmt, cstr)
1427
+ self.decref(strobj)
1428
+
1429
+ def print_string(self, text):
1430
+ fmt = self.context.insert_const_string(self.module, text)
1431
+ self.sys_write_stdout(fmt)
1432
+
1433
+ def get_null_object(self):
1434
+ return Constant(self.pyobj, None)
1435
+
1436
+ def return_none(self):
1437
+ none = self.make_none()
1438
+ self.builder.ret(none)
1439
+
1440
+ def list_pack(self, items):
1441
+ n = len(items)
1442
+ seq = self.list_new(self.context.get_constant(types.intp, n))
1443
+ with self.if_object_ok(seq):
1444
+ for i in range(n):
1445
+ idx = self.context.get_constant(types.intp, i)
1446
+ self.incref(items[i])
1447
+ self.list_setitem(seq, idx, items[i])
1448
+ return seq
1449
+
1450
+ def unserialize(self, structptr):
1451
+ """
1452
+ Unserialize some data. *structptr* should be a pointer to
1453
+ a {i8* data, i32 length, i8* hashbuf, i8* func_ptr, i32 alloc_flag}
1454
+ structure.
1455
+ """
1456
+ fnty = ir.FunctionType(
1457
+ self.pyobj, (self.voidptr, ir.IntType(32), self.voidptr)
1458
+ )
1459
+ fn = self._get_function(fnty, name="numba_unpickle")
1460
+ ptr = self.builder.extract_value(self.builder.load(structptr), 0)
1461
+ n = self.builder.extract_value(self.builder.load(structptr), 1)
1462
+ hashed = self.builder.extract_value(self.builder.load(structptr), 2)
1463
+ return self.builder.call(fn, (ptr, n, hashed))
1464
+
1465
+ def build_dynamic_excinfo_struct(self, struct_gv, exc_args):
1466
+ """
1467
+ Serialize some data at runtime. Returns a pointer to a python tuple
1468
+ (bytes_data, hash) where the first element is the serialized data as
1469
+ bytes and the second its hash.
1470
+ """
1471
+ fnty = ir.FunctionType(self.pyobj, (self.pyobj, self.pyobj))
1472
+ fn = self._get_function(fnty, name="numba_runtime_build_excinfo_struct")
1473
+ return self.builder.call(fn, (struct_gv, exc_args))
1474
+
1475
+ def serialize_uncached(self, obj):
1476
+ """
1477
+ Same as serialize_object(), but don't create a global variable,
1478
+ simply return a literal for structure:
1479
+ {i8* data, i32 length, i8* hashbuf, i8* func_ptr, i32 alloc_flag}
1480
+ """
1481
+ # First make the array constant
1482
+ data = serialize.dumps(obj)
1483
+ assert len(data) < 2**31
1484
+ name = ".const.pickledata.%s" % (
1485
+ id(obj) if config.DIFF_IR == 0 else "DIFF_IR"
1486
+ )
1487
+ bdata = cgutils.make_bytearray(data)
1488
+ # Make SHA1 hash on the pickled content
1489
+ # NOTE: update buffer size in numba_unpickle() when changing the
1490
+ # hash algorithm.
1491
+ hashed = cgutils.make_bytearray(hashlib.sha1(data).digest())
1492
+ arr = self.context.insert_unique_const(self.module, name, bdata)
1493
+ hasharr = self.context.insert_unique_const(
1494
+ self.module,
1495
+ f"{name}.sha1",
1496
+ hashed,
1497
+ )
1498
+ # Then populate the structure constant
1499
+ struct = Constant.literal_struct(
1500
+ [
1501
+ arr.bitcast(self.voidptr),
1502
+ Constant(ir.IntType(32), arr.type.pointee.count),
1503
+ hasharr.bitcast(self.voidptr),
1504
+ cgutils.get_null_value(self.voidptr),
1505
+ Constant(ir.IntType(32), 0),
1506
+ ]
1507
+ )
1508
+ return struct
1509
+
1510
+ def serialize_object(self, obj):
1511
+ """
1512
+ Serialize the given object in the bitcode, and return it
1513
+ as a pointer to a
1514
+ {i8* data, i32 length, i8* hashbuf, i8* fn_ptr, i32 alloc_flag},
1515
+ structure constant (suitable for passing to unserialize()).
1516
+ """
1517
+ try:
1518
+ gv = self.module.__serialized[obj]
1519
+ except KeyError:
1520
+ struct = self.serialize_uncached(obj)
1521
+ name = ".const.picklebuf.%s" % (
1522
+ id(obj) if config.DIFF_IR == 0 else "DIFF_IR"
1523
+ )
1524
+ gv = self.context.insert_unique_const(self.module, name, struct)
1525
+ # Make the id() (and hence the name) unique while populating the module.
1526
+ self.module.__serialized[obj] = gv
1527
+ return gv
1528
+
1529
+ def c_api_error(self):
1530
+ return cgutils.is_not_null(self.builder, self.err_occurred())
1531
+
1532
+ def to_native_value(self, typ, obj):
1533
+ """
1534
+ Unbox the Python object as the given Numba type.
1535
+ A NativeValue instance is returned.
1536
+ """
1537
+ from numba.cuda.core.boxing import unbox_unsupported
1538
+
1539
+ impl = _unboxers.lookup(typ.__class__, unbox_unsupported)
1540
+ c = _UnboxContext(self.context, self.builder, self)
1541
+ return impl(typ, obj, c)
1542
+
1543
+ def from_native_return(self, typ, val, env_manager):
1544
+ assert not isinstance(typ, types.Optional), (
1545
+ "callconv should have prevented the return of optional value"
1546
+ )
1547
+ out = self.from_native_value(typ, val, env_manager)
1548
+ return out
1549
+
1550
+ def from_native_value(self, typ, val, env_manager=None):
1551
+ """
1552
+ Box the native value of the given Numba type. A Python object
1553
+ pointer is returned (NULL if an error occurred).
1554
+ This method steals any native (NRT) reference embedded in *val*.
1555
+ """
1556
+ from numba.cuda.core.boxing import box_unsupported
1557
+
1558
+ impl = _boxers.lookup(typ.__class__, box_unsupported)
1559
+
1560
+ c = _BoxContext(self.context, self.builder, self, env_manager)
1561
+ return impl(typ, val, c)
1562
+
1563
+ def reflect_native_value(self, typ, val, env_manager=None):
1564
+ """
1565
+ Reflect the native value onto its Python original, if any.
1566
+ An error bit (as an LLVM value) is returned.
1567
+ """
1568
+ impl = _reflectors.lookup(typ.__class__)
1569
+ if impl is None:
1570
+ # Reflection isn't needed for most types
1571
+ return cgutils.false_bit
1572
+
1573
+ is_error = cgutils.alloca_once_value(self.builder, cgutils.false_bit)
1574
+ c = _ReflectContext(
1575
+ self.context, self.builder, self, env_manager, is_error
1576
+ )
1577
+ impl(typ, val, c)
1578
+ return self.builder.load(c.is_error)
1579
+
1580
+ def to_native_generator(self, obj, typ):
1581
+ """
1582
+ Extract the generator structure pointer from a generator *obj*
1583
+ (a _dynfunc.Generator instance).
1584
+ """
1585
+ gen_ptr_ty = ir.PointerType(self.context.get_data_type(typ))
1586
+ value = self.context.get_generator_state(self.builder, obj, gen_ptr_ty)
1587
+ return NativeValue(value)
1588
+
1589
+ def from_native_generator(self, val, typ, env=None):
1590
+ """
1591
+ Make a Numba generator (a _dynfunc.Generator instance) from a
1592
+ generator structure pointer *val*.
1593
+ *env* is an optional _dynfunc.Environment instance to be wrapped
1594
+ in the generator.
1595
+ """
1596
+ llty = self.context.get_data_type(typ)
1597
+ assert not llty.is_pointer
1598
+ gen_struct_size = self.context.get_abi_sizeof(llty)
1599
+
1600
+ gendesc = self.context.get_generator_desc(typ)
1601
+
1602
+ # This is the PyCFunctionWithKeywords generated by PyCallWrapper
1603
+ genfnty = ir.FunctionType(
1604
+ self.pyobj, [self.pyobj, self.pyobj, self.pyobj]
1605
+ )
1606
+ genfn = self._get_function(
1607
+ genfnty, name=gendesc.llvm_cpython_wrapper_name
1608
+ )
1609
+
1610
+ # This is the raw finalizer generated by _lower_generator_finalize_func()
1611
+ finalizerty = ir.FunctionType(ir.VoidType(), [self.voidptr])
1612
+ if typ.has_finalizer:
1613
+ finalizer = self._get_function(
1614
+ finalizerty, name=gendesc.llvm_finalizer_name
1615
+ )
1616
+ else:
1617
+ finalizer = Constant(ir.PointerType(finalizerty), None)
1618
+
1619
+ # PyObject *numba_make_generator(state_size, initial_state, nextfunc, finalizer, env)
1620
+ fnty = ir.FunctionType(
1621
+ self.pyobj,
1622
+ [
1623
+ self.py_ssize_t,
1624
+ self.voidptr,
1625
+ ir.PointerType(genfnty),
1626
+ ir.PointerType(finalizerty),
1627
+ self.voidptr,
1628
+ ],
1629
+ )
1630
+ fn = self._get_function(fnty, name="numba_make_generator")
1631
+
1632
+ state_size = Constant(self.py_ssize_t, gen_struct_size)
1633
+ initial_state = self.builder.bitcast(val, self.voidptr)
1634
+ if env is None:
1635
+ env = self.get_null_object()
1636
+ env = self.builder.bitcast(env, self.voidptr)
1637
+
1638
+ return self.builder.call(
1639
+ fn, (state_size, initial_state, genfn, finalizer, env)
1640
+ )
1641
+
1642
+ def numba_array_adaptor(self, ary, ptr):
1643
+ assert not self.context.enable_nrt
1644
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, self.voidptr])
1645
+ fn = self._get_function(fnty, name="numba_adapt_ndarray")
1646
+ fn.args[0].add_attribute("nocapture")
1647
+ fn.args[1].add_attribute("nocapture")
1648
+ return self.builder.call(fn, (ary, ptr))
1649
+
1650
+ def numba_buffer_adaptor(self, buf, ptr):
1651
+ fnty = ir.FunctionType(
1652
+ ir.VoidType(), [ir.PointerType(self.py_buffer_t), self.voidptr]
1653
+ )
1654
+ fn = self._get_function(fnty, name="numba_adapt_buffer")
1655
+ fn.args[0].add_attribute("nocapture")
1656
+ fn.args[1].add_attribute("nocapture")
1657
+ return self.builder.call(fn, (buf, ptr))
1658
+
1659
+ def complex_adaptor(self, cobj, cmplx):
1660
+ fnty = ir.FunctionType(ir.IntType(32), [self.pyobj, cmplx.type])
1661
+ fn = self._get_function(fnty, name="numba_complex_adaptor")
1662
+ return self.builder.call(fn, [cobj, cmplx])
1663
+
1664
+ def extract_record_data(self, obj, pbuf):
1665
+ fnty = ir.FunctionType(
1666
+ self.voidptr, [self.pyobj, ir.PointerType(self.py_buffer_t)]
1667
+ )
1668
+ fn = self._get_function(fnty, name="numba_extract_record_data")
1669
+ return self.builder.call(fn, [obj, pbuf])
1670
+
1671
+ def get_buffer(self, obj, pbuf):
1672
+ fnty = ir.FunctionType(
1673
+ ir.IntType(32), [self.pyobj, ir.PointerType(self.py_buffer_t)]
1674
+ )
1675
+ fn = self._get_function(fnty, name="numba_get_buffer")
1676
+ return self.builder.call(fn, [obj, pbuf])
1677
+
1678
+ def release_buffer(self, pbuf):
1679
+ fnty = ir.FunctionType(
1680
+ ir.VoidType(), [ir.PointerType(self.py_buffer_t)]
1681
+ )
1682
+ fn = self._get_function(fnty, name="numba_release_buffer")
1683
+ return self.builder.call(fn, [pbuf])
1684
+
1685
+ def extract_np_datetime(self, obj):
1686
+ fnty = ir.FunctionType(ir.IntType(64), [self.pyobj])
1687
+ fn = self._get_function(fnty, name="numba_extract_np_datetime")
1688
+ return self.builder.call(fn, [obj])
1689
+
1690
+ def extract_np_timedelta(self, obj):
1691
+ fnty = ir.FunctionType(ir.IntType(64), [self.pyobj])
1692
+ fn = self._get_function(fnty, name="numba_extract_np_timedelta")
1693
+ return self.builder.call(fn, [obj])
1694
+
1695
+ def create_np_datetime(self, val, unit_code):
1696
+ unit_code = Constant(ir.IntType(32), int(unit_code))
1697
+ fnty = ir.FunctionType(self.pyobj, [ir.IntType(64), ir.IntType(32)])
1698
+ fn = self._get_function(fnty, name="numba_create_np_datetime")
1699
+ return self.builder.call(fn, [val, unit_code])
1700
+
1701
+ def create_np_timedelta(self, val, unit_code):
1702
+ unit_code = Constant(ir.IntType(32), int(unit_code))
1703
+ fnty = ir.FunctionType(self.pyobj, [ir.IntType(64), ir.IntType(32)])
1704
+ fn = self._get_function(fnty, name="numba_create_np_timedelta")
1705
+ return self.builder.call(fn, [val, unit_code])
1706
+
1707
+ def recreate_record(self, pdata, size, dtype, env_manager):
1708
+ fnty = ir.FunctionType(
1709
+ self.pyobj,
1710
+ [ir.PointerType(ir.IntType(8)), ir.IntType(32), self.pyobj],
1711
+ )
1712
+ fn = self._get_function(fnty, name="numba_recreate_record")
1713
+ dtypeaddr = env_manager.read_const(env_manager.add_const(dtype))
1714
+ return self.builder.call(fn, [pdata, size, dtypeaddr])
1715
+
1716
+ def string_from_constant_string(self, string):
1717
+ cstr = self.context.insert_const_string(self.module, string)
1718
+ sz = self.context.get_constant(types.intp, len(string))
1719
+ return self.string_from_string_and_size(cstr, sz)
1720
+
1721
+ def call_jit_code(self, func, sig, args):
1722
+ """Calls into Numba jitted code and propagate error using the Python
1723
+ calling convention.
1724
+
1725
+ Parameters
1726
+ ----------
1727
+ func : function
1728
+ The Python function to be compiled. This function is compiled
1729
+ in nopython-mode.
1730
+ sig : numba.typing.Signature
1731
+ The function signature for *func*.
1732
+ args : Sequence[llvmlite.binding.Value]
1733
+ LLVM values to use as arguments.
1734
+
1735
+ Returns
1736
+ -------
1737
+ (is_error, res) : 2-tuple of llvmlite.binding.Value.
1738
+ is_error : true iff *func* raised an exception.
1739
+ res : Returned value from *func* iff *is_error* is false.
1740
+
1741
+ If *is_error* is true, this method will adapt the nopython exception
1742
+ into a Python exception. Caller should return NULL to Python to
1743
+ indicate an error.
1744
+ """
1745
+ # Compile *func*
1746
+ builder = self.builder
1747
+ cres = self.context.compile_subroutine(builder, func, sig)
1748
+ got_retty = cres.signature.return_type
1749
+ retty = sig.return_type
1750
+ if got_retty != retty:
1751
+ # This error indicates an error in *func* or the caller of this
1752
+ # method.
1753
+ raise errors.LoweringError(
1754
+ f"mismatching signature {got_retty} != {retty}.\n"
1755
+ )
1756
+ # Call into *func*
1757
+ status, res = self.context.call_internal_no_propagate(
1758
+ builder,
1759
+ cres.fndesc,
1760
+ sig,
1761
+ args,
1762
+ )
1763
+ # Post-call handling for *func*
1764
+ is_error_ptr = cgutils.alloca_once(builder, cgutils.bool_t, zfill=True)
1765
+ res_type = self.context.get_value_type(sig.return_type)
1766
+ res_ptr = cgutils.alloca_once(builder, res_type, zfill=True)
1767
+
1768
+ # Handle error and adapt the nopython exception into cpython exception
1769
+ with builder.if_else(status.is_error) as (has_err, no_err):
1770
+ with has_err:
1771
+ builder.store(status.is_error, is_error_ptr)
1772
+ # Set error state in the Python interpreter
1773
+ self.context.call_conv.raise_error(builder, self, status)
1774
+ with no_err:
1775
+ # Handle returned value
1776
+ res = imputils.fix_returning_optional(
1777
+ self.context,
1778
+ builder,
1779
+ sig,
1780
+ status,
1781
+ res,
1782
+ )
1783
+ builder.store(res, res_ptr)
1784
+
1785
+ is_error = builder.load(is_error_ptr)
1786
+ res = builder.load(res_ptr)
1787
+ return is_error, res
1788
+
1789
+
1790
+ class ObjModeUtils:
1791
+ """Internal utils for calling objmode dispatcher from within NPM code."""
1792
+
1793
+ def __init__(self, pyapi):
1794
+ self.pyapi = pyapi
1795
+
1796
+ def load_dispatcher(self, fnty, argtypes):
1797
+ builder = self.pyapi.builder
1798
+ tyctx = self.pyapi.context
1799
+ m = builder.module
1800
+
1801
+ # Add a global variable to cache the objmode dispatcher
1802
+ gv = ir.GlobalVariable(
1803
+ m,
1804
+ self.pyapi.pyobj,
1805
+ name=m.get_unique_name("cached_objmode_dispatcher"),
1806
+ )
1807
+ gv.initializer = gv.type.pointee(None)
1808
+ gv.linkage = "internal"
1809
+
1810
+ # Make a basic-block to common exit
1811
+ bb_end = builder.append_basic_block("bb_end")
1812
+
1813
+ if serialize.is_serialiable(fnty.dispatcher):
1814
+ serialized_dispatcher = self.pyapi.serialize_object(
1815
+ (fnty.dispatcher, tuple(argtypes)),
1816
+ )
1817
+ compile_args = self.pyapi.unserialize(serialized_dispatcher)
1818
+ # unserialize (unpickling) can fail
1819
+ failed_unser = cgutils.is_null(builder, compile_args)
1820
+ with builder.if_then(failed_unser):
1821
+ # early exit. `gv` is still null.
1822
+ builder.branch(bb_end)
1823
+
1824
+ cached = builder.load(gv)
1825
+ with builder.if_then(cgutils.is_null(builder, cached)):
1826
+ if serialize.is_serialiable(fnty.dispatcher):
1827
+ cls = type(self)
1828
+ compiler = self.pyapi.unserialize(
1829
+ self.pyapi.serialize_object(cls._call_objmode_dispatcher)
1830
+ )
1831
+ callee = self.pyapi.call_function_objargs(
1832
+ compiler,
1833
+ [compile_args],
1834
+ )
1835
+ # Clean up
1836
+ self.pyapi.decref(compiler)
1837
+ self.pyapi.decref(compile_args)
1838
+ else:
1839
+ entry_pt = fnty.dispatcher.compile(tuple(argtypes))
1840
+ callee = tyctx.add_dynamic_addr(
1841
+ builder,
1842
+ id(entry_pt),
1843
+ info="with_objectmode",
1844
+ )
1845
+ # Incref the dispatcher and cache it
1846
+ self.pyapi.incref(callee)
1847
+ builder.store(callee, gv)
1848
+ # Jump to the exit block
1849
+ builder.branch(bb_end)
1850
+ # Define the exit block
1851
+ builder.position_at_end(bb_end)
1852
+ callee = builder.load(gv)
1853
+ return callee
1854
+
1855
+ @staticmethod
1856
+ def _call_objmode_dispatcher(compile_args):
1857
+ dispatcher, argtypes = compile_args
1858
+ entrypt = dispatcher.compile(argtypes)
1859
+ return entrypt