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

Mehdi Amini llvmlistbot at llvm.org
Mon Jun 17 06:03:41 PDT 2024


================
@@ -332,11 +291,134 @@ DataLayoutSpecAttr DataLayoutSpecAttr::parse(AsmParser &parser) {
 }
 
 void DataLayoutSpecAttr::print(AsmPrinter &os) const {
-  os << DataLayoutSpecAttr::kAttrKeyword << "<";
+  os << "<";
   llvm::interleaveComma(getEntries(), os);
   os << ">";
 }
 
+//===----------------------------------------------------------------------===//
+// TargetDeviceSpecAttr
+//===----------------------------------------------------------------------===//
+
+namespace mlir {
+/// A FieldParser for key-value pairs of DeviceID-target device spec pairs that
+/// make up a target system spec.
+template <>
+struct FieldParser<DeviceIDTargetDeviceSpecPair> {
+  static FailureOr<DeviceIDTargetDeviceSpecPair> parse(AsmParser &parser) {
+    std::string deviceID;
+
+    if (failed(parser.parseString(&deviceID))) {
+      parser.emitError(parser.getCurrentLocation())
+          << "DeviceID is missing, or is not of string type";
+      return failure();
+    }
+
+    if (failed(parser.parseColon())) {
+      parser.emitError(parser.getCurrentLocation()) << "Missing colon";
+      return failure();
+    }
+
+    auto target_device_spec =
+        FieldParser<TargetDeviceSpecInterface>::parse(parser);
+    if (failed(target_device_spec)) {
+      parser.emitError(parser.getCurrentLocation())
+          << "Error in parsing target device spec";
+      return failure();
+    }
+
+    return std::make_pair(parser.getBuilder().getStringAttr(deviceID),
+                          *target_device_spec);
+  }
+};
+
+inline AsmPrinter &operator<<(AsmPrinter &printer,
+                              DeviceIDTargetDeviceSpecPair param) {
+  return printer << param.first << " : " << param.second;
+}
+
+} // namespace mlir
+
+LogicalResult
+TargetDeviceSpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
+                             ArrayRef<DataLayoutEntryInterface> entries) {
+  // Entries in a target device spec can only have StringAttr as key. It does
+  // not support type as a key. Hence not reusing
+  // DataLayoutEntryInterface::verify.
+  DenseSet<StringAttr> ids;
+  for (DataLayoutEntryInterface entry : entries) {
+    if (auto type = llvm::dyn_cast_if_present<Type>(entry.getKey())) {
+      return emitError()
+             << "dlti.target_device_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 required keys are of right type.
+    StringRef entryName = entry.getKey().get<StringAttr>().strref();
+    if (entryName == DLTIDialect::kTargetDeviceL1CacheSizeInBytesKey) {
+      IntegerAttr value =
+          llvm::dyn_cast_if_present<IntegerAttr>(entry.getValue());
+      if (!value || !value.getType().isInteger())
+        return emitError() << "target_device_spec requires value of key: "
+                           << DLTIDialect::kTargetDeviceL1CacheSizeInBytesKey
+                           << " to be of integer type";
+    } else if (entryName == DLTIDialect::kTargetDeviceMaxVectorOpWidthKey) {
+      IntegerAttr value =
+          llvm::dyn_cast_if_present<IntegerAttr>(entry.getValue());
+      if (!value || !value.getType().isInteger())
+        return emitError() << "target_device_spec requires value of key: "
+                           << DLTIDialect::kTargetDeviceMaxVectorOpWidthKey
+                           << " to be of integer type";
+    } else {
+      return emitError() << "unknown target device spec key name: "
+                         << entryName;
+    }
+  }
+
+  return success();
+}
+
+StringAttr TargetDeviceSpecAttr::getMaxVectorOpWidthIdentifier() {
+  return Builder(getContext())
+      .getStringAttr(DLTIDialect::kTargetDeviceMaxVectorOpWidthKey);
+}
+
+StringAttr TargetDeviceSpecAttr::getL1CacheSizeInBytesIdentifier() {
+  return Builder(getContext())
+      .getStringAttr(DLTIDialect::kTargetDeviceL1CacheSizeInBytesKey);
----------------
joker-eph wrote:

This hits the context unnecessarily, the dialect itself should have a cached copy of the attribute.

Anyway: can we just finally prune this PR from any method that has to do with L1 and vector width?

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


More information about the Mlir-commits mailing list