[Mlir-commits] [mlir] Reimplementing target description concept using DLTI attribute (PR #92138)

Mehdi Amini llvmlistbot at llvm.org
Wed Jun 5 21:37:50 PDT 2024


================
@@ -277,51 +236,179 @@ DataLayoutSpecAttr::combineWith(ArrayRef<DataLayoutSpecInterface> specs) const {
   return DataLayoutSpecAttr::get(getContext(), entries);
 }
 
-DataLayoutEntryListRef DataLayoutSpecAttr::getEntries() const {
-  return getImpl()->entries;
+/// Parses an attribute with syntax
+///   attr ::= `#target.` `dl_spec` `<` attr-list? `>`
+///   attr-list ::= attr
+///               | attr `,` attr-list
+Attribute DataLayoutSpecAttr::parse(AsmParser &parser, Type type) {
+  if (failed(parser.parseLess()))
+    return {};
+
+  // Empty spec.
+  if (succeeded(parser.parseOptionalGreater()))
+    return get(parser.getContext(), {});
+
+  SmallVector<DataLayoutEntryInterface> entries;
+  if (parser.parseCommaSeparatedList(
+          [&]() { return parser.parseAttribute(entries.emplace_back()); }) ||
+      parser.parseGreater())
+    return {};
+
+  return getChecked([&] { return parser.emitError(parser.getNameLoc()); },
+                    parser.getContext(), entries);
 }
 
-StringAttr
-DataLayoutSpecAttr::getEndiannessIdentifier(MLIRContext *context) const {
-  return Builder(context).getStringAttr(DLTIDialect::kDataLayoutEndiannessKey);
+void DataLayoutSpecAttr::print(AsmPrinter &os) const {
+  os << "<";
+  llvm::interleaveComma(getEntries(), os);
+  os << ">";
 }
 
-StringAttr
-DataLayoutSpecAttr::getAllocaMemorySpaceIdentifier(MLIRContext *context) const {
-  return Builder(context).getStringAttr(
-      DLTIDialect::kDataLayoutAllocaMemorySpaceKey);
+//===----------------------------------------------------------------------===//
+// TargetDeviceDescSpecAttr
+//===----------------------------------------------------------------------===//
+
+LogicalResult
+TargetDeviceDescSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
+                                 ArrayRef<DataLayoutEntryInterface> entries) {
+  // Entries in tdd_spec can only have StringAttr as key. It does not support
+  // type as a key. Hence not reusing DataLayoutEntryInterface::verify.
+  bool targetDeviceIDKeyPresentAndValid = false;
+  bool targetDeviceTypeKeyPresentAndValid = false;
+
+  DenseSet<StringAttr> ids;
+  for (DataLayoutEntryInterface entry : entries) {
+    if (auto type = llvm::dyn_cast_if_present<Type>(entry.getKey())) {
+      return emitError()
+             << "dlti.target_device_desc_spec does not allow type as a key: "
+             << type;
+    } else {
+      auto id = entry.getKey().get<StringAttr>();
+      if (!ids.insert(id).second)
+        return emitError() << "repeated layout entry key: " << id.getValue();
+    }
+
+    // check that Device ID and Device Type are present.
+    StringRef entryName = entry.getKey().get<StringAttr>().strref();
+    if (entryName == DLTIDialect::kTargetDeviceIDKey) {
+      // Also check the type of the value.
+      IntegerAttr value =
+          llvm::dyn_cast_if_present<IntegerAttr>(entry.getValue());
+      if (value && value.getType().isUnsignedInteger(32)) {
+        targetDeviceIDKeyPresentAndValid = true;
+      }
+    } else if (entryName == DLTIDialect::kTargetDeviceTypeKey) {
+      // Also check the type of the value.
+      if (auto value = llvm::dyn_cast<StringAttr>(entry.getValue())) {
+        targetDeviceTypeKeyPresentAndValid = true;
+      }
+    } else if (entryName == DLTIDialect::kTargetDeviceL1CacheSizeInBytesKey) {
+      IntegerAttr value =
+          llvm::dyn_cast_if_present<IntegerAttr>(entry.getValue());
+      if (!value || !value.getType().isUnsignedInteger(32))
----------------
joker-eph wrote:

Right: would want the IR to be `#dlti.dl_entry<"dlti.L1_cache_size_in_bytes", 4096>>` I think.

https://github.com/llvm/llvm-project/pull/92138


More information about the Mlir-commits mailing list