|
| 1 | +//===- OpAsmDialectInterface.h - OpAsm Dialect Interface --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef MLIR_IR_OPASMDIALECTINTERFACE_H |
| 10 | +#define MLIR_IR_OPASMDIALECTINTERFACE_H |
| 11 | + |
| 12 | +#include "mlir/IR/DialectInterface.h" |
| 13 | +#include "mlir/IR/Types.h" |
| 14 | +#include "mlir/IR/Value.h" |
| 15 | + |
| 16 | +namespace mlir { |
| 17 | +class AsmParsedResourceEntry; |
| 18 | +class AsmResourceBuilder; |
| 19 | + |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +// AsmDialectResourceHandle |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | + |
| 24 | +/// This class represents an opaque handle to a dialect resource entry. |
| 25 | +class AsmDialectResourceHandle { |
| 26 | +public: |
| 27 | + AsmDialectResourceHandle() = default; |
| 28 | + AsmDialectResourceHandle(void *resource, TypeID resourceID, Dialect *dialect) |
| 29 | + : resource(resource), opaqueID(resourceID), dialect(dialect) {} |
| 30 | + bool operator==(const AsmDialectResourceHandle &other) const { |
| 31 | + return resource == other.resource; |
| 32 | + } |
| 33 | + |
| 34 | + /// Return an opaque pointer to the referenced resource. |
| 35 | + void *getResource() const { return resource; } |
| 36 | + |
| 37 | + /// Return the type ID of the resource. |
| 38 | + TypeID getTypeID() const { return opaqueID; } |
| 39 | + |
| 40 | + /// Return the dialect that owns the resource. |
| 41 | + Dialect *getDialect() const { return dialect; } |
| 42 | + |
| 43 | +private: |
| 44 | + /// The opaque handle to the dialect resource. |
| 45 | + void *resource = nullptr; |
| 46 | + /// The type of the resource referenced. |
| 47 | + TypeID opaqueID; |
| 48 | + /// The dialect owning the given resource. |
| 49 | + Dialect *dialect; |
| 50 | +}; |
| 51 | + |
| 52 | +/// This class represents a CRTP base class for dialect resource handles. It |
| 53 | +/// abstracts away various utilities necessary for defined derived resource |
| 54 | +/// handles. |
| 55 | +template <typename DerivedT, typename ResourceT, typename DialectT> |
| 56 | +class AsmDialectResourceHandleBase : public AsmDialectResourceHandle { |
| 57 | +public: |
| 58 | + using Dialect = DialectT; |
| 59 | + |
| 60 | + /// Construct a handle from a pointer to the resource. The given pointer |
| 61 | + /// should be guaranteed to live beyond the life of this handle. |
| 62 | + AsmDialectResourceHandleBase(ResourceT *resource, DialectT *dialect) |
| 63 | + : AsmDialectResourceHandle(resource, TypeID::get<DerivedT>(), dialect) {} |
| 64 | + AsmDialectResourceHandleBase(AsmDialectResourceHandle handle) |
| 65 | + : AsmDialectResourceHandle(handle) { |
| 66 | + assert(handle.getTypeID() == TypeID::get<DerivedT>()); |
| 67 | + } |
| 68 | + |
| 69 | + /// Return the resource referenced by this handle. |
| 70 | + ResourceT *getResource() { |
| 71 | + return static_cast<ResourceT *>(AsmDialectResourceHandle::getResource()); |
| 72 | + } |
| 73 | + const ResourceT *getResource() const { |
| 74 | + return const_cast<AsmDialectResourceHandleBase *>(this)->getResource(); |
| 75 | + } |
| 76 | + |
| 77 | + /// Return the dialect that owns the resource. |
| 78 | + DialectT *getDialect() const { |
| 79 | + return static_cast<DialectT *>(AsmDialectResourceHandle::getDialect()); |
| 80 | + } |
| 81 | + |
| 82 | + /// Support llvm style casting. |
| 83 | + static bool classof(const AsmDialectResourceHandle *handle) { |
| 84 | + return handle->getTypeID() == TypeID::get<DerivedT>(); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +inline llvm::hash_code hash_value(const AsmDialectResourceHandle ¶m) { |
| 89 | + return llvm::hash_value(param.getResource()); |
| 90 | +} |
| 91 | + |
| 92 | +//===--------------------------------------------------------------------===// |
| 93 | +// Dialect OpAsm interface. |
| 94 | +//===--------------------------------------------------------------------===// |
| 95 | + |
| 96 | +/// A functor used to set the name of the result. See 'getAsmResultNames' below |
| 97 | +/// for more details. |
| 98 | +using OpAsmSetNameFn = function_ref<void(StringRef)>; |
| 99 | + |
| 100 | +/// A functor used to set the name of the start of a result group of an |
| 101 | +/// operation. See 'getAsmResultNames' below for more details. |
| 102 | +using OpAsmSetValueNameFn = function_ref<void(Value, StringRef)>; |
| 103 | + |
| 104 | +/// A functor used to set the name of blocks in regions directly nested under |
| 105 | +/// an operation. |
| 106 | +using OpAsmSetBlockNameFn = function_ref<void(Block *, StringRef)>; |
| 107 | + |
| 108 | +class OpAsmDialectInterface |
| 109 | + : public DialectInterface::Base<OpAsmDialectInterface> { |
| 110 | +public: |
| 111 | + OpAsmDialectInterface(Dialect *dialect) : Base(dialect) {} |
| 112 | + |
| 113 | + //===------------------------------------------------------------------===// |
| 114 | + // Aliases |
| 115 | + //===------------------------------------------------------------------===// |
| 116 | + |
| 117 | + /// Holds the result of `getAlias` hook call. |
| 118 | + enum class AliasResult { |
| 119 | + /// The object (type or attribute) is not supported by the hook |
| 120 | + /// and an alias was not provided. |
| 121 | + NoAlias, |
| 122 | + /// An alias was provided, but it might be overriden by other hook. |
| 123 | + OverridableAlias, |
| 124 | + /// An alias was provided and it should be used |
| 125 | + /// (no other hooks will be checked). |
| 126 | + FinalAlias |
| 127 | + }; |
| 128 | + |
| 129 | + /// Hooks for getting an alias identifier alias for a given symbol, that is |
| 130 | + /// not necessarily a part of this dialect. The identifier is used in place of |
| 131 | + /// the symbol when printing textual IR. These aliases must not contain `.` or |
| 132 | + /// end with a numeric digit([0-9]+). |
| 133 | + virtual AliasResult getAlias(Attribute attr, raw_ostream &os) const { |
| 134 | + return AliasResult::NoAlias; |
| 135 | + } |
| 136 | + virtual AliasResult getAlias(Type type, raw_ostream &os) const { |
| 137 | + return AliasResult::NoAlias; |
| 138 | + } |
| 139 | + |
| 140 | + //===--------------------------------------------------------------------===// |
| 141 | + // Resources |
| 142 | + //===--------------------------------------------------------------------===// |
| 143 | + |
| 144 | + /// Declare a resource with the given key, returning a handle to use for any |
| 145 | + /// references of this resource key within the IR during parsing. The result |
| 146 | + /// of `getResourceKey` on the returned handle is permitted to be different |
| 147 | + /// than `key`. |
| 148 | + virtual FailureOr<AsmDialectResourceHandle> |
| 149 | + declareResource(StringRef key) const { |
| 150 | + return failure(); |
| 151 | + } |
| 152 | + |
| 153 | + /// Return a key to use for the given resource. This key should uniquely |
| 154 | + /// identify this resource within the dialect. |
| 155 | + virtual std::string |
| 156 | + getResourceKey(const AsmDialectResourceHandle &handle) const { |
| 157 | + llvm_unreachable( |
| 158 | + "Dialect must implement `getResourceKey` when defining resources"); |
| 159 | + } |
| 160 | + |
| 161 | + /// Hook for parsing resource entries. Returns failure if the entry was not |
| 162 | + /// valid, or could otherwise not be processed correctly. Any necessary errors |
| 163 | + /// can be emitted via the provided entry. |
| 164 | + virtual LogicalResult parseResource(AsmParsedResourceEntry &entry) const; |
| 165 | + |
| 166 | + /// Hook for building resources to use during printing. The given `op` may be |
| 167 | + /// inspected to help determine what information to include. |
| 168 | + /// `referencedResources` contains all of the resources detected when printing |
| 169 | + /// 'op'. |
| 170 | + virtual void |
| 171 | + buildResources(Operation *op, |
| 172 | + const SetVector<AsmDialectResourceHandle> &referencedResources, |
| 173 | + AsmResourceBuilder &builder) const {} |
| 174 | +}; |
| 175 | +} // namespace mlir |
| 176 | + |
| 177 | +#endif |
0 commit comments