|
| 1 | +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import dpctl |
| 6 | +from llvmlite import ir as llvmir |
| 7 | +from numba.core import cgutils, types |
| 8 | +from numba.extending import intrinsic |
| 9 | + |
| 10 | +from numba_dpex import dpjit |
| 11 | + |
| 12 | + |
| 13 | +@intrinsic |
| 14 | +def are_queues_equal(typingctx, ty_queue1, ty_queue2): |
| 15 | + """Calls dpctl's libsyclinterface's DPCTLQueue_AreEq to see if two |
| 16 | + dpctl.SyclQueue objects point to the same sycl queue. |
| 17 | +
|
| 18 | + Args: |
| 19 | + typingctx: The typing context used during lowering. |
| 20 | + ty_queue1: Type of the first queue object, |
| 21 | + i.e., numba_dpex.types.DpctlSyclQueue |
| 22 | + ty_queue2: Type of the second queue object, |
| 23 | + i.e., numba_dpex.types.DpctlSyclQueue |
| 24 | +
|
| 25 | + Returns: |
| 26 | + tuple: The signature of the intrinsic function and the codegen function |
| 27 | + to lower the intrinsic. |
| 28 | + """ |
| 29 | + result_type = types.boolean |
| 30 | + sig = result_type(ty_queue1, ty_queue2) |
| 31 | + |
| 32 | + # defines the custom code generation |
| 33 | + def codegen(context, builder, sig, args): |
| 34 | + fnty = llvmir.FunctionType( |
| 35 | + cgutils.bool_t, [cgutils.voidptr_t, cgutils.voidptr_t] |
| 36 | + ) |
| 37 | + fn = cgutils.get_or_insert_function( |
| 38 | + builder.module, fnty, "DPCTLQueue_AreEq" |
| 39 | + ) |
| 40 | + qref1 = builder.extract_value(args[0], 1) |
| 41 | + qref2 = builder.extract_value(args[1], 1) |
| 42 | + |
| 43 | + ret = builder.call(fn, [qref1, qref2]) |
| 44 | + |
| 45 | + return ret |
| 46 | + |
| 47 | + return sig, codegen |
| 48 | + |
| 49 | + |
| 50 | +def test_queue_ref_access_in_dpjit(): |
| 51 | + """Tests if we can access the queue_ref attribute of a dpctl.SyclQueue |
| 52 | + PyObject inside dpjit and pass it to a native C function, in this case |
| 53 | + dpctl's libsyclinterface's DPCTLQueue_AreEq. |
| 54 | +
|
| 55 | + Checks if the result of queue equality check done inside dpjit is the |
| 56 | + same as when done in Python. |
| 57 | + """ |
| 58 | + |
| 59 | + @dpjit |
| 60 | + def test_queue_equality(queue1, queue2): |
| 61 | + return are_queues_equal(queue1, queue2) |
| 62 | + |
| 63 | + q1 = dpctl.SyclQueue() |
| 64 | + q2 = dpctl.SyclQueue() |
| 65 | + |
| 66 | + expected = q1 == q2 |
| 67 | + actual = test_queue_equality(q1, q2) |
| 68 | + |
| 69 | + assert expected == actual |
| 70 | + |
| 71 | + d = dpctl.SyclDevice() |
| 72 | + cq1 = dpctl._sycl_queue_manager.get_device_cached_queue(d) |
| 73 | + cq2 = dpctl._sycl_queue_manager.get_device_cached_queue(d) |
| 74 | + |
| 75 | + expected = cq1 == cq2 |
| 76 | + actual = test_queue_equality(cq1, cq2) |
| 77 | + |
| 78 | + assert expected == actual |
0 commit comments