|
| 1 | +# SPDX-FileCopyrightText: 2023 - 2024 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +""" |
| 6 | +Provides overloads for functions included in kernel_api.atomic_fence |
| 7 | +that generate dpcpp SPIR-V LLVM IR intrinsic function calls. |
| 8 | +""" |
| 9 | +from llvmlite import ir as llvmir |
| 10 | +from numba.core import types |
| 11 | +from numba.extending import intrinsic, overload |
| 12 | + |
| 13 | +from numba_dpex.kernel_api import atomic_fence |
| 14 | + |
| 15 | +from ..target import DPEX_KERNEL_EXP_TARGET_NAME |
| 16 | +from ._spv_atomic_inst_helper import get_memory_semantics_mask, get_scope |
| 17 | +from .spv_fn_declarations import ( |
| 18 | + _SUPPORT_CONVERGENT, |
| 19 | + get_or_insert_spv_atomic_fence_fn, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@intrinsic(target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 24 | +def _intrinsic_atomic_fence( |
| 25 | + ty_context, ty_spirv_mem_sem_mask, ty_spirv_scope |
| 26 | +): # pylint: disable=unused-argument |
| 27 | + |
| 28 | + # Signature of `__spirv_MemoryBarrier` call that is |
| 29 | + # generated for atomic_fence. It takes two arguments - |
| 30 | + # scope and memory_semantics_mask. |
| 31 | + # All arguments have to be of type unsigned int32. |
| 32 | + sig = types.void(types.uint32, types.uint32) |
| 33 | + |
| 34 | + def _intrinsic_atomic_fence_gen( |
| 35 | + context, builder, sig, args |
| 36 | + ): # pylint: disable=unused-argument |
| 37 | + callinst = builder.call( |
| 38 | + get_or_insert_spv_atomic_fence_fn(builder.module), |
| 39 | + [ |
| 40 | + builder.trunc(args[1], llvmir.IntType(32)), # scope |
| 41 | + builder.trunc(args[0], llvmir.IntType(32)), # semantics mask |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | + if _SUPPORT_CONVERGENT: # pylint: disable=duplicate-code |
| 46 | + callinst.attributes.add("convergent") |
| 47 | + callinst.attributes.add("nounwind") |
| 48 | + |
| 49 | + return ( |
| 50 | + sig, |
| 51 | + _intrinsic_atomic_fence_gen, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +@overload( |
| 56 | + atomic_fence, |
| 57 | + prefer_literal=True, |
| 58 | + target=DPEX_KERNEL_EXP_TARGET_NAME, |
| 59 | +) |
| 60 | +def ol_atomic_fence(memory_order, memory_scope): |
| 61 | + """SPIR-V overload for |
| 62 | + :meth:`numba_dpex.kernel_api.atomic_fence`. |
| 63 | +
|
| 64 | + Generates the same LLVM IR instruction as DPC++ for the SYCL |
| 65 | + `atomic_fence` function. |
| 66 | + """ |
| 67 | + spirv_memory_semantics_mask = get_memory_semantics_mask( |
| 68 | + memory_order.literal_value |
| 69 | + ) |
| 70 | + spirv_scope = get_scope(memory_scope.literal_value) |
| 71 | + |
| 72 | + def ol_atomic_fence_impl( |
| 73 | + memory_order, memory_scope |
| 74 | + ): # pylint: disable=unused-argument |
| 75 | + # pylint: disable=no-value-for-parameter |
| 76 | + return _intrinsic_atomic_fence(spirv_memory_semantics_mask, spirv_scope) |
| 77 | + |
| 78 | + return ol_atomic_fence_impl |
0 commit comments