numba-cuda 0.21.1__cp313-cp313-win_amd64.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.
- _numba_cuda_redirector.pth +4 -0
- _numba_cuda_redirector.py +89 -0
- numba_cuda/VERSION +1 -0
- numba_cuda/__init__.py +6 -0
- numba_cuda/_version.py +11 -0
- numba_cuda/numba/cuda/__init__.py +70 -0
- numba_cuda/numba/cuda/_internal/cuda_bf16.py +16394 -0
- numba_cuda/numba/cuda/_internal/cuda_fp16.py +8112 -0
- numba_cuda/numba/cuda/api.py +577 -0
- numba_cuda/numba/cuda/api_util.py +76 -0
- numba_cuda/numba/cuda/args.py +72 -0
- numba_cuda/numba/cuda/bf16.py +397 -0
- numba_cuda/numba/cuda/cache_hints.py +287 -0
- numba_cuda/numba/cuda/cext/__init__.py +2 -0
- numba_cuda/numba/cuda/cext/_devicearray.cp313-win_amd64.pyd +0 -0
- numba_cuda/numba/cuda/cext/_devicearray.cpp +159 -0
- numba_cuda/numba/cuda/cext/_devicearray.h +29 -0
- numba_cuda/numba/cuda/cext/_dispatcher.cp313-win_amd64.pyd +0 -0
- numba_cuda/numba/cuda/cext/_dispatcher.cpp +1098 -0
- numba_cuda/numba/cuda/cext/_hashtable.cpp +532 -0
- numba_cuda/numba/cuda/cext/_hashtable.h +135 -0
- numba_cuda/numba/cuda/cext/_helperlib.c +71 -0
- numba_cuda/numba/cuda/cext/_helperlib.cp313-win_amd64.pyd +0 -0
- numba_cuda/numba/cuda/cext/_helpermod.c +82 -0
- numba_cuda/numba/cuda/cext/_pymodule.h +38 -0
- numba_cuda/numba/cuda/cext/_typeconv.cp313-win_amd64.pyd +0 -0
- numba_cuda/numba/cuda/cext/_typeconv.cpp +206 -0
- numba_cuda/numba/cuda/cext/_typeof.cpp +1159 -0
- numba_cuda/numba/cuda/cext/_typeof.h +19 -0
- numba_cuda/numba/cuda/cext/capsulethunk.h +111 -0
- numba_cuda/numba/cuda/cext/mviewbuf.c +385 -0
- numba_cuda/numba/cuda/cext/mviewbuf.cp313-win_amd64.pyd +0 -0
- numba_cuda/numba/cuda/cext/typeconv.cpp +212 -0
- numba_cuda/numba/cuda/cext/typeconv.hpp +101 -0
- numba_cuda/numba/cuda/cg.py +67 -0
- numba_cuda/numba/cuda/cgutils.py +1294 -0
- numba_cuda/numba/cuda/cloudpickle/__init__.py +21 -0
- numba_cuda/numba/cuda/cloudpickle/cloudpickle.py +1598 -0
- numba_cuda/numba/cuda/cloudpickle/cloudpickle_fast.py +17 -0
- numba_cuda/numba/cuda/codegen.py +541 -0
- numba_cuda/numba/cuda/compiler.py +1396 -0
- numba_cuda/numba/cuda/core/analysis.py +758 -0
- numba_cuda/numba/cuda/core/annotations/__init__.py +0 -0
- numba_cuda/numba/cuda/core/annotations/pretty_annotate.py +288 -0
- numba_cuda/numba/cuda/core/annotations/type_annotations.py +305 -0
- numba_cuda/numba/cuda/core/base.py +1332 -0
- numba_cuda/numba/cuda/core/boxing.py +1411 -0
- numba_cuda/numba/cuda/core/bytecode.py +728 -0
- numba_cuda/numba/cuda/core/byteflow.py +2346 -0
- numba_cuda/numba/cuda/core/caching.py +744 -0
- numba_cuda/numba/cuda/core/callconv.py +392 -0
- numba_cuda/numba/cuda/core/codegen.py +171 -0
- numba_cuda/numba/cuda/core/compiler.py +199 -0
- numba_cuda/numba/cuda/core/compiler_lock.py +85 -0
- numba_cuda/numba/cuda/core/compiler_machinery.py +497 -0
- numba_cuda/numba/cuda/core/config.py +650 -0
- numba_cuda/numba/cuda/core/consts.py +124 -0
- numba_cuda/numba/cuda/core/controlflow.py +989 -0
- numba_cuda/numba/cuda/core/entrypoints.py +57 -0
- numba_cuda/numba/cuda/core/environment.py +66 -0
- numba_cuda/numba/cuda/core/errors.py +917 -0
- numba_cuda/numba/cuda/core/event.py +511 -0
- numba_cuda/numba/cuda/core/funcdesc.py +330 -0
- numba_cuda/numba/cuda/core/generators.py +387 -0
- numba_cuda/numba/cuda/core/imputils.py +509 -0
- numba_cuda/numba/cuda/core/inline_closurecall.py +1787 -0
- numba_cuda/numba/cuda/core/interpreter.py +3617 -0
- numba_cuda/numba/cuda/core/ir.py +1812 -0
- numba_cuda/numba/cuda/core/ir_utils.py +2638 -0
- numba_cuda/numba/cuda/core/optional.py +129 -0
- numba_cuda/numba/cuda/core/options.py +262 -0
- numba_cuda/numba/cuda/core/postproc.py +249 -0
- numba_cuda/numba/cuda/core/pythonapi.py +1859 -0
- numba_cuda/numba/cuda/core/registry.py +46 -0
- numba_cuda/numba/cuda/core/removerefctpass.py +123 -0
- numba_cuda/numba/cuda/core/rewrites/__init__.py +26 -0
- numba_cuda/numba/cuda/core/rewrites/ir_print.py +91 -0
- numba_cuda/numba/cuda/core/rewrites/registry.py +104 -0
- numba_cuda/numba/cuda/core/rewrites/static_binop.py +41 -0
- numba_cuda/numba/cuda/core/rewrites/static_getitem.py +189 -0
- numba_cuda/numba/cuda/core/rewrites/static_raise.py +100 -0
- numba_cuda/numba/cuda/core/sigutils.py +68 -0
- numba_cuda/numba/cuda/core/ssa.py +498 -0
- numba_cuda/numba/cuda/core/targetconfig.py +330 -0
- numba_cuda/numba/cuda/core/tracing.py +231 -0
- numba_cuda/numba/cuda/core/transforms.py +956 -0
- numba_cuda/numba/cuda/core/typed_passes.py +867 -0
- numba_cuda/numba/cuda/core/typeinfer.py +1950 -0
- numba_cuda/numba/cuda/core/unsafe/__init__.py +0 -0
- numba_cuda/numba/cuda/core/unsafe/bytes.py +67 -0
- numba_cuda/numba/cuda/core/unsafe/eh.py +67 -0
- numba_cuda/numba/cuda/core/unsafe/refcount.py +98 -0
- numba_cuda/numba/cuda/core/untyped_passes.py +1979 -0
- numba_cuda/numba/cuda/cpython/builtins.py +1153 -0
- numba_cuda/numba/cuda/cpython/charseq.py +1218 -0
- numba_cuda/numba/cuda/cpython/cmathimpl.py +560 -0
- numba_cuda/numba/cuda/cpython/enumimpl.py +103 -0
- numba_cuda/numba/cuda/cpython/iterators.py +167 -0
- numba_cuda/numba/cuda/cpython/listobj.py +1326 -0
- numba_cuda/numba/cuda/cpython/mathimpl.py +499 -0
- numba_cuda/numba/cuda/cpython/numbers.py +1475 -0
- numba_cuda/numba/cuda/cpython/rangeobj.py +289 -0
- numba_cuda/numba/cuda/cpython/slicing.py +322 -0
- numba_cuda/numba/cuda/cpython/tupleobj.py +456 -0
- numba_cuda/numba/cuda/cpython/unicode.py +2865 -0
- numba_cuda/numba/cuda/cpython/unicode_support.py +1597 -0
- numba_cuda/numba/cuda/cpython/unsafe/__init__.py +0 -0
- numba_cuda/numba/cuda/cpython/unsafe/numbers.py +64 -0
- numba_cuda/numba/cuda/cpython/unsafe/tuple.py +92 -0
- numba_cuda/numba/cuda/cuda_paths.py +691 -0
- numba_cuda/numba/cuda/cudadecl.py +556 -0
- numba_cuda/numba/cuda/cudadrv/__init__.py +14 -0
- numba_cuda/numba/cuda/cudadrv/devicearray.py +951 -0
- numba_cuda/numba/cuda/cudadrv/devices.py +249 -0
- numba_cuda/numba/cuda/cudadrv/driver.py +3222 -0
- numba_cuda/numba/cuda/cudadrv/drvapi.py +435 -0
- numba_cuda/numba/cuda/cudadrv/dummyarray.py +558 -0
- numba_cuda/numba/cuda/cudadrv/enums.py +613 -0
- numba_cuda/numba/cuda/cudadrv/error.py +48 -0
- numba_cuda/numba/cuda/cudadrv/libs.py +220 -0
- numba_cuda/numba/cuda/cudadrv/linkable_code.py +184 -0
- numba_cuda/numba/cuda/cudadrv/mappings.py +14 -0
- numba_cuda/numba/cuda/cudadrv/ndarray.py +26 -0
- numba_cuda/numba/cuda/cudadrv/nvrtc.py +193 -0
- numba_cuda/numba/cuda/cudadrv/nvvm.py +756 -0
- numba_cuda/numba/cuda/cudadrv/rtapi.py +13 -0
- numba_cuda/numba/cuda/cudadrv/runtime.py +34 -0
- numba_cuda/numba/cuda/cudaimpl.py +995 -0
- numba_cuda/numba/cuda/cudamath.py +149 -0
- numba_cuda/numba/cuda/datamodel/__init__.py +7 -0
- numba_cuda/numba/cuda/datamodel/cuda_manager.py +66 -0
- numba_cuda/numba/cuda/datamodel/cuda_models.py +1446 -0
- numba_cuda/numba/cuda/datamodel/cuda_packer.py +224 -0
- numba_cuda/numba/cuda/datamodel/cuda_registry.py +22 -0
- numba_cuda/numba/cuda/datamodel/cuda_testing.py +153 -0
- numba_cuda/numba/cuda/datamodel/manager.py +11 -0
- numba_cuda/numba/cuda/datamodel/models.py +9 -0
- numba_cuda/numba/cuda/datamodel/packer.py +9 -0
- numba_cuda/numba/cuda/datamodel/registry.py +11 -0
- numba_cuda/numba/cuda/datamodel/testing.py +11 -0
- numba_cuda/numba/cuda/debuginfo.py +903 -0
- numba_cuda/numba/cuda/decorators.py +294 -0
- numba_cuda/numba/cuda/descriptor.py +35 -0
- numba_cuda/numba/cuda/device_init.py +158 -0
- numba_cuda/numba/cuda/deviceufunc.py +1021 -0
- numba_cuda/numba/cuda/dispatcher.py +2463 -0
- numba_cuda/numba/cuda/errors.py +72 -0
- numba_cuda/numba/cuda/extending.py +697 -0
- numba_cuda/numba/cuda/flags.py +178 -0
- numba_cuda/numba/cuda/fp16.py +357 -0
- numba_cuda/numba/cuda/include/12/cuda_bf16.h +5118 -0
- numba_cuda/numba/cuda/include/12/cuda_bf16.hpp +3865 -0
- numba_cuda/numba/cuda/include/12/cuda_fp16.h +5363 -0
- numba_cuda/numba/cuda/include/12/cuda_fp16.hpp +3483 -0
- numba_cuda/numba/cuda/include/13/cuda_bf16.h +5118 -0
- numba_cuda/numba/cuda/include/13/cuda_bf16.hpp +3865 -0
- numba_cuda/numba/cuda/include/13/cuda_fp16.h +5363 -0
- numba_cuda/numba/cuda/include/13/cuda_fp16.hpp +3483 -0
- numba_cuda/numba/cuda/initialize.py +24 -0
- numba_cuda/numba/cuda/intrinsic_wrapper.py +41 -0
- numba_cuda/numba/cuda/intrinsics.py +382 -0
- numba_cuda/numba/cuda/itanium_mangler.py +214 -0
- numba_cuda/numba/cuda/kernels/__init__.py +2 -0
- numba_cuda/numba/cuda/kernels/reduction.py +265 -0
- numba_cuda/numba/cuda/kernels/transpose.py +65 -0
- numba_cuda/numba/cuda/libdevice.py +3386 -0
- numba_cuda/numba/cuda/libdevicedecl.py +20 -0
- numba_cuda/numba/cuda/libdevicefuncs.py +1060 -0
- numba_cuda/numba/cuda/libdeviceimpl.py +88 -0
- numba_cuda/numba/cuda/locks.py +19 -0
- numba_cuda/numba/cuda/lowering.py +1951 -0
- numba_cuda/numba/cuda/mathimpl.py +374 -0
- numba_cuda/numba/cuda/memory_management/__init__.py +4 -0
- numba_cuda/numba/cuda/memory_management/memsys.cu +99 -0
- numba_cuda/numba/cuda/memory_management/memsys.cuh +22 -0
- numba_cuda/numba/cuda/memory_management/nrt.cu +212 -0
- numba_cuda/numba/cuda/memory_management/nrt.cuh +48 -0
- numba_cuda/numba/cuda/memory_management/nrt.py +390 -0
- numba_cuda/numba/cuda/memory_management/nrt_context.py +438 -0
- numba_cuda/numba/cuda/misc/appdirs.py +594 -0
- numba_cuda/numba/cuda/misc/cffiimpl.py +24 -0
- numba_cuda/numba/cuda/misc/coverage_support.py +43 -0
- numba_cuda/numba/cuda/misc/dump_style.py +41 -0
- numba_cuda/numba/cuda/misc/findlib.py +75 -0
- numba_cuda/numba/cuda/misc/firstlinefinder.py +96 -0
- numba_cuda/numba/cuda/misc/gdb_hook.py +240 -0
- numba_cuda/numba/cuda/misc/literal.py +28 -0
- numba_cuda/numba/cuda/misc/llvm_pass_timings.py +412 -0
- numba_cuda/numba/cuda/misc/special.py +94 -0
- numba_cuda/numba/cuda/models.py +56 -0
- numba_cuda/numba/cuda/np/arraymath.py +5130 -0
- numba_cuda/numba/cuda/np/arrayobj.py +7635 -0
- numba_cuda/numba/cuda/np/extensions.py +11 -0
- numba_cuda/numba/cuda/np/linalg.py +3087 -0
- numba_cuda/numba/cuda/np/math/__init__.py +0 -0
- numba_cuda/numba/cuda/np/math/cmathimpl.py +558 -0
- numba_cuda/numba/cuda/np/math/mathimpl.py +487 -0
- numba_cuda/numba/cuda/np/math/numbers.py +1461 -0
- numba_cuda/numba/cuda/np/npdatetime.py +969 -0
- numba_cuda/numba/cuda/np/npdatetime_helpers.py +217 -0
- numba_cuda/numba/cuda/np/npyfuncs.py +1808 -0
- numba_cuda/numba/cuda/np/npyimpl.py +1027 -0
- numba_cuda/numba/cuda/np/numpy_support.py +798 -0
- numba_cuda/numba/cuda/np/polynomial/__init__.py +4 -0
- numba_cuda/numba/cuda/np/polynomial/polynomial_core.py +242 -0
- numba_cuda/numba/cuda/np/polynomial/polynomial_functions.py +380 -0
- numba_cuda/numba/cuda/np/ufunc/__init__.py +4 -0
- numba_cuda/numba/cuda/np/ufunc/decorators.py +203 -0
- numba_cuda/numba/cuda/np/ufunc/sigparse.py +68 -0
- numba_cuda/numba/cuda/np/ufunc/ufuncbuilder.py +65 -0
- numba_cuda/numba/cuda/np/ufunc_db.py +1282 -0
- numba_cuda/numba/cuda/np/unsafe/__init__.py +0 -0
- numba_cuda/numba/cuda/np/unsafe/ndarray.py +84 -0
- numba_cuda/numba/cuda/nvvmutils.py +254 -0
- numba_cuda/numba/cuda/printimpl.py +126 -0
- numba_cuda/numba/cuda/random.py +308 -0
- numba_cuda/numba/cuda/reshape_funcs.cu +156 -0
- numba_cuda/numba/cuda/serialize.py +267 -0
- numba_cuda/numba/cuda/simulator/__init__.py +63 -0
- numba_cuda/numba/cuda/simulator/_internal/__init__.py +4 -0
- numba_cuda/numba/cuda/simulator/_internal/cuda_bf16.py +2 -0
- numba_cuda/numba/cuda/simulator/api.py +179 -0
- numba_cuda/numba/cuda/simulator/bf16.py +4 -0
- numba_cuda/numba/cuda/simulator/compiler.py +38 -0
- numba_cuda/numba/cuda/simulator/cudadrv/__init__.py +11 -0
- numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +462 -0
- numba_cuda/numba/cuda/simulator/cudadrv/devices.py +122 -0
- numba_cuda/numba/cuda/simulator/cudadrv/driver.py +66 -0
- numba_cuda/numba/cuda/simulator/cudadrv/drvapi.py +7 -0
- numba_cuda/numba/cuda/simulator/cudadrv/dummyarray.py +7 -0
- numba_cuda/numba/cuda/simulator/cudadrv/error.py +10 -0
- numba_cuda/numba/cuda/simulator/cudadrv/libs.py +10 -0
- numba_cuda/numba/cuda/simulator/cudadrv/linkable_code.py +61 -0
- numba_cuda/numba/cuda/simulator/cudadrv/nvrtc.py +11 -0
- numba_cuda/numba/cuda/simulator/cudadrv/nvvm.py +32 -0
- numba_cuda/numba/cuda/simulator/cudadrv/runtime.py +22 -0
- numba_cuda/numba/cuda/simulator/dispatcher.py +11 -0
- numba_cuda/numba/cuda/simulator/kernel.py +320 -0
- numba_cuda/numba/cuda/simulator/kernelapi.py +509 -0
- numba_cuda/numba/cuda/simulator/memory_management/__init__.py +4 -0
- numba_cuda/numba/cuda/simulator/memory_management/nrt.py +21 -0
- numba_cuda/numba/cuda/simulator/reduction.py +19 -0
- numba_cuda/numba/cuda/simulator/tests/support.py +4 -0
- numba_cuda/numba/cuda/simulator/vector_types.py +65 -0
- numba_cuda/numba/cuda/simulator_init.py +18 -0
- numba_cuda/numba/cuda/stubs.py +635 -0
- numba_cuda/numba/cuda/target.py +505 -0
- numba_cuda/numba/cuda/testing.py +347 -0
- numba_cuda/numba/cuda/tests/__init__.py +62 -0
- numba_cuda/numba/cuda/tests/benchmarks/__init__.py +0 -0
- numba_cuda/numba/cuda/tests/benchmarks/test_kernel_launch.py +119 -0
- numba_cuda/numba/cuda/tests/cloudpickle_main_class.py +9 -0
- numba_cuda/numba/cuda/tests/core/serialize_usecases.py +113 -0
- numba_cuda/numba/cuda/tests/core/test_itanium_mangler.py +83 -0
- numba_cuda/numba/cuda/tests/core/test_serialize.py +371 -0
- numba_cuda/numba/cuda/tests/cudadrv/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_array_attr.py +147 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_context_stack.py +161 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_array_slicing.py +397 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_auto_context.py +24 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +180 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_driver.py +313 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_memory.py +187 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +621 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +247 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +100 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +198 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_events.py +53 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_host_alloc.py +72 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_init.py +138 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_inline_ptx.py +43 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_is_fp16.py +15 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_linkable_code.py +58 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +348 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_managed_alloc.py +128 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_module_callbacks.py +301 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py +174 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_nvrtc.py +28 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_nvvm_driver.py +185 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_pinned.py +39 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_profiler.py +23 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_reset_device.py +38 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_runtime.py +48 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_select_device.py +44 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_streams.py +127 -0
- numba_cuda/numba/cuda/tests/cudapy/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/cudapy/cache_usecases.py +231 -0
- numba_cuda/numba/cuda/tests/cudapy/cache_with_cpu_usecases.py +50 -0
- numba_cuda/numba/cuda/tests/cudapy/cg_cache_usecases.py +36 -0
- numba_cuda/numba/cuda/tests/cudapy/complex_usecases.py +116 -0
- numba_cuda/numba/cuda/tests/cudapy/enum_usecases.py +59 -0
- numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +62 -0
- numba_cuda/numba/cuda/tests/cudapy/jitlink.ptx +28 -0
- numba_cuda/numba/cuda/tests/cudapy/overload_usecases.py +33 -0
- numba_cuda/numba/cuda/tests/cudapy/recursion_usecases.py +104 -0
- numba_cuda/numba/cuda/tests/cudapy/test_alignment.py +47 -0
- numba_cuda/numba/cuda/tests/cudapy/test_analysis.py +1122 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array.py +344 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_alignment.py +268 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_args.py +203 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_methods.py +63 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_reductions.py +360 -0
- numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1815 -0
- numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +599 -0
- numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +377 -0
- numba_cuda/numba/cuda/tests/cudapy/test_blackscholes.py +160 -0
- numba_cuda/numba/cuda/tests/cudapy/test_boolean.py +27 -0
- numba_cuda/numba/cuda/tests/cudapy/test_byteflow.py +98 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cache_hints.py +210 -0
- numba_cuda/numba/cuda/tests/cudapy/test_caching.py +683 -0
- numba_cuda/numba/cuda/tests/cudapy/test_casting.py +265 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cffi.py +42 -0
- numba_cuda/numba/cuda/tests/cudapy/test_compiler.py +718 -0
- numba_cuda/numba/cuda/tests/cudapy/test_complex.py +370 -0
- numba_cuda/numba/cuda/tests/cudapy/test_complex_kernel.py +23 -0
- numba_cuda/numba/cuda/tests/cudapy/test_const_string.py +142 -0
- numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +178 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +193 -0
- numba_cuda/numba/cuda/tests/cudapy/test_copy_propagate.py +131 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cuda_array_interface.py +438 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cuda_jit_no_types.py +94 -0
- numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +101 -0
- numba_cuda/numba/cuda/tests/cudapy/test_debug.py +105 -0
- numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +889 -0
- numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +476 -0
- numba_cuda/numba/cuda/tests/cudapy/test_device_func.py +500 -0
- numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +820 -0
- numba_cuda/numba/cuda/tests/cudapy/test_enums.py +152 -0
- numba_cuda/numba/cuda/tests/cudapy/test_errors.py +111 -0
- numba_cuda/numba/cuda/tests/cudapy/test_exception.py +170 -0
- numba_cuda/numba/cuda/tests/cudapy/test_extending.py +1088 -0
- numba_cuda/numba/cuda/tests/cudapy/test_extending_types.py +71 -0
- numba_cuda/numba/cuda/tests/cudapy/test_fastmath.py +265 -0
- numba_cuda/numba/cuda/tests/cudapy/test_flow_control.py +1433 -0
- numba_cuda/numba/cuda/tests/cudapy/test_forall.py +57 -0
- numba_cuda/numba/cuda/tests/cudapy/test_freevar.py +34 -0
- numba_cuda/numba/cuda/tests/cudapy/test_frexp_ldexp.py +69 -0
- numba_cuda/numba/cuda/tests/cudapy/test_globals.py +62 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc.py +474 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scalar.py +167 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scheduling.py +92 -0
- numba_cuda/numba/cuda/tests/cudapy/test_idiv.py +39 -0
- numba_cuda/numba/cuda/tests/cudapy/test_inline.py +170 -0
- numba_cuda/numba/cuda/tests/cudapy/test_inspect.py +255 -0
- numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +1219 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ipc.py +263 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ir.py +598 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +276 -0
- numba_cuda/numba/cuda/tests/cudapy/test_iterators.py +101 -0
- numba_cuda/numba/cuda/tests/cudapy/test_lang.py +68 -0
- numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +123 -0
- numba_cuda/numba/cuda/tests/cudapy/test_libdevice.py +194 -0
- numba_cuda/numba/cuda/tests/cudapy/test_lineinfo.py +220 -0
- numba_cuda/numba/cuda/tests/cudapy/test_localmem.py +173 -0
- numba_cuda/numba/cuda/tests/cudapy/test_make_function_to_jit_function.py +364 -0
- numba_cuda/numba/cuda/tests/cudapy/test_mandel.py +47 -0
- numba_cuda/numba/cuda/tests/cudapy/test_math.py +842 -0
- numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +76 -0
- numba_cuda/numba/cuda/tests/cudapy/test_minmax.py +78 -0
- numba_cuda/numba/cuda/tests/cudapy/test_montecarlo.py +25 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multigpu.py +145 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multiprocessing.py +39 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multithreads.py +82 -0
- numba_cuda/numba/cuda/tests/cudapy/test_nondet.py +53 -0
- numba_cuda/numba/cuda/tests/cudapy/test_operator.py +504 -0
- numba_cuda/numba/cuda/tests/cudapy/test_optimization.py +93 -0
- numba_cuda/numba/cuda/tests/cudapy/test_overload.py +402 -0
- numba_cuda/numba/cuda/tests/cudapy/test_powi.py +128 -0
- numba_cuda/numba/cuda/tests/cudapy/test_print.py +193 -0
- numba_cuda/numba/cuda/tests/cudapy/test_py2_div_issue.py +37 -0
- numba_cuda/numba/cuda/tests/cudapy/test_random.py +117 -0
- numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +614 -0
- numba_cuda/numba/cuda/tests/cudapy/test_recursion.py +130 -0
- numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +94 -0
- numba_cuda/numba/cuda/tests/cudapy/test_retrieve_autoconverted_arrays.py +83 -0
- numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +86 -0
- numba_cuda/numba/cuda/tests/cudapy/test_slicing.py +40 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sm.py +457 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sm_creation.py +233 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ssa.py +454 -0
- numba_cuda/numba/cuda/tests/cudapy/test_stream_api.py +56 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sync.py +277 -0
- numba_cuda/numba/cuda/tests/cudapy/test_tracing.py +200 -0
- numba_cuda/numba/cuda/tests/cudapy/test_transpose.py +90 -0
- numba_cuda/numba/cuda/tests/cudapy/test_typeconv.py +333 -0
- numba_cuda/numba/cuda/tests/cudapy/test_typeinfer.py +538 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +585 -0
- numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +42 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +485 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize.py +312 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_complex.py +23 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +183 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_device.py +40 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_scalar_arg.py +40 -0
- numba_cuda/numba/cuda/tests/cudapy/test_warning.py +206 -0
- numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +331 -0
- numba_cuda/numba/cuda/tests/cudasim/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/cudasim/support.py +9 -0
- numba_cuda/numba/cuda/tests/cudasim/test_cudasim_issues.py +111 -0
- numba_cuda/numba/cuda/tests/data/__init__.py +2 -0
- numba_cuda/numba/cuda/tests/data/cta_barrier.cu +28 -0
- numba_cuda/numba/cuda/tests/data/cuda_include.cu +10 -0
- numba_cuda/numba/cuda/tests/data/error.cu +12 -0
- numba_cuda/numba/cuda/tests/data/include/add.cuh +8 -0
- numba_cuda/numba/cuda/tests/data/jitlink.cu +28 -0
- numba_cuda/numba/cuda/tests/data/jitlink.ptx +49 -0
- numba_cuda/numba/cuda/tests/data/warn.cu +12 -0
- numba_cuda/numba/cuda/tests/doc_examples/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/__init__.py +2 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/functions.cu +54 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/include/mul.cuh +8 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/saxpy.cu +14 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +86 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_cpointer.py +68 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py +81 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +141 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +160 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_matmul.py +180 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_montecarlo.py +119 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_random.py +66 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_reduction.py +80 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +206 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_ufunc.py +53 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_vecadd.py +76 -0
- numba_cuda/numba/cuda/tests/nocuda/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/nocuda/test_dummyarray.py +391 -0
- numba_cuda/numba/cuda/tests/nocuda/test_function_resolution.py +48 -0
- numba_cuda/numba/cuda/tests/nocuda/test_import.py +63 -0
- numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +252 -0
- numba_cuda/numba/cuda/tests/nocuda/test_nvvm.py +59 -0
- numba_cuda/numba/cuda/tests/nrt/__init__.py +9 -0
- numba_cuda/numba/cuda/tests/nrt/test_nrt.py +387 -0
- numba_cuda/numba/cuda/tests/nrt/test_nrt_refct.py +124 -0
- numba_cuda/numba/cuda/tests/support.py +900 -0
- numba_cuda/numba/cuda/typeconv/__init__.py +4 -0
- numba_cuda/numba/cuda/typeconv/castgraph.py +137 -0
- numba_cuda/numba/cuda/typeconv/rules.py +63 -0
- numba_cuda/numba/cuda/typeconv/typeconv.py +121 -0
- numba_cuda/numba/cuda/types/__init__.py +233 -0
- numba_cuda/numba/cuda/types/__init__.pyi +167 -0
- numba_cuda/numba/cuda/types/abstract.py +9 -0
- numba_cuda/numba/cuda/types/common.py +9 -0
- numba_cuda/numba/cuda/types/containers.py +9 -0
- numba_cuda/numba/cuda/types/cuda_abstract.py +533 -0
- numba_cuda/numba/cuda/types/cuda_common.py +110 -0
- numba_cuda/numba/cuda/types/cuda_containers.py +971 -0
- numba_cuda/numba/cuda/types/cuda_function_type.py +230 -0
- numba_cuda/numba/cuda/types/cuda_functions.py +798 -0
- numba_cuda/numba/cuda/types/cuda_iterators.py +120 -0
- numba_cuda/numba/cuda/types/cuda_misc.py +569 -0
- numba_cuda/numba/cuda/types/cuda_npytypes.py +690 -0
- numba_cuda/numba/cuda/types/cuda_scalars.py +280 -0
- numba_cuda/numba/cuda/types/ext_types.py +101 -0
- numba_cuda/numba/cuda/types/function_type.py +11 -0
- numba_cuda/numba/cuda/types/functions.py +9 -0
- numba_cuda/numba/cuda/types/iterators.py +9 -0
- numba_cuda/numba/cuda/types/misc.py +9 -0
- numba_cuda/numba/cuda/types/npytypes.py +9 -0
- numba_cuda/numba/cuda/types/scalars.py +9 -0
- numba_cuda/numba/cuda/typing/__init__.py +19 -0
- numba_cuda/numba/cuda/typing/arraydecl.py +939 -0
- numba_cuda/numba/cuda/typing/asnumbatype.py +130 -0
- numba_cuda/numba/cuda/typing/bufproto.py +70 -0
- numba_cuda/numba/cuda/typing/builtins.py +1209 -0
- numba_cuda/numba/cuda/typing/cffi_utils.py +219 -0
- numba_cuda/numba/cuda/typing/cmathdecl.py +47 -0
- numba_cuda/numba/cuda/typing/collections.py +138 -0
- numba_cuda/numba/cuda/typing/context.py +782 -0
- numba_cuda/numba/cuda/typing/ctypes_utils.py +125 -0
- numba_cuda/numba/cuda/typing/dictdecl.py +63 -0
- numba_cuda/numba/cuda/typing/enumdecl.py +74 -0
- numba_cuda/numba/cuda/typing/listdecl.py +147 -0
- numba_cuda/numba/cuda/typing/mathdecl.py +158 -0
- numba_cuda/numba/cuda/typing/npdatetime.py +322 -0
- numba_cuda/numba/cuda/typing/npydecl.py +749 -0
- numba_cuda/numba/cuda/typing/setdecl.py +115 -0
- numba_cuda/numba/cuda/typing/templates.py +1446 -0
- numba_cuda/numba/cuda/typing/typeof.py +301 -0
- numba_cuda/numba/cuda/ufuncs.py +746 -0
- numba_cuda/numba/cuda/utils.py +724 -0
- numba_cuda/numba/cuda/vector_types.py +214 -0
- numba_cuda/numba/cuda/vectorizers.py +260 -0
- numba_cuda-0.21.1.dist-info/METADATA +109 -0
- numba_cuda-0.21.1.dist-info/RECORD +488 -0
- numba_cuda-0.21.1.dist-info/WHEEL +5 -0
- numba_cuda-0.21.1.dist-info/licenses/LICENSE +26 -0
- numba_cuda-0.21.1.dist-info/licenses/LICENSE.numba +24 -0
- numba_cuda-0.21.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1294 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: BSD-2-Clause
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Generic helpers for LLVM code generation.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import collections
|
|
9
|
+
from contextlib import contextmanager, ExitStack
|
|
10
|
+
import functools
|
|
11
|
+
|
|
12
|
+
from llvmlite import ir
|
|
13
|
+
|
|
14
|
+
from numba.cuda import types
|
|
15
|
+
from numba.cuda import config, utils, debuginfo
|
|
16
|
+
import numba.cuda.datamodel
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
bool_t = ir.IntType(1)
|
|
20
|
+
int8_t = ir.IntType(8)
|
|
21
|
+
int32_t = ir.IntType(32)
|
|
22
|
+
intp_t = ir.IntType(utils.MACHINE_BITS)
|
|
23
|
+
voidptr_t = int8_t.as_pointer()
|
|
24
|
+
|
|
25
|
+
true_bit = bool_t(1)
|
|
26
|
+
false_bit = bool_t(0)
|
|
27
|
+
true_byte = int8_t(1)
|
|
28
|
+
false_byte = int8_t(0)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def as_bool_bit(builder, value):
|
|
32
|
+
return builder.icmp_unsigned("!=", value, value.type(0))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def make_anonymous_struct(builder, values, struct_type=None):
|
|
36
|
+
"""
|
|
37
|
+
Create an anonymous struct containing the given LLVM *values*.
|
|
38
|
+
"""
|
|
39
|
+
if struct_type is None:
|
|
40
|
+
struct_type = ir.LiteralStructType([v.type for v in values])
|
|
41
|
+
struct_val = struct_type(ir.Undefined)
|
|
42
|
+
for i, v in enumerate(values):
|
|
43
|
+
struct_val = builder.insert_value(struct_val, v, i)
|
|
44
|
+
return struct_val
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def make_bytearray(buf):
|
|
48
|
+
"""
|
|
49
|
+
Make a byte array constant from *buf*.
|
|
50
|
+
"""
|
|
51
|
+
b = bytearray(buf)
|
|
52
|
+
n = len(b)
|
|
53
|
+
return ir.Constant(ir.ArrayType(ir.IntType(8), n), b)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
_struct_proxy_cache = {}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def create_struct_proxy(fe_type, kind="value"):
|
|
60
|
+
"""
|
|
61
|
+
Returns a specialized StructProxy subclass for the given fe_type.
|
|
62
|
+
"""
|
|
63
|
+
cache_key = (fe_type, kind)
|
|
64
|
+
res = _struct_proxy_cache.get(cache_key)
|
|
65
|
+
if res is None:
|
|
66
|
+
base = {
|
|
67
|
+
"value": ValueStructProxy,
|
|
68
|
+
"data": DataStructProxy,
|
|
69
|
+
}[kind]
|
|
70
|
+
clsname = base.__name__ + "_" + str(fe_type)
|
|
71
|
+
bases = (base,)
|
|
72
|
+
clsmembers = dict(_fe_type=fe_type)
|
|
73
|
+
res = type(clsname, bases, clsmembers)
|
|
74
|
+
|
|
75
|
+
_struct_proxy_cache[cache_key] = res
|
|
76
|
+
return res
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def copy_struct(dst, src, repl={}):
|
|
80
|
+
"""
|
|
81
|
+
Copy structure from *src* to *dst* with replacement from *repl*.
|
|
82
|
+
"""
|
|
83
|
+
repl = repl.copy()
|
|
84
|
+
# copy data from src or use those in repl
|
|
85
|
+
for k in src._datamodel._fields:
|
|
86
|
+
v = repl.pop(k, getattr(src, k))
|
|
87
|
+
setattr(dst, k, v)
|
|
88
|
+
# use remaining key-values in repl
|
|
89
|
+
for k, v in repl.items():
|
|
90
|
+
setattr(dst, k, v)
|
|
91
|
+
return dst
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class _StructProxy(object):
|
|
95
|
+
"""
|
|
96
|
+
Creates a `Structure` like interface that is constructed with information
|
|
97
|
+
from DataModel instance. FE type must have a data model that is a
|
|
98
|
+
subclass of StructModel.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
# The following class members must be overridden by subclass
|
|
102
|
+
_fe_type = None
|
|
103
|
+
|
|
104
|
+
def __init__(self, context, builder, value=None, ref=None):
|
|
105
|
+
self._context = context
|
|
106
|
+
self._datamodel = self._context.data_model_manager[self._fe_type]
|
|
107
|
+
if not isinstance(self._datamodel, numba.cuda.datamodel.StructModel):
|
|
108
|
+
raise TypeError(
|
|
109
|
+
"Not a structure model: {0}".format(self._datamodel)
|
|
110
|
+
)
|
|
111
|
+
self._builder = builder
|
|
112
|
+
|
|
113
|
+
self._be_type = self._get_be_type(self._datamodel)
|
|
114
|
+
assert not is_pointer(self._be_type)
|
|
115
|
+
|
|
116
|
+
outer_ref, ref = self._make_refs(ref)
|
|
117
|
+
if ref.type.pointee != self._be_type:
|
|
118
|
+
raise AssertionError(
|
|
119
|
+
"bad ref type: expected %s, got %s"
|
|
120
|
+
% (self._be_type.as_pointer(), ref.type)
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
if value is not None:
|
|
124
|
+
if value.type != outer_ref.type.pointee:
|
|
125
|
+
raise AssertionError(
|
|
126
|
+
"bad value type: expected %s, got %s"
|
|
127
|
+
% (outer_ref.type.pointee, value.type)
|
|
128
|
+
)
|
|
129
|
+
self._builder.store(value, outer_ref)
|
|
130
|
+
|
|
131
|
+
self._value = ref
|
|
132
|
+
self._outer_ref = outer_ref
|
|
133
|
+
|
|
134
|
+
def _make_refs(self, ref):
|
|
135
|
+
"""
|
|
136
|
+
Return an (outer ref, value ref) pair. By default, these are
|
|
137
|
+
the same pointers, but a derived class may override this.
|
|
138
|
+
"""
|
|
139
|
+
if ref is None:
|
|
140
|
+
ref = alloca_once(self._builder, self._be_type, zfill=True)
|
|
141
|
+
return ref, ref
|
|
142
|
+
|
|
143
|
+
def _get_be_type(self, datamodel):
|
|
144
|
+
raise NotImplementedError
|
|
145
|
+
|
|
146
|
+
def _cast_member_to_value(self, index, val):
|
|
147
|
+
raise NotImplementedError
|
|
148
|
+
|
|
149
|
+
def _cast_member_from_value(self, index, val):
|
|
150
|
+
raise NotImplementedError
|
|
151
|
+
|
|
152
|
+
def _get_ptr_by_index(self, index):
|
|
153
|
+
return gep_inbounds(self._builder, self._value, 0, index)
|
|
154
|
+
|
|
155
|
+
def _get_ptr_by_name(self, attrname):
|
|
156
|
+
index = self._datamodel.get_field_position(attrname)
|
|
157
|
+
return self._get_ptr_by_index(index)
|
|
158
|
+
|
|
159
|
+
def __getattr__(self, field):
|
|
160
|
+
"""
|
|
161
|
+
Load the LLVM value of the named *field*.
|
|
162
|
+
"""
|
|
163
|
+
if not field.startswith("_"):
|
|
164
|
+
return self[self._datamodel.get_field_position(field)]
|
|
165
|
+
else:
|
|
166
|
+
raise AttributeError(field)
|
|
167
|
+
|
|
168
|
+
def __setattr__(self, field, value):
|
|
169
|
+
"""
|
|
170
|
+
Store the LLVM *value* into the named *field*.
|
|
171
|
+
"""
|
|
172
|
+
if field.startswith("_"):
|
|
173
|
+
return super(_StructProxy, self).__setattr__(field, value)
|
|
174
|
+
self[self._datamodel.get_field_position(field)] = value
|
|
175
|
+
|
|
176
|
+
def __getitem__(self, index):
|
|
177
|
+
"""
|
|
178
|
+
Load the LLVM value of the field at *index*.
|
|
179
|
+
"""
|
|
180
|
+
member_val = self._builder.load(self._get_ptr_by_index(index))
|
|
181
|
+
return self._cast_member_to_value(index, member_val)
|
|
182
|
+
|
|
183
|
+
def __setitem__(self, index, value):
|
|
184
|
+
"""
|
|
185
|
+
Store the LLVM *value* into the field at *index*.
|
|
186
|
+
"""
|
|
187
|
+
ptr = self._get_ptr_by_index(index)
|
|
188
|
+
value = self._cast_member_from_value(index, value)
|
|
189
|
+
if value.type != ptr.type.pointee:
|
|
190
|
+
if (
|
|
191
|
+
is_pointer(value.type)
|
|
192
|
+
and is_pointer(ptr.type.pointee)
|
|
193
|
+
and value.type.pointee == ptr.type.pointee.pointee
|
|
194
|
+
):
|
|
195
|
+
# Differ by address-space only
|
|
196
|
+
# Auto coerce it
|
|
197
|
+
value = self._context.addrspacecast(
|
|
198
|
+
self._builder, value, ptr.type.pointee.addrspace
|
|
199
|
+
)
|
|
200
|
+
else:
|
|
201
|
+
raise TypeError(
|
|
202
|
+
"Invalid store of {value.type} to "
|
|
203
|
+
"{ptr.type.pointee} in "
|
|
204
|
+
"{self._datamodel} "
|
|
205
|
+
"(trying to write member #{index})".format(
|
|
206
|
+
value=value, ptr=ptr, self=self, index=index
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
self._builder.store(value, ptr)
|
|
210
|
+
|
|
211
|
+
def __len__(self):
|
|
212
|
+
"""
|
|
213
|
+
Return the number of fields.
|
|
214
|
+
"""
|
|
215
|
+
return self._datamodel.field_count
|
|
216
|
+
|
|
217
|
+
def _getpointer(self):
|
|
218
|
+
"""
|
|
219
|
+
Return the LLVM pointer to the underlying structure.
|
|
220
|
+
"""
|
|
221
|
+
return self._outer_ref
|
|
222
|
+
|
|
223
|
+
def _getvalue(self):
|
|
224
|
+
"""
|
|
225
|
+
Load and return the value of the underlying LLVM structure.
|
|
226
|
+
"""
|
|
227
|
+
return self._builder.load(self._outer_ref)
|
|
228
|
+
|
|
229
|
+
def _setvalue(self, value):
|
|
230
|
+
"""
|
|
231
|
+
Store the value in this structure.
|
|
232
|
+
"""
|
|
233
|
+
assert not is_pointer(value.type)
|
|
234
|
+
assert value.type == self._be_type, (value.type, self._be_type)
|
|
235
|
+
self._builder.store(value, self._value)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class ValueStructProxy(_StructProxy):
|
|
239
|
+
"""
|
|
240
|
+
Create a StructProxy suitable for accessing regular values
|
|
241
|
+
(e.g. LLVM values or alloca slots).
|
|
242
|
+
"""
|
|
243
|
+
|
|
244
|
+
def _get_be_type(self, datamodel):
|
|
245
|
+
return datamodel.get_value_type()
|
|
246
|
+
|
|
247
|
+
def _cast_member_to_value(self, index, val):
|
|
248
|
+
return val
|
|
249
|
+
|
|
250
|
+
def _cast_member_from_value(self, index, val):
|
|
251
|
+
return val
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
class DataStructProxy(_StructProxy):
|
|
255
|
+
"""
|
|
256
|
+
Create a StructProxy suitable for accessing data persisted in memory.
|
|
257
|
+
"""
|
|
258
|
+
|
|
259
|
+
def _get_be_type(self, datamodel):
|
|
260
|
+
return datamodel.get_data_type()
|
|
261
|
+
|
|
262
|
+
def _cast_member_to_value(self, index, val):
|
|
263
|
+
model = self._datamodel.get_model(index)
|
|
264
|
+
return model.from_data(self._builder, val)
|
|
265
|
+
|
|
266
|
+
def _cast_member_from_value(self, index, val):
|
|
267
|
+
model = self._datamodel.get_model(index)
|
|
268
|
+
return model.as_data(self._builder, val)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
class Structure(object):
|
|
272
|
+
"""
|
|
273
|
+
A high-level object wrapping a alloca'ed LLVM structure, including
|
|
274
|
+
named fields and attribute access.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
# XXX Should this warrant several separate constructors?
|
|
278
|
+
def __init__(self, context, builder, value=None, ref=None, cast_ref=False):
|
|
279
|
+
self._type = context.get_struct_type(self)
|
|
280
|
+
self._context = context
|
|
281
|
+
self._builder = builder
|
|
282
|
+
if ref is None:
|
|
283
|
+
self._value = alloca_once(builder, self._type, zfill=True)
|
|
284
|
+
if value is not None:
|
|
285
|
+
assert not is_pointer(value.type)
|
|
286
|
+
assert value.type == self._type, (value.type, self._type)
|
|
287
|
+
builder.store(value, self._value)
|
|
288
|
+
else:
|
|
289
|
+
assert value is None
|
|
290
|
+
assert is_pointer(ref.type)
|
|
291
|
+
if self._type != ref.type.pointee:
|
|
292
|
+
if cast_ref:
|
|
293
|
+
ref = builder.bitcast(ref, self._type.as_pointer())
|
|
294
|
+
else:
|
|
295
|
+
raise TypeError(
|
|
296
|
+
"mismatching pointer type: got %s, expected %s"
|
|
297
|
+
% (ref.type.pointee, self._type)
|
|
298
|
+
)
|
|
299
|
+
self._value = ref
|
|
300
|
+
|
|
301
|
+
self._namemap = {}
|
|
302
|
+
self._fdmap = []
|
|
303
|
+
self._typemap = []
|
|
304
|
+
base = int32_t(0)
|
|
305
|
+
for i, (k, tp) in enumerate(self._fields):
|
|
306
|
+
self._namemap[k] = i
|
|
307
|
+
self._fdmap.append((base, int32_t(i)))
|
|
308
|
+
self._typemap.append(tp)
|
|
309
|
+
|
|
310
|
+
def _get_ptr_by_index(self, index):
|
|
311
|
+
ptr = self._builder.gep(self._value, self._fdmap[index], inbounds=True)
|
|
312
|
+
return ptr
|
|
313
|
+
|
|
314
|
+
def _get_ptr_by_name(self, attrname):
|
|
315
|
+
return self._get_ptr_by_index(self._namemap[attrname])
|
|
316
|
+
|
|
317
|
+
def __getattr__(self, field):
|
|
318
|
+
"""
|
|
319
|
+
Load the LLVM value of the named *field*.
|
|
320
|
+
"""
|
|
321
|
+
if not field.startswith("_"):
|
|
322
|
+
return self[self._namemap[field]]
|
|
323
|
+
else:
|
|
324
|
+
raise AttributeError(field)
|
|
325
|
+
|
|
326
|
+
def __setattr__(self, field, value):
|
|
327
|
+
"""
|
|
328
|
+
Store the LLVM *value* into the named *field*.
|
|
329
|
+
"""
|
|
330
|
+
if field.startswith("_"):
|
|
331
|
+
return super(Structure, self).__setattr__(field, value)
|
|
332
|
+
self[self._namemap[field]] = value
|
|
333
|
+
|
|
334
|
+
def __getitem__(self, index):
|
|
335
|
+
"""
|
|
336
|
+
Load the LLVM value of the field at *index*.
|
|
337
|
+
"""
|
|
338
|
+
|
|
339
|
+
return self._builder.load(self._get_ptr_by_index(index))
|
|
340
|
+
|
|
341
|
+
def __setitem__(self, index, value):
|
|
342
|
+
"""
|
|
343
|
+
Store the LLVM *value* into the field at *index*.
|
|
344
|
+
"""
|
|
345
|
+
ptr = self._get_ptr_by_index(index)
|
|
346
|
+
if ptr.type.pointee != value.type:
|
|
347
|
+
fmt = "Type mismatch: __setitem__(%d, ...) expected %r but got %r"
|
|
348
|
+
raise AssertionError(
|
|
349
|
+
fmt % (index, str(ptr.type.pointee), str(value.type))
|
|
350
|
+
)
|
|
351
|
+
self._builder.store(value, ptr)
|
|
352
|
+
|
|
353
|
+
def __len__(self):
|
|
354
|
+
"""
|
|
355
|
+
Return the number of fields.
|
|
356
|
+
"""
|
|
357
|
+
return len(self._namemap)
|
|
358
|
+
|
|
359
|
+
def _getpointer(self):
|
|
360
|
+
"""
|
|
361
|
+
Return the LLVM pointer to the underlying structure.
|
|
362
|
+
"""
|
|
363
|
+
return self._value
|
|
364
|
+
|
|
365
|
+
def _getvalue(self):
|
|
366
|
+
"""
|
|
367
|
+
Load and return the value of the underlying LLVM structure.
|
|
368
|
+
"""
|
|
369
|
+
return self._builder.load(self._value)
|
|
370
|
+
|
|
371
|
+
def _setvalue(self, value):
|
|
372
|
+
"""Store the value in this structure"""
|
|
373
|
+
assert not is_pointer(value.type)
|
|
374
|
+
assert value.type == self._type, (value.type, self._type)
|
|
375
|
+
self._builder.store(value, self._value)
|
|
376
|
+
|
|
377
|
+
# __iter__ is derived by Python from __len__ and __getitem__
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def alloca_once(builder, ty, size=None, name="", zfill=False):
|
|
381
|
+
"""Allocate stack memory at the entry block of the current function
|
|
382
|
+
pointed by ``builder`` with llvm type ``ty``. The optional ``size`` arg
|
|
383
|
+
set the number of element to allocate. The default is 1. The optional
|
|
384
|
+
``name`` arg set the symbol name inside the llvm IR for debugging.
|
|
385
|
+
If ``zfill`` is set, fill the memory with zeros at the current
|
|
386
|
+
use-site location. Note that the memory is always zero-filled after the
|
|
387
|
+
``alloca`` at init-site (the entry block).
|
|
388
|
+
"""
|
|
389
|
+
if isinstance(size, int):
|
|
390
|
+
size = ir.Constant(intp_t, size)
|
|
391
|
+
# suspend debug metadata emission else it links up python source lines with
|
|
392
|
+
# alloca in the entry block as well as their actual location and it makes
|
|
393
|
+
# the debug info "jump about".
|
|
394
|
+
with debuginfo.suspend_emission(builder):
|
|
395
|
+
with builder.goto_entry_block():
|
|
396
|
+
ptr = builder.alloca(ty, size=size, name=name)
|
|
397
|
+
# Always zero-fill at init-site. This is safe.
|
|
398
|
+
builder.store(ty(None), ptr)
|
|
399
|
+
# Also zero-fill at the use-site
|
|
400
|
+
if zfill:
|
|
401
|
+
builder.store(ptr.type.pointee(None), ptr)
|
|
402
|
+
return ptr
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def sizeof(builder, ptr_type):
|
|
406
|
+
"""Compute sizeof using GEP"""
|
|
407
|
+
null = ptr_type(None)
|
|
408
|
+
offset = null.gep([int32_t(1)])
|
|
409
|
+
return builder.ptrtoint(offset, intp_t)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def alloca_once_value(builder, value, name="", zfill=False):
|
|
413
|
+
"""
|
|
414
|
+
Like alloca_once(), but passing a *value* instead of a type. The
|
|
415
|
+
type is inferred and the allocated slot is also initialized with the
|
|
416
|
+
given value.
|
|
417
|
+
"""
|
|
418
|
+
storage = alloca_once(builder, value.type, zfill=zfill)
|
|
419
|
+
builder.store(value, storage)
|
|
420
|
+
return storage
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def insert_pure_function(module, fnty, name):
|
|
424
|
+
"""
|
|
425
|
+
Insert a pure function (in the functional programming sense) in the
|
|
426
|
+
given module.
|
|
427
|
+
"""
|
|
428
|
+
fn = get_or_insert_function(module, fnty, name)
|
|
429
|
+
fn.attributes.add("readonly")
|
|
430
|
+
fn.attributes.add("nounwind")
|
|
431
|
+
return fn
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def get_or_insert_function(module, fnty, name):
|
|
435
|
+
"""
|
|
436
|
+
Get the function named *name* with type *fnty* from *module*, or insert it
|
|
437
|
+
if it doesn't exist.
|
|
438
|
+
"""
|
|
439
|
+
fn = module.globals.get(name, None)
|
|
440
|
+
if fn is None:
|
|
441
|
+
fn = ir.Function(module, fnty, name)
|
|
442
|
+
return fn
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def get_or_insert_named_metadata(module, name):
|
|
446
|
+
try:
|
|
447
|
+
return module.get_named_metadata(name)
|
|
448
|
+
except KeyError:
|
|
449
|
+
return module.add_named_metadata(name)
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def add_global_variable(module, ty, name, addrspace=0):
|
|
453
|
+
unique_name = module.get_unique_name(name)
|
|
454
|
+
return ir.GlobalVariable(module, ty, unique_name, addrspace)
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
def terminate(builder, bbend):
|
|
458
|
+
bb = builder.basic_block
|
|
459
|
+
if bb.terminator is None:
|
|
460
|
+
builder.branch(bbend)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def get_null_value(ltype):
|
|
464
|
+
return ltype(None)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def is_null(builder, val):
|
|
468
|
+
null = get_null_value(val.type)
|
|
469
|
+
return builder.icmp_unsigned("==", null, val)
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def is_not_null(builder, val):
|
|
473
|
+
null = get_null_value(val.type)
|
|
474
|
+
return builder.icmp_unsigned("!=", null, val)
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def if_unlikely(builder, pred):
|
|
478
|
+
return builder.if_then(pred, likely=False)
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def if_likely(builder, pred):
|
|
482
|
+
return builder.if_then(pred, likely=True)
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
def ifnot(builder, pred):
|
|
486
|
+
return builder.if_then(builder.not_(pred))
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def increment_index(builder, val):
|
|
490
|
+
"""
|
|
491
|
+
Increment an index *val*.
|
|
492
|
+
"""
|
|
493
|
+
one = val.type(1)
|
|
494
|
+
# We pass the "nsw" flag in the hope that LLVM understands the index
|
|
495
|
+
# never changes sign. Unfortunately this doesn't always work
|
|
496
|
+
# (e.g. ndindex()).
|
|
497
|
+
return builder.add(val, one, flags=["nsw"])
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
Loop = collections.namedtuple("Loop", ("index", "do_break"))
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
@contextmanager
|
|
504
|
+
def for_range(builder, count, start=None, intp=None):
|
|
505
|
+
"""
|
|
506
|
+
Generate LLVM IR for a for-loop in [start, count).
|
|
507
|
+
*start* is equal to 0 by default.
|
|
508
|
+
|
|
509
|
+
Yields a Loop namedtuple with the following members:
|
|
510
|
+
- `index` is the loop index's value
|
|
511
|
+
- `do_break` is a no-argument callable to break out of the loop
|
|
512
|
+
"""
|
|
513
|
+
if intp is None:
|
|
514
|
+
intp = count.type
|
|
515
|
+
if start is None:
|
|
516
|
+
start = intp(0)
|
|
517
|
+
stop = count
|
|
518
|
+
|
|
519
|
+
bbcond = builder.append_basic_block("for.cond")
|
|
520
|
+
bbbody = builder.append_basic_block("for.body")
|
|
521
|
+
bbend = builder.append_basic_block("for.end")
|
|
522
|
+
|
|
523
|
+
def do_break():
|
|
524
|
+
builder.branch(bbend)
|
|
525
|
+
|
|
526
|
+
bbstart = builder.basic_block
|
|
527
|
+
builder.branch(bbcond)
|
|
528
|
+
|
|
529
|
+
with builder.goto_block(bbcond):
|
|
530
|
+
index = builder.phi(intp, name="loop.index")
|
|
531
|
+
pred = builder.icmp_signed("<", index, stop)
|
|
532
|
+
builder.cbranch(pred, bbbody, bbend)
|
|
533
|
+
|
|
534
|
+
with builder.goto_block(bbbody):
|
|
535
|
+
yield Loop(index, do_break)
|
|
536
|
+
# Update bbbody as a new basic block may have been activated
|
|
537
|
+
bbbody = builder.basic_block
|
|
538
|
+
incr = increment_index(builder, index)
|
|
539
|
+
terminate(builder, bbcond)
|
|
540
|
+
|
|
541
|
+
index.add_incoming(start, bbstart)
|
|
542
|
+
index.add_incoming(incr, bbbody)
|
|
543
|
+
|
|
544
|
+
builder.position_at_end(bbend)
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
@contextmanager
|
|
548
|
+
def for_range_slice(builder, start, stop, step, intp=None, inc=True):
|
|
549
|
+
"""
|
|
550
|
+
Generate LLVM IR for a for-loop based on a slice. Yields a
|
|
551
|
+
(index, count) tuple where `index` is the slice index's value
|
|
552
|
+
inside the loop, and `count` the iteration count.
|
|
553
|
+
|
|
554
|
+
Parameters
|
|
555
|
+
-------------
|
|
556
|
+
builder : object
|
|
557
|
+
IRBuilder object
|
|
558
|
+
start : int
|
|
559
|
+
The beginning value of the slice
|
|
560
|
+
stop : int
|
|
561
|
+
The end value of the slice
|
|
562
|
+
step : int
|
|
563
|
+
The step value of the slice
|
|
564
|
+
intp :
|
|
565
|
+
The data type
|
|
566
|
+
inc : boolean, optional
|
|
567
|
+
Signals whether the step is positive (True) or negative (False).
|
|
568
|
+
|
|
569
|
+
Returns
|
|
570
|
+
-----------
|
|
571
|
+
None
|
|
572
|
+
"""
|
|
573
|
+
if intp is None:
|
|
574
|
+
intp = start.type
|
|
575
|
+
|
|
576
|
+
bbcond = builder.append_basic_block("for.cond")
|
|
577
|
+
bbbody = builder.append_basic_block("for.body")
|
|
578
|
+
bbend = builder.append_basic_block("for.end")
|
|
579
|
+
bbstart = builder.basic_block
|
|
580
|
+
builder.branch(bbcond)
|
|
581
|
+
|
|
582
|
+
with builder.goto_block(bbcond):
|
|
583
|
+
index = builder.phi(intp, name="loop.index")
|
|
584
|
+
count = builder.phi(intp, name="loop.count")
|
|
585
|
+
if inc:
|
|
586
|
+
pred = builder.icmp_signed("<", index, stop)
|
|
587
|
+
else:
|
|
588
|
+
pred = builder.icmp_signed(">", index, stop)
|
|
589
|
+
builder.cbranch(pred, bbbody, bbend)
|
|
590
|
+
|
|
591
|
+
with builder.goto_block(bbbody):
|
|
592
|
+
yield index, count
|
|
593
|
+
bbbody = builder.basic_block
|
|
594
|
+
incr = builder.add(index, step)
|
|
595
|
+
next_count = increment_index(builder, count)
|
|
596
|
+
terminate(builder, bbcond)
|
|
597
|
+
|
|
598
|
+
index.add_incoming(start, bbstart)
|
|
599
|
+
index.add_incoming(incr, bbbody)
|
|
600
|
+
count.add_incoming(ir.Constant(intp, 0), bbstart)
|
|
601
|
+
count.add_incoming(next_count, bbbody)
|
|
602
|
+
builder.position_at_end(bbend)
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
@contextmanager
|
|
606
|
+
def for_range_slice_generic(builder, start, stop, step):
|
|
607
|
+
"""
|
|
608
|
+
A helper wrapper for for_range_slice(). This is a context manager which
|
|
609
|
+
yields two for_range_slice()-alike context managers, the first for
|
|
610
|
+
the positive step case, the second for the negative step case.
|
|
611
|
+
|
|
612
|
+
Use:
|
|
613
|
+
with for_range_slice_generic(...) as (pos_range, neg_range):
|
|
614
|
+
with pos_range as (idx, count):
|
|
615
|
+
...
|
|
616
|
+
with neg_range as (idx, count):
|
|
617
|
+
...
|
|
618
|
+
"""
|
|
619
|
+
intp = start.type
|
|
620
|
+
is_pos_step = builder.icmp_signed(">=", step, ir.Constant(intp, 0))
|
|
621
|
+
|
|
622
|
+
pos_for_range = for_range_slice(builder, start, stop, step, intp, inc=True)
|
|
623
|
+
neg_for_range = for_range_slice(builder, start, stop, step, intp, inc=False)
|
|
624
|
+
|
|
625
|
+
@contextmanager
|
|
626
|
+
def cm_cond(cond, inner_cm):
|
|
627
|
+
with cond:
|
|
628
|
+
with inner_cm as value:
|
|
629
|
+
yield value
|
|
630
|
+
|
|
631
|
+
with builder.if_else(is_pos_step, likely=True) as (then, otherwise):
|
|
632
|
+
yield cm_cond(then, pos_for_range), cm_cond(otherwise, neg_for_range)
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
@contextmanager
|
|
636
|
+
def loop_nest(builder, shape, intp, order="C"):
|
|
637
|
+
"""
|
|
638
|
+
Generate a loop nest walking a N-dimensional array.
|
|
639
|
+
Yields a tuple of N indices for use in the inner loop body,
|
|
640
|
+
iterating over the *shape* space.
|
|
641
|
+
|
|
642
|
+
If *order* is 'C' (the default), indices are incremented inside-out
|
|
643
|
+
(i.e. (0,0), (0,1), (0,2), (1,0) etc.).
|
|
644
|
+
If *order* is 'F', they are incremented outside-in
|
|
645
|
+
(i.e. (0,0), (1,0), (2,0), (0,1) etc.).
|
|
646
|
+
This has performance implications when walking an array as it impacts
|
|
647
|
+
the spatial locality of memory accesses.
|
|
648
|
+
"""
|
|
649
|
+
assert order in "CF"
|
|
650
|
+
if not shape:
|
|
651
|
+
# 0-d array
|
|
652
|
+
yield ()
|
|
653
|
+
else:
|
|
654
|
+
if order == "F":
|
|
655
|
+
_swap = lambda x: x[::-1]
|
|
656
|
+
else:
|
|
657
|
+
_swap = lambda x: x
|
|
658
|
+
with _loop_nest(builder, _swap(shape), intp) as indices:
|
|
659
|
+
assert len(indices) == len(shape)
|
|
660
|
+
yield _swap(indices)
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
@contextmanager
|
|
664
|
+
def _loop_nest(builder, shape, intp):
|
|
665
|
+
with for_range(builder, shape[0], intp=intp) as loop:
|
|
666
|
+
if len(shape) > 1:
|
|
667
|
+
with _loop_nest(builder, shape[1:], intp) as indices:
|
|
668
|
+
yield (loop.index,) + indices
|
|
669
|
+
else:
|
|
670
|
+
yield (loop.index,)
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
def pack_array(builder, values, ty=None):
|
|
674
|
+
"""
|
|
675
|
+
Pack a sequence of values in a LLVM array. *ty* should be given
|
|
676
|
+
if the array may be empty, in which case the type can't be inferred
|
|
677
|
+
from the values.
|
|
678
|
+
"""
|
|
679
|
+
n = len(values)
|
|
680
|
+
if ty is None:
|
|
681
|
+
ty = values[0].type
|
|
682
|
+
ary = ir.ArrayType(ty, n)(ir.Undefined)
|
|
683
|
+
for i, v in enumerate(values):
|
|
684
|
+
ary = builder.insert_value(ary, v, i)
|
|
685
|
+
return ary
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
def pack_struct(builder, values):
|
|
689
|
+
"""
|
|
690
|
+
Pack a sequence of values into a LLVM struct.
|
|
691
|
+
"""
|
|
692
|
+
structty = ir.LiteralStructType([v.type for v in values])
|
|
693
|
+
st = structty(ir.Undefined)
|
|
694
|
+
for i, v in enumerate(values):
|
|
695
|
+
st = builder.insert_value(st, v, i)
|
|
696
|
+
return st
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
def unpack_tuple(builder, tup, count=None):
|
|
700
|
+
"""
|
|
701
|
+
Unpack an array or structure of values, return a Python tuple.
|
|
702
|
+
"""
|
|
703
|
+
if count is None:
|
|
704
|
+
# Assuming *tup* is an aggregate
|
|
705
|
+
count = len(tup.type.elements)
|
|
706
|
+
vals = [builder.extract_value(tup, i) for i in range(count)]
|
|
707
|
+
return vals
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
def get_item_pointer(
|
|
711
|
+
context, builder, aryty, ary, inds, wraparound=False, boundscheck=False
|
|
712
|
+
):
|
|
713
|
+
# Set boundscheck=True for any pointer access that should be
|
|
714
|
+
# boundschecked. do_boundscheck() will handle enabling or disabling the
|
|
715
|
+
# actual boundschecking based on the user config.
|
|
716
|
+
shapes = unpack_tuple(builder, ary.shape, count=aryty.ndim)
|
|
717
|
+
strides = unpack_tuple(builder, ary.strides, count=aryty.ndim)
|
|
718
|
+
return get_item_pointer2(
|
|
719
|
+
context,
|
|
720
|
+
builder,
|
|
721
|
+
data=ary.data,
|
|
722
|
+
shape=shapes,
|
|
723
|
+
strides=strides,
|
|
724
|
+
layout=aryty.layout,
|
|
725
|
+
inds=inds,
|
|
726
|
+
wraparound=wraparound,
|
|
727
|
+
boundscheck=boundscheck,
|
|
728
|
+
)
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
def do_boundscheck(context, builder, ind, dimlen, axis=None):
|
|
732
|
+
def _dbg():
|
|
733
|
+
# Remove this when we figure out how to include this information
|
|
734
|
+
# in the error message.
|
|
735
|
+
if axis is not None:
|
|
736
|
+
if isinstance(axis, int):
|
|
737
|
+
printf(
|
|
738
|
+
builder,
|
|
739
|
+
"debug: IndexError: index %d is out of bounds "
|
|
740
|
+
"for axis {} with size %d\n".format(axis),
|
|
741
|
+
ind,
|
|
742
|
+
dimlen,
|
|
743
|
+
)
|
|
744
|
+
else:
|
|
745
|
+
printf(
|
|
746
|
+
builder,
|
|
747
|
+
"debug: IndexError: index %d is out of bounds "
|
|
748
|
+
"for axis %d with size %d\n",
|
|
749
|
+
ind,
|
|
750
|
+
axis,
|
|
751
|
+
dimlen,
|
|
752
|
+
)
|
|
753
|
+
else:
|
|
754
|
+
printf(
|
|
755
|
+
builder,
|
|
756
|
+
"debug: IndexError: index %d is out of bounds for size %d\n",
|
|
757
|
+
ind,
|
|
758
|
+
dimlen,
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
msg = "index is out of bounds"
|
|
762
|
+
out_of_bounds_upper = builder.icmp_signed(">=", ind, dimlen)
|
|
763
|
+
with if_unlikely(builder, out_of_bounds_upper):
|
|
764
|
+
if config.FULL_TRACEBACKS:
|
|
765
|
+
_dbg()
|
|
766
|
+
context.call_conv.return_user_exc(builder, IndexError, (msg,))
|
|
767
|
+
out_of_bounds_lower = builder.icmp_signed("<", ind, ind.type(0))
|
|
768
|
+
with if_unlikely(builder, out_of_bounds_lower):
|
|
769
|
+
if config.FULL_TRACEBACKS:
|
|
770
|
+
_dbg()
|
|
771
|
+
context.call_conv.return_user_exc(builder, IndexError, (msg,))
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
def get_item_pointer2(
|
|
775
|
+
context,
|
|
776
|
+
builder,
|
|
777
|
+
data,
|
|
778
|
+
shape,
|
|
779
|
+
strides,
|
|
780
|
+
layout,
|
|
781
|
+
inds,
|
|
782
|
+
wraparound=False,
|
|
783
|
+
boundscheck=False,
|
|
784
|
+
):
|
|
785
|
+
# Set boundscheck=True for any pointer access that should be
|
|
786
|
+
# boundschecked. do_boundscheck() will handle enabling or disabling the
|
|
787
|
+
# actual boundschecking based on the user config.
|
|
788
|
+
if wraparound:
|
|
789
|
+
# Wraparound
|
|
790
|
+
indices = []
|
|
791
|
+
for ind, dimlen in zip(inds, shape):
|
|
792
|
+
negative = builder.icmp_signed("<", ind, ind.type(0))
|
|
793
|
+
wrapped = builder.add(dimlen, ind)
|
|
794
|
+
selected = builder.select(negative, wrapped, ind)
|
|
795
|
+
indices.append(selected)
|
|
796
|
+
else:
|
|
797
|
+
indices = inds
|
|
798
|
+
if boundscheck:
|
|
799
|
+
for axis, (ind, dimlen) in enumerate(zip(indices, shape)):
|
|
800
|
+
do_boundscheck(context, builder, ind, dimlen, axis)
|
|
801
|
+
|
|
802
|
+
if not indices:
|
|
803
|
+
# Indexing with empty tuple
|
|
804
|
+
return builder.gep(data, [int32_t(0)])
|
|
805
|
+
intp = indices[0].type
|
|
806
|
+
# Indexing code
|
|
807
|
+
if layout in "CF":
|
|
808
|
+
steps = []
|
|
809
|
+
# Compute steps for each dimension
|
|
810
|
+
if layout == "C":
|
|
811
|
+
# C contiguous
|
|
812
|
+
for i in range(len(shape)):
|
|
813
|
+
last = intp(1)
|
|
814
|
+
for j in shape[i + 1 :]:
|
|
815
|
+
last = builder.mul(last, j)
|
|
816
|
+
steps.append(last)
|
|
817
|
+
elif layout == "F":
|
|
818
|
+
# F contiguous
|
|
819
|
+
for i in range(len(shape)):
|
|
820
|
+
last = intp(1)
|
|
821
|
+
for j in shape[:i]:
|
|
822
|
+
last = builder.mul(last, j)
|
|
823
|
+
steps.append(last)
|
|
824
|
+
else:
|
|
825
|
+
raise Exception("unreachable")
|
|
826
|
+
|
|
827
|
+
# Compute index
|
|
828
|
+
loc = intp(0)
|
|
829
|
+
for i, s in zip(indices, steps):
|
|
830
|
+
tmp = builder.mul(i, s)
|
|
831
|
+
loc = builder.add(loc, tmp)
|
|
832
|
+
ptr = builder.gep(data, [loc])
|
|
833
|
+
return ptr
|
|
834
|
+
else:
|
|
835
|
+
# Any layout
|
|
836
|
+
dimoffs = [builder.mul(s, i) for s, i in zip(strides, indices)]
|
|
837
|
+
offset = functools.reduce(builder.add, dimoffs)
|
|
838
|
+
return pointer_add(builder, data, offset)
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def _scalar_pred_against_zero(builder, value, fpred, icond):
|
|
842
|
+
nullval = value.type(0)
|
|
843
|
+
if isinstance(value.type, (ir.FloatType, ir.DoubleType)):
|
|
844
|
+
isnull = fpred(value, nullval)
|
|
845
|
+
elif isinstance(value.type, ir.IntType):
|
|
846
|
+
isnull = builder.icmp_signed(icond, value, nullval)
|
|
847
|
+
else:
|
|
848
|
+
raise TypeError("unexpected value type %s" % (value.type,))
|
|
849
|
+
return isnull
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
def is_scalar_zero(builder, value):
|
|
853
|
+
"""
|
|
854
|
+
Return a predicate representing whether *value* is equal to zero.
|
|
855
|
+
"""
|
|
856
|
+
return _scalar_pred_against_zero(
|
|
857
|
+
builder, value, functools.partial(builder.fcmp_ordered, "=="), "=="
|
|
858
|
+
)
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
def is_not_scalar_zero(builder, value):
|
|
862
|
+
"""
|
|
863
|
+
Return a predicate representing whether a *value* is not equal to zero.
|
|
864
|
+
(not exactly "not is_scalar_zero" because of nans)
|
|
865
|
+
"""
|
|
866
|
+
return _scalar_pred_against_zero(
|
|
867
|
+
builder, value, functools.partial(builder.fcmp_unordered, "!="), "!="
|
|
868
|
+
)
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
def is_scalar_zero_or_nan(builder, value):
|
|
872
|
+
"""
|
|
873
|
+
Return a predicate representing whether *value* is equal to either zero
|
|
874
|
+
or NaN.
|
|
875
|
+
"""
|
|
876
|
+
return _scalar_pred_against_zero(
|
|
877
|
+
builder, value, functools.partial(builder.fcmp_unordered, "=="), "=="
|
|
878
|
+
)
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
is_true = is_not_scalar_zero
|
|
882
|
+
is_false = is_scalar_zero
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
def is_scalar_neg(builder, value):
|
|
886
|
+
"""
|
|
887
|
+
Is *value* negative? Assumes *value* is signed.
|
|
888
|
+
"""
|
|
889
|
+
return _scalar_pred_against_zero(
|
|
890
|
+
builder, value, functools.partial(builder.fcmp_ordered, "<"), "<"
|
|
891
|
+
)
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
@contextmanager
|
|
895
|
+
def early_exit_if(builder, stack: ExitStack, cond):
|
|
896
|
+
"""
|
|
897
|
+
The Python code::
|
|
898
|
+
|
|
899
|
+
with contextlib.ExitStack() as stack:
|
|
900
|
+
with early_exit_if(builder, stack, cond):
|
|
901
|
+
cleanup()
|
|
902
|
+
body()
|
|
903
|
+
|
|
904
|
+
emits the code::
|
|
905
|
+
|
|
906
|
+
if (cond) {
|
|
907
|
+
<cleanup>
|
|
908
|
+
}
|
|
909
|
+
else {
|
|
910
|
+
<body>
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
This can be useful for generating code with lots of early exits, without
|
|
914
|
+
having to increase the indentation each time.
|
|
915
|
+
"""
|
|
916
|
+
then, otherwise = stack.enter_context(builder.if_else(cond, likely=False))
|
|
917
|
+
with then:
|
|
918
|
+
yield
|
|
919
|
+
stack.enter_context(otherwise)
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
def early_exit_if_null(builder, stack, obj):
|
|
923
|
+
"""
|
|
924
|
+
A convenience wrapper for :func:`early_exit_if`, for the common case where
|
|
925
|
+
the CPython API indicates an error by returning ``NULL``.
|
|
926
|
+
"""
|
|
927
|
+
return early_exit_if(builder, stack, is_null(builder, obj))
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
def guard_null(context, builder, value, exc_tuple):
|
|
931
|
+
"""
|
|
932
|
+
Guard against *value* being null or zero.
|
|
933
|
+
*exc_tuple* should be a (exception type, arguments...) tuple.
|
|
934
|
+
"""
|
|
935
|
+
with builder.if_then(is_scalar_zero(builder, value), likely=False):
|
|
936
|
+
exc = exc_tuple[0]
|
|
937
|
+
exc_args = exc_tuple[1:] or None
|
|
938
|
+
context.call_conv.return_user_exc(builder, exc, exc_args)
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
def guard_memory_error(context, builder, pointer, msg=None):
|
|
942
|
+
"""
|
|
943
|
+
Guard against *pointer* being NULL (and raise a MemoryError).
|
|
944
|
+
"""
|
|
945
|
+
assert isinstance(pointer.type, ir.PointerType), pointer.type
|
|
946
|
+
exc_args = (msg,) if msg else ()
|
|
947
|
+
with builder.if_then(is_null(builder, pointer), likely=False):
|
|
948
|
+
context.call_conv.return_user_exc(builder, MemoryError, exc_args)
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
@contextmanager
|
|
952
|
+
def if_zero(builder, value, likely=False):
|
|
953
|
+
"""
|
|
954
|
+
Execute the given block if the scalar value is zero.
|
|
955
|
+
"""
|
|
956
|
+
with builder.if_then(is_scalar_zero(builder, value), likely=likely):
|
|
957
|
+
yield
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
guard_zero = guard_null
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
def is_pointer(ltyp):
|
|
964
|
+
"""
|
|
965
|
+
Whether the LLVM type *typ* is a struct type.
|
|
966
|
+
"""
|
|
967
|
+
return isinstance(ltyp, ir.PointerType)
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def get_record_member(builder, record, offset, typ):
|
|
971
|
+
pval = gep_inbounds(builder, record, 0, offset)
|
|
972
|
+
assert not is_pointer(pval.type.pointee)
|
|
973
|
+
return builder.bitcast(pval, typ.as_pointer())
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
def is_neg_int(builder, val):
|
|
977
|
+
return builder.icmp_signed("<", val, val.type(0))
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
def gep_inbounds(builder, ptr, *inds, **kws):
|
|
981
|
+
"""
|
|
982
|
+
Same as *gep*, but add the `inbounds` keyword.
|
|
983
|
+
"""
|
|
984
|
+
return gep(builder, ptr, *inds, inbounds=True, **kws)
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
def gep(builder, ptr, *inds, **kws):
|
|
988
|
+
"""
|
|
989
|
+
Emit a getelementptr instruction for the given pointer and indices.
|
|
990
|
+
The indices can be LLVM values or Python int constants.
|
|
991
|
+
"""
|
|
992
|
+
name = kws.pop("name", "")
|
|
993
|
+
inbounds = kws.pop("inbounds", False)
|
|
994
|
+
assert not kws
|
|
995
|
+
idx = []
|
|
996
|
+
for i in inds:
|
|
997
|
+
if isinstance(i, int):
|
|
998
|
+
# NOTE: llvm only accepts int32 inside structs, not int64
|
|
999
|
+
ind = int32_t(i)
|
|
1000
|
+
else:
|
|
1001
|
+
ind = i
|
|
1002
|
+
idx.append(ind)
|
|
1003
|
+
return builder.gep(ptr, idx, name=name, inbounds=inbounds)
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
def pointer_add(builder, ptr, offset, return_type=None):
|
|
1007
|
+
"""
|
|
1008
|
+
Add an integral *offset* to pointer *ptr*, and return a pointer
|
|
1009
|
+
of *return_type* (or, if omitted, the same type as *ptr*).
|
|
1010
|
+
|
|
1011
|
+
Note the computation is done in bytes, and ignores the width of
|
|
1012
|
+
the pointed item type.
|
|
1013
|
+
"""
|
|
1014
|
+
intptr = builder.ptrtoint(ptr, intp_t)
|
|
1015
|
+
if isinstance(offset, int):
|
|
1016
|
+
offset = intp_t(offset)
|
|
1017
|
+
intptr = builder.add(intptr, offset)
|
|
1018
|
+
return builder.inttoptr(intptr, return_type or ptr.type)
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
def memset(builder, ptr, size, value):
|
|
1022
|
+
"""
|
|
1023
|
+
Fill *size* bytes starting from *ptr* with *value*.
|
|
1024
|
+
"""
|
|
1025
|
+
fn = builder.module.declare_intrinsic("llvm.memset", (voidptr_t, size.type))
|
|
1026
|
+
ptr = builder.bitcast(ptr, voidptr_t)
|
|
1027
|
+
if isinstance(value, int):
|
|
1028
|
+
value = int8_t(value)
|
|
1029
|
+
builder.call(fn, [ptr, value, size, bool_t(0)])
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
def memset_padding(builder, ptr):
|
|
1033
|
+
"""
|
|
1034
|
+
Fill padding bytes of the pointee with zeros.
|
|
1035
|
+
"""
|
|
1036
|
+
# Load existing value
|
|
1037
|
+
val = builder.load(ptr)
|
|
1038
|
+
# Fill pointee with zeros
|
|
1039
|
+
memset(builder, ptr, sizeof(builder, ptr.type), 0)
|
|
1040
|
+
# Store value back
|
|
1041
|
+
builder.store(val, ptr)
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
def global_constant(builder_or_module, name, value, linkage="internal"):
|
|
1045
|
+
"""
|
|
1046
|
+
Get or create a (LLVM module-)global constant with *name* or *value*.
|
|
1047
|
+
"""
|
|
1048
|
+
if isinstance(builder_or_module, ir.Module):
|
|
1049
|
+
module = builder_or_module
|
|
1050
|
+
else:
|
|
1051
|
+
module = builder_or_module.module
|
|
1052
|
+
data = add_global_variable(module, value.type, name)
|
|
1053
|
+
data.linkage = linkage
|
|
1054
|
+
data.global_constant = True
|
|
1055
|
+
data.initializer = value
|
|
1056
|
+
return data
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
def divmod_by_constant(builder, val, divisor):
|
|
1060
|
+
"""
|
|
1061
|
+
Compute the (quotient, remainder) of *val* divided by the constant
|
|
1062
|
+
positive *divisor*. The semantics reflects those of Python integer
|
|
1063
|
+
floor division, rather than C's / LLVM's signed division and modulo.
|
|
1064
|
+
The difference lies with a negative *val*.
|
|
1065
|
+
"""
|
|
1066
|
+
assert divisor > 0
|
|
1067
|
+
divisor = val.type(divisor)
|
|
1068
|
+
one = val.type(1)
|
|
1069
|
+
|
|
1070
|
+
quot = alloca_once(builder, val.type)
|
|
1071
|
+
|
|
1072
|
+
with builder.if_else(is_neg_int(builder, val)) as (if_neg, if_pos):
|
|
1073
|
+
with if_pos:
|
|
1074
|
+
# quot = val / divisor
|
|
1075
|
+
quot_val = builder.sdiv(val, divisor)
|
|
1076
|
+
builder.store(quot_val, quot)
|
|
1077
|
+
with if_neg:
|
|
1078
|
+
# quot = -1 + (val + 1) / divisor
|
|
1079
|
+
val_plus_one = builder.add(val, one)
|
|
1080
|
+
quot_val = builder.sdiv(val_plus_one, divisor)
|
|
1081
|
+
builder.store(builder.sub(quot_val, one), quot)
|
|
1082
|
+
|
|
1083
|
+
# rem = val - quot * divisor
|
|
1084
|
+
# (should be slightly faster than a separate modulo operation)
|
|
1085
|
+
quot_val = builder.load(quot)
|
|
1086
|
+
rem_val = builder.sub(val, builder.mul(quot_val, divisor))
|
|
1087
|
+
return quot_val, rem_val
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
def cbranch_or_continue(builder, cond, bbtrue):
|
|
1091
|
+
"""
|
|
1092
|
+
Branch conditionally or continue.
|
|
1093
|
+
|
|
1094
|
+
Note: a new block is created and builder is moved to the end of the new
|
|
1095
|
+
block.
|
|
1096
|
+
"""
|
|
1097
|
+
bbcont = builder.append_basic_block(".continue")
|
|
1098
|
+
builder.cbranch(cond, bbtrue, bbcont)
|
|
1099
|
+
builder.position_at_end(bbcont)
|
|
1100
|
+
return bbcont
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
def memcpy(builder, dst, src, count):
|
|
1104
|
+
"""
|
|
1105
|
+
Emit a memcpy to the builder.
|
|
1106
|
+
|
|
1107
|
+
Copies each element of dst to src. Unlike the C equivalent, each element
|
|
1108
|
+
can be any LLVM type.
|
|
1109
|
+
|
|
1110
|
+
Assumes
|
|
1111
|
+
-------
|
|
1112
|
+
* dst.type == src.type
|
|
1113
|
+
* count is positive
|
|
1114
|
+
"""
|
|
1115
|
+
# Note this does seem to be optimized as a raw memcpy() by LLVM
|
|
1116
|
+
# whenever possible...
|
|
1117
|
+
assert dst.type == src.type
|
|
1118
|
+
with for_range(builder, count, intp=count.type) as loop:
|
|
1119
|
+
out_ptr = builder.gep(dst, [loop.index])
|
|
1120
|
+
in_ptr = builder.gep(src, [loop.index])
|
|
1121
|
+
builder.store(builder.load(in_ptr), out_ptr)
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
def _raw_memcpy(builder, func_name, dst, src, count, itemsize, align):
|
|
1125
|
+
size_t = count.type
|
|
1126
|
+
if isinstance(itemsize, int):
|
|
1127
|
+
itemsize = ir.Constant(size_t, itemsize)
|
|
1128
|
+
|
|
1129
|
+
memcpy = builder.module.declare_intrinsic(
|
|
1130
|
+
func_name, [voidptr_t, voidptr_t, size_t]
|
|
1131
|
+
)
|
|
1132
|
+
is_volatile = false_bit
|
|
1133
|
+
builder.call(
|
|
1134
|
+
memcpy,
|
|
1135
|
+
[
|
|
1136
|
+
builder.bitcast(dst, voidptr_t),
|
|
1137
|
+
builder.bitcast(src, voidptr_t),
|
|
1138
|
+
builder.mul(count, itemsize),
|
|
1139
|
+
is_volatile,
|
|
1140
|
+
],
|
|
1141
|
+
)
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
def raw_memcpy(builder, dst, src, count, itemsize, align=1):
|
|
1145
|
+
"""
|
|
1146
|
+
Emit a raw memcpy() call for `count` items of size `itemsize`
|
|
1147
|
+
from `src` to `dest`.
|
|
1148
|
+
"""
|
|
1149
|
+
return _raw_memcpy(builder, "llvm.memcpy", dst, src, count, itemsize, align)
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
def raw_memmove(builder, dst, src, count, itemsize, align=1):
|
|
1153
|
+
"""
|
|
1154
|
+
Emit a raw memmove() call for `count` items of size `itemsize`
|
|
1155
|
+
from `src` to `dest`.
|
|
1156
|
+
"""
|
|
1157
|
+
return _raw_memcpy(
|
|
1158
|
+
builder, "llvm.memmove", dst, src, count, itemsize, align
|
|
1159
|
+
)
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
def muladd_with_overflow(builder, a, b, c):
|
|
1163
|
+
"""
|
|
1164
|
+
Compute (a * b + c) and return a (result, overflow bit) pair.
|
|
1165
|
+
The operands must be signed integers.
|
|
1166
|
+
"""
|
|
1167
|
+
p = builder.smul_with_overflow(a, b)
|
|
1168
|
+
prod = builder.extract_value(p, 0)
|
|
1169
|
+
prod_ovf = builder.extract_value(p, 1)
|
|
1170
|
+
s = builder.sadd_with_overflow(prod, c)
|
|
1171
|
+
res = builder.extract_value(s, 0)
|
|
1172
|
+
ovf = builder.or_(prod_ovf, builder.extract_value(s, 1))
|
|
1173
|
+
return res, ovf
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
def printf(builder, format, *args):
|
|
1177
|
+
"""
|
|
1178
|
+
Calls printf().
|
|
1179
|
+
Argument `format` is expected to be a Python string.
|
|
1180
|
+
Values to be printed are listed in `args`.
|
|
1181
|
+
|
|
1182
|
+
Note: There is no checking to ensure there is correct number of values
|
|
1183
|
+
in `args` and there type matches the declaration in the format string.
|
|
1184
|
+
"""
|
|
1185
|
+
assert isinstance(format, str)
|
|
1186
|
+
mod = builder.module
|
|
1187
|
+
# Make global constant for format string
|
|
1188
|
+
cstring = voidptr_t
|
|
1189
|
+
fmt_bytes = make_bytearray((format + "\00").encode("ascii"))
|
|
1190
|
+
global_fmt = global_constant(mod, "printf_format", fmt_bytes)
|
|
1191
|
+
fnty = ir.FunctionType(int32_t, [cstring], var_arg=True)
|
|
1192
|
+
# Insert printf()
|
|
1193
|
+
try:
|
|
1194
|
+
fn = mod.get_global("printf")
|
|
1195
|
+
except KeyError:
|
|
1196
|
+
fn = ir.Function(mod, fnty, name="printf")
|
|
1197
|
+
# Call
|
|
1198
|
+
ptr_fmt = builder.bitcast(global_fmt, cstring)
|
|
1199
|
+
return builder.call(fn, [ptr_fmt] + list(args))
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
def snprintf(builder, buffer, bufsz, format, *args):
|
|
1203
|
+
"""Calls libc snprintf(buffer, bufsz, format, ...args)"""
|
|
1204
|
+
assert isinstance(format, str)
|
|
1205
|
+
mod = builder.module
|
|
1206
|
+
# Make global constant for format string
|
|
1207
|
+
cstring = voidptr_t
|
|
1208
|
+
fmt_bytes = make_bytearray((format + "\00").encode("ascii"))
|
|
1209
|
+
global_fmt = global_constant(mod, "snprintf_format", fmt_bytes)
|
|
1210
|
+
fnty = ir.FunctionType(
|
|
1211
|
+
int32_t,
|
|
1212
|
+
[cstring, intp_t, cstring],
|
|
1213
|
+
var_arg=True,
|
|
1214
|
+
)
|
|
1215
|
+
# Actual symbol name of snprintf is different on win32.
|
|
1216
|
+
symbol = "snprintf"
|
|
1217
|
+
if config.IS_WIN32:
|
|
1218
|
+
symbol = "_" + symbol
|
|
1219
|
+
# Insert snprintf()
|
|
1220
|
+
try:
|
|
1221
|
+
fn = mod.get_global(symbol)
|
|
1222
|
+
except KeyError:
|
|
1223
|
+
fn = ir.Function(mod, fnty, name=symbol)
|
|
1224
|
+
# Call
|
|
1225
|
+
ptr_fmt = builder.bitcast(global_fmt, cstring)
|
|
1226
|
+
return builder.call(fn, [buffer, bufsz, ptr_fmt] + list(args))
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
def snprintf_stackbuffer(builder, bufsz, format, *args):
|
|
1230
|
+
"""Similar to `snprintf()` but the buffer is stack allocated to size
|
|
1231
|
+
*bufsz*.
|
|
1232
|
+
|
|
1233
|
+
Returns the buffer pointer as i8*.
|
|
1234
|
+
"""
|
|
1235
|
+
assert isinstance(bufsz, int)
|
|
1236
|
+
spacety = ir.ArrayType(ir.IntType(8), bufsz)
|
|
1237
|
+
space = alloca_once(builder, spacety, zfill=True)
|
|
1238
|
+
buffer = builder.bitcast(space, voidptr_t)
|
|
1239
|
+
snprintf(builder, buffer, intp_t(bufsz), format, *args)
|
|
1240
|
+
return buffer
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
def normalize_ir_text(text):
|
|
1244
|
+
"""
|
|
1245
|
+
Normalize the given string to latin1 compatible encoding that is
|
|
1246
|
+
suitable for use in LLVM IR.
|
|
1247
|
+
"""
|
|
1248
|
+
# Just re-encoding to latin1 is enough
|
|
1249
|
+
return text.encode("utf8").decode("latin1")
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
def hexdump(builder, ptr, nbytes):
|
|
1253
|
+
"""Debug print the memory region in *ptr* to *ptr + nbytes*
|
|
1254
|
+
as hex.
|
|
1255
|
+
"""
|
|
1256
|
+
bytes_per_line = 16
|
|
1257
|
+
nbytes = builder.zext(nbytes, intp_t)
|
|
1258
|
+
printf(builder, "hexdump p=%p n=%zu", ptr, nbytes)
|
|
1259
|
+
byte_t = ir.IntType(8)
|
|
1260
|
+
ptr = builder.bitcast(ptr, byte_t.as_pointer())
|
|
1261
|
+
# Loop to print the bytes in *ptr* as hex
|
|
1262
|
+
with for_range(builder, nbytes) as idx:
|
|
1263
|
+
div_by = builder.urem(idx.index, intp_t(bytes_per_line))
|
|
1264
|
+
do_new_line = builder.icmp_unsigned("==", div_by, intp_t(0))
|
|
1265
|
+
with builder.if_then(do_new_line):
|
|
1266
|
+
printf(builder, "\n")
|
|
1267
|
+
|
|
1268
|
+
offset = builder.gep(ptr, [idx.index])
|
|
1269
|
+
val = builder.load(offset)
|
|
1270
|
+
printf(builder, " %02x", val)
|
|
1271
|
+
printf(builder, "\n")
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
def is_nonelike(ty):
|
|
1275
|
+
"""returns if 'ty' is none"""
|
|
1276
|
+
return (
|
|
1277
|
+
ty is None
|
|
1278
|
+
or isinstance(ty, types.NoneType)
|
|
1279
|
+
or isinstance(ty, types.Omitted)
|
|
1280
|
+
)
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
def is_empty_tuple(ty):
|
|
1284
|
+
"""returns if 'ty' is an empty tuple"""
|
|
1285
|
+
return isinstance(ty, types.Tuple) and len(ty.types) == 0
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
def create_constant_array(ty, val):
|
|
1289
|
+
"""
|
|
1290
|
+
Create an LLVM-constant of a fixed-length array from Python values.
|
|
1291
|
+
|
|
1292
|
+
The type provided is the type of the elements.
|
|
1293
|
+
"""
|
|
1294
|
+
return ir.Constant(ir.ArrayType(ty, len(val)), val)
|