From 33625122eac7fbf456e7bfd12dc2af65507f86f8 Mon Sep 17 00:00:00 2001 From: "Karl F. A. Friebel" Date: Fri, 18 Sep 2026 15:01:58 +0200 Subject: [PATCH] Added missing `ktdf_arch` mapping APIs. - Improved `mlir::ktdf_arch::Mappable` interface method documentation. - Added `getFromOpaquePointer` to the named attribute constraints to allow using them in `llvm::DenseMap`. - Added `llvm::PointerLikeTypeTraits` for `ResourceSpecAttr` to allow using them in `llvm::PointerUnion`. - Added static overloads for the `mlir::ktdf_arch::Mappable` methods that accept `mlir::Operation*` for convenience. Signed-off-by: Karl F. A. Friebel --- .../Dialect/KTDFArch/KTDFArchAttributes.h | 23 ++++++++++ .../Dialect/KTDFArch/KTDFArchInterfaces.td | 22 ++++++++-- lib/Dialect/KTDFArch/KTDFArchOpInterfaces.cpp | 42 +++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchAttributes.h b/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchAttributes.h index b4e55dc..49447e1 100644 --- a/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchAttributes.h +++ b/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchAttributes.h @@ -59,6 +59,10 @@ struct I64Attr : IntegerAttr { [[nodiscard]] auto getValue() const -> int64_t { return IntegerAttr::getValue().getSExtValue(); } + + [[nodiscard]] static auto getFromOpaquePointer(const void* ptr) -> I64Attr { + return I64Attr(reinterpret_cast(ptr)); + } }; /// Named constraint for an attribute that stores a resource kind. @@ -99,6 +103,11 @@ struct ResourceSpecAttr : Attribute { : Attribute(static_cast(attr).getImpl()) {} /*implicit*/ ResourceSpecAttr(FlatSymbolRefAttr attr) : Attribute(static_cast(attr).getImpl()) {} + + [[nodiscard]] static auto getFromOpaquePointer(const void* ptr) + -> ResourceSpecAttr { + return ResourceSpecAttr(reinterpret_cast(ptr)); + } }; /// Named constraint for an attribute that stores a directed adjacency matrix. @@ -141,6 +150,11 @@ struct AdjacencyMatrixAttr : ElementsAttr { [[nodiscard]] auto getDim() const -> int64_t { return getShapedType().getDimSize(0); } + + [[nodiscard]] static auto getFromOpaquePointer(const void* ptr) + -> AdjacencyMatrixAttr { + return AdjacencyMatrixAttr(reinterpret_cast(ptr)); + } }; } // namespace mlir::ktdf_arch @@ -154,6 +168,15 @@ struct llvm::PointerLikeTypeTraits } }; +template <> +struct llvm::PointerLikeTypeTraits + : PointerLikeTypeTraits { + [[nodiscard]] static auto getFromVoidPointer(void* ptr) + -> mlir::ktdf_arch::ResourceSpecAttr { + return mlir::ktdf_arch::ResourceSpecAttr::getFromOpaquePointer(ptr); + } +}; + /// Auto-generated includes. #define GET_ATTRDEF_CLASSES #include "dataflow-scheduler/Dialect/KTDFArch/KTDFArchAttributes.h.inc" // IWYU pragma: export diff --git a/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchInterfaces.td b/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchInterfaces.td index 673df8c..1e5512d 100644 --- a/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchInterfaces.td +++ b/include/dataflow-scheduler/Dialect/KTDFArch/KTDFArchInterfaces.td @@ -209,7 +209,7 @@ def KTDFArch_Mappable : OpInterface<"Mappable"> { let methods = [ InterfaceMethod<[{ - Gets the attribute that indicates the resource mapping. + Gets the resource mapping as an attribute. }], "MapsToAttr", "getMapsTo", (ins), "", @@ -217,7 +217,7 @@ def KTDFArch_Mappable : OpInterface<"Mappable"> { return mlir::ktdf_arch::getProperty($_op); }]>, InterfaceMethod<[{ - Tries to set the attribute that indicates the resource mapping. + Tries to set the resource mapping from an attribute. }], "LogicalResult", "setMapsTo", (ins "MapsToAttr":$maps_to), "", @@ -226,7 +226,7 @@ def KTDFArch_Mappable : OpInterface<"Mappable"> { return llvm::success(); }]>, InterfaceMethod<[{ - Removes the attribute that indicates the resource mapping. + Removes the resource mapping as an attribute. }], "MapsToAttr", "removeMapsTo", (ins), "", @@ -250,6 +250,22 @@ def KTDFArch_Mappable : OpInterface<"Mappable"> { /// Gets the implementation for @p op , or the default. [[nodiscard]] static auto getInterfaceFor(Operation* op) -> Concept*; + /// Gets the resource mapping of @p op , if it has any. + [[nodiscard]] static auto getMapsTo(Operation* op) -> MapsToAttr; + + /// Tries to set the resource mapping of @p op. + static auto setMapsTo(Operation* op, MapsToAttr maps_to) -> LogicalResult; + + /// Tries to remove the resource mapping of @p op. + static auto removeMapsTo(Operation* op) -> MapsToAttr; + + /// Verifies the mapping of @p op to @p resources . + static auto verifyMapping(Operation* op, ArrayRef resources) -> LogicalResult; + + /// Gets the resource mapping of @p op , or inherits from a parent. + /// + /// See getOrInheritMapsTo() for more information. + [[nodiscard]] static auto getOrInheritMapsTo(Operation* op) -> std::pair; /// Gets the resource mapping of this op, or inherits it from a parent. /// /// Walks the IR upwards from @p mappable , returning the value of the first diff --git a/lib/Dialect/KTDFArch/KTDFArchOpInterfaces.cpp b/lib/Dialect/KTDFArch/KTDFArchOpInterfaces.cpp index 82e58c5..c33b056 100644 --- a/lib/Dialect/KTDFArch/KTDFArchOpInterfaces.cpp +++ b/lib/Dialect/KTDFArch/KTDFArchOpInterfaces.cpp @@ -139,6 +139,48 @@ auto Mappable::getInterfaceFor(Operation* op) -> Concept* { return &default_mappable; } +auto Mappable::getMapsTo(Operation* op) -> MapsToAttr { + if (auto mappable = llvm::dyn_cast(op); mappable) { + return mappable.getMapsTo(); + } + + return nullptr; +} + +auto Mappable::setMapsTo(Operation* op, MapsToAttr maps_to) -> LogicalResult { + if (auto mappable = llvm::dyn_cast(op); mappable) { + return mappable.setMapsTo(maps_to); + } + + return failure(); +} + +auto Mappable::removeMapsTo(Operation* op) -> MapsToAttr { + if (auto mappable = llvm::dyn_cast(op); mappable) { + return mappable.removeMapsTo(); + } + + return nullptr; +} + +auto Mappable::verifyMapping(Operation* op, ArrayRef resources) + -> LogicalResult { + if (auto mappable = llvm::dyn_cast(op); mappable) { + return mappable.verifyMapping(resources); + } + + return success(); +} + +auto Mappable::getOrInheritMapsTo(Operation* op) + -> std::pair { + if (auto mappable = dyn_cast(op); mappable) { + return mappable.getOrInheritMapsTo(); + } + + return {nullptr, nullptr}; +} + auto Mappable::getOrInheritMapsTo() -> std::pair { for (auto self = *this; self; self = self->getParentOfType()) { if (const auto mapping = self.getMapsTo(); mapping) {