|
| 1 | +# SPDX-FileCopyrightText: 2023 - 2024 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +""" |
| 6 | +Implements the SPIR-V overloads for the kernel_api.AtomicRef class methods. |
| 7 | +""" |
| 8 | + |
| 9 | +import operator |
| 10 | +from functools import reduce |
| 11 | + |
| 12 | +from numba.core import cgutils, errors, types |
| 13 | +from numba.core.cpu import CPUContext |
| 14 | +from numba.extending import intrinsic, overload |
| 15 | +from numba.np.arrayobj import get_itemsize as get_itemsize_np |
| 16 | + |
| 17 | +from numba_dpex.core.types import USMNdArray |
| 18 | +from numba_dpex.kernel_api import PrivateArray |
| 19 | +from numba_dpex.ocl.oclimpl import _get_target_data |
| 20 | +from numba_dpex.utils import address_space as AddressSpace |
| 21 | + |
| 22 | +from ..target import DPEX_KERNEL_EXP_TARGET_NAME |
| 23 | + |
| 24 | + |
| 25 | +def get_itemsize_spirv(context, array_type): |
| 26 | + """ |
| 27 | + Return the item size for the given array or buffer type. |
| 28 | + """ |
| 29 | + targetdata = _get_target_data(context) |
| 30 | + lldtype = context.get_data_type(array_type.dtype) |
| 31 | + return lldtype.get_abi_size(targetdata) |
| 32 | + |
| 33 | + |
| 34 | +def require_literal(literal_type): |
| 35 | + if not hasattr(literal_type, "__len__"): |
| 36 | + if not isinstance(literal_type, types.Literal): |
| 37 | + raise errors.TypingError("requires literal type") |
| 38 | + return |
| 39 | + |
| 40 | + for i in range(len(literal_type)): |
| 41 | + if not isinstance(literal_type[i], types.Literal): |
| 42 | + raise errors.TypingError("requires literal type") |
| 43 | + |
| 44 | + |
| 45 | +@intrinsic(target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 46 | +def _intrinsic_private_array_ctor(ty_context, ty_shape, ty_dtype): |
| 47 | + require_literal(ty_shape) |
| 48 | + |
| 49 | + if not isinstance(ty_dtype, types.DType): |
| 50 | + raise errors.TypingError("Second argument must be instance of DType") |
| 51 | + |
| 52 | + ndim = 1 |
| 53 | + if hasattr(ty_shape, "__len__"): |
| 54 | + ndim = len(ty_shape) |
| 55 | + |
| 56 | + ty_array = USMNdArray( |
| 57 | + dtype=ty_dtype.dtype, |
| 58 | + ndim=ndim, |
| 59 | + layout="C", |
| 60 | + addrspace=AddressSpace.PRIVATE, |
| 61 | + ) |
| 62 | + |
| 63 | + sig = ty_array(ty_shape, ty_dtype) |
| 64 | + |
| 65 | + def codegen(context: CPUContext, builder, sig, args): |
| 66 | + shape = args[0] |
| 67 | + ty_shape = sig.args[0] |
| 68 | + ty_array = sig.return_type |
| 69 | + # Create array object |
| 70 | + ary = context.make_array(ty_array)(context, builder) |
| 71 | + |
| 72 | + itemsize = get_itemsize_spirv(context, ty_array) |
| 73 | + ll_itemsize = cgutils.intp_t(itemsize) |
| 74 | + |
| 75 | + if isinstance(ty_shape, types.BaseTuple): |
| 76 | + shapes = cgutils.unpack_tuple(builder, shape) |
| 77 | + else: |
| 78 | + ty_shape = (ty_shape,) |
| 79 | + shapes = (shape,) |
| 80 | + shapes = [ |
| 81 | + context.cast(builder, value, fromty, types.intp) |
| 82 | + for fromty, value in zip(ty_shape, shapes) |
| 83 | + ] |
| 84 | + |
| 85 | + off = ll_itemsize |
| 86 | + strides = [] |
| 87 | + if ty_array.layout == "F": |
| 88 | + for s in shapes: |
| 89 | + strides.append(off) |
| 90 | + off = builder.mul(off, s) |
| 91 | + else: |
| 92 | + for s in reversed(shapes): |
| 93 | + strides.append(off) |
| 94 | + off = builder.mul(off, s) |
| 95 | + strides.reverse() |
| 96 | + |
| 97 | + dataptr = cgutils.alloca_once( |
| 98 | + builder, |
| 99 | + context.get_data_type(ty_array.dtype), |
| 100 | + size=reduce(operator.mul, [s.literal_value for s in ty_shape]), |
| 101 | + ) |
| 102 | + |
| 103 | + context.populate_array( |
| 104 | + ary, |
| 105 | + data=dataptr, |
| 106 | + shape=shapes, |
| 107 | + strides=strides, |
| 108 | + itemsize=ll_itemsize, |
| 109 | + ) |
| 110 | + |
| 111 | + return ary._getvalue() |
| 112 | + |
| 113 | + return ( |
| 114 | + sig, |
| 115 | + codegen, |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@overload( |
| 120 | + PrivateArray, |
| 121 | + prefer_literal=True, |
| 122 | + target=DPEX_KERNEL_EXP_TARGET_NAME, |
| 123 | +) |
| 124 | +def ol_private_array_ctor( |
| 125 | + shape, |
| 126 | + dtype, |
| 127 | +): |
| 128 | + """Overload of the constructor for the class |
| 129 | + class:`numba_dpex.kernel_api.AtomicRef`. |
| 130 | +
|
| 131 | + Raises: |
| 132 | + errors.TypingError: If the `ref` argument is not a UsmNdArray type. |
| 133 | + errors.TypingError: If the dtype of the `ref` is not supported in an |
| 134 | + AtomicRef. |
| 135 | + errors.TypingError: If the device does not support atomic operations on |
| 136 | + the dtype of the `ref`. |
| 137 | + errors.TypingError: If the `memory_order`, `address_type`, or |
| 138 | + `memory_scope` arguments could not be parsed as integer literals. |
| 139 | + errors.TypingError: If the `address_space` argument is different from |
| 140 | + the address space attribute of the `ref` argument. |
| 141 | + errors.TypingError: If the address space is PRIVATE. |
| 142 | +
|
| 143 | + """ |
| 144 | + |
| 145 | + def ol_private_array_ctor_impl( |
| 146 | + shape, |
| 147 | + dtype, |
| 148 | + ): |
| 149 | + # pylint: disable=no-value-for-parameter |
| 150 | + return _intrinsic_private_array_ctor(shape, dtype) |
| 151 | + |
| 152 | + return ol_private_array_ctor_impl |
0 commit comments