| 1 | //===- DLTI.cpp - Data Layout And Target Info MLIR Dialect Implementation -===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 10 | #include "mlir/IR/Builders.h" |
| 11 | #include "mlir/IR/BuiltinAttributes.h" |
| 12 | #include "mlir/IR/BuiltinDialect.h" |
| 13 | #include "mlir/IR/BuiltinOps.h" |
| 14 | #include "mlir/IR/BuiltinTypes.h" |
| 15 | #include "mlir/IR/Dialect.h" |
| 16 | |
| 17 | #include "llvm/ADT/MapVector.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/TypeSwitch.h" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | |
| 23 | #include "mlir/Dialect/DLTI/DLTIOpInterfaces.cpp.inc" |
| 24 | |
| 25 | #include "mlir/Dialect/DLTI/DLTIDialect.cpp.inc" |
| 26 | |
| 27 | #define GET_ATTRDEF_CLASSES |
| 28 | #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc" |
| 29 | |
| 30 | #define DEBUG_TYPE "dlti" |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Common parsing utility functions. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | /// Parse an entry which can either be of the form `key = value` or a |
| 37 | /// #dlti.dl_entry attribute. When `tryType=true` the key can be a type, |
| 38 | /// otherwise only quoted strings are allowed. The grammar is as follows: |
| 39 | /// entry ::= ((type | quoted-string) `=` attr) | dl-entry-attr |
| 40 | static ParseResult parseKeyValuePair(AsmParser &parser, |
| 41 | DataLayoutEntryInterface &entry, |
| 42 | bool tryType = false) { |
| 43 | Attribute value; |
| 44 | |
| 45 | if (tryType) { |
| 46 | Type type; |
| 47 | OptionalParseResult parsedType = parser.parseOptionalType(result&: type); |
| 48 | if (parsedType.has_value()) { |
| 49 | if (failed(Result: parsedType.value())) |
| 50 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 51 | << "error while parsing type DLTI key" ; |
| 52 | |
| 53 | if (failed(Result: parser.parseEqual()) || failed(Result: parser.parseAttribute(result&: value))) |
| 54 | return failure(); |
| 55 | |
| 56 | entry = DataLayoutEntryAttr::get(key: type, value); |
| 57 | return ParseResult::success(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | std::string ident; |
| 62 | OptionalParseResult parsedStr = parser.parseOptionalString(string: &ident); |
| 63 | if (parsedStr.has_value() && succeeded(Result: parsedStr.value())) { |
| 64 | if (failed(Result: parser.parseEqual()) || failed(Result: parser.parseAttribute(result&: value))) |
| 65 | return failure(); // Assume that an error has already been emitted. |
| 66 | |
| 67 | entry = DataLayoutEntryAttr::get( |
| 68 | key: StringAttr::get(context: parser.getContext(), bytes: ident), value); |
| 69 | return ParseResult::success(); |
| 70 | } |
| 71 | |
| 72 | OptionalParseResult parsedEntry = parser.parseAttribute(result&: entry); |
| 73 | if (parsedEntry.has_value()) { |
| 74 | if (succeeded(Result: parsedEntry.value())) |
| 75 | return parsedEntry.value(); |
| 76 | return failure(); // Assume that an error has already been emitted. |
| 77 | } |
| 78 | return parser.emitError(loc: parser.getCurrentLocation()) |
| 79 | << "failed to parse DLTI entry" ; |
| 80 | } |
| 81 | |
| 82 | /// Construct a requested attribute by parsing list of entries occurring within |
| 83 | /// a pair of `<` and `>`, optionally allow types as keys and an empty list. |
| 84 | /// The grammar is as follows: |
| 85 | /// bracketed-entry-list ::=`<` entry-list `>` |
| 86 | /// entry-list ::= | entry | entry `,` entry-list |
| 87 | /// entry ::= ((type | quoted-string) `=` attr) | dl-entry-attr |
| 88 | template <class Attr> |
| 89 | static Attribute parseAngleBracketedEntries(AsmParser &parser, Type ty, |
| 90 | bool tryType = false, |
| 91 | bool allowEmpty = false) { |
| 92 | SmallVector<DataLayoutEntryInterface> entries; |
| 93 | if (failed(parser.parseCommaSeparatedList( |
| 94 | AsmParser::Delimiter::LessGreater, [&]() { |
| 95 | return parseKeyValuePair(parser, entry&: entries.emplace_back(), tryType); |
| 96 | }))) |
| 97 | return {}; |
| 98 | |
| 99 | if (entries.empty() && !allowEmpty) { |
| 100 | parser.emitError(loc: parser.getNameLoc()) << "no DLTI entries provided" ; |
| 101 | return {}; |
| 102 | } |
| 103 | |
| 104 | return Attr::getChecked([&] { return parser.emitError(loc: parser.getNameLoc()); }, |
| 105 | parser.getContext(), ArrayRef(entries)); |
| 106 | } |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // Common printing utility functions. |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | /// Convert pointer-union keys to strings. |
| 113 | static std::string keyToStr(DataLayoutEntryKey key) { |
| 114 | std::string buf; |
| 115 | TypeSwitch<DataLayoutEntryKey>(key) |
| 116 | .Case<StringAttr, Type>( // The only two kinds of key we know of. |
| 117 | caseFn: [&](auto key) { llvm::raw_string_ostream(buf) << key; }); |
| 118 | return buf; |
| 119 | } |
| 120 | |
| 121 | /// Pretty-print entries, each in `key = value` format, separated by commas. |
| 122 | template <class T> |
| 123 | static void printAngleBracketedEntries(AsmPrinter &os, T &&entries) { |
| 124 | os << "<" ; |
| 125 | llvm::interleaveComma(std::forward<T>(entries), os, [&](auto entry) { |
| 126 | os << keyToStr(entry.getKey()) << " = " << entry.getValue(); |
| 127 | }); |
| 128 | os << ">" ; |
| 129 | } |
| 130 | |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | // Common verifying utility functions. |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | /// Verify entries, with the option to disallow types as keys. |
| 136 | static LogicalResult verifyEntries(function_ref<InFlightDiagnostic()> emitError, |
| 137 | ArrayRef<DataLayoutEntryInterface> entries, |
| 138 | bool allowTypes = true) { |
| 139 | DenseSet<DataLayoutEntryKey> keys; |
| 140 | for (DataLayoutEntryInterface entry : entries) { |
| 141 | if (!entry) |
| 142 | return emitError() << "contained invalid DLTI entry" ; |
| 143 | DataLayoutEntryKey key = entry.getKey(); |
| 144 | if (key.isNull()) |
| 145 | return emitError() << "contained invalid DLTI key" ; |
| 146 | if (!allowTypes && dyn_cast<Type>(Val&: key)) |
| 147 | return emitError() << "type as DLTI key is not allowed" ; |
| 148 | if (auto strKey = dyn_cast<StringAttr>(Val&: key)) |
| 149 | if (strKey.getValue().empty()) |
| 150 | return emitError() << "empty string as DLTI key is not allowed" ; |
| 151 | if (!keys.insert(V: key).second) |
| 152 | return emitError() << "repeated DLTI key: " << keyToStr(key); |
| 153 | if (!entry.getValue()) |
| 154 | return emitError() << "value associated to DLTI key " << keyToStr(key) |
| 155 | << " is invalid" ; |
| 156 | } |
| 157 | return success(); |
| 158 | } |
| 159 | |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | // DataLayoutEntryAttr |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | namespace mlir { |
| 164 | namespace detail { |
| 165 | class DataLayoutEntryAttrStorage : public AttributeStorage { |
| 166 | public: |
| 167 | using KeyTy = std::pair<DataLayoutEntryKey, Attribute>; |
| 168 | |
| 169 | DataLayoutEntryAttrStorage(DataLayoutEntryKey entryKey, Attribute value) |
| 170 | : entryKey(entryKey), value(value) {} |
| 171 | |
| 172 | static DataLayoutEntryAttrStorage * |
| 173 | construct(AttributeStorageAllocator &allocator, const KeyTy &key) { |
| 174 | return new (allocator.allocate<DataLayoutEntryAttrStorage>()) |
| 175 | DataLayoutEntryAttrStorage(key.first, key.second); |
| 176 | } |
| 177 | |
| 178 | bool operator==(const KeyTy &other) const { |
| 179 | return other.first == entryKey && other.second == value; |
| 180 | } |
| 181 | |
| 182 | DataLayoutEntryKey entryKey; |
| 183 | Attribute value; |
| 184 | }; |
| 185 | } // namespace detail |
| 186 | } // namespace mlir |
| 187 | |
| 188 | DataLayoutEntryAttr DataLayoutEntryAttr::get(StringAttr key, Attribute value) { |
| 189 | return Base::get(ctx: key.getContext(), args&: key, args&: value); |
| 190 | } |
| 191 | |
| 192 | DataLayoutEntryAttr DataLayoutEntryAttr::get(Type key, Attribute value) { |
| 193 | return Base::get(ctx: key.getContext(), args&: key, args&: value); |
| 194 | } |
| 195 | |
| 196 | DataLayoutEntryKey DataLayoutEntryAttr::getKey() const { |
| 197 | return getImpl()->entryKey; |
| 198 | } |
| 199 | |
| 200 | Attribute DataLayoutEntryAttr::getValue() const { return getImpl()->value; } |
| 201 | |
| 202 | /// Parses an attribute with syntax: |
| 203 | /// dl-entry-attr ::= `#dlti.` `dl_entry` `<` (type | quoted-string) `,` |
| 204 | /// attr `>` |
| 205 | Attribute DataLayoutEntryAttr::parse(AsmParser &parser, Type type) { |
| 206 | if (failed(Result: parser.parseLess())) |
| 207 | return {}; |
| 208 | |
| 209 | Type typeKey = nullptr; |
| 210 | std::string identifier; |
| 211 | SMLoc idLoc = parser.getCurrentLocation(); |
| 212 | OptionalParseResult parsedType = parser.parseOptionalType(result&: typeKey); |
| 213 | if (parsedType.has_value() && failed(Result: parsedType.value())) |
| 214 | return {}; |
| 215 | if (!parsedType.has_value()) { |
| 216 | OptionalParseResult parsedString = parser.parseOptionalString(string: &identifier); |
| 217 | if (!parsedString.has_value() || failed(Result: parsedString.value())) { |
| 218 | parser.emitError(loc: idLoc) << "expected a type or a quoted string" ; |
| 219 | return {}; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | Attribute value; |
| 224 | if (failed(Result: parser.parseComma()) || failed(Result: parser.parseAttribute(result&: value)) || |
| 225 | failed(Result: parser.parseGreater())) |
| 226 | return {}; |
| 227 | |
| 228 | return typeKey ? get(key: typeKey, value) |
| 229 | : get(key: parser.getBuilder().getStringAttr(bytes: identifier), value); |
| 230 | } |
| 231 | |
| 232 | void DataLayoutEntryAttr::print(AsmPrinter &printer) const { |
| 233 | printer << "<" << keyToStr(key: getKey()) << ", " << getValue() << ">" ; |
| 234 | } |
| 235 | |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | // DLTIMapAttr |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | |
| 240 | /// Parses an attribute with syntax: |
| 241 | /// map-attr ::= `#dlti.` `map` `<` entry-list `>` |
| 242 | /// entry-list ::= entry | entry `,` entry-list |
| 243 | /// entry ::= ((type | quoted-string) `=` attr) | dl-entry-attr |
| 244 | Attribute MapAttr::parse(AsmParser &parser, Type type) { |
| 245 | return parseAngleBracketedEntries<MapAttr>(parser, ty: type, /*tryType=*/true, |
| 246 | /*allowEmpty=*/true); |
| 247 | } |
| 248 | |
| 249 | void MapAttr::print(AsmPrinter &printer) const { |
| 250 | printAngleBracketedEntries(os&: printer, entries: getEntries()); |
| 251 | } |
| 252 | |
| 253 | LogicalResult MapAttr::verify(function_ref<InFlightDiagnostic()> emitError, |
| 254 | ArrayRef<DataLayoutEntryInterface> entries) { |
| 255 | return verifyEntries(emitError, entries); |
| 256 | } |
| 257 | |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | // DataLayoutSpecAttr |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
| 262 | LogicalResult |
| 263 | DataLayoutSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError, |
| 264 | ArrayRef<DataLayoutEntryInterface> entries) { |
| 265 | return verifyEntries(emitError, entries); |
| 266 | } |
| 267 | |
| 268 | /// Given a list of old and a list of new entries, overwrites old entries with |
| 269 | /// new ones if they have matching keys, appends new entries to the old entry |
| 270 | /// list otherwise. |
| 271 | static void |
| 272 | overwriteDuplicateEntries(SmallVectorImpl<DataLayoutEntryInterface> &oldEntries, |
| 273 | ArrayRef<DataLayoutEntryInterface> newEntries) { |
| 274 | unsigned oldEntriesSize = oldEntries.size(); |
| 275 | for (DataLayoutEntryInterface entry : newEntries) { |
| 276 | // We expect a small (dozens) number of entries, so it is practically |
| 277 | // cheaper to iterate over the list linearly rather than to create an |
| 278 | // auxiliary hashmap to avoid duplication. Also note that we never need to |
| 279 | // check for duplicate keys the values that were added from `newEntries`. |
| 280 | bool replaced = false; |
| 281 | for (unsigned i = 0; i < oldEntriesSize; ++i) { |
| 282 | if (oldEntries[i].getKey() == entry.getKey()) { |
| 283 | oldEntries[i] = entry; |
| 284 | replaced = true; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | if (!replaced) |
| 289 | oldEntries.push_back(Elt: entry); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /// Combines a data layout spec into the given lists of entries organized by |
| 294 | /// type class and identifier, overwriting them if necessary. Fails to combine |
| 295 | /// if the two entries with identical keys are not compatible. |
| 296 | static LogicalResult combineOneSpec( |
| 297 | DataLayoutSpecInterface spec, |
| 298 | llvm::MapVector<TypeID, DataLayoutEntryList> &entriesForType, |
| 299 | llvm::MapVector<StringAttr, DataLayoutEntryInterface> &entriesForID) { |
| 300 | // A missing spec should be fine. |
| 301 | if (!spec) |
| 302 | return success(); |
| 303 | |
| 304 | llvm::MapVector<TypeID, DataLayoutEntryList> newEntriesForType; |
| 305 | llvm::MapVector<StringAttr, DataLayoutEntryInterface> newEntriesForID; |
| 306 | spec.bucketEntriesByType(types&: newEntriesForType, ids&: newEntriesForID); |
| 307 | |
| 308 | // Combine non-Type DL entries first so they are visible to the |
| 309 | // `type.areCompatible` method, allowing to query global properties. |
| 310 | for (const auto &kvp : newEntriesForID) { |
| 311 | StringAttr id = cast<StringAttr>(Val: kvp.second.getKey()); |
| 312 | Dialect *dialect = id.getReferencedDialect(); |
| 313 | if (!entriesForID.count(Key: id)) { |
| 314 | entriesForID[id] = kvp.second; |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | // Attempt to combine the entries using the dialect interface. If the |
| 319 | // dialect is not loaded for some reason, use the default combinator |
| 320 | // that conservatively accepts identical entries only. |
| 321 | entriesForID[id] = |
| 322 | dialect ? cast<DataLayoutDialectInterface>(Val: dialect)->combine( |
| 323 | outer: entriesForID[id], inner: kvp.second) |
| 324 | : DataLayoutDialectInterface::defaultCombine(outer: entriesForID[id], |
| 325 | inner: kvp.second); |
| 326 | if (!entriesForID[id]) |
| 327 | return failure(); |
| 328 | } |
| 329 | |
| 330 | // Try overwriting the old entries with the new ones. |
| 331 | for (auto &kvp : newEntriesForType) { |
| 332 | if (!entriesForType.count(Key: kvp.first)) { |
| 333 | entriesForType[kvp.first] = std::move(kvp.second); |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | Type typeSample = cast<Type>(Val: kvp.second.front().getKey()); |
| 338 | assert(&typeSample.getDialect() != |
| 339 | typeSample.getContext()->getLoadedDialect<BuiltinDialect>() && |
| 340 | "unexpected data layout entry for built-in type" ); |
| 341 | |
| 342 | auto interface = cast<DataLayoutTypeInterface>(Val&: typeSample); |
| 343 | // TODO: Revisit this method and call once |
| 344 | // https://github.com/llvm/llvm-project/issues/130321 gets resolved. |
| 345 | if (!interface.areCompatible(oldLayout: entriesForType.lookup(Key: kvp.first), newLayout: kvp.second, |
| 346 | newSpec: spec, identified: entriesForID)) |
| 347 | return failure(); |
| 348 | |
| 349 | overwriteDuplicateEntries(oldEntries&: entriesForType[kvp.first], newEntries: kvp.second); |
| 350 | } |
| 351 | |
| 352 | return success(); |
| 353 | } |
| 354 | |
| 355 | DataLayoutSpecAttr |
| 356 | DataLayoutSpecAttr::combineWith(ArrayRef<DataLayoutSpecInterface> specs) const { |
| 357 | // Only combine with attributes of the same kind. |
| 358 | // TODO: reconsider this when the need arises. |
| 359 | if (any_of(Range&: specs, P: [](DataLayoutSpecInterface spec) { |
| 360 | return !llvm::isa<DataLayoutSpecAttr>(Val: spec); |
| 361 | })) |
| 362 | return {}; |
| 363 | |
| 364 | // Combine all specs in order, with `this` being the last one. |
| 365 | llvm::MapVector<TypeID, DataLayoutEntryList> entriesForType; |
| 366 | llvm::MapVector<StringAttr, DataLayoutEntryInterface> entriesForID; |
| 367 | for (DataLayoutSpecInterface spec : specs) |
| 368 | if (failed(Result: combineOneSpec(spec, entriesForType, entriesForID))) |
| 369 | return nullptr; |
| 370 | if (failed(Result: combineOneSpec(spec: *this, entriesForType, entriesForID))) |
| 371 | return nullptr; |
| 372 | |
| 373 | // Rebuild the linear list of entries. |
| 374 | SmallVector<DataLayoutEntryInterface> entries; |
| 375 | llvm::append_range(C&: entries, R: llvm::make_second_range(c&: entriesForID)); |
| 376 | for (const auto &kvp : entriesForType) |
| 377 | llvm::append_range(C&: entries, R: kvp.second); |
| 378 | |
| 379 | return DataLayoutSpecAttr::get(context: getContext(), entries); |
| 380 | } |
| 381 | |
| 382 | StringAttr |
| 383 | DataLayoutSpecAttr::getEndiannessIdentifier(MLIRContext *context) const { |
| 384 | return Builder(context).getStringAttr(bytes: DLTIDialect::kDataLayoutEndiannessKey); |
| 385 | } |
| 386 | |
| 387 | StringAttr DataLayoutSpecAttr::getDefaultMemorySpaceIdentifier( |
| 388 | MLIRContext *context) const { |
| 389 | return Builder(context).getStringAttr( |
| 390 | bytes: DLTIDialect::kDataLayoutDefaultMemorySpaceKey); |
| 391 | } |
| 392 | |
| 393 | StringAttr |
| 394 | DataLayoutSpecAttr::getAllocaMemorySpaceIdentifier(MLIRContext *context) const { |
| 395 | return Builder(context).getStringAttr( |
| 396 | bytes: DLTIDialect::kDataLayoutAllocaMemorySpaceKey); |
| 397 | } |
| 398 | |
| 399 | StringAttr DataLayoutSpecAttr::getProgramMemorySpaceIdentifier( |
| 400 | MLIRContext *context) const { |
| 401 | return Builder(context).getStringAttr( |
| 402 | bytes: DLTIDialect::kDataLayoutProgramMemorySpaceKey); |
| 403 | } |
| 404 | |
| 405 | StringAttr |
| 406 | DataLayoutSpecAttr::getGlobalMemorySpaceIdentifier(MLIRContext *context) const { |
| 407 | return Builder(context).getStringAttr( |
| 408 | bytes: DLTIDialect::kDataLayoutGlobalMemorySpaceKey); |
| 409 | } |
| 410 | |
| 411 | StringAttr |
| 412 | DataLayoutSpecAttr::getManglingModeIdentifier(MLIRContext *context) const { |
| 413 | return Builder(context).getStringAttr( |
| 414 | bytes: DLTIDialect::kDataLayoutManglingModeKey); |
| 415 | } |
| 416 | |
| 417 | StringAttr |
| 418 | DataLayoutSpecAttr::getStackAlignmentIdentifier(MLIRContext *context) const { |
| 419 | return Builder(context).getStringAttr( |
| 420 | bytes: DLTIDialect::kDataLayoutStackAlignmentKey); |
| 421 | } |
| 422 | |
| 423 | StringAttr DataLayoutSpecAttr::getFunctionPointerAlignmentIdentifier( |
| 424 | MLIRContext *context) const { |
| 425 | return Builder(context).getStringAttr( |
| 426 | bytes: DLTIDialect::kDataLayoutFunctionPointerAlignmentKey); |
| 427 | } |
| 428 | |
| 429 | StringAttr |
| 430 | DataLayoutSpecAttr::getLegalIntWidthsIdentifier(MLIRContext *context) const { |
| 431 | return Builder(context).getStringAttr( |
| 432 | bytes: DLTIDialect::kDataLayoutLegalIntWidthsKey); |
| 433 | } |
| 434 | |
| 435 | /// Parses an attribute with syntax: |
| 436 | /// dl-spec-attr ::= `#dlti.` `dl_spec` `<` entry-list `>` |
| 437 | /// entry-list ::= | entry | entry `,` entry-list |
| 438 | /// entry ::= ((type | quoted-string) = attr) | dl-entry-attr |
| 439 | Attribute DataLayoutSpecAttr::parse(AsmParser &parser, Type type) { |
| 440 | return parseAngleBracketedEntries<DataLayoutSpecAttr>(parser, ty: type, |
| 441 | /*tryType=*/true, |
| 442 | /*allowEmpty=*/true); |
| 443 | } |
| 444 | |
| 445 | void DataLayoutSpecAttr::print(AsmPrinter &printer) const { |
| 446 | printAngleBracketedEntries(os&: printer, entries: getEntries()); |
| 447 | } |
| 448 | |
| 449 | //===----------------------------------------------------------------------===// |
| 450 | // TargetDeviceSpecAttr |
| 451 | //===----------------------------------------------------------------------===// |
| 452 | |
| 453 | LogicalResult |
| 454 | TargetDeviceSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError, |
| 455 | ArrayRef<DataLayoutEntryInterface> entries) { |
| 456 | return verifyEntries(emitError, entries, /*allowTypes=*/false); |
| 457 | } |
| 458 | |
| 459 | /// Parses an attribute with syntax: |
| 460 | /// dev-spec-attr ::= `#dlti.` `target_device_spec` `<` entry-list `>` |
| 461 | /// entry-list ::= entry | entry `,` entry-list |
| 462 | /// entry ::= (quoted-string `=` attr) | dl-entry-attr |
| 463 | Attribute TargetDeviceSpecAttr::parse(AsmParser &parser, Type type) { |
| 464 | return parseAngleBracketedEntries<TargetDeviceSpecAttr>(parser, ty: type); |
| 465 | } |
| 466 | |
| 467 | void TargetDeviceSpecAttr::print(AsmPrinter &printer) const { |
| 468 | printAngleBracketedEntries(os&: printer, entries: getEntries()); |
| 469 | } |
| 470 | |
| 471 | //===----------------------------------------------------------------------===// |
| 472 | // TargetSystemSpecAttr |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | |
| 475 | LogicalResult |
| 476 | TargetSystemSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError, |
| 477 | ArrayRef<DataLayoutEntryInterface> entries) { |
| 478 | DenseSet<TargetSystemSpecInterface::DeviceID> deviceIds; |
| 479 | |
| 480 | for (const auto &entry : entries) { |
| 481 | auto deviceId = |
| 482 | llvm::dyn_cast<TargetSystemSpecInterface::DeviceID>(Val: entry.getKey()); |
| 483 | if (!deviceId) |
| 484 | return emitError() << "non-string key of DLTI system spec" ; |
| 485 | |
| 486 | if (auto targetDeviceSpec = |
| 487 | llvm::dyn_cast<TargetDeviceSpecInterface>(Val: entry.getValue())) { |
| 488 | if (failed(Result: TargetDeviceSpecAttr::verify(emitError, |
| 489 | entries: targetDeviceSpec.getEntries()))) |
| 490 | return failure(); // Assume sub-verifier outputted error message. |
| 491 | } else { |
| 492 | return emitError() << "value associated with key " << deviceId |
| 493 | << " is not a DLTI device spec" ; |
| 494 | } |
| 495 | |
| 496 | // Check that device IDs are unique across all entries. |
| 497 | if (!deviceIds.insert(V: deviceId).second) |
| 498 | return emitError() << "repeated device ID in dlti.target_system_spec: " |
| 499 | << deviceId; |
| 500 | } |
| 501 | |
| 502 | return success(); |
| 503 | } |
| 504 | |
| 505 | /// Parses an attribute with syntax: |
| 506 | /// sys-spec-attr ::= `#dlti.` `target_system_spec` `<` entry-list `>` |
| 507 | /// entry-list ::= entry | entry `,` entry-list |
| 508 | /// entry ::= (quoted-string `=` dev-spec-attr) | dl-entry-attr |
| 509 | Attribute TargetSystemSpecAttr::parse(AsmParser &parser, Type type) { |
| 510 | return parseAngleBracketedEntries<TargetSystemSpecAttr>(parser, ty: type); |
| 511 | } |
| 512 | |
| 513 | void TargetSystemSpecAttr::print(AsmPrinter &printer) const { |
| 514 | printAngleBracketedEntries(os&: printer, entries: getEntries()); |
| 515 | } |
| 516 | |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | // DLTIDialect |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | |
| 521 | static constexpr llvm::StringLiteral kDltiAttrName = "dlti" ; |
| 522 | |
| 523 | FailureOr<Attribute> mlir::dlti::detail::queryDlti(Operation *op, |
| 524 | DataLayoutEntryKey key) { |
| 525 | auto queryable = op->getInherentAttrOfType<DLTIQueryInterface>(name: kDltiAttrName); |
| 526 | if (!queryable) |
| 527 | return failure(); |
| 528 | return queryable.query(key); |
| 529 | } |
| 530 | |
| 531 | LogicalResult mlir::dlti::detail::setDlti(Operation *op, DataLayoutEntryKey key, |
| 532 | Attribute value) { |
| 533 | if (key.isNull()) |
| 534 | return failure(); |
| 535 | if (auto stringKey = dyn_cast<StringAttr>(Val&: key); |
| 536 | stringKey && stringKey.getValue().empty()) |
| 537 | return failure(); |
| 538 | |
| 539 | std::optional<Attribute> inherent = op->getInherentAttr(name: kDltiAttrName); |
| 540 | if (!inherent) |
| 541 | return failure(); |
| 542 | auto current = dyn_cast_or_null<DLTIQueryInterface>(Val&: *inherent); |
| 543 | if (current && !isa<MapAttr>(Val: current)) |
| 544 | return failure(); |
| 545 | |
| 546 | SmallVector<DataLayoutEntryInterface> entries; |
| 547 | if (auto map = dyn_cast_or_null<MapAttr>(Val&: current)) |
| 548 | llvm::append_range(C&: entries, R: map.getEntries()); |
| 549 | |
| 550 | auto makeEntry = [&]() -> DataLayoutEntryInterface { |
| 551 | if (auto type = dyn_cast<Type>(Val&: key)) |
| 552 | return DataLayoutEntryAttr::get(key: type, value); |
| 553 | return DataLayoutEntryAttr::get(key: cast<StringAttr>(Val&: key), value); |
| 554 | }; |
| 555 | auto entry = llvm::find_if(Range&: entries, P: [&](DataLayoutEntryInterface entry) { |
| 556 | return entry.getKey() == key; |
| 557 | }); |
| 558 | if (entry != entries.end()) { |
| 559 | if (value) |
| 560 | *entry = makeEntry(); |
| 561 | else |
| 562 | entries.erase(CI: entry); |
| 563 | } else if (value) { |
| 564 | entries.push_back(Elt: makeEntry()); |
| 565 | } else { |
| 566 | return success(); |
| 567 | } |
| 568 | |
| 569 | StringAttr attrName = StringAttr::get(context: op->getContext(), bytes: kDltiAttrName); |
| 570 | if (entries.empty()) { |
| 571 | op->setInherentAttr(name: attrName, value: {}); |
| 572 | return success(); |
| 573 | } |
| 574 | op->setInherentAttr(name: attrName, value: MapAttr::get(context: op->getContext(), entries)); |
| 575 | return success(); |
| 576 | } |
| 577 | |
| 578 | FailureOr<Attribute> |
| 579 | dlti::query(Operation *op, ArrayRef<DataLayoutEntryKey> keys, bool emitError) { |
| 580 | if (!op) |
| 581 | return failure(); |
| 582 | |
| 583 | if (keys.empty()) { |
| 584 | if (emitError) { |
| 585 | auto diag = op->emitError() << "target op of failed DLTI query" ; |
| 586 | diag.attachNote(noteLoc: op->getLoc()) << "no keys provided to attempt query with" ; |
| 587 | } |
| 588 | return failure(); |
| 589 | } |
| 590 | |
| 591 | Attribute currentAttr; |
| 592 | Operation *queryOp = op; |
| 593 | for (; queryOp; queryOp = queryOp->getParentOp()) { |
| 594 | auto queryable = dyn_cast<DLTIQueryOpInterface>(Val: queryOp); |
| 595 | if (!queryable) |
| 596 | continue; |
| 597 | FailureOr<Attribute> result = queryable.queryDlti(key: keys.front()); |
| 598 | if (succeeded(Result: result)) { |
| 599 | currentAttr = *result; |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if (!currentAttr) { |
| 605 | if (emitError) { |
| 606 | auto diag = op->emitError() << "target op of failed DLTI query" ; |
| 607 | diag.attachNote(noteLoc: op->getLoc()) |
| 608 | << "no DLTI-queryable operation on the target or any of its " |
| 609 | "ancestors could answer key " |
| 610 | << keyToStr(key: keys.front()); |
| 611 | } |
| 612 | return failure(); |
| 613 | } |
| 614 | |
| 615 | for (auto &&[idx, key] : llvm::enumerate(First: keys.drop_front())) { |
| 616 | if (auto map = dyn_cast<DLTIQueryInterface>(Val&: currentAttr)) { |
| 617 | auto maybeAttr = map.query(key); |
| 618 | if (failed(Result: maybeAttr)) { |
| 619 | if (emitError) { |
| 620 | auto diag = op->emitError() << "target op of failed DLTI query" ; |
| 621 | diag.attachNote(noteLoc: queryOp->getLoc()) |
| 622 | << "key " << keyToStr(key) |
| 623 | << " has no DLTI-mapping per attr: " << map; |
| 624 | } |
| 625 | return failure(); |
| 626 | } |
| 627 | currentAttr = *maybeAttr; |
| 628 | } else { |
| 629 | if (emitError) { |
| 630 | std::string commaSeparatedKeys; |
| 631 | llvm::interleave( |
| 632 | c: keys.take_front(N: idx + 1), // All prior keys. |
| 633 | each_fn: [&](auto key) { commaSeparatedKeys += keyToStr(key); }, |
| 634 | between_fn: [&]() { commaSeparatedKeys += "," ; }); |
| 635 | |
| 636 | auto diag = op->emitError() << "target op of failed DLTI query" ; |
| 637 | diag.attachNote(noteLoc: queryOp->getLoc()) |
| 638 | << "got non-DLTI-queryable attribute upon looking up keys [" |
| 639 | << commaSeparatedKeys << "] at op" ; |
| 640 | } |
| 641 | return failure(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return currentAttr; |
| 646 | } |
| 647 | |
| 648 | FailureOr<Attribute> dlti::query(Operation *op, ArrayRef<StringRef> keys, |
| 649 | bool emitError) { |
| 650 | if (!op) |
| 651 | return failure(); |
| 652 | |
| 653 | MLIRContext *ctx = op->getContext(); |
| 654 | SmallVector<DataLayoutEntryKey> entryKeys; |
| 655 | entryKeys.reserve(N: keys.size()); |
| 656 | for (StringRef key : keys) |
| 657 | entryKeys.push_back(Elt: StringAttr::get(context: ctx, bytes: key)); |
| 658 | |
| 659 | return dlti::query(op, keys: entryKeys, emitError); |
| 660 | } |
| 661 | |
| 662 | namespace { |
| 663 | struct ModuleDLTIQueryOpInterface |
| 664 | : DLTIQueryOpInterface::ExternalModel<ModuleDLTIQueryOpInterface, |
| 665 | ModuleOp> {}; |
| 666 | |
| 667 | class TargetDataLayoutInterface : public DataLayoutDialectInterface { |
| 668 | public: |
| 669 | using DataLayoutDialectInterface::DataLayoutDialectInterface; |
| 670 | |
| 671 | LogicalResult verifyEntry(DataLayoutEntryInterface entry, |
| 672 | Location loc) const final { |
| 673 | StringRef entryName = cast<StringAttr>(Val: entry.getKey()).strref(); |
| 674 | if (entryName == DLTIDialect::kDataLayoutEndiannessKey) { |
| 675 | auto value = dyn_cast<StringAttr>(Val: entry.getValue()); |
| 676 | if (value && |
| 677 | (value.getValue() == DLTIDialect::kDataLayoutEndiannessBig || |
| 678 | value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle)) |
| 679 | return success(); |
| 680 | return emitError(loc) << "'" << entryName |
| 681 | << "' data layout entry is expected to be either '" |
| 682 | << DLTIDialect::kDataLayoutEndiannessBig << "' or '" |
| 683 | << DLTIDialect::kDataLayoutEndiannessLittle << "'" ; |
| 684 | } |
| 685 | if (entryName == DLTIDialect::kDataLayoutDefaultMemorySpaceKey || |
| 686 | entryName == DLTIDialect::kDataLayoutAllocaMemorySpaceKey || |
| 687 | entryName == DLTIDialect::kDataLayoutProgramMemorySpaceKey || |
| 688 | entryName == DLTIDialect::kDataLayoutGlobalMemorySpaceKey || |
| 689 | entryName == DLTIDialect::kDataLayoutStackAlignmentKey || |
| 690 | entryName == DLTIDialect::kDataLayoutFunctionPointerAlignmentKey || |
| 691 | entryName == DLTIDialect::kDataLayoutLegalIntWidthsKey || |
| 692 | entryName == DLTIDialect::kDataLayoutManglingModeKey) |
| 693 | return success(); |
| 694 | return emitError(loc) << "unknown data layout entry name: " << entryName; |
| 695 | } |
| 696 | }; |
| 697 | } // namespace |
| 698 | |
| 699 | void DLTIDialect::initialize() { |
| 700 | addAttributes< |
| 701 | #define GET_ATTRDEF_LIST |
| 702 | #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc" |
| 703 | >(); |
| 704 | addInterfaces<TargetDataLayoutInterface>(); |
| 705 | ModuleOp::attachInterface<ModuleDLTIQueryOpInterface>(context&: *getContext()); |
| 706 | } |
| 707 | |
| 708 | LogicalResult DLTIDialect::verifyOperationAttribute(Operation *op, |
| 709 | NamedAttribute attr) { |
| 710 | if (attr.getName() == DLTIDialect::kDataLayoutAttrName) { |
| 711 | if (!llvm::isa<DataLayoutSpecAttr>(Val: attr.getValue())) { |
| 712 | return op->emitError() << "'" << DLTIDialect::kDataLayoutAttrName |
| 713 | << "' is expected to be a #dlti.dl_spec attribute" ; |
| 714 | } |
| 715 | if (isa<ModuleOp>(Val: op)) |
| 716 | return detail::verifyDataLayoutOp(op); |
| 717 | return success(); |
| 718 | } |
| 719 | |
| 720 | if (attr.getName() == DLTIDialect::kTargetSystemDescAttrName) { |
| 721 | if (!llvm::isa<TargetSystemSpecAttr>(Val: attr.getValue())) { |
| 722 | return op->emitError() |
| 723 | << "'" << DLTIDialect::kTargetSystemDescAttrName |
| 724 | << "' is expected to be a #dlti.target_system_spec attribute" ; |
| 725 | } |
| 726 | return success(); |
| 727 | } |
| 728 | |
| 729 | if (attr.getName() == DLTIDialect::kMapAttrName) { |
| 730 | if (!llvm::isa<MapAttr>(Val: attr.getValue())) { |
| 731 | return op->emitError() << "'" << DLTIDialect::kMapAttrName |
| 732 | << "' is expected to be a #dlti.map attribute" ; |
| 733 | } |
| 734 | return success(); |
| 735 | } |
| 736 | |
| 737 | return op->emitError() << "attribute '" << attr.getName().getValue() |
| 738 | << "' not supported by dialect" ; |
| 739 | } |
| 740 | |