|
24 | 24 | get_memory_semantics_mask,
|
25 | 25 | get_scope,
|
26 | 26 | )
|
| 27 | +from .spv_fn_generator import ( |
| 28 | + get_or_insert_atomic_load_fn, |
| 29 | + get_or_insert_spv_atomic_exchange_fn, |
| 30 | + get_or_insert_spv_atomic_store_fn, |
| 31 | +) |
27 | 32 |
|
28 | 33 |
|
29 | 34 | def _parse_enum_or_int_literal_(literal_int) -> int:
|
@@ -209,6 +214,107 @@ def codegen(context, builder, sig, args):
|
209 | 214 | )
|
210 | 215 |
|
211 | 216 |
|
| 217 | +@intrinsic(target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 218 | +def _intrinsic_load( |
| 219 | + ty_context, ty_atomic_ref # pylint: disable=unused-argument |
| 220 | +): |
| 221 | + sig = ty_atomic_ref.dtype(ty_atomic_ref) |
| 222 | + |
| 223 | + def _intrinsic_load_gen(context, builder, sig, args): |
| 224 | + atomic_ref_ty = sig.args[0] |
| 225 | + fn = get_or_insert_atomic_load_fn( |
| 226 | + context, builder.module, atomic_ref_ty |
| 227 | + ) |
| 228 | + |
| 229 | + spirv_memory_semantics_mask = get_memory_semantics_mask( |
| 230 | + atomic_ref_ty.memory_order |
| 231 | + ) |
| 232 | + spirv_scope = get_scope(atomic_ref_ty.memory_scope) |
| 233 | + |
| 234 | + fn_args = [ |
| 235 | + builder.extract_value( |
| 236 | + args[0], |
| 237 | + context.data_model_manager.lookup( |
| 238 | + atomic_ref_ty |
| 239 | + ).get_field_position("ref"), |
| 240 | + ), |
| 241 | + context.get_constant(types.int32, spirv_scope), |
| 242 | + context.get_constant(types.int32, spirv_memory_semantics_mask), |
| 243 | + ] |
| 244 | + |
| 245 | + return builder.call(fn, fn_args) |
| 246 | + |
| 247 | + return sig, _intrinsic_load_gen |
| 248 | + |
| 249 | + |
| 250 | +@intrinsic(target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 251 | +def _intrinsic_store( |
| 252 | + ty_context, ty_atomic_ref, ty_val |
| 253 | +): # pylint: disable=unused-argument |
| 254 | + sig = types.void(ty_atomic_ref, ty_val) |
| 255 | + |
| 256 | + def _intrinsic_store_gen(context, builder, sig, args): |
| 257 | + atomic_ref_ty = sig.args[0] |
| 258 | + atomic_store_fn = get_or_insert_spv_atomic_store_fn( |
| 259 | + context, builder.module, atomic_ref_ty |
| 260 | + ) |
| 261 | + |
| 262 | + atomic_store_fn_args = [ |
| 263 | + builder.extract_value( |
| 264 | + args[0], |
| 265 | + context.data_model_manager.lookup( |
| 266 | + atomic_ref_ty |
| 267 | + ).get_field_position("ref"), |
| 268 | + ), |
| 269 | + context.get_constant( |
| 270 | + types.int32, get_scope(atomic_ref_ty.memory_scope) |
| 271 | + ), |
| 272 | + context.get_constant( |
| 273 | + types.int32, |
| 274 | + get_memory_semantics_mask(atomic_ref_ty.memory_order), |
| 275 | + ), |
| 276 | + args[1], |
| 277 | + ] |
| 278 | + |
| 279 | + builder.call(atomic_store_fn, atomic_store_fn_args) |
| 280 | + |
| 281 | + return sig, _intrinsic_store_gen |
| 282 | + |
| 283 | + |
| 284 | +@intrinsic(target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 285 | +def _intrinsic_exchange( |
| 286 | + ty_context, ty_atomic_ref, ty_val # pylint: disable=unused-argument |
| 287 | +): |
| 288 | + sig = ty_atomic_ref.dtype(ty_atomic_ref, ty_val) |
| 289 | + |
| 290 | + def _intrinsic_exchange_gen(context, builder, sig, args): |
| 291 | + atomic_ref_ty = sig.args[0] |
| 292 | + atomic_exchange_fn = get_or_insert_spv_atomic_exchange_fn( |
| 293 | + context, builder.module, atomic_ref_ty |
| 294 | + ) |
| 295 | + |
| 296 | + atomic_exchange_fn_args = [ |
| 297 | + builder.extract_value( |
| 298 | + args[0], |
| 299 | + context.data_model_manager.lookup( |
| 300 | + atomic_ref_ty |
| 301 | + ).get_field_position("ref"), |
| 302 | + ), |
| 303 | + context.get_constant( |
| 304 | + types.int32, get_scope(atomic_ref_ty.memory_scope) |
| 305 | + ), |
| 306 | + context.get_constant( |
| 307 | + types.int32, |
| 308 | + get_memory_semantics_mask(atomic_ref_ty.memory_order), |
| 309 | + ), |
| 310 | + args[1], |
| 311 | + ] |
| 312 | + |
| 313 | + return builder.call(atomic_exchange_fn, atomic_exchange_fn_args) |
| 314 | + |
| 315 | + return sig, _intrinsic_exchange_gen |
| 316 | + |
| 317 | + |
212 | 318 | def _check_if_supported_ref(ref):
|
213 | 319 | supported = True
|
214 | 320 |
|
@@ -516,3 +622,72 @@ def ol_fetch_xor_impl(atomic_ref, val):
|
516 | 622 | return _intrinsic_fetch_xor(atomic_ref, val)
|
517 | 623 |
|
518 | 624 | return ol_fetch_xor_impl
|
| 625 | + |
| 626 | + |
| 627 | +@overload_method(AtomicRefType, "load", target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 628 | +def ol_load(atomic_ref): # pylint: disable=unused-argument |
| 629 | + """SPIR-V overload for |
| 630 | + :meth:`numba_dpex.experimental.kernel_iface.AtomicRef.load`. |
| 631 | +
|
| 632 | + Generates the same LLVM IR instruction as dpcpp for the |
| 633 | + `atomic_ref::load` function. |
| 634 | +
|
| 635 | + """ |
| 636 | + |
| 637 | + def ol_load_impl(atomic_ref): |
| 638 | + # pylint: disable=no-value-for-parameter |
| 639 | + return _intrinsic_load(atomic_ref) |
| 640 | + |
| 641 | + return ol_load_impl |
| 642 | + |
| 643 | + |
| 644 | +@overload_method(AtomicRefType, "store", target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 645 | +def ol_store(atomic_ref, val): |
| 646 | + """SPIR-V overload for |
| 647 | + :meth:`numba_dpex.experimental.kernel_iface.AtomicRef.store`. |
| 648 | +
|
| 649 | + Generates the same LLVM IR instruction as dpcpp for the |
| 650 | + `atomic_ref::store` function. |
| 651 | +
|
| 652 | + Raises: |
| 653 | + TypingError: When the dtype of the value stored does not match the |
| 654 | + dtype of the AtomicRef type. |
| 655 | + """ |
| 656 | + |
| 657 | + if atomic_ref.dtype != val: |
| 658 | + raise errors.TypingError( |
| 659 | + f"Type of value to store: {val} does not match the type of the " |
| 660 | + f"reference: {atomic_ref.dtype} stored in the atomic ref." |
| 661 | + ) |
| 662 | + |
| 663 | + def ol_store_impl(atomic_ref, val): |
| 664 | + # pylint: disable=no-value-for-parameter |
| 665 | + return _intrinsic_store(atomic_ref, val) |
| 666 | + |
| 667 | + return ol_store_impl |
| 668 | + |
| 669 | + |
| 670 | +@overload_method(AtomicRefType, "exchange", target=DPEX_KERNEL_EXP_TARGET_NAME) |
| 671 | +def ol_exchange(atomic_ref, val): |
| 672 | + """SPIR-V overload for |
| 673 | + :meth:`numba_dpex.experimental.kernel_iface.AtomicRef.exchange`. |
| 674 | +
|
| 675 | + Generates the same LLVM IR instruction as dpcpp for the |
| 676 | + `atomic_ref::exchange` function. |
| 677 | +
|
| 678 | + Raises: |
| 679 | + TypingError: When the dtype of the value passed to `exchange` |
| 680 | + does not match the dtype of the AtomicRef type. |
| 681 | + """ |
| 682 | + |
| 683 | + if atomic_ref.dtype != val: |
| 684 | + raise errors.TypingError( |
| 685 | + f"Type of value to exchange: {val} does not match the type of the " |
| 686 | + f"reference: {atomic_ref.dtype} stored in the atomic ref." |
| 687 | + ) |
| 688 | + |
| 689 | + def ol_exchange_impl(atomic_ref, val): |
| 690 | + # pylint: disable=no-value-for-parameter |
| 691 | + return _intrinsic_exchange(atomic_ref, val) |
| 692 | + |
| 693 | + return ol_exchange_impl |
0 commit comments