Skip to content

Commit 9ef7352

Browse files
committed
[lang] Remove nvvm_mlir_interfaces.py
Signed-off-by: Greg Bonik <gbonik@nvidia.com>
1 parent 9c3ee3c commit 9ef7352

9 files changed

Lines changed: 161 additions & 872 deletions

File tree

‎.flake8‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ exclude =
1616
*docs/venv,
1717
*docs/source/_templates,
1818
experimental/cuda-lang/src/cuda/lang/_stub/nvvm.py,
19-
experimental/cuda-lang/src/cuda/lang/_stub/nvvm_mlir_interfaces.py,
2019
experimental/cuda-lang/src/cuda/lang/_stub/_libdevice.py,
2120
per-file-ignores =
2221
src/cuda/tile/_stub.py: W291

‎experimental/cuda-lang/src/cuda/lang/_ir/op_impl/core_api_impl.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from cuda.lang._enums import CachePolicy
66
from cuda.lang._exception import InvalidValueError, TypeCheckingError
77
from cuda.lang._ir.op_defs import RawNVVMIntrinsic, BitCast, InlinePTX
8+
from cuda.tile import MemoryScope
89
from ..type import (
910
DTypeConstructor,
1011
MemorySpace,
@@ -322,3 +323,34 @@ def impl_create_fractional_cache_policy(
322323
read_write_operands=(),
323324
)
324325
return results[0]
326+
327+
328+
@impl(core_api.memory_barrier)
329+
def memory_barrier_impl(scope: Var) -> None:
330+
scope2intrin = {
331+
MemoryScope.BLOCK: "llvm.nvvm.membar.cta",
332+
MemoryScope.CLUSTER: "llvm.nvvm.fence_sc_cluster",
333+
MemoryScope.DEVICE: "llvm.nvvm.membar.gl",
334+
MemoryScope.SYS: "llvm.nvvm.membar.sys",
335+
}
336+
scope = require_constant_enum(scope, MemoryScope)
337+
intrinsic = scope2intrin.get(scope)
338+
if intrinsic is None:
339+
valid = ", ".join(x._name_ for x in scope2intrin.keys())
340+
raise InvalidValueError(f"Invalid memory scope '{scope}'. Valid values: {valid}")
341+
add_operation_variadic(
342+
RawNVVMIntrinsic, (),
343+
intrinsic=intrinsic,
344+
operands_=()
345+
)
346+
347+
348+
@impl(core_api.grid_dependency_control_wait, fixed_args=["wait"])
349+
@impl(core_api.grid_dependency_control_launch_dependents, fixed_args=["launch.dependents"])
350+
def grid_dependency_control_action_impl(action: str) -> None:
351+
add_operation_variadic(
352+
RawNVVMIntrinsic, (),
353+
intrinsic="llvm.nvvm."
354+
"griddepcontrol." + action,
355+
operands_=()
356+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from cuda.lang._enums import PrefetchLevel, CachePolicy
5+
from cuda.lang._ir.op_defs import RawNVVMIntrinsic
6+
from cuda.lang._ir.type import TensorMapTy
7+
from cuda.lang._ir.type_checking_helpers import require_pointer_type
8+
from cuda.lang._stub.prefetch import prefetch, prefetch_uniform, prefetch_tensor_map
9+
from cuda.tile._datatype import PointerInfo
10+
from cuda.tile._exception import InvalidValueError, TypeCheckingError
11+
from cuda.tile._ir.ir import Var, add_operation_variadic
12+
from cuda.tile._ir.op_impl import ImplRegistry, require_constant_enum
13+
from cuda.tile._ir.type import NONE
14+
from cuda.tile._memory_model import MemorySpace
15+
16+
_registry = ImplRegistry()
17+
impl = _registry.impl
18+
19+
20+
def prefetch_impl_registry() -> ImplRegistry:
21+
return _registry
22+
23+
24+
@impl(prefetch)
25+
def prefetch_impl(address: Var, level: Var, eviction_priority: Var):
26+
address_ty = require_pointer_type(address)
27+
address_space = PointerInfo(address_ty.pointer_dtype).memory_space
28+
level = require_constant_enum(level, PrefetchLevel)
29+
30+
if eviction_priority.get_type() == NONE:
31+
evict_str = ""
32+
else:
33+
eviction_priority = require_constant_enum(eviction_priority, CachePolicy)
34+
if level != PrefetchLevel.L2:
35+
raise InvalidValueError("Prefetch eviction priority is supported only for L2")
36+
priority_map = {CachePolicy.L2_EVICT_NORMAL: "normal", CachePolicy.L2_EVICT_LAST: "last"}
37+
suffix = priority_map.get(eviction_priority)
38+
if suffix is None:
39+
valid = ", ".join(x._name_ for x in priority_map.keys())
40+
raise InvalidValueError(f"Invalid eviction priority {eviction_priority._name_}."
41+
f" Accepted values: {valid}.")
42+
if address_space != MemorySpace.GLOBAL:
43+
raise TypeCheckingError(f"Pointer in GLOBAL address space is required"
44+
f" when eviction priority is specified;"
45+
f" received a {address_space._name_} pointer instead.")
46+
evict_str = f".evict.{suffix}"
47+
48+
address_space_map = {
49+
MemorySpace.GENERIC: "",
50+
MemorySpace.GLOBAL: ".global",
51+
MemorySpace.LOCAL: ".local",
52+
}
53+
address_space_str = address_space_map.get(address_space)
54+
if address_space_str is None:
55+
valid = ", ".join(x._name_ for x in address_space_map.keys())
56+
raise TypeCheckingError(f"Invalid pointer address space {address_space._name_}."
57+
f" Accepted address spaces: {valid}.")
58+
59+
level_map = {PrefetchLevel.L1: "L1", PrefetchLevel.L2: "L2"}
60+
level_str = level_map[level]
61+
62+
add_operation_variadic(
63+
RawNVVMIntrinsic,
64+
(),
65+
intrinsic=f"llvm.nvvm.prefetch{address_space_str}.{level_str}{evict_str}",
66+
operands_=(address,)
67+
)
68+
69+
70+
@impl(prefetch_uniform)
71+
def prefetch_uniform_impl(address: Var):
72+
address_ty = require_pointer_type(address)
73+
address_space = PointerInfo(address_ty.pointer_dtype).memory_space
74+
if address_space != MemorySpace.GENERIC:
75+
raise TypeCheckingError(f"Expected a pointer in GENERIC address space;"
76+
f" received {address_space._name_} instead.")
77+
78+
add_operation_variadic(
79+
RawNVVMIntrinsic,
80+
(),
81+
intrinsic="llvm.nvvm.prefetchu.L1",
82+
operands_=(address,)
83+
)
84+
85+
86+
@impl(prefetch_tensor_map)
87+
def prefetch_tensor_map_impl(tensor_map: Var):
88+
if isinstance(tensor_map.get_type(), TensorMapTy):
89+
from cuda.lang._ir.ops import tensor_map_as_opaque_ptr
90+
tensor_map = tensor_map_as_opaque_ptr(tensor_map)
91+
else:
92+
address_ty = require_pointer_type(tensor_map)
93+
address_space = PointerInfo(address_ty.pointer_dtype).memory_space
94+
valid_spaces = (MemorySpace.GENERIC, MemorySpace.CONSTANT)
95+
if address_space not in valid_spaces:
96+
valid = ", ".join(x._name_ for x in valid_spaces)
97+
raise TypeCheckingError(f"Invalid address space {address_space._name_}."
98+
f" Accepted address spaces: {valid}")
99+
add_operation_variadic(
100+
RawNVVMIntrinsic,
101+
(),
102+
intrinsic="llvm.nvvm.prefetch.tensormap",
103+
operands_=(tensor_map,)
104+
)

‎experimental/cuda-lang/src/cuda/lang/_ir/ops.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
contiguous_strides_from_shape,
166166
pointer_impl_registry,
167167
)
168+
from .op_impl.prefetch_impl import prefetch_impl_registry
168169
from .op_impl.copy_async_impl import copy_async_impl_registry
169170
from .op_impl.barrier_impl import barrier_impl_registry
170171
from .op_impl.mbarrier_impl import mbarrier_impl_registry
@@ -187,6 +188,7 @@
187188
cuda_lang_impl_registry.update(fence_impl_registry())
188189
cuda_lang_impl_registry.update(vector_impl_registry())
189190
cuda_lang_impl_registry.update(pointer_impl_registry())
191+
cuda_lang_impl_registry.update(prefetch_impl_registry())
190192
cuda_lang_impl_registry.update(copy_async_impl_registry())
191193
cuda_lang_impl_registry.update(barrier_impl_registry())
192194
cuda_lang_impl_registry.update(mbarrier_impl_registry())
@@ -786,11 +788,15 @@ def tensor_map_get_transaction_bytes_impl(self: Var, mode: Var):
786788
return loosely_typed_const(element_count * map_ty.element_bitwidth // 8)
787789

788790

791+
def tensor_map_as_opaque_ptr(self: Var):
792+
result_ty = PointerTy(opaque_pointer_dtype())
793+
return add_operation(TensorMapAsOpaquePtr, result_ty, tensor_map=self)
794+
795+
789796
@impl(tensor_map.TensorMap.as_opaque_ptr)
790797
def tensor_map_as_opaque_ptr_impl(self: Var):
791798
require_tensor_map_ty(self)
792-
result_ty = PointerTy(opaque_pointer_dtype())
793-
return add_operation(TensorMapAsOpaquePtr, result_ty, tensor_map=self)
799+
return tensor_map_as_opaque_ptr(self)
794800

795801

796802
@impl(cluster_launch_control_try_cancel)

‎experimental/cuda-lang/src/cuda/lang/_stub/core_api.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,23 +1124,19 @@ def nanosleep(nanoseconds: int):
11241124
nvvm.nanosleep(nanoseconds)
11251125

11261126

1127+
@stub
11271128
def memory_barrier(scope: MemoryScope) -> None:
11281129
"""Issue a memory fence with the given scope."""
1129-
return nvvm_mlir_interfaces.memory_barrier(scope=scope)
11301130

11311131

1132+
@stub
11321133
def grid_dependency_control_wait() -> None:
11331134
"""Wait for prerequisite grids in a programmatic dependent launch."""
1134-
nvvm_mlir_interfaces.griddepcontrol(
1135-
kind=nvvm_mlir_interfaces.GridDepActionKind.wait
1136-
)
11371135

11381136

1137+
@stub
11391138
def grid_dependency_control_launch_dependents() -> None:
11401139
"""Launch dependent grids in a programmatic dependent launch."""
1141-
nvvm_mlir_interfaces.griddepcontrol(
1142-
kind=nvvm_mlir_interfaces.GridDepActionKind.launch_dependents
1143-
)
11441140

11451141

11461142
@stub
@@ -1189,4 +1185,3 @@ def cdiv(x, y, /):
11891185

11901186
# Need these imports at the end in order to overcome the circular import problem
11911187
from . import nvvm # noqa: E402
1192-
from . import nvvm_mlir_interfaces # noqa: E402

0 commit comments

Comments
 (0)