|
| 1 | +# SPDX-FileCopyrightText: 2024 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +"""Contains SPIR-V specific array functions.""" |
| 6 | + |
| 7 | +import operator |
| 8 | +from functools import reduce |
| 9 | +from typing import Union |
| 10 | + |
| 11 | +import llvmlite.ir as llvmir |
| 12 | +from llvmlite.ir.builder import IRBuilder |
| 13 | +from numba.core import cgutils, errors, types |
| 14 | +from numba.core.base import BaseContext |
| 15 | + |
| 16 | +from numba_dpex.kernel_api_impl.spirv.target import SPIRVTargetContext |
| 17 | +from numba_dpex.ocl.oclimpl import _get_target_data |
| 18 | + |
| 19 | + |
| 20 | +def get_itemsize(context: SPIRVTargetContext, array_type: types.Array): |
| 21 | + """ |
| 22 | + Return the item size for the given array or buffer type. |
| 23 | + Same as numba.np.arrayobj.get_itemsize, but using spirv data. |
| 24 | + """ |
| 25 | + targetdata = _get_target_data(context) |
| 26 | + lldtype = context.get_data_type(array_type.dtype) |
| 27 | + return lldtype.get_abi_size(targetdata) |
| 28 | + |
| 29 | + |
| 30 | +def require_literal(literal_type: types.Type): |
| 31 | + """Checks if the numba type is Literal. If iterable object is passed, |
| 32 | + checks that every element is Literal. |
| 33 | +
|
| 34 | + Raises: |
| 35 | + TypingError: When argument is not Iterable. |
| 36 | + """ |
| 37 | + if not hasattr(literal_type, "__len__"): |
| 38 | + if not isinstance(literal_type, types.Literal): |
| 39 | + raise errors.TypingError("requires literal type") |
| 40 | + return |
| 41 | + |
| 42 | + for i, _ in enumerate(literal_type): |
| 43 | + if not isinstance(literal_type[i], types.Literal): |
| 44 | + raise errors.TypingError("requires literal type") |
| 45 | + |
| 46 | + |
| 47 | +def make_spirv_array( # pylint: disable=too-many-arguments |
| 48 | + context: SPIRVTargetContext, |
| 49 | + builder: IRBuilder, |
| 50 | + ty_array: types.Array, |
| 51 | + ty_shape: Union[types.IntegerLiteral, types.BaseTuple], |
| 52 | + shape: llvmir.Value, |
| 53 | + data: llvmir.Value, |
| 54 | +): |
| 55 | + """Makes SPIR-V array and fills it data.""" |
| 56 | + # Create array object |
| 57 | + ary = context.make_array(ty_array)(context, builder) |
| 58 | + |
| 59 | + itemsize = get_itemsize(context, ty_array) |
| 60 | + ll_itemsize = cgutils.intp_t(itemsize) |
| 61 | + |
| 62 | + if isinstance(ty_shape, types.BaseTuple): |
| 63 | + shapes = cgutils.unpack_tuple(builder, shape) |
| 64 | + else: |
| 65 | + ty_shape = (ty_shape,) |
| 66 | + shapes = (shape,) |
| 67 | + shapes = [ |
| 68 | + context.cast(builder, value, fromty, types.intp) |
| 69 | + for fromty, value in zip(ty_shape, shapes) |
| 70 | + ] |
| 71 | + |
| 72 | + off = ll_itemsize |
| 73 | + strides = [] |
| 74 | + if ty_array.layout == "F": |
| 75 | + for s in shapes: |
| 76 | + strides.append(off) |
| 77 | + off = builder.mul(off, s) |
| 78 | + else: |
| 79 | + for s in reversed(shapes): |
| 80 | + strides.append(off) |
| 81 | + off = builder.mul(off, s) |
| 82 | + strides.reverse() |
| 83 | + |
| 84 | + context.populate_array( |
| 85 | + ary, |
| 86 | + data=data, |
| 87 | + shape=shapes, |
| 88 | + strides=strides, |
| 89 | + itemsize=ll_itemsize, |
| 90 | + ) |
| 91 | + |
| 92 | + return ary |
| 93 | + |
| 94 | + |
| 95 | +def allocate_array_data_on_stack( |
| 96 | + context: BaseContext, |
| 97 | + builder: IRBuilder, |
| 98 | + ty_array: types.Array, |
| 99 | + ty_shape: Union[types.IntegerLiteral, types.BaseTuple], |
| 100 | +): |
| 101 | + """Allocates flat array of given shape on the stack.""" |
| 102 | + if not isinstance(ty_shape, types.BaseTuple): |
| 103 | + ty_shape = (ty_shape,) |
| 104 | + |
| 105 | + return cgutils.alloca_once( |
| 106 | + builder, |
| 107 | + context.get_data_type(ty_array.dtype), |
| 108 | + size=reduce(operator.mul, [s.literal_value for s in ty_shape]), |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +def make_spirv_generic_array_on_stack( |
| 113 | + context: SPIRVTargetContext, |
| 114 | + builder: IRBuilder, |
| 115 | + ty_array: types.Array, |
| 116 | + ty_shape: Union[types.IntegerLiteral, types.BaseTuple], |
| 117 | + shape: llvmir.Value, |
| 118 | +): |
| 119 | + """Makes SPIR-V array of given shape with empty data.""" |
| 120 | + data = allocate_array_data_on_stack(context, builder, ty_array, ty_shape) |
| 121 | + return make_spirv_array(context, builder, ty_array, ty_shape, shape, data) |
0 commit comments