|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
4 | 4 |
|
5 | 5 | from .._gpu_ops_gen import * |
| 6 | +from .._gpu_ops_gen import _Dialect |
6 | 7 | from .._gpu_enum_gen import * |
7 | 8 | from ..._mlir_libs._mlirDialectsGPU import * |
| 9 | +from typing import Callable, Sequence, Union, Optional, List |
| 10 | + |
| 11 | +try: |
| 12 | + from ...ir import ( |
| 13 | + FunctionType, |
| 14 | + TypeAttr, |
| 15 | + StringAttr, |
| 16 | + UnitAttr, |
| 17 | + Block, |
| 18 | + InsertionPoint, |
| 19 | + ArrayAttr, |
| 20 | + Type, |
| 21 | + DictAttr, |
| 22 | + Attribute, |
| 23 | + DenseI32ArrayAttr, |
| 24 | + ) |
| 25 | + from .._ods_common import ( |
| 26 | + get_default_loc_context as _get_default_loc_context, |
| 27 | + _cext as _ods_cext, |
| 28 | + ) |
| 29 | +except ImportError as e: |
| 30 | + raise RuntimeError("Error loading imports from extension module") from e |
| 31 | + |
| 32 | + |
| 33 | +@_ods_cext.register_operation(_Dialect, replace=True) |
| 34 | +class GPUFuncOp(GPUFuncOp): |
| 35 | + __doc__ = GPUFuncOp.__doc__ |
| 36 | + |
| 37 | + KERNEL_ATTR_NAME = "gpu.kernel" |
| 38 | + KNOWN_BLOCK_SIZE_ATTR_NAME = "known_block_size" |
| 39 | + KNOWN_GRID_SIZE_ATTR_NAME = "known_grid_size" |
| 40 | + |
| 41 | + FUNCTION_TYPE_ATTR_NAME = "function_type" |
| 42 | + SYM_NAME_ATTR_NAME = "sym_name" |
| 43 | + ARGUMENT_ATTR_NAME = "arg_attrs" |
| 44 | + RESULT_ATTR_NAME = "res_attrs" |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + function_type: Union[FunctionType, TypeAttr], |
| 49 | + sym_name: Optional[Union[str, StringAttr]] = None, |
| 50 | + kernel: Optional[bool] = None, |
| 51 | + workgroup_attrib_attrs: Optional[Sequence[dict]] = None, |
| 52 | + private_attrib_attrs: Optional[Sequence[dict]] = None, |
| 53 | + known_block_size: Optional[Union[Sequence[int], DenseI32ArrayAttr]] = None, |
| 54 | + known_grid_size: Optional[Union[Sequence[int], DenseI32ArrayAttr]] = None, |
| 55 | + loc=None, |
| 56 | + ip=None, |
| 57 | + body_builder: Optional[Callable[[GPUFuncOp], None]] = None, |
| 58 | + ): |
| 59 | + """ |
| 60 | + Create a GPUFuncOp with the provided `function_type`, `sym_name`, |
| 61 | + `kernel`, `workgroup_attrib_attrs`, `private_attrib_attrs`, `known_block_size`, |
| 62 | + `known_grid_size`, and `body_builder`. |
| 63 | + - `function_type` is a FunctionType or a TypeAttr. |
| 64 | + - `sym_name` is a string or a StringAttr representing the function name. |
| 65 | + - `kernel` is a boolean representing whether the function is a kernel. |
| 66 | + - `workgroup_attrib_attrs` is an optional list of dictionaries. |
| 67 | + - `private_attrib_attrs` is an optional list of dictionaries. |
| 68 | + - `known_block_size` is an optional list of integers or a DenseI32ArrayAttr representing the known block size. |
| 69 | + - `known_grid_size` is an optional list of integers or a DenseI32ArrayAttr representing the known grid size. |
| 70 | + - `body_builder` is an optional callback. When provided, a new entry block |
| 71 | + is created and the callback is invoked with the new op as argument within |
| 72 | + an InsertionPoint context already set for the block. The callback is |
| 73 | + expected to insert a terminator in the block. |
| 74 | + """ |
| 75 | + function_type = ( |
| 76 | + TypeAttr.get(function_type) |
| 77 | + if not isinstance(function_type, TypeAttr) |
| 78 | + else function_type |
| 79 | + ) |
| 80 | + super().__init__( |
| 81 | + function_type, |
| 82 | + workgroup_attrib_attrs=workgroup_attrib_attrs, |
| 83 | + private_attrib_attrs=private_attrib_attrs, |
| 84 | + loc=loc, |
| 85 | + ip=ip, |
| 86 | + ) |
| 87 | + |
| 88 | + if isinstance(sym_name, str): |
| 89 | + self.attributes[self.SYM_NAME_ATTR_NAME] = StringAttr.get(sym_name) |
| 90 | + elif isinstance(sym_name, StringAttr): |
| 91 | + self.attributes[self.SYM_NAME_ATTR_NAME] = sym_name |
| 92 | + else: |
| 93 | + raise ValueError("sym_name must be a string or a StringAttr") |
| 94 | + |
| 95 | + if kernel: |
| 96 | + self.attributes[self.KERNEL_ATTR_NAME] = UnitAttr.get() |
| 97 | + |
| 98 | + if known_block_size is not None: |
| 99 | + if isinstance(known_block_size, Sequence): |
| 100 | + block_size = DenseI32ArrayAttr.get(known_block_size) |
| 101 | + self.attributes[self.KNOWN_BLOCK_SIZE_ATTR_NAME] = block_size |
| 102 | + elif isinstance(known_block_size, DenseI32ArrayAttr): |
| 103 | + self.attributes[self.KNOWN_BLOCK_SIZE_ATTR_NAME] = known_block_size |
| 104 | + else: |
| 105 | + raise ValueError( |
| 106 | + "known_block_size must be a list of integers or a DenseI32ArrayAttr" |
| 107 | + ) |
| 108 | + |
| 109 | + if known_grid_size is not None: |
| 110 | + if isinstance(known_grid_size, Sequence): |
| 111 | + grid_size = DenseI32ArrayAttr.get(known_grid_size) |
| 112 | + self.attributes[self.KNOWN_GRID_SIZE_ATTR_NAME] = grid_size |
| 113 | + elif isinstance(known_grid_size, DenseI32ArrayAttr): |
| 114 | + self.attributes[self.KNOWN_GRID_SIZE_ATTR_NAME] = known_grid_size |
| 115 | + else: |
| 116 | + raise ValueError( |
| 117 | + "known_grid_size must be a list of integers or a DenseI32ArrayAttr" |
| 118 | + ) |
| 119 | + |
| 120 | + if body_builder is not None: |
| 121 | + with InsertionPoint(self.add_entry_block()): |
| 122 | + body_builder(self) |
| 123 | + |
| 124 | + @property |
| 125 | + def name(self) -> StringAttr: |
| 126 | + return StringAttr(self.attributes[self.SYM_NAME_ATTR_NAME]) |
| 127 | + |
| 128 | + @property |
| 129 | + def is_kernel(self) -> bool: |
| 130 | + return self.KERNEL_ATTR_NAME in self.attributes |
| 131 | + |
| 132 | + def add_entry_block(self) -> Block: |
| 133 | + if len(self.body.blocks) > 0: |
| 134 | + raise RuntimeError(f"Entry block already exists for {self.name.value}") |
| 135 | + |
| 136 | + function_type = self.function_type.value |
| 137 | + return self.body.blocks.append( |
| 138 | + *function_type.inputs, |
| 139 | + arg_locs=[self.location for _ in function_type.inputs], |
| 140 | + ) |
| 141 | + |
| 142 | + @property |
| 143 | + def entry_block(self) -> Block: |
| 144 | + if len(self.body.blocks) == 0: |
| 145 | + raise RuntimeError( |
| 146 | + f"Entry block does not exist for {self.name.value}." |
| 147 | + + " Do you need to call the add_entry_block() method on this GPUFuncOp?" |
| 148 | + ) |
| 149 | + return self.body.blocks[0] |
| 150 | + |
| 151 | + @property |
| 152 | + def arguments(self) -> Sequence[Type]: |
| 153 | + return self.function_type.value.inputs |
0 commit comments