18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/TypeSwitch.h"
23 #include "mlir/Dialect/DLTI/DLTIDialect.cpp.inc"
25 #define GET_ATTRDEF_CLASSES
26 #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc"
28 #define DEBUG_TYPE "dlti"
39 DataLayoutEntryInterface &entry,
40 bool tryType =
false) {
47 if (failed(parsedType.
value()))
49 <<
"error while parsing type DLTI key";
55 return ParseResult::success();
67 return ParseResult::success();
72 if (succeeded(parsedEntry.
value()))
73 return parsedEntry.
value();
77 <<
"failed to parse DLTI entry";
89 bool allowEmpty =
false) {
93 return parseKeyValuePair(parser, entries.emplace_back(), tryType);
97 if (entries.empty() && !allowEmpty) {
114 .Case<StringAttr,
Type>(
115 [&](
auto key) { llvm::raw_string_ostream(buf) << key; });
123 llvm::interleaveComma(std::forward<T>(entries), os, [&](
auto entry) {
124 os <<
keyToStr(entry.getKey()) <<
" = " << entry.getValue();
136 bool allowTypes =
true) {
138 for (DataLayoutEntryInterface entry : entries) {
140 return emitError() <<
"contained invalid DLTI entry";
143 return emitError() <<
"contained invalid DLTI key";
144 if (!allowTypes && dyn_cast<Type>(key))
145 return emitError() <<
"type as DLTI key is not allowed";
146 if (
auto strKey = dyn_cast<StringAttr>(key))
147 if (strKey.getValue().empty())
148 return emitError() <<
"empty string as DLTI key is not allowed";
149 if (!keys.insert(key).second)
151 if (!entry.getValue())
165 using KeyTy = std::pair<DataLayoutEntryKey, Attribute>;
187 return Base::get(key.getContext(), key, value);
195 return getImpl()->entryKey;
198 Attribute DataLayoutEntryAttr::getValue()
const {
return getImpl()->value; }
207 Type typeKey =
nullptr;
208 std::string identifier;
216 parser.
emitError(idLoc) <<
"expected a type or a quoted string";
226 return typeKey ?
get(typeKey, value)
227 :
get(parser.getBuilder().getStringAttr(identifier), value);
231 printer <<
"<" <<
keyToStr(getKey()) <<
", " << getValue() <<
">";
243 return parseAngleBracketedEntries<MapAttr>(parser, type,
true,
272 unsigned oldEntriesSize = oldEntries.size();
273 for (DataLayoutEntryInterface entry : newEntries) {
278 bool replaced =
false;
279 for (
unsigned i = 0; i < oldEntriesSize; ++i) {
280 if (oldEntries[i].getKey() == entry.getKey()) {
281 oldEntries[i] = entry;
287 oldEntries.push_back(entry);
295 DataLayoutSpecInterface spec,
296 llvm::MapVector<TypeID, DataLayoutEntryList> &entriesForType,
297 llvm::MapVector<StringAttr, DataLayoutEntryInterface> &entriesForID) {
302 llvm::MapVector<TypeID, DataLayoutEntryList> newEntriesForType;
303 llvm::MapVector<StringAttr, DataLayoutEntryInterface> newEntriesForID;
304 spec.bucketEntriesByType(newEntriesForType, newEntriesForID);
308 for (
const auto &kvp : newEntriesForID) {
309 StringAttr
id = cast<StringAttr>(kvp.second.getKey());
310 Dialect *dialect =
id.getReferencedDialect();
311 if (!entriesForID.count(
id)) {
312 entriesForID[id] = kvp.second;
320 dialect ? cast<DataLayoutDialectInterface>(dialect)->combine(
321 entriesForID[
id], kvp.second)
324 if (!entriesForID[
id])
329 for (
auto &kvp : newEntriesForType) {
330 if (!entriesForType.count(kvp.first)) {
331 entriesForType[kvp.first] = std::move(kvp.second);
335 Type typeSample = cast<Type>(kvp.second.front().getKey());
338 "unexpected data layout entry for built-in type");
340 auto interface = cast<DataLayoutTypeInterface>(typeSample);
343 if (!interface.areCompatible(entriesForType.lookup(kvp.first), kvp.second,
357 if (any_of(specs, [](DataLayoutSpecInterface spec) {
358 return !llvm::isa<DataLayoutSpecAttr>(spec);
363 llvm::MapVector<TypeID, DataLayoutEntryList> entriesForType;
364 llvm::MapVector<StringAttr, DataLayoutEntryInterface> entriesForID;
365 for (DataLayoutSpecInterface spec : specs)
373 llvm::append_range(entries, llvm::make_second_range(entriesForID));
374 for (
const auto &kvp : entriesForType)
375 llvm::append_range(entries, kvp.second);
381 DataLayoutSpecAttr::getEndiannessIdentifier(
MLIRContext *context)
const {
385 StringAttr DataLayoutSpecAttr::getDefaultMemorySpaceIdentifier(
388 DLTIDialect::kDataLayoutDefaultMemorySpaceKey);
392 DataLayoutSpecAttr::getAllocaMemorySpaceIdentifier(
MLIRContext *context)
const {
394 DLTIDialect::kDataLayoutAllocaMemorySpaceKey);
397 StringAttr DataLayoutSpecAttr::getProgramMemorySpaceIdentifier(
400 DLTIDialect::kDataLayoutProgramMemorySpaceKey);
404 DataLayoutSpecAttr::getGlobalMemorySpaceIdentifier(
MLIRContext *context)
const {
406 DLTIDialect::kDataLayoutGlobalMemorySpaceKey);
410 DataLayoutSpecAttr::getManglingModeIdentifier(
MLIRContext *context)
const {
412 DLTIDialect::kDataLayoutManglingModeKey);
416 DataLayoutSpecAttr::getStackAlignmentIdentifier(
MLIRContext *context)
const {
418 DLTIDialect::kDataLayoutStackAlignmentKey);
421 StringAttr DataLayoutSpecAttr::getFunctionPointerAlignmentIdentifier(
424 DLTIDialect::kDataLayoutFunctionPointerAlignmentKey);
428 DataLayoutSpecAttr::getLegalIntWidthsIdentifier(
MLIRContext *context)
const {
430 DLTIDialect::kDataLayoutLegalIntWidthsKey);
438 return parseAngleBracketedEntries<DataLayoutSpecAttr>(parser, type,
462 return parseAngleBracketedEntries<TargetDeviceSpecAttr>(parser, type);
478 for (
const auto &entry : entries) {
480 llvm::dyn_cast<TargetSystemSpecInterface::DeviceID>(entry.getKey());
482 return emitError() <<
"non-string key of DLTI system spec";
484 if (
auto targetDeviceSpec =
485 llvm::dyn_cast<TargetDeviceSpecInterface>(entry.getValue())) {
487 targetDeviceSpec.getEntries())))
490 return emitError() <<
"value associated with key " << deviceId
491 <<
" is not a DLTI device spec";
495 if (!deviceIds.insert(deviceId).second)
496 return emitError() <<
"repeated device ID in dlti.target_system_spec: "
508 return parseAngleBracketedEntries<TargetSystemSpecAttr>(parser, type);
522 static std::pair<DLTIQueryInterface, Operation *>
524 DLTIQueryInterface queryable = {};
529 if ((queryable = dyn_cast<DLTIQueryInterface>(attr.getValue())))
533 return std::pair(queryable, op);
543 auto diag = op->
emitError() <<
"target op of failed DLTI query";
544 diag.attachNote(op->
getLoc()) <<
"no keys provided to attempt query with";
550 Operation *reportOp = (queryOp ? queryOp : op);
554 auto diag = op->
emitError() <<
"target op of failed DLTI query";
556 <<
"no DLTI-queryable attrs on target op or any of its ancestors";
563 if (
auto map = dyn_cast<DLTIQueryInterface>(currentAttr)) {
564 auto maybeAttr = map.query(key);
565 if (failed(maybeAttr)) {
567 auto diag = op->
emitError() <<
"target op of failed DLTI query";
570 <<
" has no DLTI-mapping per attr: " << map;
574 currentAttr = *maybeAttr;
577 std::string commaSeparatedKeys;
579 keys.take_front(idx),
580 [&](
auto key) { commaSeparatedKeys += keyToStr(key); },
581 [&]() { commaSeparatedKeys +=
","; });
583 auto diag = op->
emitError() <<
"target op of failed DLTI query";
585 <<
"got non-DLTI-queryable attribute upon looking up keys ["
586 << commaSeparatedKeys <<
"] at op";
602 entryKeys.reserve(keys.size());
603 for (StringRef key : keys)
609 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutAttrName;
610 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessKey;
611 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessBig;
612 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessLittle;
619 LogicalResult verifyEntry(DataLayoutEntryInterface entry,
621 StringRef entryName = cast<StringAttr>(entry.getKey()).strref();
622 if (entryName == DLTIDialect::kDataLayoutEndiannessKey) {
623 auto value = dyn_cast<StringAttr>(entry.getValue());
625 (value.getValue() == DLTIDialect::kDataLayoutEndiannessBig ||
626 value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle))
628 return emitError(loc) <<
"'" << entryName
629 <<
"' data layout entry is expected to be either '"
630 << DLTIDialect::kDataLayoutEndiannessBig <<
"' or '"
631 << DLTIDialect::kDataLayoutEndiannessLittle <<
"'";
633 if (entryName == DLTIDialect::kDataLayoutDefaultMemorySpaceKey ||
634 entryName == DLTIDialect::kDataLayoutAllocaMemorySpaceKey ||
635 entryName == DLTIDialect::kDataLayoutProgramMemorySpaceKey ||
636 entryName == DLTIDialect::kDataLayoutGlobalMemorySpaceKey ||
637 entryName == DLTIDialect::kDataLayoutStackAlignmentKey ||
638 entryName == DLTIDialect::kDataLayoutFunctionPointerAlignmentKey ||
639 entryName == DLTIDialect::kDataLayoutLegalIntWidthsKey ||
640 entryName == DLTIDialect::kDataLayoutManglingModeKey)
642 return emitError(loc) <<
"unknown data layout entry name: " << entryName;
647 void DLTIDialect::initialize() {
649 #define GET_ATTRDEF_LIST
650 #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc"
652 addInterfaces<TargetDataLayoutInterface>();
655 LogicalResult DLTIDialect::verifyOperationAttribute(
Operation *op,
657 if (attr.
getName() == DLTIDialect::kDataLayoutAttrName) {
658 if (!llvm::isa<DataLayoutSpecAttr>(attr.
getValue())) {
659 return op->
emitError() <<
"'" << DLTIDialect::kDataLayoutAttrName
660 <<
"' is expected to be a #dlti.dl_spec attribute";
662 if (isa<ModuleOp>(op))
667 if (attr.
getName() == DLTIDialect::kTargetSystemDescAttrName) {
668 if (!llvm::isa<TargetSystemSpecAttr>(attr.
getValue())) {
670 <<
"'" << DLTIDialect::kTargetSystemDescAttrName
671 <<
"' is expected to be a #dlti.target_system_spec attribute";
676 if (attr.
getName() == DLTIDialect::kMapAttrName) {
677 if (!llvm::isa<MapAttr>(attr.
getValue())) {
678 return op->
emitError() <<
"'" << DLTIDialect::kMapAttrName
679 <<
"' is expected to be a #dlti.map attribute";
685 <<
"' not supported by dialect";
static LogicalResult verifyEntries(function_ref< InFlightDiagnostic()> emitError, ArrayRef< DataLayoutEntryInterface > entries, bool allowTypes=true)
Verify entries, with the option to disallow types as keys.
static void printAngleBracketedEntries(AsmPrinter &os, T &&entries)
Pretty-print entries, each in key = value format, separated by commas.
static std::pair< DLTIQueryInterface, Operation * > getClosestQueryable(Operation *op)
Retrieve the first DLTIQueryInterface-implementing attribute that is attached to op or such an attr o...
static std::string keyToStr(DataLayoutEntryKey key)
Convert pointer-union keys to strings.
static void overwriteDuplicateEntries(SmallVectorImpl< DataLayoutEntryInterface > &oldEntries, ArrayRef< DataLayoutEntryInterface > newEntries)
Given a list of old and a list of new entries, overwrites old entries with new ones if they have matc...
static Attribute parseAngleBracketedEntries(AsmParser &parser, Type ty, bool tryType=false, bool allowEmpty=false)
Construct a requested attribute by parsing list of entries occurring within a pair of < and >,...
static LogicalResult combineOneSpec(DataLayoutSpecInterface spec, llvm::MapVector< TypeID, DataLayoutEntryList > &entriesForType, llvm::MapVector< StringAttr, DataLayoutEntryInterface > &entriesForID)
Combines a data layout spec into the given lists of entries organized by type class and identifier,...
static ParseResult parseKeyValuePair(AsmParser &parser, DataLayoutEntryInterface &entry, bool tryType=false)
Parse an entry which can either be of the form key = value or a #dlti.dl_entry attribute.
static MLIRContext * getContext(OpFoldResult val)
static std::string diag(const llvm::Value &value)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
@ LessGreater
<> brackets surrounding zero or more operands.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual OptionalParseResult parseOptionalType(Type &result)=0
Parse an optional type.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseEqual()=0
Parse a = token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalString(std::string *string)=0
Parse a quoted string token if present.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Base storage class appearing in an attribute.
Attributes are known-constant values of operations.
This class is a general helper class for creating context-global objects like types,...
StringAttr getStringAttr(const Twine &bytes)
An interface to be implemented by dialects that can have identifiers in the data layout specification...
DataLayoutDialectInterface(Dialect *dialect)
static DataLayoutEntryInterface defaultCombine(DataLayoutEntryInterface outer, DataLayoutEntryInterface inner)
Default implementation of entry combination that combines identical entries and returns null otherwis...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
This class represents a diagnostic that is inflight and set to be reported.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
Dialect * getLoadedDialect(StringRef name)
Get a registered IR dialect with the given namespace.
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
Location getLoc()
The source location the operation was defined or derived from.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
This class implements Optional functionality for ParseResult.
ParseResult value() const
Access the internal ParseResult value.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This is a utility allocator used to allocate memory for instances of derived types.
T * allocate()
Allocate an instance of the provided type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Dialect & getDialect() const
Get the dialect this type is registered to.
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
DataLayoutEntryKey entryKey
DataLayoutEntryAttrStorage(DataLayoutEntryKey entryKey, Attribute value)
bool operator==(const KeyTy &other) const
std::pair< DataLayoutEntryKey, Attribute > KeyTy
static DataLayoutEntryAttrStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
LogicalResult verifyDataLayoutOp(Operation *op)
Verifies that the operation implementing the data layout interface, or a module operation,...
FailureOr< Attribute > query(Operation *op, ArrayRef< DataLayoutEntryKey > keys, bool emitError=false)
Perform a DLTI-query at op, recursively querying each key of keys on query interface-implementing att...
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...