18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/TypeSwitch.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/MathExtras.h"
25 #include "mlir/Dialect/DLTI/DLTIDialect.cpp.inc"
27 #define GET_ATTRDEF_CLASSES
28 #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc"
30 #define DEBUG_TYPE "dlti"
41 DataLayoutEntryInterface &entry,
42 bool tryType =
false) {
49 if (failed(parsedType.
value()))
51 <<
"error while parsing type DLTI key";
57 return ParseResult::success();
69 return ParseResult::success();
74 if (succeeded(parsedEntry.
value()))
75 return parsedEntry.
value();
79 <<
"failed to parse DLTI entry";
91 bool allowEmpty =
false) {
95 return parseKeyValuePair(parser, entries.emplace_back(), tryType);
99 if (entries.empty() && !allowEmpty) {
116 .Case<StringAttr,
Type>(
117 [&](
auto key) { llvm::raw_string_ostream(buf) << key; });
125 llvm::interleaveComma(std::forward<T>(entries), os, [&](
auto entry) {
126 os <<
keyToStr(entry.getKey()) <<
" = " << entry.getValue();
138 bool allowTypes =
true) {
140 for (DataLayoutEntryInterface entry : entries) {
142 return emitError() <<
"contained invalid DLTI entry";
145 return emitError() <<
"contained invalid DLTI key";
146 if (!allowTypes && dyn_cast<Type>(key))
147 return emitError() <<
"type as DLTI key is not allowed";
148 if (
auto strKey = dyn_cast<StringAttr>(key))
149 if (strKey.getValue().empty())
150 return emitError() <<
"empty string as DLTI key is not allowed";
151 if (!keys.insert(key).second)
153 if (!entry.getValue())
167 using KeyTy = std::pair<DataLayoutEntryKey, Attribute>;
189 return Base::get(key.getContext(), key, value);
197 return getImpl()->entryKey;
200 Attribute DataLayoutEntryAttr::getValue()
const {
return getImpl()->value; }
209 Type typeKey =
nullptr;
210 std::string identifier;
218 parser.
emitError(idLoc) <<
"expected a type or a quoted string";
228 return typeKey ?
get(typeKey, value)
229 :
get(parser.getBuilder().getStringAttr(identifier), value);
233 printer <<
"<" <<
keyToStr(getKey()) <<
", " << getValue() <<
">";
245 return parseAngleBracketedEntries<MapAttr>(parser, type,
true,
274 unsigned oldEntriesSize = oldEntries.size();
275 for (DataLayoutEntryInterface entry : newEntries) {
280 bool replaced =
false;
281 for (
unsigned i = 0; i < oldEntriesSize; ++i) {
282 if (oldEntries[i].getKey() == entry.getKey()) {
283 oldEntries[i] = entry;
289 oldEntries.push_back(entry);
297 DataLayoutSpecInterface spec,
298 llvm::MapVector<TypeID, DataLayoutEntryList> &entriesForType,
299 llvm::MapVector<StringAttr, DataLayoutEntryInterface> &entriesForID) {
304 llvm::MapVector<TypeID, DataLayoutEntryList> newEntriesForType;
305 llvm::MapVector<StringAttr, DataLayoutEntryInterface> newEntriesForID;
306 spec.bucketEntriesByType(newEntriesForType, newEntriesForID);
310 for (
const auto &kvp : newEntriesForID) {
311 StringAttr
id = cast<StringAttr>(kvp.second.getKey());
312 Dialect *dialect =
id.getReferencedDialect();
313 if (!entriesForID.count(
id)) {
314 entriesForID[id] = kvp.second;
322 dialect ? cast<DataLayoutDialectInterface>(dialect)->combine(
323 entriesForID[
id], kvp.second)
326 if (!entriesForID[
id])
331 for (
auto &kvp : newEntriesForType) {
332 if (!entriesForType.count(kvp.first)) {
333 entriesForType[kvp.first] = std::move(kvp.second);
337 Type typeSample = cast<Type>(kvp.second.front().getKey());
340 "unexpected data layout entry for built-in type");
342 auto interface = cast<DataLayoutTypeInterface>(typeSample);
345 if (!interface.areCompatible(entriesForType.lookup(kvp.first), kvp.second,
359 if (any_of(specs, [](DataLayoutSpecInterface spec) {
360 return !llvm::isa<DataLayoutSpecAttr>(spec);
365 llvm::MapVector<TypeID, DataLayoutEntryList> entriesForType;
366 llvm::MapVector<StringAttr, DataLayoutEntryInterface> entriesForID;
367 for (DataLayoutSpecInterface spec : specs)
375 llvm::append_range(entries, llvm::make_second_range(entriesForID));
376 for (
const auto &kvp : entriesForType)
377 llvm::append_range(entries, kvp.second);
383 DataLayoutSpecAttr::getEndiannessIdentifier(
MLIRContext *context)
const {
387 StringAttr DataLayoutSpecAttr::getDefaultMemorySpaceIdentifier(
390 DLTIDialect::kDataLayoutDefaultMemorySpaceKey);
394 DataLayoutSpecAttr::getAllocaMemorySpaceIdentifier(
MLIRContext *context)
const {
396 DLTIDialect::kDataLayoutAllocaMemorySpaceKey);
399 StringAttr DataLayoutSpecAttr::getProgramMemorySpaceIdentifier(
402 DLTIDialect::kDataLayoutProgramMemorySpaceKey);
406 DataLayoutSpecAttr::getGlobalMemorySpaceIdentifier(
MLIRContext *context)
const {
408 DLTIDialect::kDataLayoutGlobalMemorySpaceKey);
412 DataLayoutSpecAttr::getManglingModeIdentifier(
MLIRContext *context)
const {
414 DLTIDialect::kDataLayoutManglingModeKey);
418 DataLayoutSpecAttr::getStackAlignmentIdentifier(
MLIRContext *context)
const {
420 DLTIDialect::kDataLayoutStackAlignmentKey);
428 return parseAngleBracketedEntries<DataLayoutSpecAttr>(parser, type,
452 return parseAngleBracketedEntries<TargetDeviceSpecAttr>(parser, type);
468 for (
const auto &entry : entries) {
470 llvm::dyn_cast<TargetSystemSpecInterface::DeviceID>(entry.getKey());
472 return emitError() <<
"non-string key of DLTI system spec";
474 if (
auto targetDeviceSpec =
475 llvm::dyn_cast<TargetDeviceSpecInterface>(entry.getValue())) {
477 targetDeviceSpec.getEntries())))
480 return emitError() <<
"value associated with key " << deviceId
481 <<
" is not a DLTI device spec";
485 if (!deviceIds.insert(deviceId).second)
486 return emitError() <<
"repeated device ID in dlti.target_system_spec: "
498 return parseAngleBracketedEntries<TargetSystemSpecAttr>(parser, type);
512 static std::pair<DLTIQueryInterface, Operation *>
514 DLTIQueryInterface queryable = {};
519 if ((queryable = dyn_cast<DLTIQueryInterface>(attr.getValue())))
523 return std::pair(queryable, op);
533 auto diag = op->
emitError() <<
"target op of failed DLTI query";
534 diag.attachNote(op->
getLoc()) <<
"no keys provided to attempt query with";
540 Operation *reportOp = (queryOp ? queryOp : op);
544 auto diag = op->
emitError() <<
"target op of failed DLTI query";
546 <<
"no DLTI-queryable attrs on target op or any of its ancestors";
553 if (
auto map = dyn_cast<DLTIQueryInterface>(currentAttr)) {
554 auto maybeAttr = map.query(key);
555 if (failed(maybeAttr)) {
557 auto diag = op->
emitError() <<
"target op of failed DLTI query";
560 <<
" has no DLTI-mapping per attr: " << map;
564 currentAttr = *maybeAttr;
567 std::string commaSeparatedKeys;
569 keys.take_front(idx),
570 [&](
auto key) { commaSeparatedKeys += keyToStr(key); },
571 [&]() { commaSeparatedKeys +=
","; });
573 auto diag = op->
emitError() <<
"target op of failed DLTI query";
575 <<
"got non-DLTI-queryable attribute upon looking up keys ["
576 << commaSeparatedKeys <<
"] at op";
592 entryKeys.reserve(keys.size());
593 for (StringRef key : keys)
599 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutAttrName;
600 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessKey;
601 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessBig;
602 constexpr
const StringLiteral mlir::DLTIDialect::kDataLayoutEndiannessLittle;
609 LogicalResult verifyEntry(DataLayoutEntryInterface entry,
611 StringRef entryName = cast<StringAttr>(entry.getKey()).strref();
612 if (entryName == DLTIDialect::kDataLayoutEndiannessKey) {
613 auto value = dyn_cast<StringAttr>(entry.getValue());
615 (value.getValue() == DLTIDialect::kDataLayoutEndiannessBig ||
616 value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle))
618 return emitError(loc) <<
"'" << entryName
619 <<
"' data layout entry is expected to be either '"
620 << DLTIDialect::kDataLayoutEndiannessBig <<
"' or '"
621 << DLTIDialect::kDataLayoutEndiannessLittle <<
"'";
623 if (entryName == DLTIDialect::kDataLayoutDefaultMemorySpaceKey ||
624 entryName == DLTIDialect::kDataLayoutAllocaMemorySpaceKey ||
625 entryName == DLTIDialect::kDataLayoutProgramMemorySpaceKey ||
626 entryName == DLTIDialect::kDataLayoutGlobalMemorySpaceKey ||
627 entryName == DLTIDialect::kDataLayoutStackAlignmentKey ||
628 entryName == DLTIDialect::kDataLayoutManglingModeKey)
630 return emitError(loc) <<
"unknown data layout entry name: " << entryName;
635 void DLTIDialect::initialize() {
637 #define GET_ATTRDEF_LIST
638 #include "mlir/Dialect/DLTI/DLTIAttrs.cpp.inc"
640 addInterfaces<TargetDataLayoutInterface>();
643 LogicalResult DLTIDialect::verifyOperationAttribute(
Operation *op,
645 if (attr.
getName() == DLTIDialect::kDataLayoutAttrName) {
646 if (!llvm::isa<DataLayoutSpecAttr>(attr.
getValue())) {
647 return op->
emitError() <<
"'" << DLTIDialect::kDataLayoutAttrName
648 <<
"' is expected to be a #dlti.dl_spec attribute";
650 if (isa<ModuleOp>(op))
655 if (attr.
getName() == DLTIDialect::kTargetSystemDescAttrName) {
656 if (!llvm::isa<TargetSystemSpecAttr>(attr.
getValue())) {
658 <<
"'" << DLTIDialect::kTargetSystemDescAttrName
659 <<
"' is expected to be a #dlti.target_system_spec attribute";
664 if (attr.
getName() == DLTIDialect::kMapAttrName) {
665 if (!llvm::isa<MapAttr>(attr.
getValue())) {
666 return op->
emitError() <<
"'" << DLTIDialect::kMapAttrName
667 <<
"' is expected to be a #dlti.map attribute";
673 <<
"' 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,...