• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IntelPython / numba-dpex / 8146163543

04 Mar 2024 07:52PM UTC coverage: 82.881% (+0.2%) from 82.704%
8146163543

push

github

web-flow
Merge pull request #1370 from IntelPython/feature/private_array

Add PrivateArray kernel_api

1560 of 2141 branches covered (72.86%)

Branch coverage included in aggregate %.

86 of 90 new or added lines in 4 files covered. (95.56%)

6438 of 7509 relevant lines covered (85.74%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/numba_dpex/kernel_api_impl/spirv/arrayobj.py
1
# SPDX-FileCopyrightText: 2024 Intel Corporation
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
"""Contains SPIR-V specific array functions."""
1✔
6

7
import operator
1✔
8
from functools import reduce
1✔
9
from typing import Union
1✔
10

11
import llvmlite.ir as llvmir
1✔
12
from llvmlite.ir.builder import IRBuilder
1✔
13
from numba.core import cgutils, errors, types
1✔
14
from numba.core.base import BaseContext
1✔
15

16
from numba_dpex.kernel_api_impl.spirv.target import SPIRVTargetContext
1✔
17
from numba_dpex.ocl.oclimpl import _get_target_data
1✔
18

19

20
def get_itemsize(context: SPIRVTargetContext, array_type: types.Array):
1✔
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)
1✔
26
    lldtype = context.get_data_type(array_type.dtype)
1✔
27
    return lldtype.get_abi_size(targetdata)
1✔
28

29

30
def require_literal(literal_type: types.Type):
1✔
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__"):
1✔
38
        if not isinstance(literal_type, types.Literal):
1✔
39
            raise errors.TypingError("requires literal type")
1✔
40
        return
1✔
41

42
    for i, _ in enumerate(literal_type):
1✔
43
        if not isinstance(literal_type[i], types.Literal):
1✔
44
            raise errors.TypingError("requires literal type")
1✔
45

46

47
def make_spirv_array(  # pylint: disable=too-many-arguments
1✔
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)
1✔
58

59
    itemsize = get_itemsize(context, ty_array)
1✔
60
    ll_itemsize = cgutils.intp_t(itemsize)
1✔
61

62
    if isinstance(ty_shape, types.BaseTuple):
1✔
63
        shapes = cgutils.unpack_tuple(builder, shape)
1✔
64
    else:
65
        ty_shape = (ty_shape,)
1✔
66
        shapes = (shape,)
1✔
67
    shapes = [
1✔
68
        context.cast(builder, value, fromty, types.intp)
69
        for fromty, value in zip(ty_shape, shapes)
70
    ]
71

72
    off = ll_itemsize
1✔
73
    strides = []
1✔
74
    if ty_array.layout == "F":
1!
NEW
75
        for s in shapes:
×
NEW
76
            strides.append(off)
×
NEW
77
            off = builder.mul(off, s)
×
78
    else:
79
        for s in reversed(shapes):
1✔
80
            strides.append(off)
1✔
81
            off = builder.mul(off, s)
1✔
82
        strides.reverse()
1✔
83

84
    context.populate_array(
1✔
85
        ary,
86
        data=data,
87
        shape=shapes,
88
        strides=strides,
89
        itemsize=ll_itemsize,
90
    )
91

92
    return ary
1✔
93

94

95
def allocate_array_data_on_stack(
1✔
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):
1✔
103
        ty_shape = (ty_shape,)
1✔
104

105
    return cgutils.alloca_once(
1✔
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(
1✔
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)
1✔
121
    return make_spirv_array(context, builder, ty_array, ty_shape, shape, data)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc