Skip to content

Commit cb7d671

Browse files
committed
[lang] Lower FP32 even vectors to packed NVVM operations
Signed-off-by: Qiqi Xiao <qiqix@nvidia.com>
1 parent ed23c7f commit cb7d671

9 files changed

Lines changed: 493 additions & 148 deletions

File tree

experimental/cuda-lang/src/cuda/lang/_compile.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ConstantConstraint,
4444
)
4545
from cuda.lang._exception import CompilerExecutionError
46+
from cuda.lang._target import TargetInfo
4647
from ._execution import kernel
4748
from cuda.lang._ir.ops import cuda_lang_impl_registry
4849
from ._ir._host_program import HostProgram, get_host_programs_by_var
@@ -283,18 +284,25 @@ def _dump(phase: str, contents: object) -> None:
283284
if log_flags.log_flattened_ir:
284285
_dump("Flattened IR", flattened_ir)
285286

286-
mlir_module = ir2mlir(signature, flattened_ir, ctx, compiler_options)
287-
mlir_text = str(mlir_module)
288-
289-
if log_flags.log_mlir:
290-
_dump("MLIR", mlir_text)
291-
292287
if gpu_name is None or arch is None:
293288
cc = compute_capability or get_compute_capability()
294289
suffix = "a" if cc >= (9, 0) else ""
295290
gpu_name = gpu_name or cc.gpu_name + suffix
296291
arch = arch or cc.arch + suffix
297292

293+
target_info = TargetInfo.from_arch(arch)
294+
mlir_module = ir2mlir(
295+
signature,
296+
flattened_ir,
297+
ctx,
298+
compiler_options,
299+
target_info,
300+
)
301+
mlir_text = str(mlir_module)
302+
303+
if log_flags.log_mlir:
304+
_dump("MLIR", mlir_text)
305+
298306
need_nvvm = log_flags.log_nvvm or keep_nvvm
299307
need_ptx = log_flags.log_ptx or keep_ptx
300308
ptx_compiler_options = compiler_options._ptx_compiler_options

experimental/cuda-lang/src/cuda/lang/_ir/op_defs.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from enum import Enum, auto
88

99
import cuda.lang._mlir as mlir
10-
from cuda.lang._enums import MemoryOrder
10+
from cuda.lang._enums import MemoryOrder, RoundingMode, SaturationMode
1111
from cuda.tile._memory_model import MemoryScope
1212
from cuda.tile._ir.ir import MemoryEffect
1313
import cuda.lang._datatype as datatype
@@ -143,3 +143,15 @@ class ReinterpretPointerAsArray(Operation, opcode="reinterpret_ptr_as_array"):
143143
@dataclass
144144
class TensorMapAsOpaquePtr(Operation, opcode="tensor_map_as_opaque_ptr"):
145145
tensor_map: Var = operand()
146+
147+
148+
@dataclass(eq=False)
149+
class FmaOperation(Operation, opcode="fma"):
150+
x: Var = operand()
151+
y: Var = operand()
152+
z: Var = operand()
153+
rounding_mode: RoundingMode = attribute()
154+
saturation_mode: SaturationMode = attribute()
155+
flush_to_zero: bool = attribute()
156+
relu: bool = attribute()
157+
oob: bool = attribute()

experimental/cuda-lang/src/cuda/lang/_ir/op_impl/math_impl.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
import enum
65
import operator
76

87
from cuda.tile._ir.ir import add_operation_variadic
@@ -22,6 +21,7 @@
2221
RawNVVMIntrinsic,
2322
RawMLIROperation,
2423
ForeignFunction,
24+
FmaOperation,
2525
)
2626
from cuda.lang._ir.type_checking_helpers import (
2727
broadcast_to_same_shape,
@@ -58,15 +58,6 @@
5858
impl = _registry.impl
5959

6060

61-
# TODO(ajm): need to bump llvm bindings to get this enum
62-
class _FmaSaturationMode(enum.Enum):
63-
NONE = 0
64-
SAT = 1
65-
66-
def _print_mlir_unqualified(self, printer):
67-
printer(("none", "sat")[self.value])
68-
69-
7061
def math_impl_registry() -> ImplRegistry:
7162
return _registry
7263

@@ -111,12 +102,12 @@ def math_fma_impl(
111102
y = promote_and_broadcast_to(y, ty)
112103
z = promote_and_broadcast_to(z, ty)
113104

114-
rounding_modes = {
115-
RoundingMode.RM: mlir.nvvm.FPRoundingMode.RM,
116-
RoundingMode.RN: mlir.nvvm.FPRoundingMode.RN,
117-
RoundingMode.RP: mlir.nvvm.FPRoundingMode.RP,
118-
RoundingMode.RZ: mlir.nvvm.FPRoundingMode.RZ,
119-
}
105+
rounding_modes = (
106+
RoundingMode.RM,
107+
RoundingMode.RN,
108+
RoundingMode.RP,
109+
RoundingMode.RZ,
110+
)
120111
if rounding_mode not in rounding_modes:
121112
valid = ", ".join(str(mode) for mode in rounding_modes)
122113
raise TypeCheckingError(
@@ -127,25 +118,17 @@ def math_fma_impl(
127118
raise TypeCheckingError(
128119
"fma does not implement SaturationMode.SATFINITE yet"
129120
)
130-
saturation_modes = {
131-
SaturationMode.NONE: _FmaSaturationMode.NONE,
132-
SaturationMode.SAT: _FmaSaturationMode.SAT,
133-
}
134-
135-
rns = mlir.nvvm.FPRoundingModeAttr(value=rounding_modes[rounding_mode])
136-
sat = mlir.nvvm.SaturationModeAttr(value=saturation_modes[saturation_mode])
137121
return add_operation(
138-
RawMLIROperation,
122+
FmaOperation,
139123
ty,
140-
op_name="nvvm.fma",
141-
operands_=(x, y, z),
142-
mlir_attributes=(
143-
("rnd", rns),
144-
("sat", sat),
145-
("ftz", mlir.BoolAttr(value=flush_to_zero)),
146-
("relu", mlir.BoolAttr(value=relu)),
147-
("oob", mlir.BoolAttr(value=oob)),
148-
),
124+
x=x,
125+
y=y,
126+
z=z,
127+
rounding_mode=rounding_mode,
128+
saturation_mode=saturation_mode,
129+
flush_to_zero=flush_to_zero,
130+
relu=relu,
131+
oob=oob,
149132
)
150133

151134

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
LoadPointer,
102102
ReinterpretPointerAsArray,
103103
BitCast,
104+
FmaOperation,
104105
)
105106
from .op_impl.core_api_impl import core_api_impl_registry
106107
from .type_checking_helpers import (
@@ -942,6 +943,7 @@ def _call_foreign_function_impl(func: Var, return_type: Var, parameters: Var):
942943
"RawMLIROperation",
943944
"Fence",
944945
"ForeignFunction",
946+
"FmaOperation",
945947
"VectorGetItem",
946948
"VectorReduce",
947949
)

0 commit comments

Comments
 (0)