|
| 1 | +# Copyright 2024 The JAX Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Lowering rules and pass for the MLIR Mosaic GPU dialect.""" |
| 16 | + |
| 17 | +from collections.abc import Callable |
| 18 | +import functools |
| 19 | +import operator |
| 20 | +from typing import Sequence, Type |
| 21 | + |
| 22 | +from jax._src.interpreters import mlir as mlir_interpreter |
| 23 | +from jax._src.lib import mosaic_gpu_dialect as mgpu |
| 24 | + |
| 25 | +from jaxlib.mlir import ir |
| 26 | +from jaxlib.mlir.dialects import gpu |
| 27 | +from jaxlib.mlir.dialects import llvm |
| 28 | +from jaxlib.mlir.dialects import memref |
| 29 | +from jaxlib.mlir.dialects import nvvm |
| 30 | +from .utils import c, memref_ptr, single_thread_predicate |
| 31 | + |
| 32 | + |
| 33 | +MlirLoweringRule = Callable[[ir.Operation | ir.OpView], Sequence[ir.Value]] |
| 34 | + |
| 35 | + |
| 36 | +_lowerings: dict[str, MlirLoweringRule] = {} |
| 37 | + |
| 38 | + |
| 39 | +# TODO(bchetioui): Remove this when minimum jaxlib version >= 0.4.36. |
| 40 | +# Jaxlib doesn't contain Mosaic GPU dialect bindings. |
| 41 | +InitializeBarrierOp = mgpu.InitializeBarrierOp if mgpu is not None else None |
| 42 | + |
| 43 | +def _register_lowering( |
| 44 | + op: str | Type[ir.OpView] |
| 45 | +) -> Callable[[MlirLoweringRule], MlirLoweringRule]: |
| 46 | + def wrapper(f): |
| 47 | + op_name = op if isinstance(op, str) else op.OPERATION_NAME # pytype: disable=attribute-error |
| 48 | + _lowerings[op_name] = f |
| 49 | + return f |
| 50 | + |
| 51 | + return wrapper |
| 52 | + |
| 53 | + |
| 54 | +def _lowered_barrier_type() -> ir.Type: |
| 55 | + return ir.IntegerType.get_signless(64) |
| 56 | + |
| 57 | + |
| 58 | +def _gpu_address_space_to_nvptx(address_space: gpu.AddressSpace) -> int: |
| 59 | + match address_space: |
| 60 | + case gpu.AddressSpace.Global: |
| 61 | + return 1 |
| 62 | + case gpu.AddressSpace.Workgroup: |
| 63 | + return 3 |
| 64 | + case _: |
| 65 | + raise NotImplementedError(f"address_space not supported: {address_space}") |
| 66 | + |
| 67 | + |
| 68 | +@_register_lowering(InitializeBarrierOp) |
| 69 | +def _initialize_barrier_op_lowering_rule( |
| 70 | + initialize_barrier_op: InitializeBarrierOp) -> Sequence[ir.Value]: |
| 71 | + |
| 72 | + shape = initialize_barrier_op.barriers_ref.type.shape |
| 73 | + num_barriers = functools.reduce(operator.mul, shape, 1) |
| 74 | + |
| 75 | + i32 = ir.IntegerType.get_signless(32) |
| 76 | + workgroup_nvptx_address_space = _gpu_address_space_to_nvptx( |
| 77 | + gpu.AddressSpace.Workgroup) |
| 78 | + ptr_ty = ir.Type.parse(f"!llvm.ptr<{workgroup_nvptx_address_space}>") |
| 79 | + |
| 80 | + lowered_barrier_type = _lowered_barrier_type() |
| 81 | + lowered_barrier_ref = memref.alloca( |
| 82 | + ir.MemRefType.get(shape, lowered_barrier_type), [], []) |
| 83 | + barrier_ref_address = memref_ptr( |
| 84 | + lowered_barrier_ref, memory_space=workgroup_nvptx_address_space) |
| 85 | + |
| 86 | + predicate = single_thread_predicate(per_block=True) |
| 87 | + for i in range(num_barriers): |
| 88 | + nvvm.mbarrier_init_shared( |
| 89 | + llvm.getelementptr(ptr_ty, barrier_ref_address, [], [i], |
| 90 | + lowered_barrier_type), |
| 91 | + c(initialize_barrier_op.arrival_count.value, i32), |
| 92 | + predicate=predicate |
| 93 | + ) |
| 94 | + return barrier_ref_address, |
| 95 | + |
| 96 | + |
| 97 | +def lower_mgpu_dialect(module: ir.Module): |
| 98 | + module.context.append_dialect_registry(mlir_interpreter.upstream_dialects) |
| 99 | + module.context.load_all_available_dialects() |
| 100 | + |
| 101 | + lowered_operations: set[ir.Operation | ir.OpView] = set() |
| 102 | + |
| 103 | + def _lower_op(op: ir.OpView): |
| 104 | + if op.name not in _lowerings: |
| 105 | + return |
| 106 | + lowering_rule = _lowerings[op.name] |
| 107 | + new_results = lowering_rule(op) |
| 108 | + for old, new in zip(op.results, new_results): |
| 109 | + old.replace_all_uses_with(new) |
| 110 | + lowered_operations.add(op) |
| 111 | + |
| 112 | + def _traverse_and_lower_op(op: ir.OpView): |
| 113 | + for region in op.operation.regions: |
| 114 | + for block in region: |
| 115 | + for block_op in list(block): |
| 116 | + with ir.InsertionPoint(block_op): |
| 117 | + _traverse_and_lower_op(block_op) |
| 118 | + _lower_op(op) |
| 119 | + |
| 120 | + with ir.InsertionPoint(module.body): |
| 121 | + for op in module.body: |
| 122 | + _traverse_and_lower_op(op) |
| 123 | + |
| 124 | + for lowered_op in lowered_operations: |
| 125 | + lowered_op.erase() |
0 commit comments