|
16 | 16 | include "mlir/Dialect/GPU/IR/GPUBase.td"
|
17 | 17 | include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
|
18 | 18 |
|
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | +// GPU kernel attribute |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +def GPU_KernelAttr : GPU_Attr<"Kernel", "kernel"> { |
| 24 | + let description = [{ |
| 25 | + GPU attribute for storing metadata related to a compiled kernel. It |
| 26 | + contains the attribute dictionary of the LLVM function used to generate the |
| 27 | + kernel, as well as an optional dictionary for additional metadata, like |
| 28 | + occupancy information. |
| 29 | + |
| 30 | + Examples: |
| 31 | + ```mlir |
| 32 | + #gpu.kernel<{sym_name = "test_fusion__part_0", ...}, |
| 33 | + metadata = {reg_count = 255, ...}> |
| 34 | + ``` |
| 35 | + }]; |
| 36 | + let parameters = (ins |
| 37 | + "DictionaryAttr":$func_attrs, |
| 38 | + OptionalParameter<"DictionaryAttr", "metadata dictionary">:$metadata |
| 39 | + ); |
| 40 | + let assemblyFormat = [{ |
| 41 | + `<` $func_attrs (`,` `metadata` `=` $metadata^ )? `>` |
| 42 | + }]; |
| 43 | + let builders = [ |
| 44 | + AttrBuilderWithInferredContext<(ins "DictionaryAttr":$funcAttrs, |
| 45 | + CArg<"DictionaryAttr", |
| 46 | + "nullptr">:$metadata), [{ |
| 47 | + assert(funcAttrs && "invalid function attributes dictionary"); |
| 48 | + return $_get(funcAttrs.getContext(), funcAttrs, metadata); |
| 49 | + }]>, |
| 50 | + AttrBuilderWithInferredContext<(ins "Operation*":$kernel, |
| 51 | + CArg<"DictionaryAttr", |
| 52 | + "nullptr">:$metadata)> |
| 53 | + ]; |
| 54 | + let extraClassDeclaration = [{ |
| 55 | + /// Returns the function attribute corresponding to key or nullptr if missing. |
| 56 | + Attribute getAttr(StringRef key) const { |
| 57 | + return getFuncAttrs().get(key); |
| 58 | + } |
| 59 | + template <typename ConcreteAttr> |
| 60 | + ConcreteAttr getAttr(StringRef key) const { |
| 61 | + return llvm::dyn_cast_or_null<ConcreteAttr>(getAttr(key)); |
| 62 | + } |
| 63 | + Attribute getAttr(StringAttr key) const; |
| 64 | + template <typename ConcreteAttr> |
| 65 | + ConcreteAttr getAttr(StringAttr key) const { |
| 66 | + return llvm::dyn_cast_or_null<ConcreteAttr>(getAttr(key)); |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns the name of the kernel. |
| 70 | + StringAttr getName() const { |
| 71 | + return getAttr<StringAttr>("sym_name"); |
| 72 | + } |
| 73 | + |
| 74 | + /// Returns the metadta attribute corresponding to key or nullptr if missing. |
| 75 | + Attribute getMDAttr(StringRef key) const { |
| 76 | + if (DictionaryAttr attrs = getMetadata()) |
| 77 | + return attrs.get(key); |
| 78 | + return nullptr; |
| 79 | + } |
| 80 | + template <typename ConcreteAttr> |
| 81 | + ConcreteAttr getMDAttr(StringRef key) const { |
| 82 | + return llvm::dyn_cast_or_null<ConcreteAttr>(getMDAttr(key)); |
| 83 | + } |
| 84 | + Attribute getMDAttr(StringAttr key) const; |
| 85 | + template <typename ConcreteAttr> |
| 86 | + ConcreteAttr getMDAttr(StringAttr key) const { |
| 87 | + return llvm::dyn_cast_or_null<ConcreteAttr>(getMDAttr(key)); |
| 88 | + } |
| 89 | + |
| 90 | + /// Helper function for appending metadata to a kernel attribute. |
| 91 | + KernelAttr appendMetadata(ArrayRef<NamedAttribute> attrs) const; |
| 92 | + }]; |
| 93 | +} |
| 94 | + |
| 95 | +//===----------------------------------------------------------------------===// |
| 96 | +// GPU kernel table attribute |
| 97 | +//===----------------------------------------------------------------------===// |
| 98 | + |
| 99 | +def GPU_KernelTableAttr : GPU_Attr<"KernelTable", "kernel_table"> { |
| 100 | + let description = [{ |
| 101 | + GPU attribute representing a table of kernels metadata. All the attributes |
| 102 | + in the dictionary must be of type `#gpu.kernel`. |
| 103 | + |
| 104 | + Examples: |
| 105 | + ```mlir |
| 106 | + #gpu.kernel_table<{kernel0 = #gpu.kernel<...>}> |
| 107 | + ``` |
| 108 | + }]; |
| 109 | + let parameters = (ins |
| 110 | + "DictionaryAttr":$kernel_table |
| 111 | + ); |
| 112 | + let assemblyFormat = [{ |
| 113 | + `<` $kernel_table `>` |
| 114 | + }]; |
| 115 | + let builders = [ |
| 116 | + AttrBuilderWithInferredContext<(ins "DictionaryAttr":$kernel_table), [{ |
| 117 | + assert(kernel_table && "invalid kernel table"); |
| 118 | + return $_get(kernel_table.getContext(), kernel_table); |
| 119 | + }]> |
| 120 | + ]; |
| 121 | + let skipDefaultBuilders = 1; |
| 122 | + let genVerifyDecl = 1; |
| 123 | + let extraClassDeclaration = [{ |
| 124 | + /// Helper iterator class for traversing the kernel table. |
| 125 | + struct KernelIterator |
| 126 | + : llvm::mapped_iterator_base<KernelIterator, |
| 127 | + llvm::ArrayRef<NamedAttribute>::iterator, |
| 128 | + std::pair<StringAttr, KernelAttr>> { |
| 129 | + using llvm::mapped_iterator_base< |
| 130 | + KernelIterator, llvm::ArrayRef<NamedAttribute>::iterator, |
| 131 | + std::pair<StringAttr, KernelAttr>>::mapped_iterator_base; |
| 132 | + /// Map the iterator to the kernel name and a KernelAttribute. |
| 133 | + std::pair<StringAttr, KernelAttr> mapElement(NamedAttribute attr) const { |
| 134 | + return {attr.getName(), llvm::cast<KernelAttr>(attr.getValue())}; |
| 135 | + } |
| 136 | + }; |
| 137 | + auto begin() const { |
| 138 | + return KernelIterator(getKernelTable().begin()); |
| 139 | + } |
| 140 | + auto end() const { |
| 141 | + return KernelIterator(getKernelTable().end()); |
| 142 | + } |
| 143 | + size_t size() const { |
| 144 | + return getKernelTable().size(); |
| 145 | + } |
| 146 | + }]; |
| 147 | +} |
| 148 | + |
19 | 149 | //===----------------------------------------------------------------------===//
|
20 | 150 | // GPU object attribute.
|
21 | 151 | //===----------------------------------------------------------------------===//
|
@@ -63,16 +193,29 @@ def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
|
63 | 193 | #gpu.object<#nvvm.target, properties = {O = 3 : i32}, assembly = "..."> // An assembly object with additional properties.
|
64 | 194 | #gpu.object<#rocdl.target, bin = "..."> // A binary object.
|
65 | 195 | #gpu.object<#nvvm.target, "..."> // A fatbin object.
|
| 196 | + #gpu.object<#nvvm.target, kernels = #gpu.kernel_table<...>, "..."> // An object with a kernel table. |
66 | 197 | ```
|
67 | 198 | }];
|
68 | 199 | let parameters = (ins
|
69 | 200 | "Attribute":$target,
|
70 | 201 | DefaultValuedParameter<"CompilationTarget", "CompilationTarget::Fatbin">:$format,
|
71 | 202 | "StringAttr":$object,
|
72 |
| - OptionalParameter<"DictionaryAttr">:$properties |
| 203 | + OptionalParameter<"DictionaryAttr">:$properties, |
| 204 | + OptionalParameter<"KernelTableAttr">:$kernels |
73 | 205 | );
|
| 206 | + let builders = [ |
| 207 | + AttrBuilderWithInferredContext<(ins "Attribute":$target, |
| 208 | + "CompilationTarget":$format, |
| 209 | + "StringAttr":$object, |
| 210 | + CArg<"DictionaryAttr", "nullptr">:$properties, |
| 211 | + CArg<"KernelTableAttr", "nullptr">:$kernels), [{ |
| 212 | + assert(target && "invalid target"); |
| 213 | + return $_get(target.getContext(), target, format, object, properties, kernels); |
| 214 | + }]> |
| 215 | + ]; |
74 | 216 | let assemblyFormat = [{ `<`
|
75 |
| - $target `,` (`properties` `=` $properties ^ `,`)? |
| 217 | + $target `,` (`properties` `=` $properties^ `,`)? |
| 218 | + (`kernels` `=` $kernels^ `,`)? |
76 | 219 | custom<Object>($format, $object)
|
77 | 220 | `>`
|
78 | 221 | }];
|
|
0 commit comments